def _get_config_key_completion(self): resource = self._get_one_resource_value() namespace = self.client.get_namespace( self._get_kube_api_resource_type(), resource) result = self._read_namespaced_resource(resource, namespace) prompts = [] ObjectUtils.get_nodes(result.to_dict(), prompts) return CompletionContent(self._get_completion(prompts))
def _resource_completion(self, short_mode=True): results = [] kube_resource_type = self._get_kube_resource_type() completion_file_name = pkg_resources.resource_filename( 'omc_kube.assets', '_%(kube_resource_type)s_completion.json' % locals()) prompts = [] # get_all_dict_Keys(result.to_dict(), prompts) with open(completion_file_name) as f: result = json.load(f) ObjectUtils.get_nodes(result, prompts) results.extend(self._get_completion(prompts)) return CompletionContent(results)
def delete(self): 'delete node by configuration key' # todo@rain: to support delete entired resource and completion cache if 'completion' in self._get_params(): resource = self._get_one_resource_value() namespace = self.client.get_namespace( self._get_kube_api_resource_type(), resource) result = self._read_namespaced_resource(resource, namespace) prompts = [] ObjectUtils.get_nodes(result.to_dict(), prompts) self._print_completion(prompts) return config_key = self._get_one_resource_value() parent_resource = self._get_one_resource_value( self._get_kube_resource_type()) namespace = self.client.get_namespace( self._get_kube_api_resource_type(), parent_resource) the_result = self._read_namespaced_resource(parent_resource, namespace, _preload_content=False) result = json.loads(the_result.data.decode('UTF-8')) params = self._get_action_params() config_value = params[0] if len(params) > 0 else None # orig_value = ObjectUtils.get_node(result, config_key) # # convert type # config_value = type(orig_value)(config_value) # set_obj_value(result, config_key, config_value) # todo: use apply instead once apply provided patch_func = getattr( self.client, 'patch_namespaced_' + self._get_kube_api_resource_type()) # patch_object = utils.build_object(config_key, config_value) patch_object = StrategicMergePatch.get_instance( ).gen_strategic_merge_patch(result, config_key.split('.'), config_value, 'delete', []) new_result = patch_func(parent_resource, namespace, patch_object) console.log(new_result)
def set(self): 'update restore by configuration key' # https://github.com/kubernetes/kubernetes/blob/cea1d4e20b4a7886d8ff65f34c6d4f95efcb4742/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch/patch.go # implement Strategic merge patch myself # https://stackoverflow.com/questions/36307950/kubernetes-api-call-equivalent-to-kubectl-apply if 'completion' in self._get_params(): resource = self._get_one_resource_value() namespace = self.client.get_namespace( self._get_kube_api_resource_type(), resource) result = self._read_namespaced_resource(resource, namespace) prompts = [] ObjectUtils.get_nodes(result.to_dict(), prompts) self._print_completion(prompts) return config_key = self._get_one_resource_value() parent_resource = self._get_one_resource_value( self._get_kube_resource_type()) namespace = self.client.get_namespace( self._get_kube_api_resource_type(), parent_resource) result = self._read_namespaced_resource(parent_resource, namespace) params = self._get_action_params() config_value = params[0] orig_value = ObjectUtils.get_node(result, config_key) # convert type config_value = type(orig_value)(config_value) ObjectUtils.set_node(result, config_key, config_value) # todo: use apply instead once apply provided patch_func = getattr( self.client, 'patch_namespaced_' + self._get_kube_api_resource_type()) # patch_object = utils.build_object(config_key, config_value) patch_object = StrategicMergePatch.get_instance( ).gen_strategic_merge_patch(result, config_key.split('.'), config_value, 'set', []) new_result = patch_func(parent_resource, namespace, patch_object) console.log(new_result)