def test_apply_global(client: lightkube.Client): req = respx.patch("https://localhost:9443/api/v1/nodes/xy?fieldManager=test").respond( json={'metadata': {'name': 'xy'}}) node = client.apply(Node(metadata=ObjectMeta(name='xy')), field_manager='test') assert node.metadata.name == 'xy' assert req.calls[0][0].headers['Content-Type'] == "application/apply-patch+yaml" # sub-resource + force req = respx.patch("https://localhost:9443/api/v1/nodes/xx/status?fieldManager=a&force=true").respond( json={'metadata': {'name': 'xx'}}) node = client.apply(Node.Status(), name='xx', field_manager='a', force=True) assert node.metadata.name == 'xx' assert req.calls[0][0].headers['Content-Type'] == "application/apply-patch+yaml"
def test_apply_namespaced(client: lightkube.Client): req = respx.patch("https://localhost:9443/api/v1/namespaces/default/pods/xy?fieldManager=test").respond( json={'metadata': {'name': 'xy'}}) pod = client.apply(Pod(metadata=ObjectMeta(name='xy')), field_manager='test') assert pod.metadata.name == 'xy' assert req.calls[0][0].headers['Content-Type'] == "application/apply-patch+yaml" # custom namespace, force req = respx.patch("https://localhost:9443/api/v1/namespaces/other/pods/xz?fieldManager=a&force=true").respond( json={'metadata': {'name': 'xz'}}) pod = client.apply(Pod(metadata=ObjectMeta(name='xz', namespace='other')), field_manager='a', force=True) assert pod.metadata.name == 'xz' assert req.calls[0][0].headers['Content-Type'] == "application/apply-patch+yaml" # sub-resource req = respx.patch("https://localhost:9443/api/v1/namespaces/default/pods/xx/status?fieldManager=a").respond( json={'metadata': {'name': 'xx'}}) pod = client.apply(Pod.Status(), name='xx', field_manager='a') assert pod.metadata.name == 'xx' assert req.calls[0][0].headers['Content-Type'] == "application/apply-patch+yaml"
def test_apply(obj_name): client = Client(field_manager='lightkube') config = ConfigMap( apiVersion='v1', # apiVersion and kind are required for server-side apply kind='ConfigMap', metadata=ObjectMeta(name=obj_name, namespace='default'), data={'key1': 'value1', 'key2': 'value2'} ) # create with apply c = client.apply(config) try: assert c.metadata.name == obj_name assert c.data['key1'] == 'value1' assert c.data['key2'] == 'value2' # modify config.data['key2'] = 'new value' del config.data['key1'] config.data['key3'] = 'value3' c = client.apply(config) assert c.data['key2'] == 'new value' assert c.data['key3'] == 'value3' assert 'key1' not in c.data # remove all keys config.data.clear() c = client.apply(config) assert not c.data # use the patch equivalent config.data['key1'] = 'new value' c = client.patch(ConfigMap, obj_name, config.to_dict(), patch_type=PatchType.APPLY) assert c.data['key1'] == 'new value' finally: client.delete(ConfigMap, name=obj_name)
class ResourceHandler: def __init__(self, app_name, model_name): """A Lightkube API interface. Args: - app_name: name of the application - model_name: name of the Juju model this charm is deployed to """ self.app_name = app_name self.model_name = model_name self.log = logging.getLogger(__name__) # Every lightkube API call will use the model name as the namespace by default self.lightkube_client = Client(namespace=self.model_name, field_manager="lightkube") self.env = Environment(loader=FileSystemLoader('src')) def delete_resource(self, obj, namespace=None, ignore_not_found=False, ignore_unauthorized=False): try: self.lightkube_client.delete(type(obj), obj.metadata.name, namespace=namespace) except ApiError as err: self.log.exception( "ApiError encountered while attempting to delete resource.") if err.status.message is not None: if "not found" in err.status.message and ignore_not_found: self.log.error( f"Ignoring not found error:\n{err.status.message}") elif "(Unauthorized)" in err.status.message and ignore_unauthorized: # Ignore error from https://bugs.launchpad.net/juju/+bug/1941655 self.log.error( f"Ignoring unauthorized error:\n{err.status.message}") else: self.log.error(err.status.message) raise else: raise def delete_existing_resources( self, resource, namespace=None, ignore_not_found=False, ignore_unauthorized=False, labels=None, ): if labels is None: labels = {} for obj in self.lightkube_client.list( resource, labels={"app.juju.is/created-by": f"{self.app_name}"}.update(labels), namespace=namespace, ): self.delete_resource( obj, namespace=namespace, ignore_not_found=ignore_not_found, ignore_unauthorized=ignore_unauthorized, ) def apply_manifest(self, manifest, namespace=None): for obj in codecs.load_all_yaml(manifest): self.lightkube_client.apply(obj, namespace=namespace) def delete_manifest(self, manifest, namespace=None, ignore_not_found=False, ignore_unauthorized=False): for obj in codecs.load_all_yaml(manifest): self.delete_resource( obj, namespace=namespace, ignore_not_found=ignore_not_found, ignore_unauthorized=ignore_unauthorized, ) def get_custom_resource_class_from_filename(self, filename: str): """Returns a class representing a namespaced K8s resource. Args: - filename: name of the manifest file defining the resource """ # TODO: this is a generic context that is used for rendering # the manifest files. We should improve how we do this # and make it more generic. context = { 'namespace': 'namespace', 'app_name': 'name', 'name': 'generic_resource', 'request_headers': 'request_headers', 'response_headers': 'response_headers', 'port': 'port', 'service': 'service', } manifest = self.env.get_template(filename).render(context) manifest_dict = yaml.safe_load(manifest) ns_resource = codecs.from_dict(manifest_dict, client=self.lightkube_client) return type(ns_resource) def reconcile_desired_resources( self, resource, desired_resources: Union[str, TextIO, None], namespace: str = None, ) -> None: """Reconciles the desired list of resources of any kind. Args: resource: resource kind (e.g. Service, Pod) desired_resources: all desired resources in manifest form as str namespace: namespace of the resource """ existing_resources = self.lightkube_client.list( resource, labels={ "app.juju.is/created-by": f"{self.app_name}", f"app.{self.app_name}.io/is-workload-entity": "true", }, namespace=namespace, ) if desired_resources is not None: desired_resources_list = codecs.load_all_yaml(desired_resources) diff_obj = in_left_not_right(left=existing_resources, right=desired_resources_list) for obj in diff_obj: self.delete_resource(obj) self.apply_manifest(desired_resources, namespace=namespace)