Пример #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 _get_r_u_deployment(self, rolling_update):
     if rolling_update:
         return client.AppsV1beta1RollingUpdateDeployment(
             max_surge=rolling_update.get('max_surge'),
             max_unavailable=rolling_update.get('max_unavailable'))
Пример #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}