I can't create services account via python

5/25/2020

I try to create a service account via kubernetes client of Python, but in the post return the dict with the manifest, but do not create the service account.

My code is :

import kubernetes.client
from kubernetes import client, config

from kubernetes.client.rest import ApiException
from pprint import pprint
from kubernetes import config



config.load_kube_config()
client.configuration.debug = True
v1 = client.CoreV1Api()
# create an instance of the API class
namespace = 'users' # str | object name and auth scope, such as for teams and projects
body = {'metadata': {'name': 'test.david'} }

pretty = 'true'
dry_run = 'All' # str | When present, indicates that modifications should not be persisted. 
An invalid or unrecognized dryRun directive will result in an error response and no further 
processing of the request. Valid values are: - All: all dry run stages will be processed 
(optional)
try:
   api_response = v1.create_namespaced_service_account(namespace,body,dry_run=dry_run, 
   pretty=pretty)
   pprint(api_response)
except ApiException as e:
   print("Exception when calling CoreV1Api->create_namespaced_service_account: %s\n" % e)

The response :

{'api_version': 'v1',
'automount_service_account_token': None,
'image_pull_secrets': None,
'kind': 'ServiceAccount',
'metadata': {'annotations': None,
          'cluster_name': None,
          'creation_timestamp': datetime.datetime(2020, 5, 25, 23, 30, 26, tzinfo=tzutc()),
          'deletion_grace_period_seconds': None,
          'deletion_timestamp': None,
          'finalizers': None,
          'generate_name': None,
          'generation': None,
          'initializers': None,
          'labels': None,
          'managed_fields': None,
          'name': 'test.david',
          'namespace': 'users',
          'owner_references': None,
          'resource_version': None,
          'self_link': '/api/v1/namespaces/users/serviceaccounts/test.david',
          'uid': 'b64cff7c-9edf-11ea-8b22-0a714f906f03'},
'secrets': None}

what am I doing wrong?

-- david fernandez
kubernetes
kubernetes-python-client

1 Answer

5/28/2020

Need to set

dry_run = ''

As when dry_run present, indicates that modifications should not be persisted.

-- hoque
Source: StackOverflow