class Client(object): def __init__(self, config, provider): ''' Args: config (obj): Object of the configuration data provider (str): String value of the provider that is being used ''' self.config = config self.provider = provider # Choose the type of provider that is being used. Error out if it is not available if provider is "kubernetes": self.connection = KubeKubernetesClient(config) logger.debug("Using Kubernetes Provider KubeClient library") elif provider is "openshift": self.connection = KubeOpenshiftClient(config) logger.debug("Using OpenShift Provider KubeClient library") else: raise KubeClientError("No provider by that name.") # Create an object using its respective API def create(self, obj, namespace="default"): self.connection.create(obj, namespace) # Delete an object using its respective API def delete(self, obj, namespace="default"): self.connection.delete(obj, namespace) # Current support: kubernetes only def namespaces(self): return self.connection.namespaces()
def test_k8s_delete(mock_class): # Mock the API class mock_class.return_value = FakeClient() mock_class.kind_to_resource_name.return_value = 'Pod' k8s_object = { "apiVersion": "v1", "kind": "Pod", "metadata": { "labels": { "app": "helloapache" }, "name": "helloapache" }, "spec": { "containers": [{ "image": "$image", "name": "helloapache", "ports": [{ "containerPort": 80, "hostPort": 80, "protocol": "TCP" }] }] } } a = KubeOpenshiftClient(config) a.delete(k8s_object, "foobar")
def test_oc_delete(mock_class): mock_class.return_value = FakeClient() mock_class.kind_to_resource_name.return_value = 'Route' oc_object = {"apiVersion": "v1", "kind": "Route", "metadata": {"labels": {"name": "helloapache-route"}, "name": "helloapache-route"}, "spec": { "host": "$endpoint", "to": [{"kind": "Service", "name": "helloapache-svc"}]}} a = KubeOpenshiftClient(config) a.delete(oc_object, "foobar")
def test_delete(mock_class): # Mock the API class mock_class.return_value = FakeClient() mock_class.kind_to_resource_name.return_value = 'Pod' k8s_object = {"apiVersion": "v1", "kind": "Pod", "metadata": {"labels": {"app": "helloapache"}, "name": "helloapache"}, "spec": { "containers": [{"image": "$image", "name": "helloapache", "ports": [{"containerPort": 80, "hostPort": 80, "protocol": "TCP"}]}]}} a = KubeOpenshiftClient(config) a.delete(k8s_object, "foobar")
def test_process_template(mock_class): # Mock the API class mock_class.return_value = FakeOpenshiftTemplateClient() mock_class.kind_to_resource_name.return_value = 'template' openshift_template = {"kind": "Template", "apiVersion": "v1", "metadata": {"name": "foobar"}, "objects": [{"kind": "Service", "apiVersion": "v1", "metadata": {"name": "cakephp-mysql-example", "annotations": { "description": "Exposes and load balances the application pods"}}, "spec": {"ports": [{"name": "web", "port": 8080, "targetPort": 8080}], "selector": {"name": "cakephp-mysql-example"}}}]} a = KubeOpenshiftClient(config) a.create(openshift_template, "foobar") a.delete(openshift_template, "foobar")
def test_oc_delete(mock_class): mock_class.return_value = FakeClient() mock_class.kind_to_resource_name.return_value = 'Route' oc_object = { "apiVersion": "v1", "kind": "Route", "metadata": { "labels": { "name": "helloapache-route" }, "name": "helloapache-route" }, "spec": { "host": "$endpoint", "to": [{ "kind": "Service", "name": "helloapache-svc" }] } } a = KubeOpenshiftClient(config) a.delete(oc_object, "foobar")