Пример #1
0
 def k8s_object(self):
     depl = client.AppsV1beta1Deployment(
         metadata=client.V1ObjectMeta(
             name=self.name,
             labels=self.labels
         ),
         spec=client.AppsV1beta1DeploymentSpec(
             strategy=client.AppsV1beta1DeploymentStrategy(
                 type='RollingUpdate',
                 rolling_update=client.AppsV1beta1RollingUpdateDeployment(
                     max_surge=0
                 )
             ),
             template=client.V1PodTemplateSpec(
                 metadata=client.V1ObjectMeta(
                     labels=self.template_labels),
                 spec=client.V1PodSpec(
                     affinity=client.V1Affinity(
                         pod_anti_affinity=client.V1PodAntiAffinity(
                             required_during_scheduling_ignored_during_execution=[
                                 {"topologyKey": e2e_globals.ANTI_AFFINITY_KEY},
                             ]
                         ),
                     ),
                     volumes=[client.V1Volume(
                         name='data',
                         config_map=client.V1ConfigMapVolumeSource(
                             name=self.cfgmap_name)
                     )]
                     ,
                     containers=[client.V1Container(
                         image=e2e_globals.TEST_DEPLOYMENT_IMAGE,
                         name="testapp",
                         volume_mounts=[client.V1VolumeMount(
                             name='data',
                             mount_path='/usr/share/nginx/html')
                         ],
                         ports=[client.V1ContainerPort(
                             container_port=e2e_globals.TEST_CONTAINER_PORT)],
                         resources=client.V1ResourceRequirements(
                             requests={
                                 'cpu': '1m',
                                 'memory': '1Mi',
                             },
                         ),
                     )])),
             replicas=self.replicas)
     )
     if self.vol_claim_name is not None:
         volume = client.V1Volume(name='test-volume',
                                  persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
                                      claim_name=self.vol_claim_name))
         mount = client.V1VolumeMount(
             name='test-volume',
             mount_path='/usr/blank'
         )
         depl.spec.template.spec.containers[0].volume_mounts.append(mount)
         depl.spec.template.spec.volumes.append(volume)
     return depl
Пример #2
0
    def __init__(self, name, master, containers, labels={}, replicas=1, api_version='extensions/v1beta1',
                 cluster_name=None, namespace='default', generate_name=None, min_ready_seconds=0,
                 progress_deadline_seconds=600, deployment_strategy_type='RollingUpdate',
                 dns_policy='ClusterFirstWithHostNet', deployment_strategy_rolling_update=None, selectors=None,
                 deployment_object=None, volumes=[]):
        """
        Create a new deployment object in the specified cluster with specified label.

        :param name:str name of the deployment.
        :param master: KubernetesMaster the master object that has all the clients and the config to connect to.
        :param containers: list(V1Container) this can be produces using the self.define_container method on mater.
        :param labels:list({string:string}) labels to apply to the pod
        :param replicas: number of replicas to maintain until the end of lifetime of deployment.
        :param cluster_name:str cluster to create the pod on
        :param namespace: str namespace to relate the pod to
        :param dns_policy:str set DNS policy for containers within the pod. One of 'ClusterFirstWithHostNet', 'ClusterFirst' or 'Default'. Defaults to "ClusterFirst". To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.
        :param selector:{string:string}  is a selector which must be true for the deployment to fit on a node or cluster.
        :param generate_name: str the generated name for deployment.
        :param progress_deadline_seconds: int The maximum time in seconds for a deployment to make progress before it is considered to be failed. The deployment controller will continue to process failed deployments and a condition with a ProgressDeadlineExceeded reason will be surfaced in the deployment status. Note that progress will not be estimated during the time a deployment is paused. Defaults to 600s.
        :param deployment_strategy_type: str Type of deployment. Can be "Recreate" or "RollingUpdate". Default is RollingUpdate.
        :param deployment_strategy_rolling_update: {maxSurge:int, maxUnavailable: int}
        :param min_ready_seconds: int Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0 (pod will be considered available as soon as it is ready)
        :param volumes: list(V1Volume) can be created from the define_?_volume methods
        """
        self.object = deployment_object
        if not deployment_object:
            kind = 'Deployment'
            labels.update({'app': name})
            # Create and configure pod spec section
            pod_spec = client.V1PodTemplateSpec(metadata=client.V1ObjectMeta(labels=labels),
                                                spec=client.V1PodSpec(containers=containers, dns_policy=dns_policy,
                                                                      volumes=volumes))

            # create deployment_strategy
            deployment_strategy = client.AppsV1beta1DeploymentStrategy(rolling_update=deployment_strategy_rolling_update,
                                                                       type=deployment_strategy_type)

            # Create the specification of deployment
            selector = None
            if selectors:
                selector = client.V1LabelSelector([], selectors)

            deployment_spec = client.ExtensionsV1beta1DeploymentSpec(replicas=replicas, template=pod_spec,
                                                                     progress_deadline_seconds=progress_deadline_seconds,
                                                                     min_ready_seconds=min_ready_seconds,
                                                                     strategy=deployment_strategy, selector=selector)
            # Instantiate the deployment object
            self.object = client.ExtensionsV1beta1Deployment(api_version=api_version, kind=kind, spec=deployment_spec,
                                                             metadata=client.V1ObjectMeta(name=name, cluster_name=cluster_name, namespace=namespace,
                                                                                          generate_name=generate_name))
        self.master = master
