예제 #1
0
def create_api_client(api="BatchV1"):
    """Create Kubernetes API client using config.

    :param api: String which represents which Kubernetes API to spawn. By
        default BatchV1.
    :returns: Kubernetes python client object for a specific API i.e. BatchV1.
    """
    k8s_config.load_incluster_config()
    api_configuration = client.Configuration()
    api_configuration.verify_ssl = False
    if api == "extensions/v1beta1":
        api_client = client.ExtensionsV1beta1Api()
    elif api == "CoreV1":
        api_client = client.CoreV1Api()
    elif api == "StorageV1":
        api_client = client.StorageV1Api()
    elif api == "AppsV1":
        api_client = client.AppsV1Api()
    elif api == "networking.k8s.io/v1beta1":
        api_client = client.NetworkingV1beta1Api()
    elif api == "CustomObjectsApi":
        api_client = client.CustomObjectsApi()
    else:
        api_client = client.BatchV1Api()
    return api_client
예제 #2
0
def resource_deploy(namespace,resourceid,serviceid,versionid,resourcename,versionname,dockerhost,imagename,buildtag,portnumber): 
  # config.load_kube_config("/home/ubuntu/.kube/config")
  config.load_kube_config("/home/app/web/kubeconfig")
  try:
    api_instance=client.AppsV1Api()
    status,status_message,data = create_deployment(api_instance,namespace,dockerhost,imagename,buildtag,resourceid,portnumber)
    api_instancestatus = status
    print("api_instancestatus: ", api_instancestatus)
    print(status_message)
    print(data)
    if status == "success":
      api_instance=client.CoreV1Api()
      status,status_message,data = create_service(api_instance,namespace,resourceid,portnumber)
      createservicestatus = status
      print("createservicestatus: ", createservicestatus)
      if status == "success":
        api_instance=client.NetworkingV1beta1Api()
        resourceurl="/"+versionname+"/"+resourcename+'/$2'
        resourceendpoint="/liveapp/"+serviceid+"/"+versionname+"/"+resourcename+'(/|$)(.*)'
        status,status_message,data = create_ingress(api_instance,namespace,resourceendpoint,resourceurl,resourceid)
        create_ingress_status = status
        print("create_ingress_status: ", create_ingress_status)
      else:
        raise Exception(data)
    else:
      raise Exception(data)
  except Exception as Error:
    status= "error"
    status_message= "Error in Deployment",
    data= str(Error)
  return(status, status_message ,data)
예제 #3
0
 def __init__(self, config_path=None):
     if config_path is None:
         config.load_incluster_config()
     else:
         config.load_kube_config(config_path)
     self._core = client.CoreV1Api()
     self._networking = client.NetworkingV1beta1Api()
예제 #4
0
    def __init__(self,
                 api_client,
                 name,
                 issuer,
                 host,
                 paths=[],
                 namespace='default'):
        self.api = client.NetworkingV1beta1Api(api_client)
        self.name = name
        self.namespace = namespace

        self._issuer = issuer
        self._host = host
        self._paths = paths

        context = {
            'name': name,
            'issuer': issuer,
            'host': host,
            'paths': paths,
        }

        config = yaml.safe_load(
            self.generate_template('ingress.yaml.j2', context))
        NetworkingV1beta1Ingress.__init__(self,
                                          api_version=config['apiVersion'],
                                          kind=config['kind'],
                                          metadata=config['metadata'],
                                          spec=config['spec'],
                                          status=None)
예제 #5
0
    def __init__(self, **kwargs):
        aToken = None
        with open('/var/run/secrets/kubernetes.io/serviceaccount/token',
                  'r') as fToken:
            aToken = fToken.read()

        # Create a configuration object
        aConfiguration = client.Configuration()

        # Specify the endpoint of your Kube cluster
        aConfiguration.host = "https://{}:{}".format(
            os.getenv('KUBERNETES_SERVICE_HOST'),
            os.getenv('KUBERNETES_SERVICE_PORT'))

        # Security part.
        # In this simple example we are not going to verify the SSL certificate of
        # the remote cluster (for simplicity reason)
        aConfiguration.verify_ssl = False
        # Nevertheless if you want to do it you can with these 2 parameters
        # configuration.verify_ssl=True
        # ssl_ca_cert is the filepath to the file that contains the certificate.
        # configuration.ssl_ca_cert="certificate"

        aConfiguration.api_key = {"authorization": "Bearer " + aToken}

        # Create a ApiClient with our config
        aApiClient = client.ApiClient(aConfiguration)

        self._client_deployment = client.AppsV1Api(aApiClient)
        self._client_service = client.CoreV1Api(aApiClient)
        self.api_instance = client.NetworkingV1beta1Api(aApiClient)
