2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-15 10:00:48 -04:00
|
|
|
package resources
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-08-29 10:41:09 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-06-15 10:00:48 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-08-29 08:30:20 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/kubernetes"
|
2022-07-18 06:28:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/versions"
|
2022-06-15 10:00:48 -04:00
|
|
|
apps "k8s.io/api/apps/v1"
|
|
|
|
k8s "k8s.io/api/core/v1"
|
|
|
|
rbac "k8s.io/api/rbac/v1"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
)
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
type joinServiceDaemonset struct {
|
2022-06-15 10:00:48 -04:00
|
|
|
ClusterRole rbac.ClusterRole
|
|
|
|
ClusterRoleBinding rbac.ClusterRoleBinding
|
|
|
|
ConfigMap k8s.ConfigMap
|
|
|
|
DaemonSet apps.DaemonSet
|
|
|
|
ServiceAccount k8s.ServiceAccount
|
|
|
|
Service k8s.Service
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
// NewJoinServiceDaemonset returns a daemonset for the join service.
|
2022-08-29 10:41:09 -04:00
|
|
|
func NewJoinServiceDaemonset(csp, measurementsJSON, enforcedPCRsJSON, initialIdKeyDigest, enforceIdKeyDigest string, measurementSalt []byte) *joinServiceDaemonset {
|
|
|
|
joinConfigData := map[string]string{
|
|
|
|
constants.MeasurementsFilename: measurementsJSON,
|
|
|
|
constants.EnforcedPCRsFilename: enforcedPCRsJSON,
|
|
|
|
}
|
|
|
|
if cloudprovider.FromString(csp) == cloudprovider.Azure {
|
|
|
|
joinConfigData[constants.EnforceIdKeyDigestFilename] = enforceIdKeyDigest
|
|
|
|
joinConfigData[constants.IdKeyDigestFilename] = initialIdKeyDigest
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
return &joinServiceDaemonset{
|
2022-06-15 10:00:48 -04:00
|
|
|
ClusterRole: rbac.ClusterRole{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
APIVersion: "rbac.authorization.k8s.io/v1",
|
|
|
|
Kind: "ClusterRole",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
Labels: map[string]string{
|
2022-07-05 08:13:19 -04:00
|
|
|
"k8s-app": "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Rules: []rbac.PolicyRule{
|
|
|
|
{
|
|
|
|
APIGroups: []string{""},
|
|
|
|
Resources: []string{"secrets"},
|
2022-06-21 05:10:32 -04:00
|
|
|
Verbs: []string{"get", "list", "create", "update"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
APIGroups: []string{"rbac.authorization.k8s.io"},
|
|
|
|
Resources: []string{"roles", "rolebindings"},
|
|
|
|
Verbs: []string{"create", "update"},
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ClusterRoleBinding: rbac.ClusterRoleBinding{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
APIVersion: "rbac.authorization.k8s.io/v1",
|
|
|
|
Kind: "ClusterRoleBinding",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
RoleRef: rbac.RoleRef{
|
|
|
|
APIGroup: "rbac.authorization.k8s.io",
|
|
|
|
Kind: "ClusterRole",
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
Subjects: []rbac.Subject{
|
|
|
|
{
|
|
|
|
Kind: "ServiceAccount",
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-08-29 10:49:44 -04:00
|
|
|
Namespace: constants.ConstellationNamespace,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DaemonSet: apps.DaemonSet{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
APIVersion: "apps/v1",
|
|
|
|
Kind: "DaemonSet",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-08-29 10:49:44 -04:00
|
|
|
Namespace: constants.ConstellationNamespace,
|
2022-06-15 10:00:48 -04:00
|
|
|
Labels: map[string]string{
|
2022-07-05 08:13:19 -04:00
|
|
|
"k8s-app": "join-service",
|
|
|
|
"component": "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
"kubernetes.io/cluster-service": "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: apps.DaemonSetSpec{
|
|
|
|
Selector: &meta.LabelSelector{
|
|
|
|
MatchLabels: map[string]string{
|
2022-07-05 08:13:19 -04:00
|
|
|
"k8s-app": "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Template: k8s.PodTemplateSpec{
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
2022-07-05 08:13:19 -04:00
|
|
|
"k8s-app": "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: k8s.PodSpec{
|
|
|
|
PriorityClassName: "system-cluster-critical",
|
2022-07-05 08:13:19 -04:00
|
|
|
ServiceAccountName: "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
Tolerations: []k8s.Toleration{
|
|
|
|
{
|
|
|
|
Key: "CriticalAddonsOnly",
|
|
|
|
Operator: k8s.TolerationOpExists,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "node-role.kubernetes.io/master",
|
|
|
|
Operator: k8s.TolerationOpEqual,
|
|
|
|
Value: "true",
|
|
|
|
Effect: k8s.TaintEffectNoSchedule,
|
|
|
|
},
|
2022-06-28 11:03:28 -04:00
|
|
|
{
|
|
|
|
Key: "node-role.kubernetes.io/control-plane",
|
|
|
|
Operator: k8s.TolerationOpExists,
|
|
|
|
Effect: k8s.TaintEffectNoSchedule,
|
|
|
|
},
|
2022-06-15 10:00:48 -04:00
|
|
|
{
|
|
|
|
Operator: k8s.TolerationOpExists,
|
|
|
|
Effect: k8s.TaintEffectNoExecute,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operator: k8s.TolerationOpExists,
|
|
|
|
Effect: k8s.TaintEffectNoSchedule,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Only run on control plane nodes
|
|
|
|
NodeSelector: map[string]string{
|
2022-07-26 11:08:57 -04:00
|
|
|
"node-role.kubernetes.io/control-plane": "",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
Containers: []k8s.Container{
|
|
|
|
{
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-07-18 06:28:02 -04:00
|
|
|
Image: versions.JoinImage,
|
2022-06-15 10:00:48 -04:00
|
|
|
Ports: []k8s.ContainerPort{
|
|
|
|
{
|
2022-07-05 08:13:19 -04:00
|
|
|
ContainerPort: constants.JoinServicePort,
|
2022-06-15 10:00:48 -04:00
|
|
|
Name: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SecurityContext: &k8s.SecurityContext{
|
|
|
|
Privileged: func(b bool) *bool { return &b }(true),
|
|
|
|
},
|
|
|
|
Args: []string{
|
|
|
|
fmt.Sprintf("--cloud-provider=%s", csp),
|
|
|
|
fmt.Sprintf("--kms-endpoint=kms.kube-system:%d", constants.KMSPort),
|
|
|
|
},
|
|
|
|
VolumeMounts: []k8s.VolumeMount{
|
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
ReadOnly: true,
|
2022-06-29 10:13:01 -04:00
|
|
|
MountPath: constants.ServiceBasePath,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "kubeadm",
|
|
|
|
ReadOnly: true,
|
|
|
|
MountPath: "/etc/kubernetes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Volumes: []k8s.Volume{
|
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
VolumeSource: k8s.VolumeSource{
|
2022-07-18 06:28:02 -04:00
|
|
|
Projected: &k8s.ProjectedVolumeSource{
|
|
|
|
Sources: []k8s.VolumeProjection{
|
|
|
|
{
|
|
|
|
ConfigMap: &k8s.ConfigMapProjection{
|
|
|
|
LocalObjectReference: k8s.LocalObjectReference{
|
2022-08-29 10:49:44 -04:00
|
|
|
Name: constants.JoinConfigMap,
|
2022-07-18 06:28:02 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ConfigMap: &k8s.ConfigMapProjection{
|
|
|
|
LocalObjectReference: k8s.LocalObjectReference{
|
|
|
|
Name: constants.K8sVersion,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-31 14:10:49 -04:00
|
|
|
{
|
|
|
|
ConfigMap: &k8s.ConfigMapProjection{
|
|
|
|
LocalObjectReference: k8s.LocalObjectReference{
|
|
|
|
Name: constants.InternalConfigMap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "kubeadm",
|
|
|
|
VolumeSource: k8s.VolumeSource{
|
|
|
|
HostPath: &k8s.HostPathVolumeSource{
|
|
|
|
Path: "/etc/kubernetes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServiceAccount: k8s.ServiceAccount{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "ServiceAccount",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-08-29 10:49:44 -04:00
|
|
|
Namespace: constants.ConstellationNamespace,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: k8s.Service{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "Service",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2022-07-05 08:13:19 -04:00
|
|
|
Name: "join-service",
|
2022-08-29 10:49:44 -04:00
|
|
|
Namespace: constants.ConstellationNamespace,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
Spec: k8s.ServiceSpec{
|
|
|
|
Type: k8s.ServiceTypeNodePort,
|
|
|
|
Ports: []k8s.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "grpc",
|
|
|
|
Protocol: k8s.ProtocolTCP,
|
2022-07-05 08:13:19 -04:00
|
|
|
Port: constants.JoinServicePort,
|
|
|
|
TargetPort: intstr.IntOrString{IntVal: constants.JoinServicePort},
|
2022-07-05 05:41:31 -04:00
|
|
|
NodePort: constants.JoinServiceNodePort,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{
|
2022-07-05 08:13:19 -04:00
|
|
|
"k8s-app": "join-service",
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ConfigMap: k8s.ConfigMap{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "ConfigMap",
|
|
|
|
},
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2022-08-29 10:49:44 -04:00
|
|
|
Name: constants.JoinConfigMap,
|
|
|
|
Namespace: constants.ConstellationNamespace,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
2022-08-29 10:41:09 -04:00
|
|
|
Data: joinConfigData,
|
2022-07-26 04:58:39 -04:00
|
|
|
BinaryData: map[string][]byte{
|
|
|
|
constants.MeasurementSaltFilename: measurementSalt,
|
2022-06-15 10:00:48 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal the daemonset using the Kubernetes resource marshaller.
|
2022-07-05 08:13:19 -04:00
|
|
|
func (a *joinServiceDaemonset) Marshal() ([]byte, error) {
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.MarshalK8SResources(a)
|
2022-06-15 10:00:48 -04:00
|
|
|
}
|