Пример #3
0
def add_servers():

    if count_servers():
        return "You can't have more than 2 servers", 403
    game_id = request.json['game_id']
    params = request.json['parms']
    u_ip = request.remote_addr

    game = get_game_by_id(game_id)
    try:
        game.validate_params(params, game)
    except Exception as e:
        return str(e), 404

    uid = uuid.uuid4().hex[:12]
    name = "gaas-{}".format(uid)
    labels = {
        "app": "gaas",
        "game": game_id,
        "server": uid,
        "creator": u_ip,
    }
    metadata = client.V1ObjectMeta(
        labels=labels,
        name=name,
    )
    ip_ext = alloc_ip()
    extra_env = [
        client.V1EnvVar(name="IP_ALLOC", value=ip_ext),
        client.V1EnvVar(name="IP_CREATOR", value=u_ip)
    ]
    containers = game.make_deployment(params)
    for container in containers:
        if container.env:
            container.env.extend(extra_env)
        else:
            container.env = extra_env
        if not container.resources:
            container.resources = client.V1ResourceRequirements(limits={
                "cpu": "2",
                "memory": "1G"
            },
                                                                requests={
                                                                    "cpu": "1",
                                                                    "memory":
                                                                    "1G"
                                                                })
    deployment = client.V1Deployment(spec=client.V1DeploymentSpec(
        replicas=1,
        strategy=client.AppsV1beta1DeploymentStrategy(
            rolling_update=client.AppsV1beta1RollingUpdateDeployment(
                max_surge=0, max_unavailable=1)),
        selector=client.V1LabelSelector(match_labels=labels, ),
        template=client.V1PodTemplateSpec(spec=client.V1PodSpec(
            containers=containers,
            termination_grace_period_seconds=0,
            affinity=client.V1Affinity(node_affinity=client.V1NodeAffinity(
                required_during_scheduling_ignored_during_execution=client.
                V1NodeSelector(node_selector_terms=[
                    client.V1NodeSelectorTerm(match_expressions=[
                        client.V1NodeSelectorRequirement(
                            key="kubernetes.io/role",
                            operator="NotIn",
                            values=["shared"])
                    ])
                ])))))))
    service = client.V1Service(spec=client.V1ServiceSpec(
        type="ClusterIP",
        selector=labels,
        ports=game.make_service(params),
    ))
    deployment.metadata = metadata
    deployment.spec.template.metadata = metadata
    service.metadata = metadata

    client.AppsV1Api().create_namespaced_deployment(
        namespace="gaas",
        body=deployment,
    )

    service_resp = client.CoreV1Api().create_namespaced_service(
        namespace="gaas",
        body=service,
    )
    return {"uid": uid, "ip": u_ip}
