from kubernetes import client, config
# Load the Kubernetes configuration
config.load_kube_config()
# Create a Kubernetes API client
api_client = client.ApiClient()
# Create a ServiceAccount
service_account = client.V1ServiceAccount(
metadata=client.V1ObjectMeta(name="restricted-service-account")
)
# Create the ServiceAccount in the cluster
api_instance = client.CoreV1Api(api_client)
api_instance.create_namespaced_service_account(
namespace="your-namespace",
body=service_account
)
# Modify the deployment manifest to use the newly created ServiceAccount
deployment_manifest = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "your-deployment",
"namespace": "your-namespace"
},
"spec": {
"template": {
"spec": {
"serviceAccountName": "restricted-service-account",
# Add other container specifications here
}
}
}
}
# Update the deployment in the cluster
api_instance = client.AppsV1Api(api_client)
api_instance.patch_namespaced_deployment(
name="your-deployment",
namespace="your-namespace",
body=deployment_manifest
)