예제 #6
0
def read_ingress(site_name):
    not_set = "NOT_SET"
    k8s_settings = frappe.get_single("K8s Bench Settings")
    if not k8s_settings.namespace:
        frappe.local.response["http_status_code"] = 501
        return {
            "namespace": k8s_settings.namespace or not_set,
        }

    load_config()
    networking_v1_api = client.NetworkingV1beta1Api()
    try:
        ingress = networking_v1_api.read_namespaced_ingress(
            site_name, k8s_settings.namespace
        )
        return to_dict(ingress)
    except (ApiException, Exception) as e:
        status_code = getattr(e, "status", 500)
        out = {
            "error": e,
            "params": {"site_name": site_name, "namespace": k8s_settings.namespace},
        }

        reason = getattr(e, "reason")
        if reason:
            out["reason"] = reason

        frappe.log_error(
            out, "Exception: NetworkingV1beta1Api->read_namespaced_ingress"
        )
        frappe.local.response["http_status_code"] = status_code
        return out
def test_list_namespaced_ingress_namespace_doesnt_exist():
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    with pytest.raises(ApiException) as err:
        v1.list_namespaced_ingress("no-namespace")
    assert err.value.status == 404
    assert err.value.reason == "Not Found"
def test_list_ingress_for_all_namespaces(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    assert len(v1.list_ingress_for_all_namespaces().items) == 0
    v1.create_namespaced_ingress("default", body=body)
    assert len(v1.list_ingress_for_all_namespaces().items) == 1
예제 #9
0
def delete_ingress(name, namespace):
    try:
        config.load_kube_config("/home/app/web/kubeconfig")
        api_instance = client.NetworkingV1beta1Api()
        api_response = api_instance.delete_namespaced_ingress(name, namespace)
        return ("success", "Ingress_deleted")
    except Exception as error:
        return ("Failed", str(error))
예제 #10
0
def list_ingresses(namespace: str = 'default', core_api: Optional[client.NetworkingV1beta1Api] = None):
    # Creation of the Deployment in specified namespace
    # (Can replace "default" with a namespace you may have created)

    networking_v1_beta1_api = core_api or client.NetworkingV1beta1Api()
    try:
        return networking_v1_beta1_api.list_namespaced_ingress(namespace=namespace)
    except (ApiException, ApiTypeError) as e:
        raise api.ApiError(400, f"Error when listing ingresses in namespace {namespace}: {str(e)}")
예제 #11
0
def crea():

    config.load_kube_config()
    apps_v1_api = client.AppsV1Api()
    networking_v1_beta1_api = client.NetworkingV1beta1Api()

    create_deployment(apps_v1_api)
    create_service()
    create_ingress(networking_v1_beta1_api)
예제 #12
0
    def __init__(self, namespace=K8S_NAMESPACE):
        self.ns = namespace

        # config.load_kube_config(context="docker-desktop")
        config.load_incluster_config()

        self.app_api = client.AppsV1Api()
        self.core_api = client.CoreV1Api()
        self.net_api = client.NetworkingV1beta1Api()
예제 #13
0
def main():
    # Fetching and loading local Kubernetes Information
    config.load_kube_config()
    apps_v1_api = client.AppsV1Api()
    networking_v1_beta1_api = client.NetworkingV1beta1Api()

    create_deployment(apps_v1_api)
    create_service()
    create_ingress(networking_v1_beta1_api)
def test_patch_namespaced_ingress_ingress_doesnt_exist(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    body = generate_ingress(yaml.safe_load(ingress_yaml))
    new_body = None
    v1.create_namespaced_ingress("default", body=body)
    with pytest.raises(ValueError) as err:
        v1.patch_namespaced_ingress("no-service", "default", new_body)
    assert "Missing the required parameter `body`" in str(err.value)
 def get_resource_api(api_client: client.ApiClient = None,
                      **kwargs) -> "client.NetworkingV1beta1Api":
     """
     Returns an instance of the kubernetes API client associated with
     this object.
     """
     if api_client:
         kwargs["apl_client"] = api_client
     return client.NetworkingV1beta1Api(**kwargs)
def test_list_ingress_for_all_namespaces_label_selector(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    assert len(v1.list_ingress_for_all_namespaces().items) == 0
    v1.create_namespaced_ingress("default", body=body)
    assert len(v1.list_ingress_for_all_namespaces().items) == 1
    filtered = v1.list_ingress_for_all_namespaces(label_selector="app=foo")
    assert len(filtered.items) == 1
def test_patch_namespaced_ingress_namespace_doesnt_exist(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    body = generate_ingress(yaml.safe_load(ingress_yaml))
    new_body = None
    v1.create_namespaced_ingress("default", body=body)
    with pytest.raises(ApiException) as err:
        v1.patch_namespaced_ingress("foobar", "no-namespace", new_body)
    assert err.value.status == 404
    assert err.value.reason == "Not Found"
def test_read_namespaced_ingress_ingress_doesnt_exist(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    v1.create_namespaced_ingress("default", body=body)
    with pytest.raises(ApiException) as err:
        v1.read_namespaced_ingress("no-ingress", "default")
    assert err.value.status == 404
    assert err.value.reason == "Not Found"
def test_create_namespaced_ingress_already_exists(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    v1.create_namespaced_ingress("default", body=body)
    with pytest.raises(ApiException) as err:
        v1.create_namespaced_ingress("default", body=body)
    assert err.value.status == 409
    assert err.value.reason == "AlreadyExists"
def test_create_namespaced_ingress_wrong_kind(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    ingress_yaml = ingress_yaml.replace("kind: Ingress", "kind: Igres")
    raw_body = yaml.safe_load(ingress_yaml)
    with pytest.raises(ApiException) as err:
        body = generate_ingress(raw_body)
        v1.create_namespaced_ingress("default", body=body)
    assert err.value.status == 400
    assert err.value.reason == "Bad Request"
def test_list_namespaced_ingress_field_selector(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    v1.create_namespaced_ingress("default", body=body)
    ingresss = v1.list_namespaced_ingress("default")
    ingresss_filter = v1.list_namespaced_ingress(
        "default", field_selector="metadata.name=foobar")
    assert len(ingresss.items) == 1
    assert len(ingresss_filter.items) == 1
예제 #22
0
 def init(self, config_path: Optional[str] = None):
     # Sets config
     if config_path is None:
         kubernetes.config.load_incluster_config()
     else:
         kubernetes.config.load_kube_config(config_path)
     # Inits API clients
     self.core = client.CoreV1Api()
     self.crds = client.CustomObjectsApi()
     self._networking = client.NetworkingV1beta1Api()
     self._app = client.AppsV1Api()
예제 #23
0
    def networking_v1_beta1_api(self):
        """Networking client used for creating the ingress

        :return: networking client
        """
        if not self._networking_v1_beta1_api:
            from kubernetes import client

            self._networking_v1_beta1_api = client.NetworkingV1beta1Api(
                api_client=self._k8s_client)
        return self._networking_v1_beta1_api
def test_replace_namespaced_ingress_invalid_body(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    with pytest.raises(ApiException) as err:
        new_ingress_yaml = ingress_yaml.replace("kind: Ingress", "kind: Igres")
        body = generate_ingress(yaml.safe_load(ingress_yaml))
        new_body = generate_ingress(yaml.safe_load(new_ingress_yaml))
        v1.create_namespaced_ingress("default", body=body)
        v1.replace_namespaced_ingress("foobar", "default", new_body)
    assert err.value.status == 400
    assert err.value.reason == "Bad Request"
예제 #25
0
 def __init__(self, provider_data, deck=None):
     self._provider_data = provider_data
     self._deck = deck
     if self._deck:
         self._namespace = self._deck["environment"][0][
             "namespace"] or "default"
     else:
         self._namespace = "default"
     self._api_client = config.new_client_from_config(
         provider_data.kubeconfig_path)
     self._core_api = client.CoreV1Api(self._api_client)
     self._networking_api = client.NetworkingV1beta1Api(self._api_client)
def test_patch_namespaced_ingress(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    new_ingress_yaml = ingress_yaml.replace('"app": "foo"', '"app": "bar"')
    body = generate_ingress(yaml.safe_load(ingress_yaml))
    new_body = generate_ingress(yaml.safe_load(new_ingress_yaml))
    new_ingress = v1.create_namespaced_ingress("default", body=body)
    assert new_ingress.metadata.labels == {"app": "foo"}
    patched_ingress = v1.patch_namespaced_ingress("foobar", "default",
                                                  new_body)
    assert new_ingress == patched_ingress
    assert patched_ingress.metadata.labels == {"app": "bar"}
def test_create_namespaced_ingress(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    assert len(v1.list_ingress_for_all_namespaces().items) == 0
    new_ingress = v1.create_namespaced_ingress("default", body=body)
    assert new_ingress.metadata.name == "foobar"
    assert new_ingress.metadata.namespace == "default"
    assert new_ingress.kind == "Ingress"
    assert new_ingress.spec.rules[0].host == "example.com"
    assert len(v1.list_ingress_for_all_namespaces().items) == 1
def test_read_namespaced_ingress(ingress_yaml):
    config.load_kube_config()
    v1 = client.NetworkingV1beta1Api()
    raw_body = yaml.safe_load(ingress_yaml)
    body = generate_ingress(raw_body)
    new_ingress = v1.create_namespaced_ingress("default", body=body)
    read_ingress = v1.read_namespaced_ingress("foobar", "default")
    assert read_ingress.metadata.name == "foobar"
    assert read_ingress.metadata.namespace == "default"
    assert read_ingress.metadata.labels == {"app": "foo"}
    assert read_ingress.kind == "Ingress"
    assert read_ingress == new_ingress
def main():
    # Configs can be set in Configuration class directly or using helper utility
    config_file = os.path.join(os.path.dirname(__file__), "kubeconfig-sa")
    config.load_kube_config(config_file)

    v1 = client.CoreV1Api()
    namespace = "spark-jobs"

    options = []
    # List all Spark applications
    ret = v1.list_namespaced_pod(namespace=namespace,
                                 label_selector="spark-role=driver")
    for pod in ret.items:
        options.append(pod.metadata.labels["app-name"])

    title = "Please choose a Spark Application: "
    if options:
        app_name, index = pick(options, title)
    else:
        print("No Spark app")
        sys.exit()

    # ---

    # NodePort URL
    # ui_service_name = app_name + "-ui-svc"
    # core_v1_api = client.CoreV1Api()
    #
    # ui_svc = core_v1_api.read_namespaced_service(name=ui_service_name, namespace="spark-jobs")
    # node_port = ui_svc.spec.ports[0].node_port
    #
    # # Choose a random node
    # nodes = core_v1_api.list_node()
    # n = random.randint(0, len(nodes.items) - 1)
    # node = nodes.items[n]
    #
    # external_ip = filter(lambda addr: addr.type == "ExternalIP", node.status.addresses)[0].address
    #
    # print("The Spark Web UI is available at http://%s:%s" % (external_ip, node_port))

    # Ingress URL
    ui_ingress_name = app_name + "-ui-ingress"
    networking_v1_beta1_api = client.NetworkingV1beta1Api()
    ui_ingress_status = networking_v1_beta1_api.read_namespaced_ingress_status(
        name=ui_ingress_name, namespace="spark-jobs")

    if ui_ingress_status.status.load_balancer.ingress is not None:
        external_ip = ui_ingress_status.status.load_balancer.ingress[0].ip
        print("The Spark Web UI is available at http://%s/%s" %
              (external_ip, app_name))
    else:
        print("Ingress not yet available")
예제 #30
0
 def __init__(self, config_path=None):
     if config_path is None:
         config.load_incluster_config()
     else:
         config.load_kube_config(config_path)
     self._core = client.CoreV1Api()
     self._networking = client.NetworkingV1beta1Api()
     self._app = client.AppsV1Api()
     self._custom_object = client.CustomObjectsApi()
     self._client = client.ApiClient()
     self._api_server_url = 'http://{}:{}'.format(
         os.environ.get('FL_API_SERVER_HOST', 'fedlearner-apiserver'),
         os.environ.get('FL_API_SERVER_PORT', 8101))