Пример #4
0
def add(ip, game_id, params):
    game=get_game_by_id(game_id)
    game.validate_params(params)
    uid=uuid.uuid4().hex[:12]
    name="gaas-{}".format(uid)
    labels={
        "app": "gaas",
        "game": game_id,
        "server": uid,
        "creator": ip,
    }
    metadata=client.V1ObjectMeta(
        labels=labels,
        name=name,
    )
    ip_ext=alloc_ip()
    extra_env=[client.V1EnvVar(
        name="IP_ALLOC",
        value=ip_ext
    ), client.V1EnvVar(
        name="IP_CREATOR",
        value=ip
    )]
    containers = game.make_deployment(params)
    generic_ports = []
    # TODO(bluecmd): Hack to work around that not all
    # ports are routed to the VIP by default. This allows
    # outgoing connections from inside the pod on the VIP.
    for p in range(50000, 50016):
        generic_ports.append(client.V1ServicePort(
            name="internal-tcp-" + str(p), port=p, target_port=p, protocol="TCP"))
        generic_ports.append(client.V1ServicePort(
            name="internal-udp-" + str(p), port=p, target_port=p, protocol="UDP"))
    for container in containers:
        if container.env:
            container.env.extend(extra_env)
        else:
            container.env = extra_env
        if not container.resources:
            container.resources=client.V1ResourceRequirements(
                limits={
                    "cpu": "4",
                    "memory": "32G"
                },
                requests={
                    "cpu": "2",
                    "memory": "16G"
                }
            )
    deployment=client.V1Deployment(
            spec=client.V1DeploymentSpec(
                replicas=1,
                strategy=client.AppsV1beta1DeploymentStrategy(
                    rolling_update=client.AppsV1beta1RollingUpdateDeployment(
                        max_surge=0,
                        max_unavailable=1
                    )
                ),
                selector=client.V1LabelSelector(
                    match_labels=labels,
                ),
                template=client.V1PodTemplateSpec(
                    spec=client.V1PodSpec(
                        containers=containers,
                        termination_grace_period_seconds=0,
                        # TODO(bluecmd): Hack to work around that not all
                        # ports are routed to the VIP by default. This allows
                        # outgoing connections from inside the pod on the VIP.
                        security_context=client.V1PodSecurityContext(
                            sysctls=[client.V1Sysctl(
                                name='net.ipv4.ip_local_port_range',
                                value='50000 50015')]),
                        affinity=client.V1Affinity(
                            node_affinity=client.V1NodeAffinity(
                                required_during_scheduling_ignored_during_execution=client.V1NodeSelector(
                                    node_selector_terms=[
                                        client.V1NodeSelectorTerm(
                                            match_expressions=[
                                                client.V1NodeSelectorRequirement(
                                                    key="kubernetes.io/role",
                                                    operator="NotIn",
                                                    values=["shared"]
                                                )
                                            ]
                                        )
                                    ]
                                )
                            )
                        )
                    )
                )
            )
    )
    service=client.V1Service(
        spec=client.V1ServiceSpec(
            type="ClusterIP",
            selector=labels,
            ports=game.make_service(params) + generic_ports,
            external_i_ps=[ip_ext],
        )
    )
    deployment.metadata=metadata
    deployment.spec.template.metadata=metadata
    service.metadata=metadata
    service.metadata.annotations={"kube-router.io/service.dsr": "tunnel"}

    client.AppsV1Api().create_namespaced_deployment(
        namespace=NAMESPACE,
        body=deployment,
    )

    service_resp = client.CoreV1Api().create_namespaced_service(
        namespace=NAMESPACE,
        body=service,
    )

    return {"uid": uid, "ip": ip}