2022-03-22 11:03:15 -04:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2022-05-19 11:18:22 -04:00
|
|
|
"context"
|
2022-06-15 10:00:48 -04:00
|
|
|
"encoding/json"
|
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
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi"
|
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
|
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/role"
|
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/util"
|
2022-06-15 10:00:48 -04:00
|
|
|
attestationtypes "github.com/edgelesssys/constellation/internal/attestation/types"
|
2022-06-28 12:23:24 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
// KubeWrapper implements Cluster interface.
|
2022-03-22 11:03:15 -04:00
|
|
|
type KubeWrapper struct {
|
2022-06-15 10:00:48 -04:00
|
|
|
cloudProvider string
|
|
|
|
clusterUtil clusterUtil
|
|
|
|
configProvider configurationProvider
|
|
|
|
client k8sapi.Client
|
|
|
|
kubeconfigReader configReader
|
|
|
|
cloudControllerManager CloudControllerManager
|
|
|
|
cloudNodeManager CloudNodeManager
|
|
|
|
clusterAutoscaler ClusterAutoscaler
|
|
|
|
providerMetadata ProviderMetadata
|
|
|
|
initialMeasurementsJSON []byte
|
2022-06-28 12:23:24 -04:00
|
|
|
getIPAddr func() (string, error)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new KubeWrapper with real values.
|
2022-05-24 04:04:42 -04:00
|
|
|
func New(cloudProvider string, clusterUtil clusterUtil, configProvider configurationProvider, client k8sapi.Client, cloudControllerManager CloudControllerManager,
|
2022-06-15 10:00:48 -04:00
|
|
|
cloudNodeManager CloudNodeManager, clusterAutoscaler ClusterAutoscaler, providerMetadata ProviderMetadata, initialMeasurementsJSON []byte,
|
2022-05-24 04:04:42 -04:00
|
|
|
) *KubeWrapper {
|
2022-03-22 11:03:15 -04:00
|
|
|
return &KubeWrapper{
|
2022-06-15 10:00:48 -04:00
|
|
|
cloudProvider: cloudProvider,
|
|
|
|
clusterUtil: clusterUtil,
|
|
|
|
configProvider: configProvider,
|
|
|
|
client: client,
|
|
|
|
kubeconfigReader: &KubeconfigReader{fs: afero.Afero{Fs: afero.NewOsFs()}},
|
|
|
|
cloudControllerManager: cloudControllerManager,
|
|
|
|
cloudNodeManager: cloudNodeManager,
|
|
|
|
clusterAutoscaler: clusterAutoscaler,
|
|
|
|
providerMetadata: providerMetadata,
|
|
|
|
initialMeasurementsJSON: initialMeasurementsJSON,
|
2022-06-28 12:23:24 -04:00
|
|
|
getIPAddr: util.GetIPAddr,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
type KMSConfig struct {
|
|
|
|
MasterSecret []byte
|
|
|
|
KMSURI string
|
|
|
|
StorageURI string
|
|
|
|
KeyEncryptionKeyID string
|
|
|
|
UseExistingKEK bool
|
|
|
|
}
|
|
|
|
|
2022-04-26 05:22:21 -04:00
|
|
|
// InitCluster initializes a new Kubernetes cluster and applies pod network provider.
|
2022-06-15 10:00:48 -04:00
|
|
|
func (k *KubeWrapper) InitCluster(
|
2022-06-21 11:59:12 -04:00
|
|
|
ctx context.Context, autoscalingNodeGroups []string, cloudServiceAccountURI, k8sVersion string,
|
|
|
|
id attestationtypes.ID, kmsConfig KMSConfig, sshUsers map[string]string,
|
2022-06-28 12:33:27 -04:00
|
|
|
) ([]byte, error) {
|
2022-05-19 11:18:22 -04:00
|
|
|
// TODO: k8s version should be user input
|
2022-06-21 11:59:12 -04:00
|
|
|
if err := k.clusterUtil.InstallComponents(ctx, k8sVersion); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, err
|
2022-05-19 11:18:22 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 12:23:24 -04:00
|
|
|
ip, err := k.getIPAddr()
|
2022-06-21 11:59:12 -04:00
|
|
|
if err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, err
|
2022-06-21 11:59:12 -04:00
|
|
|
}
|
|
|
|
nodeName := ip
|
2022-05-24 04:04:42 -04:00
|
|
|
var providerID string
|
2022-06-28 12:23:24 -04:00
|
|
|
var instance metadata.InstanceMetadata
|
2022-05-24 04:04:42 -04:00
|
|
|
var publicIP string
|
|
|
|
var nodePodCIDR string
|
|
|
|
var subnetworkPodCIDR string
|
|
|
|
// this is the IP in "kubeadm init --control-plane-endpoint=<IP/DNS>:<port>" hence the unfortunate name
|
|
|
|
var controlPlaneEndpointIP string
|
|
|
|
var nodeIP string
|
|
|
|
|
|
|
|
// Step 1: retrieve cloud metadata for Kubernetes configuration
|
|
|
|
if k.providerMetadata.Supported() {
|
2022-06-21 11:59:12 -04:00
|
|
|
instance, err = k.providerMetadata.Self(ctx)
|
2022-05-24 04:04:42 -04:00
|
|
|
if err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("retrieving own instance metadata failed: %w", err)
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
|
|
|
nodeName = k8sCompliantHostname(instance.Name)
|
|
|
|
providerID = instance.ProviderID
|
|
|
|
if len(instance.PrivateIPs) > 0 {
|
|
|
|
nodeIP = instance.PrivateIPs[0]
|
|
|
|
}
|
|
|
|
if len(instance.PublicIPs) > 0 {
|
|
|
|
publicIP = instance.PublicIPs[0]
|
|
|
|
}
|
|
|
|
if len(instance.AliasIPRanges) > 0 {
|
|
|
|
nodePodCIDR = instance.AliasIPRanges[0]
|
|
|
|
}
|
2022-06-21 11:59:12 -04:00
|
|
|
subnetworkPodCIDR, err = k.providerMetadata.GetSubnetworkCIDR(ctx)
|
2022-05-24 04:04:42 -04:00
|
|
|
if err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("retrieving subnetwork CIDR failed: %w", err)
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
|
|
|
controlPlaneEndpointIP = publicIP
|
|
|
|
if k.providerMetadata.SupportsLoadBalancer() {
|
2022-06-21 11:59:12 -04:00
|
|
|
controlPlaneEndpointIP, err = k.providerMetadata.GetLoadBalancerIP(ctx)
|
2022-05-24 04:04:42 -04:00
|
|
|
if err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("retrieving load balancer IP failed: %w", err)
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: configure kubeadm init config
|
|
|
|
initConfig := k.configProvider.InitConfiguration(k.cloudControllerManager.Supported())
|
|
|
|
initConfig.SetNodeIP(nodeIP)
|
|
|
|
initConfig.SetCertSANs([]string{publicIP, nodeIP})
|
|
|
|
initConfig.SetNodeName(nodeName)
|
|
|
|
initConfig.SetProviderID(providerID)
|
|
|
|
initConfig.SetControlPlaneEndpoint(controlPlaneEndpointIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
initConfigYAML, err := initConfig.Marshal()
|
|
|
|
if err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("encoding kubeadm init configuration as YAML: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
if err := k.clusterUtil.InitCluster(ctx, initConfigYAML); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("kubeadm init: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
kubeConfig, err := k.GetKubeconfig()
|
|
|
|
if err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("reading kubeconfig after cluster initialization: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
k.client.SetKubeconfig(kubeConfig)
|
2022-05-24 04:04:42 -04:00
|
|
|
|
|
|
|
// Step 3: configure & start kubernetes controllers
|
|
|
|
|
|
|
|
setupPodNetworkInput := k8sapi.SetupPodNetworkInput{
|
|
|
|
CloudProvider: k.cloudProvider,
|
|
|
|
NodeName: nodeName,
|
|
|
|
FirstNodePodCIDR: nodePodCIDR,
|
|
|
|
SubnetworkPodCIDR: subnetworkPodCIDR,
|
|
|
|
ProviderID: providerID,
|
|
|
|
}
|
|
|
|
if err = k.clusterUtil.SetupPodNetwork(ctx, setupPodNetworkInput); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("setting up pod network: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
kms := resources.NewKMSDeployment(k.cloudProvider, kmsConfig.MasterSecret)
|
2022-04-12 10:07:17 -04:00
|
|
|
if err = k.clusterUtil.SetupKMS(k.client, kms); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("setting up kms: %w", err)
|
2022-04-12 10:07:17 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
if err := k.setupJoinService(k.cloudProvider, k.initialMeasurementsJSON, id); err != nil {
|
|
|
|
return nil, fmt.Errorf("setting up join service failed: %w", err)
|
2022-06-15 10:00:48 -04:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
if err := k.setupCCM(ctx, subnetworkPodCIDR, cloudServiceAccountURI, instance); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("setting up cloud controller manager: %w", err)
|
2022-03-25 05:42:27 -04:00
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
if err := k.setupCloudNodeManager(); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("setting up cloud node manager: %w", err)
|
2022-03-25 05:49:18 -04:00
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
if err := k.setupClusterAutoscaler(instance, cloudServiceAccountURI, autoscalingNodeGroups); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("setting up cluster autoscaler: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 10:23:19 -04:00
|
|
|
accessManager := resources.NewAccessManagerDeployment(sshUsers)
|
|
|
|
if err := k.clusterUtil.SetupAccessManager(k.client, accessManager); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("failed to setup access-manager: %w", err)
|
2022-06-13 10:23:19 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
if err := k.clusterUtil.SetupVerificationService(
|
|
|
|
k.client, resources.NewVerificationDaemonSet(k.cloudProvider),
|
|
|
|
); err != nil {
|
2022-06-28 12:33:27 -04:00
|
|
|
return nil, fmt.Errorf("failed to setup verification service: %w", err)
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 10:01:21 -04:00
|
|
|
go k.clusterUtil.FixCilium(nodeName)
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
return k.GetKubeconfig()
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-26 05:22:21 -04:00
|
|
|
// JoinCluster joins existing Kubernetes cluster.
|
2022-06-28 12:23:24 -04:00
|
|
|
func (k *KubeWrapper) JoinCluster(ctx context.Context, args *kubeadm.BootstrapTokenDiscovery, certKey string, peerRole role.Role) error {
|
2022-05-19 11:18:22 -04:00
|
|
|
// TODO: k8s version should be user input
|
2022-06-28 12:23:24 -04:00
|
|
|
if err := k.clusterUtil.InstallComponents(ctx, "1.23.6"); err != nil {
|
2022-05-19 11:18:22 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
// Step 1: retrieve cloud metadata for Kubernetes configuration
|
2022-06-28 12:23:24 -04:00
|
|
|
nodeInternalIP, err := k.getIPAddr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeName := nodeInternalIP
|
2022-05-24 04:04:42 -04:00
|
|
|
var providerID string
|
|
|
|
if k.providerMetadata.Supported() {
|
2022-06-28 12:23:24 -04:00
|
|
|
instance, err := k.providerMetadata.Self(ctx)
|
2022-05-24 04:04:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving own instance metadata failed: %w", err)
|
|
|
|
}
|
|
|
|
providerID = instance.ProviderID
|
|
|
|
nodeName = instance.Name
|
|
|
|
if len(instance.PrivateIPs) > 0 {
|
|
|
|
nodeInternalIP = instance.PrivateIPs[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodeName = k8sCompliantHostname(nodeName)
|
|
|
|
|
|
|
|
// Step 2: configure kubeadm join config
|
|
|
|
|
|
|
|
joinConfig := k.configProvider.JoinConfiguration(k.cloudControllerManager.Supported())
|
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-06-29 09:26:29 -04:00
|
|
|
if peerRole == role.ControlPlane {
|
2022-05-24 04:04:42 -04:00
|
|
|
joinConfig.SetControlPlane(nodeInternalIP, certKey)
|
2022-04-25 11:24:48 -04:00
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
joinConfigYAML, err := joinConfig.Marshal()
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("encoding kubeadm join configuration as YAML: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
if err := k.clusterUtil.JoinCluster(ctx, joinConfigYAML); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("joining cluster: %v; %w ", string(joinConfigYAML), err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 10:01:21 -04:00
|
|
|
go k.clusterUtil.FixCilium(nodeName)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
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
|
|
|
|
}
|
2022-06-29 09:26:29 -04:00
|
|
|
// replace the cluster.Server endpoint (127.0.0.1:16443) in admin.conf with the first bootstrapper endpoint (10.118.0.1:6443)
|
2022-03-22 11:03:15 -04:00
|
|
|
// 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).
|
2022-05-24 04:04:42 -04:00
|
|
|
func (k *KubeWrapper) GetKubeadmCertificateKey(ctx context.Context) (string, error) {
|
|
|
|
return k.clusterUtil.GetControlPlaneJoinCertificateKey(ctx)
|
2022-04-25 11:24:48 -04:00
|
|
|
}
|
|
|
|
|
2022-05-04 08:32:34 -04:00
|
|
|
// GetJoinToken returns a bootstrap (join) token.
|
2022-05-24 04:04:42 -04:00
|
|
|
func (k *KubeWrapper) GetJoinToken(ctx context.Context, ttl time.Duration) (*kubeadm.BootstrapTokenDiscovery, error) {
|
|
|
|
return k.clusterUtil.CreateJoinToken(ctx, ttl)
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
func (k *KubeWrapper) setupJoinService(csp string, measurementsJSON []byte, id attestationtypes.ID) error {
|
2022-06-15 10:00:48 -04:00
|
|
|
idJSON, err := json.Marshal(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
joinConfiguration := resources.NewJoinServiceDaemonset(csp, string(measurementsJSON), string(idJSON))
|
2022-06-15 10:00:48 -04:00
|
|
|
|
2022-07-05 08:13:19 -04:00
|
|
|
return k.clusterUtil.SetupJoinService(k.client, joinConfiguration)
|
2022-06-15 10:00:48 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 12:23:24 -04:00
|
|
|
func (k *KubeWrapper) setupCCM(ctx context.Context, subnetworkPodCIDR, cloudServiceAccountURI string, instance metadata.InstanceMetadata) error {
|
2022-05-24 04:04:42 -04:00
|
|
|
if !k.cloudControllerManager.Supported() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ccmConfigMaps, err := k.cloudControllerManager.ConfigMaps(instance)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("defining ConfigMaps for CCM failed: %w", err)
|
|
|
|
}
|
2022-06-28 10:08:05 -04:00
|
|
|
ccmSecrets, err := k.cloudControllerManager.Secrets(ctx, instance.ProviderID, cloudServiceAccountURI)
|
2022-05-24 04:04:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("defining Secrets for CCM failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cloudControllerManagerConfiguration := resources.NewDefaultCloudControllerManagerDeployment(
|
|
|
|
k.cloudControllerManager.Name(), k.cloudControllerManager.Image(), k.cloudControllerManager.Path(), subnetworkPodCIDR,
|
|
|
|
k.cloudControllerManager.ExtraArgs(), k.cloudControllerManager.Volumes(), k.cloudControllerManager.VolumeMounts(), k.cloudControllerManager.Env(),
|
|
|
|
)
|
|
|
|
if err := k.clusterUtil.SetupCloudControllerManager(k.client, cloudControllerManagerConfiguration, ccmConfigMaps, ccmSecrets); err != nil {
|
|
|
|
return fmt.Errorf("failed to setup cloud-controller-manager: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KubeWrapper) setupCloudNodeManager() error {
|
|
|
|
if !k.cloudNodeManager.Supported() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cloudNodeManagerConfiguration := resources.NewDefaultCloudNodeManagerDeployment(
|
|
|
|
k.cloudNodeManager.Image(), k.cloudNodeManager.Path(), k.cloudNodeManager.ExtraArgs(),
|
|
|
|
)
|
|
|
|
if err := k.clusterUtil.SetupCloudNodeManager(k.client, cloudNodeManagerConfiguration); err != nil {
|
|
|
|
return fmt.Errorf("failed to setup cloud-node-manager: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:23:24 -04:00
|
|
|
func (k *KubeWrapper) setupClusterAutoscaler(instance metadata.InstanceMetadata, cloudServiceAccountURI string, autoscalingNodeGroups []string) error {
|
2022-05-24 04:04:42 -04:00
|
|
|
if !k.clusterAutoscaler.Supported() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-28 10:08:05 -04:00
|
|
|
caSecrets, err := k.clusterAutoscaler.Secrets(instance.ProviderID, cloudServiceAccountURI)
|
2022-05-24 04:04:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("defining Secrets for cluster-autoscaler failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterAutoscalerConfiguration := resources.NewDefaultAutoscalerDeployment(k.clusterAutoscaler.Volumes(), k.clusterAutoscaler.VolumeMounts(), k.clusterAutoscaler.Env())
|
|
|
|
clusterAutoscalerConfiguration.SetAutoscalerCommand(k.clusterAutoscaler.Name(), autoscalingNodeGroups)
|
|
|
|
if err := k.clusterUtil.SetupAutoscaling(k.client, clusterAutoscalerConfiguration, caSecrets); err != nil {
|
|
|
|
return fmt.Errorf("failed to setup cluster-autoscaler: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// k8sCompliantHostname transforms a hostname to an RFC 1123 compliant, lowercase subdomain as required by Kubernetes node names.
|
|
|
|
// The following regex is used by k8s for validation: /^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/ .
|
|
|
|
// Only a simple heuristic is used for now (to lowercase, replace underscores).
|
|
|
|
func k8sCompliantHostname(in string) string {
|
|
|
|
hostname := strings.ToLower(in)
|
|
|
|
hostname = strings.ReplaceAll(hostname, "_", "-")
|
|
|
|
return hostname
|
2022-05-04 08:32:34 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|