def test_replication_controller_apis(self): client = DynamicClient(api_client.ApiClient(configuration=self.config)) api = client.resources.get( api_version='v1', kind='ReplicationController') name = 'frontend-' + short_uuid() rc_manifest = { 'apiVersion': 'v1', 'kind': 'ReplicationController', 'metadata': {'labels': {'name': name}, 'name': name}, 'spec': {'replicas': 2, 'selector': {'name': name}, 'template': {'metadata': { 'labels': {'name': name}}, 'spec': {'containers': [{ 'image': 'nginx', 'name': 'nginx', 'ports': [{'containerPort': 80, 'protocol': 'TCP'}]}]}}}} resp = api.create( body=rc_manifest, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertEqual(2, resp.spec.replicas) resp = api.get( name=name, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertEqual(2, resp.spec.replicas) api.delete( name=name, namespace='default', propagation_policy='Background')
def test_create_deployment(self): client = api_client.ApiClient(configuration=self.config) api = apps_v1_api.AppsV1Api(client) name = 'nginx-deployment-' + str(uuid.uuid4()) deployment = '''apiVersion: apps/v1 kind: Deployment metadata: name: %s spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.15.4 ports: - containerPort: 80 ''' resp = api.create_namespaced_deployment(body=yaml.safe_load( deployment % name), namespace="default") resp = api.read_namespaced_deployment(name, 'default') self.assertIsNotNone(resp) options = v1_delete_options.V1DeleteOptions() resp = api.delete_namespaced_deployment(name, 'default', body=options)
def get_service_url(v1_service): print("Getting service url...") node_port = v1_service.spec.ports[0].node_port # print("node_port", node_port) # cluster_ip = v1_service.spec.cluster_ip # print("cluster_ip", cluster_ip) # lb_ip = v1_service.spec.load_balancer_ip # print("lb_ip", lb_ip) config = k8s_config.Configuration() client1 = api_client.ApiClient(configuration=config) v1 = core_v1_api.CoreV1Api(client1) nodes = v1.list_node() addresses = nodes.items[0].status.addresses node_ip = None for addr in addresses: if addr.type == 'ExternalIP': node_ip = addr.address # print(node_ip) # print("trying internal ips") if not node_ip: for addr in addresses: if addr.type == 'InternalIP': node_ip = addr.address # print(node_ip) pod_service_url = 'http://%s:%s' % (node_ip, node_port) return pod_service_url
def test_configmap_apis(self): client = DynamicClient(api_client.ApiClient(configuration=self.config)) api = client.resources.get(api_version='v1', kind='ConfigMap') name = 'test-configmap-' + short_uuid() test_configmap = { "kind": "ConfigMap", "apiVersion": "v1", "metadata": { "name": name, }, "data": { "config.json": "{\"command\":\"/usr/bin/mysqld_safe\"}", "frontend.cnf": "[mysqld]\nbind-address = 10.0.0.3\n" } } resp = api.create(body=test_configmap, namespace='default') self.assertEqual(name, resp.metadata.name) resp = api.get(name=name, namespace='default') self.assertEqual(name, resp.metadata.name) test_configmap['data']['config.json'] = "{}" resp = api.patch(name=name, namespace='default', body=test_configmap) resp = api.delete(name=name, body={}, namespace='default') resp = api.get(namespace='default', pretty=True) self.assertEqual([], resp.items)
def test_replication_controller_apis(self): client = api_client.ApiClient(configuration=self.config) api = core_v1_api.CoreV1Api(client) name = 'frontend-' + short_uuid() rc_manifest = { 'apiVersion': 'v1', 'kind': 'ReplicationController', 'metadata': {'labels': {'name': name}, 'name': name}, 'spec': {'replicas': 2, 'selector': {'name': name}, 'template': {'metadata': { 'labels': {'name': name}}, 'spec': {'containers': [{ 'image': 'nginx', 'name': 'nginx', 'ports': [{'containerPort': 80, 'protocol': 'TCP'}]}]}}}} resp = api.create_namespaced_replication_controller( body=rc_manifest, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertEqual(2, resp.spec.replicas) resp = api.read_namespaced_replication_controller( name=name, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertEqual(2, resp.spec.replicas) resp = api.delete_namespaced_replication_controller( name=name, body={}, namespace='default')
def test_configmap_apis(self): client = api_client.ApiClient(configuration=self.config) api = core_v1_api.CoreV1Api(client) name = 'test-configmap-' + short_uuid() test_configmap = { "kind": "ConfigMap", "apiVersion": "v1", "metadata": { "name": name, }, "data": { "config.json": "{\"command\":\"/usr/bin/mysqld_safe\"}", "frontend.cnf": "[mysqld]\nbind-address = 10.0.0.3\nport = 3306\n" } } resp = api.create_namespaced_config_map( body=test_configmap, namespace='default' ) self.assertEqual(name, resp.metadata.name) resp = api.read_namespaced_config_map( name=name, namespace='default') self.assertEqual(name, resp.metadata.name) test_configmap['data']['config.json'] = "{}" resp = api.patch_namespaced_config_map( name=name, namespace='default', body=test_configmap) resp = api.delete_namespaced_config_map( name=name, body={}, namespace='default') resp = api.list_namespaced_config_map('default', pretty=True) self.assertEqual([], resp.items)
def _get_k8s_api_client(self): cluster_uuid = self.context["tenant"]["cluster"] cluster = self._get_cluster(cluster_uuid) cluster_template = self._get_cluster_template( cluster.cluster_template_id) key_file = None cert_file = None ca_certs = None if not cluster_template.tls_disabled: dir = self.context["ca_certs_directory"] key_file = cluster_uuid + ".key" key_file = os.path.join(dir, key_file) cert_file = cluster_uuid + ".crt" cert_file = os.path.join(dir, cert_file) ca_certs = cluster_uuid + "_ca.crt" ca_certs = os.path.join(dir, ca_certs) if hasattr(k8s_config, "ConfigurationObject"): # k8sclient < 4.0.0 config = k8s_config.ConfigurationObject() else: config = k8s_config.Configuration() config.host = cluster.api_address config.ssl_ca_cert = ca_certs config.cert_file = cert_file config.key_file = key_file client = api_client.ApiClient(config=config) return core_v1_api.CoreV1Api(client)
def test_job_apis(self): client = api_client.ApiClient('http://127.0.0.1:8080/') api = batch_v1_api.BatchV1Api(client) name = 'test-job-' + str(uuid.uuid4()) job_manifest = { 'kind': 'Job', 'spec': { 'template': {'spec': {'containers': [ {'image': 'busybox', 'name': name, 'command': ["sh", "-c", "sleep 5"] }], 'restartPolicy': 'Never'}, 'metadata': {'name': name}}}, 'apiVersion': 'batch/v1', 'metadata': {'name': name}} resp = api.create_namespaced_job( body=job_manifest, namespace='default') self.assertEqual(name, resp.metadata.name) resp = api.read_namespaced_job( name=name, namespace='default') self.assertEqual(name, resp.metadata.name) resp = api.delete_namespaced_job( name=name, body={}, namespace='default')
def test_portforward_http(self): client = api_client.ApiClient(configuration=self.config) api = core_v1_api.CoreV1Api(client) name = 'portforward-http-' + short_uuid() pod_manifest = { 'apiVersion': 'v1', 'kind': 'Pod', 'metadata': { 'name': name }, 'spec': { 'containers': [{ 'name': 'nginx', 'image': 'nginx', }] } } resp = api.create_namespaced_pod(body=pod_manifest, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertTrue(resp.status.phase) while True: resp = api.read_namespaced_pod(name=name, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertTrue(resp.status.phase) if resp.status.phase != 'Pending': break time.sleep(1) def kubernetes_create_connection(address, *args, **kwargs): dns_name = address[0] if isinstance(dns_name, bytes): dns_name = dns_name.decode() dns_name = dns_name.split(".") if len(dns_name) != 3 or dns_name[2] != "kubernetes": return socket_create_connection(address, *args, **kwargs) pf = portforward(api.connect_get_namespaced_pod_portforward, dns_name[0], dns_name[1], ports=str(address[1])) return pf.socket(address[1]) socket_create_connection = socket.create_connection try: socket.create_connection = kubernetes_create_connection response = urllib_request.urlopen('http://%s.default.kubernetes/' % name) html = response.read().decode('utf-8') finally: socket.create_connection = socket_create_connection self.assertEqual(response.code, 200) self.assertTrue('<h1>Welcome to nginx!</h1>' in html) resp = api.delete_namespaced_pod(name=name, body={}, namespace='default')
def test_node_apis(self): client = DynamicClient(api_client.ApiClient(configuration=self.config)) api = client.resources.get(api_version='v1', kind='Node') for item in api.get().items: node = api.get(name=item.metadata.name) self.assertTrue(len(dict(node.metadata.labels)) > 0)
def test_node_apis(self): client = api_client.ApiClient(config=self.config) api = core_v1_api.CoreV1Api(client) for item in api.list_node().items: node = api.read_node(name=item.metadata.name) self.assertTrue(len(node.metadata.labels) > 0) self.assertTrue(isinstance(node.metadata.labels, dict))
def get_api(self): configuration = client.Configuration() configuration.host = self.url configuration.verify_ssl = False configuration.api_key = {"authorization": "Bearer " + self.token} c = api_client.ApiClient(configuration=configuration) api = core_v1_api.CoreV1Api(c) return api
def handler(event: Dict[str, Any], context: Optional[Dict[str, Any]]) -> Any: create_kubeconfig() api_CoreV1 = client.CoreV1Api() userspace_dc = dynamic.DynamicClient(client=api_client.ApiClient()).resources.get( group=ORBIT_API_GROUP, api_version=ORBIT_API_VERSION, kind=USERSPACE_CR_KIND ) manage_user_namespace(event, api_CoreV1, userspace_dc)
def main(): # Creating a dynamic client client = dynamic.DynamicClient( api_client.ApiClient(configuration=config.load_kube_config()) ) # fetching the configmap api api = client.resources.get(api_version="v1", kind="ConfigMap") configmap_name = "test-configmap" configmap_manifest = { "kind": "ConfigMap", "apiVersion": "v1", "metadata": { "name": configmap_name, "labels": { "foo": "bar", }, }, "data": { "config.json": '{"command":"/usr/bin/mysqld_safe"}', "frontend.cnf": "[mysqld]\nbind-address = 10.0.0.3\n", }, } # Creating configmap `test-configmap` in the `default` namespace configmap = api.create(body=configmap_manifest, namespace="default") print("\n[INFO] configmap `test-configmap` created\n") # Listing the configmaps in the `default` namespace configmap_list = api.get( name=configmap_name, namespace="default", label_selector="foo=bar" ) print("NAME:\n%s\n" % (configmap_list.metadata.name)) print("DATA:\n%s\n" % (configmap_list.data)) # Updating the configmap's data, `config.json` configmap_manifest["data"]["config.json"] = "{}" configmap_patched = api.patch( name=configmap_name, namespace="default", body=configmap_manifest ) print("\n[INFO] configmap `test-configmap` patched\n") print("NAME:\n%s\n" % (configmap_patched.metadata.name)) print("DATA:\n%s\n" % (configmap_patched.data)) # Deleting configmap `test-configmap` from the `default` namespace configmap_deleted = api.delete(name=configmap_name, body={}, namespace="default") print("\n[INFO] configmap `test-configmap` deleted\n")
def setUpClass(cls): config = base.get_e2e_configuration() cls.client = DynamicClient(api_client.ApiClient(configuration=config)) cls.pod_manifest = { 'apiVersion': 'v1', 'kind': 'Pod', 'metadata': {'name': 'foo-pod'}, 'spec': {'containers': [{'name': "main", 'image': "busybox"}]}, }
def preferred_client(cls) -> dynamic.DynamicClient: c = cls.api_clients.get("preferred") if c is None: raise ValueError( f"no preferred api client defined for object {cls.__name__}", ) return c(client=api_client.ApiClient()).resources.get( group=cls.group, api_version=cls.api_version, kind=cls.kind )
def __init__(self, spec, name_generator=None, atomic_inst=None): super(Kubernetes, self).__init__(None, name_generator=name_generator, atomic_inst=atomic_inst) self._spec = spec # NOTE(andreykurilin): KubernetesClient doesn't provide any __version__ # property to identify the client version (you are welcome to fix # this code if I'm wrong). Let's check for some backward incompatible # changes to identify the way to communicate with it. if hasattr(k8s_config, "ConfigurationObject"): # Actually, it is `k8sclient < 4.0.0`, so it can be # kubernetesclient 2.0 or even less, but it doesn't make any # difference for us self._k8s_client_version = 3 else: self._k8s_client_version = 4 if self._k8s_client_version == 3: config = k8s_config.ConfigurationObject() else: config = k8s_config.Configuration() config.host = self._spec["server"] config.ssl_ca_cert = self._spec["certificate-authority"] if self._spec.get("api_key"): config.api_key = {"authorization": self._spec["api_key"]} if self._spec.get("api_key_prefix"): config.api_key_prefix = { "authorization": self._spec["api_key_prefix"] } else: config.cert_file = self._spec["client-certificate"] config.key_file = self._spec["client-key"] if self._spec.get("tls_insecure", False): config.verify_ssl = False if self._k8s_client_version == 3: api = api_client.ApiClient(config=config) else: api = api_client.ApiClient(configuration=config) self.api = api self.v1_client = core_v1_api.CoreV1Api(api)
def setUpClass(cls): super(BaseK8sTest, cls).setUpClass() cls.kube_api_url = cls.cs.clusters.get(cls.cluster.uuid).api_address config = k8s_config.Configuration() config.host = cls.kube_api_url config.ssl_ca_cert = cls.ca_file config.cert_file = cls.cert_file config.key_file = cls.key_file k8s_client = api_client.ApiClient(configuration=config) cls.k8s_api = core_v1_api.CoreV1Api(k8s_client)
def __init__(self, config_file=None) -> None: self.log = getLogger(f'{__name__}.{self.__class__.__name__}') if config_file is None: try: config.load_kube_config() except: config.load_incluster_config() self.api = client.AppsV1Api() else: self.api = apps_v1_api.AppsV1Api(api_client.ApiClient(configuration=config_file))
def test_cache_decoder_resource_and_subresource(self): client = DynamicClient(api_client.ApiClient(configuration=self.config)) # first invalidate cache client.resources.invalidate_cache() # do Discoverer.__init__ client = DynamicClient(api_client.ApiClient(configuration=self.config)) # the resources of client will use _cache['resources'] in memory deploy1 = client.resources.get(kind='Deployment') # do Discoverer.__init__ client = DynamicClient(api_client.ApiClient(configuration=self.config)) # the resources of client will use _cache['resources'] decode from cache file deploy2 = client.resources.get(kind='Deployment') # test Resource is the same self.assertTrue(deploy1 == deploy2) # test Subresource is the same self.assertTrue(deploy1.status == deploy2.status)
def get_k8s_clients(conf): config = k8s_config.Configuration() config.host = conf.kubernetes.kube_host config.verify_ssl = False client = api_client.ApiClient(configuration=config) v1 = core_v1_api.CoreV1Api(client) v1extention = extensions_v1beta1_api.ExtensionsV1beta1Api(client) clients = {'v1': v1, 'v1extention': v1extention} return clients
def setUp(self): super(BaseK8sTest, self).setUp() self.kube_api_url = self.cs.clusters.get(self.cluster.uuid).api_address config = k8s_config.Configuration() config.host = self.kube_api_url config.ssl_ca_cert = self.ca_file config.cert_file = self.cert_file config.key_file = self.key_file k8s_client = api_client.ApiClient(configuration=config) self.k8s_api = core_v1_api.CoreV1Api(k8s_client) # TODO(coreypobrien) https://bugs.launchpad.net/magnum/+bug/1551824 utils.wait_for_condition(self._is_api_ready, 5, 600)
def test_service_apis(self): client = api_client.ApiClient(configuration=self.config) api = core_v1_api.CoreV1Api(client) name = 'frontend-' + short_uuid() service_manifest = { 'apiVersion': 'v1', 'kind': 'Service', 'metadata': { 'labels': { 'name': name }, 'name': name, 'resourceversion': 'v1' }, 'spec': { 'ports': [{ 'name': 'port', 'port': 80, 'protocol': 'TCP', 'targetPort': 80 }], 'selector': { 'name': name } } } resp = api.create_namespaced_service(body=service_manifest, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertTrue(resp.status) resp = api.read_namespaced_service(name=name, namespace='default') self.assertEqual(name, resp.metadata.name) self.assertTrue(resp.status) service_manifest['spec']['ports'] = [{ 'name': 'new', 'port': 8080, 'protocol': 'TCP', 'targetPort': 8080 }] resp = api.patch_namespaced_service(body=service_manifest, name=name, namespace='default') self.assertEqual(2, len(resp.spec.ports)) self.assertTrue(resp.status) resp = api.delete_namespaced_service(name=name, body={}, namespace='default')
def test_service_apis(): from kubernetes import client k8s_url = 'https://127.0.0.1:6443' with open('token.txt', 'r') as file: token = file.read().strip('\n') configuration = client.Configuration() configuration.host = k8s_url configuration.verify_ssl = False configuration.api_key = {"authorization": "Bearer " + token} client1 = api_client.ApiClient(configuration=configuration) api = core_v1_api.CoreV1Api(client1) ret = api.list_pod_for_all_namespaces(watch=False) print(ret)
def refresh_api_client(self) -> None: c = self.api_clients.get(self.version) # If we didn't find the client in the api_clients dict, use the # preferred version. if c is None: c = self.api_clients.get("preferred") if c is None: raise ValueError( "unknown version specified and no preferred version " f"defined for resource ({self.version})" ) # If we did find it, initialize that client version. self._api_client = c(client=api_client.ApiClient()).resources.get( group=self.group, api_version=self.api_version, kind=self.kind )
def create(context, conf, **kwargs): register_opts(conf) client_config = conf.k8s_client LOG.info('Creating the kubernetes client with url %s.', client_config.k8s_host) config = ConfigurationObject() config.host = client_config.k8s_host config.ssl_ca_cert = client_config.k8s_ssl_ca_cert config.cert_file = client_config.k8s_cert_file config.key_file = client_config.k8s_key_file k8s_api_client = api_client.ApiClient(config=config) k8s_core_v1_api = client.CoreV1Api(k8s_api_client) return k8s_core_v1_api
def get_api(api_name): client = dynamic.DynamicClient( api_client.ApiClient(configuration=config.load_kube_config())) _apis = { "msp": (lambda: client.resources.get(api_version="openebs.io/v1alpha1", kind="MayastorPool")), "msv": (lambda: client.resources.get(api_version="openebs.io/v1alpha1", kind="MayastorVolume")), "pvc": (lambda: client.resources.get(api_version="v1", kind="PersistentVolumeClaim")), "pod": (lambda: client.resources.get(api_version="v1", kind="Pod")), } return _apis[api_name]()
def get_k8sClient(self, auth_plugin): config = client.ConfigurationObject() config.host = auth_plugin['auth_url'] if ('username' in auth_plugin) and ('password' in auth_plugin): config.username = auth_plugin['username'] config.password = auth_plugin['password'] else: config.api_key_prefix['authorization'] = 'Bearer' config.api_key['authorization'] = auth_plugin['bearer_token'] if auth_plugin['ssl_ca_cert'] is not None: config.ssl_ca_cert = auth_plugin['ssl_ca_cert'] config.verify_ssl = True else: config.verify_ssl = False k8s_client = api_client.ApiClient(config=config) return k8s_client
def __init__(self, config, manifest_path=None): """ A class to configure k8s after boot Args: config (str): File path for the kubernetes configuration file manfiest_path (str): Path for kubernetes manifests to be applied """ self.config = config if not manifest_path: manifest_path = MANIFESTSPATH self.manifest_path = manifest_path kube_config.load_kube_config(config_file=config) config = Configuration() self.api = k8sclient.CoreV1Api() self.client = api_client.ApiClient(configuration=config)
def test_watch_configmaps(self): client = api_client.ApiClient(configuration=self.config) api = core_v1_api.CoreV1Api(client) # create a configmap name_a = 'configmap-a-' + short_uuid() configmap_a = config_map_with_value(name_a, "a") api.create_namespaced_config_map( body=configmap_a, namespace='default') # list all configmaps and extract the resource version resp = api.list_namespaced_config_map('default', label_selector="e2e-tests=true") rv = resp.metadata.resource_version # create another configmap name_b = 'configmap-b-' + short_uuid() configmap_b = config_map_with_value(name_b, "b") api.create_namespaced_config_map( body=configmap_b, namespace='default') # patch configmap b configmap_b['data']['config'] = "{}" api.patch_namespaced_config_map( name=name_b, namespace='default', body=configmap_b) # delete all configmaps api.delete_collection_namespaced_config_map( namespace='default', label_selector="e2e-tests=true") w = watch.Watch() # expect to observe all events happened after the initial LIST expect = ['ADDED', 'MODIFIED', 'DELETED', 'DELETED'] i = 0 # start watching with the resource version we got from the LIST for event in w.stream(api.list_namespaced_config_map, namespace='default', resource_version=rv, timeout_seconds=5, label_selector="e2e-tests=true"): self.assertEqual(event['type'], expect[i]) # Kubernetes doesn't guarantee the order of the two objects # being deleted if i < 2: self.assertEqual(event['object'].metadata.name, name_b) i = i + 1 self.assertEqual(i, 4)