def _make_secrets_init_container(self, app_spec, image, env_vars=None): if env_vars is None: env_vars = {} env_vars.update({"K8S_DEPLOYMENT": app_spec.name}) environment = [EnvVar(name=k, value=v) for k, v in env_vars.items()] container = Container(name=self.SECRETS_INIT_CONTAINER_NAME, image=image, imagePullPolicy="IfNotPresent", env=environment, envFrom=[ EnvFromSource(configMapRef=ConfigMapEnvSource(name=self.SECRETS_INIT_CONTAINER_NAME, optional=True)) ], volumeMounts=self._make_volume_mounts(app_spec, is_init_container=True)) return container
def deploy(self, app_spec, selector, labels, besteffort_qos_is_required): LOG.info("Creating new deployment for %s", app_spec.name) deployment_labels = merge_dicts(app_spec.labels.deployment, labels) metadata = ObjectMeta(name=app_spec.name, namespace=app_spec.namespace, labels=deployment_labels, annotations=app_spec.annotations.deployment) container_ports = [ ContainerPort(name=port_spec.name, containerPort=port_spec.target_port) for port_spec in app_spec.ports ] env = self._make_env(app_spec) pull_policy = "IfNotPresent" if (":" in app_spec.image and ":latest" not in app_spec.image) else "Always" env_from = [ EnvFromSource(configMapRef=ConfigMapEnvSource(name=app_spec.name, optional=True)) ] containers = [ Container( name=app_spec.name, image=app_spec.image, ports=container_ports, env=env, envFrom=env_from, lifecycle=self._lifecycle, livenessProbe=_make_probe(app_spec.health_checks.liveness), readinessProbe=_make_probe(app_spec.health_checks.readiness), imagePullPolicy=pull_policy, volumeMounts=self._make_volume_mounts(app_spec), resources=_make_resource_requirements(app_spec.resources)) ] automount_service_account_token = app_spec.admin_access init_containers = [] service_account_name = "default" pod_spec = PodSpec( containers=containers, initContainers=init_containers, volumes=self._make_volumes(app_spec), serviceAccountName=service_account_name, automountServiceAccountToken=automount_service_account_token, terminationGracePeriodSeconds=self._grace_period) pod_labels = merge_dicts(app_spec.labels.pod, _add_status_label(labels)) pod_metadata = ObjectMeta(name=app_spec.name, namespace=app_spec.namespace, labels=pod_labels, annotations=app_spec.annotations.pod) pod_template_spec = PodTemplateSpec(metadata=pod_metadata, spec=pod_spec) replicas = app_spec.replicas # we must avoid that the deployment scales up to app_spec.replicas if autoscaler has set another value if should_have_autoscaler(app_spec): try: deployment = Deployment.get(app_spec.name, app_spec.namespace) replicas = deployment.spec.replicas except NotFound: pass deployment_strategy = DeploymentStrategy( rollingUpdate=RollingUpdateDeployment( maxUnavailable=self._max_unavailable, maxSurge=self._max_surge)) if app_spec.replicas == 1 and app_spec.singleton: deployment_strategy = DeploymentStrategy( rollingUpdate=RollingUpdateDeployment(maxUnavailable=1, maxSurge=0)) spec = DeploymentSpec(replicas=replicas, selector=LabelSelector(matchLabels=selector), template=pod_template_spec, revisionHistoryLimit=5, strategy=deployment_strategy) deployment = Deployment.get_or_create(metadata=metadata, spec=spec) _clear_pod_init_container_annotations(deployment) self._datadog.apply(deployment, app_spec, besteffort_qos_is_required) self._prometheus.apply(deployment, app_spec) self._secrets.apply(deployment, app_spec) deployment.save()