init: add cluster version to kubernetes components

This commit is contained in:
Leonard Cohnen 2023-01-04 17:03:40 +01:00 committed by 3u13r
parent 4803edd4a0
commit 25c3a8a1f3
7 changed files with 22 additions and 26 deletions

View File

@ -12,7 +12,6 @@ import (
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubelet" "github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubelet"
"github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/kubernetes" "github.com/edgelesssys/constellation/v2/internal/kubernetes"
"github.com/edgelesssys/constellation/v2/internal/versions"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeletconf "k8s.io/kubelet/config/v1beta1" kubeletconf "k8s.io/kubelet/config/v1beta1"
@ -33,7 +32,7 @@ const (
type KubdeadmConfiguration struct{} type KubdeadmConfiguration struct{}
// InitConfiguration returns a new init configuration. // InitConfiguration returns a new init configuration.
func (c *KubdeadmConfiguration) InitConfiguration(externalCloudProvider bool, k8sVersion versions.ValidK8sVersion) KubeadmInitYAML { func (c *KubdeadmConfiguration) InitConfiguration(externalCloudProvider bool, clusterVersion string) KubeadmInitYAML {
var cloudProvider string var cloudProvider string
if externalCloudProvider { if externalCloudProvider {
cloudProvider = "external" cloudProvider = "external"
@ -63,7 +62,7 @@ func (c *KubdeadmConfiguration) InitConfiguration(externalCloudProvider bool, k8
APIVersion: kubeadm.SchemeGroupVersion.String(), APIVersion: kubeadm.SchemeGroupVersion.String(),
}, },
// Target kubernetes version of the control plane. // Target kubernetes version of the control plane.
KubernetesVersion: versions.VersionConfigs[k8sVersion].PatchVersion, KubernetesVersion: clusterVersion,
// necessary to be able to access the kubeapi server through localhost // necessary to be able to access the kubeapi server through localhost
APIServer: kubeadm.APIServer{ APIServer: kubeadm.APIServer{
ControlPlaneComponent: kubeadm.ControlPlaneComponent{ ControlPlaneComponent: kubeadm.ControlPlaneComponent{

View File

@ -28,11 +28,11 @@ func TestInitConfiguration(t *testing.T) {
config KubeadmInitYAML config KubeadmInitYAML
}{ }{
"kubeadm init config can be created": { "kubeadm init config can be created": {
config: kubeadmConfig.InitConfiguration(true, versions.Default), config: kubeadmConfig.InitConfiguration(true, versions.VersionConfigs[versions.Default].ClusterVersion),
}, },
"kubeadm init config with all fields can be created": { "kubeadm init config with all fields can be created": {
config: func() KubeadmInitYAML { config: func() KubeadmInitYAML {
c := kubeadmConfig.InitConfiguration(true, versions.Default) c := kubeadmConfig.InitConfiguration(true, versions.VersionConfigs[versions.Default].ClusterVersion)
c.SetNodeIP("192.0.2.0") c.SetNodeIP("192.0.2.0")
c.SetNodeName("node") c.SetNodeName("node")
c.SetProviderID("somecloudprovider://instance-id") c.SetProviderID("somecloudprovider://instance-id")
@ -65,8 +65,8 @@ func TestInitConfigurationKubeadmCompatibility(t *testing.T) {
wantErr bool wantErr bool
}{ }{
"Kubeadm accepts version 'Latest'": { "Kubeadm accepts version 'Latest'": {
config: kubeadmConfig.InitConfiguration(true, versions.Default), config: kubeadmConfig.InitConfiguration(true, versions.VersionConfigs[versions.Default].ClusterVersion),
expectedVersion: versions.VersionConfigs[versions.Default].PatchVersion, expectedVersion: versions.VersionConfigs[versions.Default].ClusterVersion,
}, },
"Kubeadm receives incompatible version": { "Kubeadm receives incompatible version": {
config: kubeadmConfig.InitConfiguration(true, "1.19"), config: kubeadmConfig.InitConfiguration(true, "1.19"),

View File

@ -46,7 +46,7 @@ type configReader interface {
// configurationProvider provides kubeadm init and join configuration. // configurationProvider provides kubeadm init and join configuration.
type configurationProvider interface { type configurationProvider interface {
InitConfiguration(externalCloudProvider bool, k8sVersion versions.ValidK8sVersion) k8sapi.KubeadmInitYAML InitConfiguration(externalCloudProvider bool, k8sVersion string) k8sapi.KubeadmInitYAML
JoinConfiguration(externalCloudProvider bool) k8sapi.KubeadmJoinYAML JoinConfiguration(externalCloudProvider bool) k8sapi.KubeadmJoinYAML
} }
@ -92,11 +92,7 @@ func (k *KubeWrapper) InitCluster(
enforceIDKeyDigest bool, idKeyDigest []byte, azureCVM bool, enforceIDKeyDigest bool, idKeyDigest []byte, azureCVM bool,
helmReleasesRaw []byte, conformanceMode bool, kubernetesComponents versions.ComponentVersions, log *logger.Logger, helmReleasesRaw []byte, conformanceMode bool, kubernetesComponents versions.ComponentVersions, log *logger.Logger,
) ([]byte, error) { ) ([]byte, error) {
k8sVersion, err := versions.NewValidK8sVersion(versionString) log.With(zap.String("version", versionString)).Infof("Installing Kubernetes components")
if err != nil {
return nil, err
}
log.With(zap.String("version", string(k8sVersion))).Infof("Installing Kubernetes components")
if err := k.clusterUtil.InstallComponents(ctx, kubernetesComponents); err != nil { if err := k.clusterUtil.InstallComponents(ctx, kubernetesComponents); err != nil {
return nil, err return nil, err
} }
@ -141,7 +137,7 @@ func (k *KubeWrapper) InitCluster(
// Step 2: configure kubeadm init config // Step 2: configure kubeadm init config
ccmSupported := cloudprovider.FromString(k.cloudProvider) == cloudprovider.Azure || ccmSupported := cloudprovider.FromString(k.cloudProvider) == cloudprovider.Azure ||
cloudprovider.FromString(k.cloudProvider) == cloudprovider.GCP cloudprovider.FromString(k.cloudProvider) == cloudprovider.GCP
initConfig := k.configProvider.InitConfiguration(ccmSupported, k8sVersion) initConfig := k.configProvider.InitConfiguration(ccmSupported, versionString)
initConfig.SetNodeIP(nodeIP) initConfig.SetNodeIP(nodeIP)
initConfig.SetCertSANs([]string{nodeIP}) initConfig.SetCertSANs([]string{nodeIP})
initConfig.SetNodeName(nodeName) initConfig.SetNodeName(nodeName)
@ -171,7 +167,7 @@ func (k *KubeWrapper) InitCluster(
} }
// Setup the K8s components ConfigMap. // Setup the K8s components ConfigMap.
k8sComponentsConfigMap, err := k.setupK8sComponentsConfigMap(ctx, kubernetesComponents) k8sComponentsConfigMap, err := k.setupK8sComponentsConfigMap(ctx, kubernetesComponents, versionString)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to setup k8s version ConfigMap: %w", err) return nil, fmt.Errorf("failed to setup k8s version ConfigMap: %w", err)
} }
@ -323,7 +319,7 @@ func (k *KubeWrapper) GetKubeconfig() ([]byte, error) {
// setupK8sComponentsConfigMap applies a ConfigMap (cf. server-side apply) to store the installed k8s components. // setupK8sComponentsConfigMap applies a ConfigMap (cf. server-side apply) to store the installed k8s components.
// It returns the name of the ConfigMap. // It returns the name of the ConfigMap.
func (k *KubeWrapper) setupK8sComponentsConfigMap(ctx context.Context, components versions.ComponentVersions) (string, error) { func (k *KubeWrapper) setupK8sComponentsConfigMap(ctx context.Context, components versions.ComponentVersions, clusterVersion string) (string, error) {
componentsMarshalled, err := json.Marshal(components) componentsMarshalled, err := json.Marshal(components)
if err != nil { if err != nil {
return "", fmt.Errorf("marshalling component versions: %w", err) return "", fmt.Errorf("marshalling component versions: %w", err)
@ -342,7 +338,8 @@ func (k *KubeWrapper) setupK8sComponentsConfigMap(ctx context.Context, component
Namespace: "kube-system", Namespace: "kube-system",
}, },
Data: map[string]string{ Data: map[string]string{
constants.K8sComponentsFieldName: string(componentsMarshalled), constants.ComponentsListKey: string(componentsMarshalled),
constants.K8sVersionFieldName: clusterVersion,
}, },
} }

View File

@ -556,7 +556,7 @@ type stubConfigProvider struct {
joinConfig k8sapi.KubeadmJoinYAML joinConfig k8sapi.KubeadmJoinYAML
} }
func (s *stubConfigProvider) InitConfiguration(_ bool, _ versions.ValidK8sVersion) k8sapi.KubeadmInitYAML { func (s *stubConfigProvider) InitConfiguration(_ bool, _ string) k8sapi.KubeadmInitYAML {
return s.initConfig return s.initConfig
} }

View File

@ -148,7 +148,7 @@ func (i *initCmd) initialize(cmd *cobra.Command, newDialer func(validator *cloud
KeyEncryptionKeyId: "", KeyEncryptionKeyId: "",
UseExistingKek: false, UseExistingKek: false,
CloudServiceAccountUri: serviceAccURI, CloudServiceAccountUri: serviceAccURI,
KubernetesVersion: conf.KubernetesVersion, KubernetesVersion: versions.VersionConfigs[k8sVersion].ClusterVersion,
KubernetesComponents: versions.VersionConfigs[k8sVersion].KubernetesComponents.ToInitProto(), KubernetesComponents: versions.VersionConfigs[k8sVersion].KubernetesComponents.ToInitProto(),
HelmDeployments: helmDeployments, HelmDeployments: helmDeployments,
EnforcedPcrs: conf.EnforcedPCRs(), EnforcedPcrs: conf.EnforcedPCRs(),

View File

@ -113,8 +113,8 @@ const (
// AzureCVM is the name of the file indicating whether the cluster is expected to run on CVMs or not. // AzureCVM is the name of the file indicating whether the cluster is expected to run on CVMs or not.
AzureCVM = "azureCVM" AzureCVM = "azureCVM"
// K8sComponentsFieldName is the name of the of the key holding the configMap name that holds the components configuration. // K8sVersionFieldName is the name of the of the key holding the wanted Kubernetes version.
K8sComponentsFieldName = "components" K8sVersionFieldName = "cluster-version"
// ComponentsListKey is the name of the key holding the list of components in the components configMap. // ComponentsListKey is the name of the key holding the list of components in the components configMap.
ComponentsListKey = "components" ComponentsListKey = "components"

View File

@ -103,7 +103,7 @@ const (
// VersionConfigs holds download URLs for all required kubernetes components for every supported version. // VersionConfigs holds download URLs for all required kubernetes components for every supported version.
var VersionConfigs = map[ValidK8sVersion]KubernetesVersion{ var VersionConfigs = map[ValidK8sVersion]KubernetesVersion{
V1_23: { V1_23: {
PatchVersion: "v1.23.15", // renovate:kubernetes-release ClusterVersion: "v1.23.15", // renovate:kubernetes-release
KubernetesComponents: ComponentVersions{ KubernetesComponents: ComponentVersions{
{ {
URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release
@ -149,7 +149,7 @@ var VersionConfigs = map[ValidK8sVersion]KubernetesVersion{
ClusterAutoscalerImage: "registry.k8s.io/autoscaling/cluster-autoscaler:v1.23.1@sha256:cd2101ba67f3d6ec719f7792d4bdaa3a50e1b716f3a9ccee8931086496c655b7", // renovate:container ClusterAutoscalerImage: "registry.k8s.io/autoscaling/cluster-autoscaler:v1.23.1@sha256:cd2101ba67f3d6ec719f7792d4bdaa3a50e1b716f3a9ccee8931086496c655b7", // renovate:container
}, },
V1_24: { V1_24: {
PatchVersion: "v1.24.9", // renovate:kubernetes-release ClusterVersion: "v1.24.9", // renovate:kubernetes-release
KubernetesComponents: ComponentVersions{ KubernetesComponents: ComponentVersions{
{ {
URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release
@ -195,7 +195,7 @@ var VersionConfigs = map[ValidK8sVersion]KubernetesVersion{
ClusterAutoscalerImage: "registry.k8s.io/autoscaling/cluster-autoscaler:v1.24.0@sha256:5bd22353ae7f30c9abfaa08189281367ef47ea1b3d09eb13eb26bd13de241e72", // renovate:container ClusterAutoscalerImage: "registry.k8s.io/autoscaling/cluster-autoscaler:v1.24.0@sha256:5bd22353ae7f30c9abfaa08189281367ef47ea1b3d09eb13eb26bd13de241e72", // renovate:container
}, },
V1_25: { V1_25: {
PatchVersion: "v1.25.5", // renovate:kubernetes-release ClusterVersion: "v1.25.5", // renovate:kubernetes-release
KubernetesComponents: ComponentVersions{ KubernetesComponents: ComponentVersions{
{ {
URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release
@ -244,7 +244,7 @@ var VersionConfigs = map[ValidK8sVersion]KubernetesVersion{
ClusterAutoscalerImage: "registry.k8s.io/autoscaling/cluster-autoscaler:v1.25.0@sha256:f509ffab618dbd07d129b69ec56963aac7f61aaa792851206b54a2f0bbe046df", // renovate:container ClusterAutoscalerImage: "registry.k8s.io/autoscaling/cluster-autoscaler:v1.25.0@sha256:f509ffab618dbd07d129b69ec56963aac7f61aaa792851206b54a2f0bbe046df", // renovate:container
}, },
V1_26: { V1_26: {
PatchVersion: "v1.26.0", // renovate:kubernetes-release ClusterVersion: "v1.26.0", // renovate:kubernetes-release
KubernetesComponents: ComponentVersions{ KubernetesComponents: ComponentVersions{
{ {
URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release URL: "https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz", // renovate:cni-plugins-release
@ -296,7 +296,7 @@ var VersionConfigs = map[ValidK8sVersion]KubernetesVersion{
// KubernetesVersion bundles download URLs to all version-releated binaries necessary for installing/deploying a particular Kubernetes version. // KubernetesVersion bundles download URLs to all version-releated binaries necessary for installing/deploying a particular Kubernetes version.
type KubernetesVersion struct { type KubernetesVersion struct {
PatchVersion string ClusterVersion string
KubernetesComponents ComponentVersions KubernetesComponents ComponentVersions
CloudControllerManagerImageAWS string // k8s version dependency. CloudControllerManagerImageAWS string // k8s version dependency.
CloudControllerManagerImageGCP string // Using self-built image until resolved: https://github.com/kubernetes/cloud-provider-gcp/issues/289 CloudControllerManagerImageGCP string // Using self-built image until resolved: https://github.com/kubernetes/cloud-provider-gcp/issues/289