2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package k8sapi
|
|
|
|
|
|
|
|
import (
|
2022-05-20 11:30:37 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubelet"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2022-05-20 11:30:37 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-03-22 11:03:15 -04:00
|
|
|
kubeletconf "k8s.io/kubelet/config/v1beta1"
|
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
2022-07-15 03:33:11 -04:00
|
|
|
kubeconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Uses types defined here: https://kubernetes.io/docs/reference/config-api/kubeadm-config.v1beta3/
|
|
|
|
// Slimmed down to the fields we require
|
|
|
|
|
|
|
|
const (
|
2022-05-20 11:30:37 -04:00
|
|
|
auditLogDir = "/var/log/kubernetes/audit/"
|
|
|
|
auditLogFile = "audit.log"
|
|
|
|
auditPolicyPath = "/etc/kubernetes/audit-policy.yaml"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// KubdeadmConfiguration is used to generate kubeadm configurations.
|
2022-10-21 04:16:44 -04:00
|
|
|
type KubdeadmConfiguration struct{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// InitConfiguration returns a new init configuration.
|
2022-10-21 04:16:44 -04:00
|
|
|
func (c *KubdeadmConfiguration) InitConfiguration(externalCloudProvider bool, k8sVersion versions.ValidK8sVersion) KubeadmInitYAML {
|
2022-04-27 10:37:05 -04:00
|
|
|
var cloudProvider string
|
|
|
|
if externalCloudProvider {
|
|
|
|
cloudProvider = "external"
|
|
|
|
}
|
2022-07-22 09:05:04 -04:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
return KubeadmInitYAML{
|
|
|
|
InitConfiguration: kubeadm.InitConfiguration{
|
2022-05-20 11:30:37 -04:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2022-03-22 11:03:15 -04:00
|
|
|
APIVersion: kubeadm.SchemeGroupVersion.String(),
|
|
|
|
Kind: "InitConfiguration",
|
|
|
|
},
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
2022-07-26 11:08:57 -04:00
|
|
|
CRISocket: "unix:///run/containerd/containerd.sock",
|
2022-03-22 11:03:15 -04:00
|
|
|
KubeletExtraArgs: map[string]string{
|
2022-04-27 10:37:05 -04:00
|
|
|
"cloud-provider": cloudProvider,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// AdvertiseAddress will be overwritten later
|
|
|
|
LocalAPIEndpoint: kubeadm.APIEndpoint{
|
2022-08-01 10:51:34 -04:00
|
|
|
BindPort: constants.KubernetesPort,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
2022-07-18 06:28:02 -04:00
|
|
|
// https://pkg.go.dev/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3#ClusterConfiguration
|
2022-03-22 11:03:15 -04:00
|
|
|
ClusterConfiguration: kubeadm.ClusterConfiguration{
|
2022-05-20 11:30:37 -04:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2022-03-22 11:03:15 -04:00
|
|
|
Kind: "ClusterConfiguration",
|
|
|
|
APIVersion: kubeadm.SchemeGroupVersion.String(),
|
|
|
|
},
|
2022-07-18 06:28:02 -04:00
|
|
|
// Target kubernetes version of the control plane.
|
2022-07-22 09:05:04 -04:00
|
|
|
KubernetesVersion: versions.VersionConfigs[k8sVersion].PatchVersion,
|
2022-03-22 11:03:15 -04:00
|
|
|
// necessary to be able to access the kubeapi server through localhost
|
|
|
|
APIServer: kubeadm.APIServer{
|
2022-05-09 10:12:15 -04:00
|
|
|
ControlPlaneComponent: kubeadm.ControlPlaneComponent{
|
|
|
|
ExtraArgs: map[string]string{
|
2022-09-28 04:49:13 -04:00
|
|
|
"audit-policy-file": auditPolicyPath,
|
|
|
|
"audit-log-path": filepath.Join(auditLogDir, auditLogFile), // CIS benchmark
|
|
|
|
"audit-log-maxage": "30", // CIS benchmark - Default value of Rancher
|
|
|
|
"audit-log-maxbackup": "10", // CIS benchmark - Default value of Rancher
|
|
|
|
"audit-log-maxsize": "100", // CIS benchmark - Default value of Rancher
|
|
|
|
"profiling": "false", // CIS benchmark
|
|
|
|
"egress-selector-config-file": "/etc/kubernetes/egress-selector-configuration.yaml",
|
2022-07-15 03:33:11 -04:00
|
|
|
"kubelet-certificate-authority": filepath.Join(
|
|
|
|
kubeconstants.KubernetesDir,
|
|
|
|
kubeconstants.DefaultCertificateDir,
|
|
|
|
kubeconstants.CACertName,
|
|
|
|
),
|
2022-05-09 10:12:15 -04:00
|
|
|
"tls-cipher-suites": "TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384,TLS_CHACHA20_POLY1305_SHA256," +
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256," +
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384," +
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256," +
|
|
|
|
"TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256," +
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305," +
|
|
|
|
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,TLS_RSA_WITH_3DES_EDE_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA," +
|
|
|
|
"TLS_RSA_WITH_AES_128_GCM_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_RSA_WITH_AES_256_GCM_SHA384", // CIS benchmark
|
|
|
|
},
|
2022-05-20 11:30:37 -04:00
|
|
|
ExtraVolumes: []kubeadm.HostPathMount{
|
|
|
|
{
|
|
|
|
Name: "audit-log",
|
|
|
|
HostPath: auditLogDir,
|
|
|
|
MountPath: auditLogDir,
|
|
|
|
ReadOnly: false,
|
|
|
|
PathType: corev1.HostPathDirectoryOrCreate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "audit",
|
|
|
|
HostPath: auditPolicyPath,
|
|
|
|
MountPath: auditPolicyPath,
|
|
|
|
ReadOnly: true,
|
|
|
|
PathType: corev1.HostPathFile,
|
|
|
|
},
|
2022-08-31 21:40:29 -04:00
|
|
|
{
|
|
|
|
Name: "egress-config",
|
|
|
|
HostPath: "/etc/kubernetes/egress-selector-configuration.yaml",
|
|
|
|
MountPath: "/etc/kubernetes/egress-selector-configuration.yaml",
|
|
|
|
ReadOnly: true,
|
|
|
|
PathType: corev1.HostPathFile,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "konnectivity-uds",
|
|
|
|
HostPath: "/etc/kubernetes/konnectivity-server",
|
|
|
|
MountPath: "/etc/kubernetes/konnectivity-server",
|
|
|
|
ReadOnly: false,
|
|
|
|
PathType: corev1.HostPathDirectoryOrCreate,
|
|
|
|
},
|
2022-05-20 11:30:37 -04:00
|
|
|
},
|
2022-05-09 10:12:15 -04:00
|
|
|
},
|
2022-07-05 08:14:11 -04:00
|
|
|
CertSANs: []string{"127.0.0.1"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
ControllerManager: kubeadm.ControlPlaneComponent{
|
|
|
|
ExtraArgs: map[string]string{
|
2022-05-09 10:12:15 -04:00
|
|
|
"flex-volume-plugin-dir": "/opt/libexec/kubernetes/kubelet-plugins/volume/exec/",
|
|
|
|
"cloud-provider": cloudProvider,
|
|
|
|
"configure-cloud-routes": "false",
|
|
|
|
"profiling": "false", // CIS benchmark
|
|
|
|
"terminated-pod-gc-threshold": "1000", // CIS benchmark - Default value of Rancher
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Scheduler: kubeadm.ControlPlaneComponent{
|
|
|
|
ExtraArgs: map[string]string{
|
|
|
|
"profiling": "false",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// warning: this config is applied to every node in the cluster!
|
|
|
|
KubeletConfiguration: kubeletconf.KubeletConfiguration{
|
2022-05-10 01:50:28 -04:00
|
|
|
ProtectKernelDefaults: true, // CIS benchmark
|
|
|
|
TLSCipherSuites: []string{
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305",
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
"TLS_RSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
"TLS_RSA_WITH_AES_128_GCM_SHA256",
|
|
|
|
}, // CIS benchmark
|
2022-08-31 21:40:29 -04:00
|
|
|
StaticPodPath: "/etc/kubernetes/manifests",
|
2022-05-20 11:30:37 -04:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2022-03-22 11:03:15 -04:00
|
|
|
APIVersion: kubeletconf.SchemeGroupVersion.String(),
|
|
|
|
Kind: "KubeletConfiguration",
|
|
|
|
},
|
2022-05-24 04:04:42 -04:00
|
|
|
RegisterWithTaints: []corev1.Taint{
|
|
|
|
{
|
|
|
|
Key: "node.cloudprovider.kubernetes.io/uninitialized",
|
|
|
|
Value: "true",
|
|
|
|
Effect: corev1.TaintEffectPreferNoSchedule,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "node.cilium.io/agent-not-ready",
|
|
|
|
Value: "true",
|
|
|
|
Effect: corev1.TaintEffectPreferNoSchedule,
|
|
|
|
},
|
|
|
|
},
|
2022-07-15 03:33:11 -04:00
|
|
|
TLSCertFile: kubelet.CertificateFilename,
|
|
|
|
TLSPrivateKeyFile: kubelet.KeyFilename,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// JoinConfiguration returns a new kubeadm join configuration.
|
2022-10-21 04:16:44 -04:00
|
|
|
func (c *KubdeadmConfiguration) JoinConfiguration(externalCloudProvider bool) KubeadmJoinYAML {
|
2022-04-27 10:37:05 -04:00
|
|
|
var cloudProvider string
|
|
|
|
if externalCloudProvider {
|
|
|
|
cloudProvider = "external"
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
return KubeadmJoinYAML{
|
|
|
|
JoinConfiguration: kubeadm.JoinConfiguration{
|
2022-05-20 11:30:37 -04:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2022-03-22 11:03:15 -04:00
|
|
|
APIVersion: kubeadm.SchemeGroupVersion.String(),
|
|
|
|
Kind: "JoinConfiguration",
|
|
|
|
},
|
|
|
|
NodeRegistration: kubeadm.NodeRegistrationOptions{
|
2022-07-26 11:08:57 -04:00
|
|
|
CRISocket: "unix:///run/containerd/containerd.sock",
|
2022-03-22 11:03:15 -04:00
|
|
|
KubeletExtraArgs: map[string]string{
|
2022-04-27 10:37:05 -04:00
|
|
|
"cloud-provider": cloudProvider,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Discovery: kubeadm.Discovery{
|
|
|
|
BootstrapToken: &kubeadm.BootstrapTokenDiscovery{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
KubeletConfiguration: kubeletconf.KubeletConfiguration{
|
2022-05-20 11:30:37 -04:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2022-03-22 11:03:15 -04:00
|
|
|
APIVersion: kubeletconf.SchemeGroupVersion.String(),
|
|
|
|
Kind: "KubeletConfiguration",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// KubeadmJoinYAML holds configuration for kubeadm join workflow.
|
2022-03-22 11:03:15 -04:00
|
|
|
type KubeadmJoinYAML struct {
|
|
|
|
JoinConfiguration kubeadm.JoinConfiguration
|
|
|
|
KubeletConfiguration kubeletconf.KubeletConfiguration
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetNodeName sets the node name.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmJoinYAML) SetNodeName(nodeName string) {
|
|
|
|
k.JoinConfiguration.NodeRegistration.Name = nodeName
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetAPIServerEndpoint sets the api server endpoint.
|
2022-07-08 04:59:59 -04:00
|
|
|
func (k *KubeadmJoinYAML) SetAPIServerEndpoint(apiServerEndpoint string) {
|
2022-03-22 11:03:15 -04:00
|
|
|
k.JoinConfiguration.Discovery.BootstrapToken.APIServerEndpoint = apiServerEndpoint
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetToken sets the boostrap token.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmJoinYAML) SetToken(token string) {
|
|
|
|
k.JoinConfiguration.Discovery.BootstrapToken.Token = token
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// AppendDiscoveryTokenCaCertHash appends another trusted discovery token CA hash.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmJoinYAML) AppendDiscoveryTokenCaCertHash(discoveryTokenCaCertHash string) {
|
|
|
|
k.JoinConfiguration.Discovery.BootstrapToken.CACertHashes = append(k.JoinConfiguration.Discovery.BootstrapToken.CACertHashes, discoveryTokenCaCertHash)
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetNodeIP sets the node IP.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmJoinYAML) SetNodeIP(nodeIP string) {
|
|
|
|
if k.JoinConfiguration.NodeRegistration.KubeletExtraArgs == nil {
|
|
|
|
k.JoinConfiguration.NodeRegistration.KubeletExtraArgs = map[string]string{"node-ip": nodeIP}
|
|
|
|
} else {
|
|
|
|
k.JoinConfiguration.NodeRegistration.KubeletExtraArgs["node-ip"] = nodeIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetProviderID sets the provider ID.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmJoinYAML) SetProviderID(providerID string) {
|
|
|
|
k.KubeletConfiguration.ProviderID = providerID
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetControlPlane sets the control plane with the advertised address.
|
2022-07-11 07:29:22 -04:00
|
|
|
func (k *KubeadmJoinYAML) SetControlPlane(advertiseAddress string) {
|
2022-04-25 11:24:48 -04:00
|
|
|
k.JoinConfiguration.ControlPlane = &kubeadm.JoinControlPlane{
|
|
|
|
LocalAPIEndpoint: kubeadm.APIEndpoint{
|
|
|
|
AdvertiseAddress: advertiseAddress,
|
2022-08-01 10:51:34 -04:00
|
|
|
BindPort: constants.KubernetesPort,
|
2022-04-25 11:24:48 -04:00
|
|
|
},
|
|
|
|
}
|
2022-07-11 07:29:22 -04:00
|
|
|
k.JoinConfiguration.SkipPhases = []string{"control-plane-prepare/download-certs"}
|
2022-04-25 11:24:48 -04:00
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// Marshal into a k8s resource YAML.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmJoinYAML) Marshal() ([]byte, error) {
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.MarshalK8SResources(k)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// KubeadmInitYAML holds configuration for kubeadm init workflow.
|
2022-03-22 11:03:15 -04:00
|
|
|
type KubeadmInitYAML struct {
|
|
|
|
InitConfiguration kubeadm.InitConfiguration
|
|
|
|
ClusterConfiguration kubeadm.ClusterConfiguration
|
|
|
|
KubeletConfiguration kubeletconf.KubeletConfiguration
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetNodeName sets name of node.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmInitYAML) SetNodeName(nodeName string) {
|
|
|
|
k.InitConfiguration.NodeRegistration.Name = nodeName
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
// SetCertSANs sets the SANs for the certificate.
|
|
|
|
func (k *KubeadmInitYAML) SetCertSANs(certSANs []string) {
|
|
|
|
for _, certSAN := range certSANs {
|
|
|
|
if certSAN == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
k.ClusterConfiguration.APIServer.CertSANs = append(k.ClusterConfiguration.APIServer.CertSANs, certSAN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetControlPlaneEndpoint sets the control plane endpoint if controlPlaneEndpoint is not empty.
|
|
|
|
func (k *KubeadmInitYAML) SetControlPlaneEndpoint(controlPlaneEndpoint string) {
|
|
|
|
if controlPlaneEndpoint != "" {
|
|
|
|
k.ClusterConfiguration.ControlPlaneEndpoint = controlPlaneEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetNodeIP sets the node IP.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmInitYAML) SetNodeIP(nodeIP string) {
|
|
|
|
if k.InitConfiguration.NodeRegistration.KubeletExtraArgs == nil {
|
|
|
|
k.InitConfiguration.NodeRegistration.KubeletExtraArgs = map[string]string{"node-ip": nodeIP}
|
|
|
|
} else {
|
|
|
|
k.InitConfiguration.NodeRegistration.KubeletExtraArgs["node-ip"] = nodeIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// SetProviderID sets the provider ID.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmInitYAML) SetProviderID(providerID string) {
|
|
|
|
if k.InitConfiguration.NodeRegistration.KubeletExtraArgs == nil {
|
|
|
|
k.InitConfiguration.NodeRegistration.KubeletExtraArgs = map[string]string{"provider-id": providerID}
|
|
|
|
} else {
|
|
|
|
k.InitConfiguration.NodeRegistration.KubeletExtraArgs["provider-id"] = providerID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// Marshal into a k8s resource YAML.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (k *KubeadmInitYAML) Marshal() ([]byte, error) {
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.MarshalK8SResources(k)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|