Path: blob/main/install/installer/pkg/components/ws-daemon/daemonset.go
2501 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package wsdaemon56import (7"fmt"89"github.com/gitpod-io/gitpod/installer/pkg/cluster"10"github.com/gitpod-io/gitpod/installer/pkg/common"1112appsv1 "k8s.io/api/apps/v1"13corev1 "k8s.io/api/core/v1"14"k8s.io/apimachinery/pkg/api/resource"15metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"16"k8s.io/apimachinery/pkg/runtime"17"k8s.io/apimachinery/pkg/util/intstr"18"k8s.io/utils/pointer"19)2021func daemonset(ctx *common.RenderContext) ([]runtime.Object, error) {22cfg := ctx.Config23labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDaemonset)2425configHash, err := common.ObjectHash(configmap(ctx))26if err != nil {27return nil, err28}2930initContainers := []corev1.Container{31{32Name: "seccomp-profile-installer",33Image: ctx.ImageName(cfg.Repository, "seccomp-profile-installer", ctx.VersionManifest.Components.WSDaemon.UserNamespaces.SeccompProfileInstaller.Version),34Command: []string{35"/bin/sh",36"-c",37fmt.Sprintf("cp -f /installer/workspace_default.json /mnt/dst/workspace_default_%s.json", ctx.VersionManifest.Version),38},39VolumeMounts: []corev1.VolumeMount{{40Name: "hostseccomp",41MountPath: "/mnt/dst",42}},43SecurityContext: &corev1.SecurityContext{Privileged: pointer.Bool(true)},44},45{46Name: "sysctl",47Image: ctx.ImageName(cfg.Repository, "ws-daemon", ctx.VersionManifest.Components.WSDaemon.Version),48Command: []string{49"sh",50"-c",51`(52echo "running sysctls" &&53sysctl -w net.core.somaxconn=4096 &&54sysctl -w "net.ipv4.ip_local_port_range=5000 65000" &&55sysctl -w "net.ipv4.tcp_tw_reuse=1" &&56sysctl -w fs.inotify.max_user_watches=1000000 &&57sysctl -w "kernel.dmesg_restrict=1" &&58sysctl -w vm.unprivileged_userfaultfd=059) && echo "done!" || echo "failed!"60`,61},62SecurityContext: &corev1.SecurityContext{Privileged: pointer.Bool(true)},63},64}6566volumes := []corev1.Volume{67{68Name: "hostfs",69VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{70Path: "/",71}},72},73{74Name: "working-area-mk2",75VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{76Path: HostWorkingAreaMk2,77Type: func() *corev1.HostPathType { r := corev1.HostPathDirectoryOrCreate; return &r }(),78}},79},80{81Name: "tls-certs",82VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: TLSSecretName}},83},84{85Name: "config",86VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{87LocalObjectReference: corev1.LocalObjectReference{Name: Component},88}},89},90{91Name: "containerd-socket",92VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{93Path: ctx.Config.Workspace.Runtime.ContainerDSocketDir,94}},95},96{97Name: "node-fs0",98VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{99Path: ctx.Config.Workspace.Runtime.ContainerDRuntimeDir,100Type: func() *corev1.HostPathType { r := corev1.HostPathDirectory; return &r }(),101}},102},103{104Name: "node-mounts",105VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{106Path: "/proc/mounts",107Type: func() *corev1.HostPathType { r := corev1.HostPathFile; return &r }(),108}},109},110{111Name: "node-cgroups",112VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{113Path: "/sys/fs/cgroup",114Type: func() *corev1.HostPathType { r := corev1.HostPathDirectory; return &r }(),115}},116},117{118Name: "node-hosts",119VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{120Path: "/etc/hosts",121Type: func() *corev1.HostPathType { r := corev1.HostPathFile; return &r }(),122}},123},124{125Name: "node-linux-src",126VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{127Path: "/usr/src",128Type: func() *corev1.HostPathType { r := corev1.HostPathDirectory; return &r }(),129}},130},131{132Name: "hostseccomp",133VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: "/var/lib/kubelet/seccomp"}},134},135{136Name: "gcloud-tmp",137VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{138Path: HostBackupPath,139Type: func() *corev1.HostPathType { r := corev1.HostPathDirectoryOrCreate; return &r }(),140}},141},142common.CAVolume(),143}144145volumeMounts := []corev1.VolumeMount{146{147Name: "working-area-mk2",148MountPath: ContainerWorkingAreaMk2,149MountPropagation: func() *corev1.MountPropagationMode { r := corev1.MountPropagationBidirectional; return &r }(),150},151{152Name: "config",153MountPath: "/config",154},155{156Name: "containerd-socket",157MountPath: "/mnt/containerd",158},159{160Name: "node-fs0",161MountPath: "/mnt/node0",162},163{164Name: "node-mounts",165ReadOnly: true,166MountPath: "/mnt/mounts",167MountPropagation: func() *corev1.MountPropagationMode { r := corev1.MountPropagationHostToContainer; return &r }(),168},169{170Name: "node-cgroups",171MountPath: "/mnt/node-cgroups",172MountPropagation: func() *corev1.MountPropagationMode { r := corev1.MountPropagationHostToContainer; return &r }(),173},174{175Name: "node-hosts",176MountPath: "/mnt/hosts",177},178{179Name: "tls-certs",180MountPath: "/certs",181},182{183Name: "gcloud-tmp",184MountPath: "/mnt/sync-tmp",185},186common.CAVolumeMount(),187}188189tolerations := []corev1.Toleration{190{191Key: "node.kubernetes.io/disk-pressure",192Operator: "Exists",193Effect: "NoExecute",194},195{196Key: "node.kubernetes.io/memory-pressure",197Operator: "Exists",198Effect: "NoExecute",199},200{201Key: "node.kubernetes.io/out-of-disk",202Operator: "Exists",203Effect: "NoExecute",204},205{206Operator: "Exists",207},208}209210podSpec := corev1.PodSpec{211Volumes: volumes,212InitContainers: initContainers,213Containers: []corev1.Container{214{215Name: Component,216Image: ctx.ImageName(ctx.Config.Repository, Component, ctx.VersionManifest.Components.WSDaemon.Version),217Args: []string{218"run",219"--config",220"/config/config.json",221},222Ports: []corev1.ContainerPort{{223Name: "rpc",224ContainerPort: ServicePort,225}},226Env: common.CustomizeEnvvar(ctx, Component, common.MergeEnv(227common.DefaultEnv(&cfg),228common.WorkspaceTracingEnv(ctx, Component),229common.NodeNameEnv(ctx),230)),231Resources: common.ResourceRequirements(ctx, Component, Component, corev1.ResourceRequirements{Requests: corev1.ResourceList{232"cpu": resource.MustParse("500m"),233"memory": resource.MustParse("2Gi"),234}}),235VolumeMounts: volumeMounts,236ImagePullPolicy: corev1.PullIfNotPresent,237SecurityContext: &corev1.SecurityContext{238Privileged: pointer.Bool(true),239},240ReadinessProbe: &corev1.Probe{241ProbeHandler: corev1.ProbeHandler{242HTTPGet: &corev1.HTTPGetAction{243Path: "/ready",244Port: intstr.IntOrString{IntVal: ReadinessPort},245},246},247InitialDelaySeconds: 5,248PeriodSeconds: 5,249TimeoutSeconds: 1,250SuccessThreshold: 2,251FailureThreshold: 5,252},253LivenessProbe: &corev1.Probe{254ProbeHandler: corev1.ProbeHandler{255HTTPGet: &corev1.HTTPGetAction{256Path: "/live",257Port: intstr.IntOrString{IntVal: ReadinessPort},258},259},260InitialDelaySeconds: 5,261PeriodSeconds: 10,262TimeoutSeconds: 1,263SuccessThreshold: 1,264FailureThreshold: 5,265},266},267*common.KubeRBACProxyContainer(ctx),268},269RestartPolicy: corev1.RestartPolicyAlways,270TerminationGracePeriodSeconds: pointer.Int64(30),271DNSPolicy: corev1.DNSClusterFirst,272ServiceAccountName: Component,273HostPID: true,274Affinity: cluster.WithNodeAffinity(cluster.AffinityLabelWorkspacesRegular, cluster.AffinityLabelWorkspacesHeadless),275Tolerations: tolerations,276PriorityClassName: common.SystemNodeCritical,277EnableServiceLinks: pointer.Bool(false),278}279280err = common.AddStorageMounts(ctx, &podSpec, Component)281if err != nil {282return nil, err283}284285return []runtime.Object{&appsv1.DaemonSet{286TypeMeta: common.TypeMetaDaemonset,287ObjectMeta: metav1.ObjectMeta{288Name: Component,289Namespace: ctx.Namespace,290Labels: labels,291Annotations: common.CustomizeAnnotation(ctx, Component, common.TypeMetaDaemonset),292},293Spec: appsv1.DaemonSetSpec{294Selector: &metav1.LabelSelector{MatchLabels: common.DefaultLabels(Component)},295Template: corev1.PodTemplateSpec{296ObjectMeta: metav1.ObjectMeta{297Labels: labels,298Annotations: common.CustomizeAnnotation(ctx, Component, common.TypeMetaDaemonset, func() map[string]string {299return map[string]string{300common.AnnotationConfigChecksum: configHash,301}302}),303},304Spec: podSpec,305},306UpdateStrategy: common.DaemonSetRolloutStrategy(),307},308}}, nil309}310311312