2

I have below configmap.yml i want to patch/update date field from python script from container in kubernates deployment i searched various side but couldn't get any reference to do that. Any reference or code sample would be a great help

apiVersion: v1
kind: ConfigMap
metadata:
  name: sample-configmap
  labels:
    app: test
    parameter-type: sample
data:
  storage.ini: |
    [DateInfo]
    date=1970-01-01T00:00:00.01Z

I went through this reference code but couldn't figure out what will be content of body and which parameter i should use and which parameter i should neglect

partially update the specified ConfigMap

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest
import ApiException
from pprint import pprint

configuration = kubernetes.client.Configuration()
# Configure API key authorization: BearerToken configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'
    
# Defining host is optional and default to http://localhost configuration.host = "http://localhost"
    
# Enter a context with an instance of the API kubernetes.client
with kubernetes.client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = kubernetes.client.CoreV1Api(api_client)
    name = 'name_example' # str | name of the ConfigMap
    namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
    body = None # object |
    pretty = 'pretty_example'
    dry_run = 'dry_run_example'
    field_manager = 'field_manager_example'
    force = True 
    
try:
    api_response = api_instance.patch_namespaced_config_map(name, namespace, body, pretty=pretty, dry_run=dry_run, field_manager=field_manager, force=force)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e)
Zoro
  • 420
  • 5
  • 16
Sahil Saxena
  • 131
  • 4
  • 11

1 Answers1

6

The body parameter in patch_namespaced_config_map is the actual configmap data that you want to patch and needs to be first obtained with read_namespaced_config_map.

Following steps are required for all the operations that have the body argument:

  1. Get the data using the read_*/get_*method
  2. Use the data returned in the first step in the API modifying the object.

Further, for most cases, it is enough to pass the required arguments namely name, namespace and body but here is the info about each:

Parameters

Name Type Description Notes
name str name of the ConfigMap
namespace str object name and auth scope, such as for teams and projects
body object
pretty str If 'true', then the output is pretty printed. [optional]
dry_run 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]
field_manager str fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). [optional]
force bool Force is going to "force" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests. [optional]

Review the K8s python client README for the list of all supported APIs and their usage.

Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35