2022-03-22 11:03:15 -04:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2022-05-19 11:18:22 -04:00
|
|
|
"context"
|
2022-03-22 11:03:15 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-05-04 08:32:34 -04:00
|
|
|
"time"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/kubernetes/k8sapi"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/kubernetes/k8sapi/resources"
|
2022-04-25 11:24:48 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/role"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// k8s pod network cidr. This value was chosen to match the default flannel pod network.
|
|
|
|
const (
|
|
|
|
podNetworkCidr = "10.244.0.0/16"
|
|
|
|
serviceCidr = "10.245.0.0/24"
|
|
|
|
)
|
|
|
|
|
|
|
|
// configReader provides kubeconfig as []byte.
|
|
|
|
type configReader interface {
|
|
|
|
ReadKubeconfig() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// configurationProvider provides kubeadm init and join configuration.
|
|
|
|
type configurationProvider interface {
|
2022-04-27 10:37:05 -04:00
|
|
|
InitConfiguration(externalCloudProvider bool) k8sapi.KubeadmInitYAML
|
|
|
|
JoinConfiguration(externalCloudProvider bool) k8sapi.KubeadmJoinYAML
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// KubeWrapper implements ClusterWrapper interface.
|
|
|
|
type KubeWrapper struct {
|
|
|
|
clusterUtil k8sapi.ClusterUtil
|
|
|
|
configProvider configurationProvider
|
|
|
|
client k8sapi.Client
|
|
|
|
kubeconfigReader configReader
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new KubeWrapper with real values.
|
|
|
|
func New(clusterUtil k8sapi.ClusterUtil, configProvider configurationProvider, client k8sapi.Client) *KubeWrapper {
|
|
|
|
return &KubeWrapper{
|
|
|
|
clusterUtil: clusterUtil,
|
|
|
|
configProvider: configProvider,
|
|
|
|
client: client,
|
|
|
|
kubeconfigReader: &KubeconfigReader{fs: afero.Afero{Fs: afero.NewOsFs()}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 05:22:21 -04:00
|
|
|
// InitCluster initializes a new Kubernetes cluster and applies pod network provider.
|
2022-05-04 08:32:34 -04:00
|
|
|
func (k *KubeWrapper) InitCluster(in InitClusterInput) error {
|
2022-05-19 11:18:22 -04:00
|
|
|
// TODO: k8s version should be user input
|
|
|
|
if err := k.clusterUtil.InstallComponents(context.TODO(), "1.23.6"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:37:05 -04:00
|
|
|
initConfig := k.configProvider.InitConfiguration(in.SupportsCloudControllerManager)
|
2022-03-22 11:03:15 -04:00
|
|
|
initConfig.SetApiServerAdvertiseAddress(in.APIServerAdvertiseIP)
|
2022-03-25 05:51:59 -04:00
|
|
|
initConfig.SetNodeIP(in.NodeIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
initConfig.SetNodeName(in.NodeName)
|
|
|
|
initConfig.SetPodNetworkCIDR(podNetworkCidr)
|
|
|
|
initConfig.SetServiceCIDR(serviceCidr)
|
|
|
|
initConfig.SetProviderID(in.ProviderID)
|
|
|
|
initConfigYAML, err := initConfig.Marshal()
|
|
|
|
if err != nil {
|
2022-05-04 08:32:34 -04:00
|
|
|
return fmt.Errorf("encoding kubeadm init configuration as YAML failed: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-04 08:32:34 -04:00
|
|
|
if err := k.clusterUtil.InitCluster(initConfigYAML); err != nil {
|
|
|
|
return fmt.Errorf("kubeadm init failed: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
kubeConfig, err := k.GetKubeconfig()
|
|
|
|
if err != nil {
|
2022-05-04 08:32:34 -04:00
|
|
|
return fmt.Errorf("reading kubeconfig after cluster initialization failed: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
k.client.SetKubeconfig(kubeConfig)
|
|
|
|
flannel := resources.NewDefaultFlannelDeployment()
|
|
|
|
if err = k.clusterUtil.SetupPodNetwork(k.client, flannel); err != nil {
|
2022-05-04 08:32:34 -04:00
|
|
|
return fmt.Errorf("setup of pod network failed: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-12 10:07:17 -04:00
|
|
|
kms := resources.NewKMSDeployment(in.MasterSecret)
|
|
|
|
if err = k.clusterUtil.SetupKMS(k.client, kms); err != nil {
|
|
|
|
return fmt.Errorf("setup of kms failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:42:27 -04:00
|
|
|
if in.SupportsCloudControllerManager {
|
|
|
|
cloudControllerManagerConfiguration := resources.NewDefaultCloudControllerManagerDeployment(
|
|
|
|
in.CloudControllerManagerName, in.CloudControllerManagerImage, in.CloudControllerManagerPath, in.CloudControllerManagerExtraArgs,
|
|
|
|
in.CloudControllerManagerVolumes, in.CloudControllerManagerVolumeMounts, in.CloudControllerManagerEnv,
|
|
|
|
)
|
|
|
|
if err := k.clusterUtil.SetupCloudControllerManager(k.client, cloudControllerManagerConfiguration, in.CloudControllerManagerConfigMaps, in.CloudControllerManagerSecrets); err != nil {
|
2022-05-04 08:32:34 -04:00
|
|
|
return fmt.Errorf("failed to setup cloud-controller-manager: %w", err)
|
2022-03-25 05:42:27 -04:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 05:49:18 -04:00
|
|
|
|
|
|
|
if in.SupportsCloudNodeManager {
|
|
|
|
cloudNodeManagerConfiguration := resources.NewDefaultCloudNodeManagerDeployment(
|
|
|
|
in.CloudNodeManagerImage, in.CloudNodeManagerPath, in.CloudNodeManagerExtraArgs,
|
|
|
|
)
|
|
|
|
if err := k.clusterUtil.SetupCloudNodeManager(k.client, cloudNodeManagerConfiguration); err != nil {
|
2022-05-04 08:32:34 -04:00
|
|
|
return fmt.Errorf("failed to setup cloud-node-manager: %w", err)
|
2022-03-25 05:49:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
if in.SupportClusterAutoscaler {
|
2022-03-29 07:25:04 -04:00
|
|
|
clusterAutoscalerConfiguration := resources.NewDefaultAutoscalerDeployment(in.AutoscalingVolumes, in.AutoscalingVolumeMounts, in.AutoscalingEnv)
|
2022-03-22 11:03:15 -04:00
|
|
|
clusterAutoscalerConfiguration.SetAutoscalerCommand(in.AutoscalingCloudprovider, in.AutoscalingNodeGroups)
|
2022-03-29 07:25:04 -04:00
|
|
|
if err := k.clusterUtil.SetupAutoscaling(k.client, clusterAutoscalerConfiguration, in.AutoscalingSecrets); err != nil {
|
2022-05-04 08:32:34 -04:00
|
|
|
return fmt.Errorf("failed to setup cluster-autoscaler: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:32:34 -04:00
|
|
|
return nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-26 05:22:21 -04:00
|
|
|
// JoinCluster joins existing Kubernetes cluster.
|
2022-04-27 10:37:05 -04:00
|
|
|
func (k *KubeWrapper) JoinCluster(args *kubeadm.BootstrapTokenDiscovery, nodeName, nodeInternalIP, nodeVPNIP, providerID, certKey string, ccmSupported bool, peerRole role.Role) error {
|
2022-05-19 11:18:22 -04:00
|
|
|
// TODO: k8s version should be user input
|
|
|
|
if err := k.clusterUtil.InstallComponents(context.TODO(), "1.23.6"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:37:05 -04:00
|
|
|
joinConfig := k.configProvider.JoinConfiguration(ccmSupported)
|
2022-03-22 11:03:15 -04:00
|
|
|
joinConfig.SetApiServerEndpoint(args.APIServerEndpoint)
|
|
|
|
joinConfig.SetToken(args.Token)
|
|
|
|
joinConfig.AppendDiscoveryTokenCaCertHash(args.CACertHashes[0])
|
2022-04-25 11:24:48 -04:00
|
|
|
joinConfig.SetNodeIP(nodeInternalIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
joinConfig.SetNodeName(nodeName)
|
|
|
|
joinConfig.SetProviderID(providerID)
|
2022-04-25 11:24:48 -04:00
|
|
|
if peerRole == role.Coordinator {
|
|
|
|
joinConfig.SetControlPlane(nodeVPNIP, certKey)
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
joinConfigYAML, err := joinConfig.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding kubeadm join configuration as YAML failed: %w", err)
|
|
|
|
}
|
|
|
|
if err := k.clusterUtil.JoinCluster(joinConfigYAML); err != nil {
|
|
|
|
return fmt.Errorf("joining cluster failed: %v %w ", string(joinConfigYAML), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKubeconfig returns the current nodes kubeconfig of stored on disk.
|
|
|
|
func (k *KubeWrapper) GetKubeconfig() ([]byte, error) {
|
|
|
|
kubeconf, err := k.kubeconfigReader.ReadKubeconfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// replace the cluster.Server endpoint (127.0.0.1:16443) in admin.conf with the first coordinator endpoint (10.118.0.1:6443)
|
|
|
|
// kube-api server listens on 10.118.0.1:6443
|
|
|
|
// 127.0.0.1:16443 is the high availability balancer nginx endpoint, runnining localy on all nodes
|
|
|
|
// alternatively one could also start a local high availability balancer.
|
|
|
|
return []byte(strings.ReplaceAll(string(kubeconf), "127.0.0.1:16443", "10.118.0.1:6443")), nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:24:48 -04:00
|
|
|
// GetKubeadmCertificateKey return the key needed to join the Cluster as Control-Plane (has to be executed on a control-plane; errors otherwise).
|
|
|
|
func (k *KubeWrapper) GetKubeadmCertificateKey() (string, error) {
|
|
|
|
return k.clusterUtil.GetControlPlaneJoinCertificateKey()
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:32:34 -04:00
|
|
|
// GetJoinToken returns a bootstrap (join) token.
|
|
|
|
func (k *KubeWrapper) GetJoinToken(ttl time.Duration) (*kubeadm.BootstrapTokenDiscovery, error) {
|
|
|
|
return k.clusterUtil.CreateJoinToken(ttl)
|
|
|
|
}
|
|
|
|
|
2022-05-19 11:18:22 -04:00
|
|
|
// StartKubelet starts the kubelet service.
|
|
|
|
func (k *KubeWrapper) StartKubelet() error {
|
|
|
|
return k.clusterUtil.StartKubelet()
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type fakeK8SClient struct {
|
|
|
|
kubeconfig []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFakeK8SClient creates a new, fake k8s client where apply always works.
|
|
|
|
func NewFakeK8SClient([]byte) (k8sapi.Client, error) {
|
|
|
|
return &fakeK8SClient{}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-26 05:22:21 -04:00
|
|
|
// Apply fakes applying Kubernetes resources.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (f *fakeK8SClient) Apply(resources resources.Marshaler, forceConflicts bool) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKubeconfig stores the kubeconfig given to it.
|
|
|
|
func (f *fakeK8SClient) SetKubeconfig(kubeconfig []byte) {
|
|
|
|
f.kubeconfig = kubeconfig
|
|
|
|
}
|