From e054f892b3749240ca62915b50683f82b13e4ada Mon Sep 17 00:00:00 2001 From: Artiom Diomin Date: Tue, 6 Aug 2024 17:34:15 +0300 Subject: [PATCH 1/2] Deploy default-storage-class addon by default Unless it's already configured and there is no another default StorageClass Signed-off-by: Artiom Diomin --- pkg/addons/ensure.go | 37 +++++++++++++++++++++++++--- pkg/localhelm/helm3.go | 2 +- pkg/tasks/kubeconfig.go | 2 +- pkg/templates/resources/resources.go | 1 + 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/pkg/addons/ensure.go b/pkg/addons/ensure.go index 0ab90a0a6..c1142979e 100644 --- a/pkg/addons/ensure.go +++ b/pkg/addons/ensure.go @@ -19,21 +19,22 @@ package addons import ( "fmt" "io/fs" + "slices" "github.com/pkg/errors" + kubeoneapi "k8c.io/kubeone/pkg/apis/kubeone" "k8c.io/kubeone/pkg/fail" "k8c.io/kubeone/pkg/state" "k8c.io/kubeone/pkg/templates/resources" "k8c.io/kubeone/pkg/templates/weave" + + storagev1 "k8s.io/api/storage/v1" ) const ( // addonLabel is applied to all objects deployed using addons addonLabel = "kubeone.io/addon" - - // defaultStorageClass addon defines name of the default-storage-class addon - defaultStorageClassAddonName = "default-storage-class" ) // embeddedAddons is a list of addons that are embedded in the KubeOne @@ -140,6 +141,33 @@ func collectAddons(s *state.State) []addonAction { addonsToDeploy = ensureCCMAddons(s, addonsToDeploy) } + var foundDefaultStorageClass bool + + if s.Cluster.Addons != nil && !slices.ContainsFunc(s.Cluster.Addons.Addons, func(addon kubeoneapi.Addon) bool { + return addon.Name == resources.AddonDefaultStorageClass + }) { + var existingStorageClasses storagev1.StorageClassList + + if s.DynamicClient != nil { + if err := s.DynamicClient.List(s.Context, &existingStorageClasses); err != nil { + s.Logger.Error(fail.KubeClient(err, "listing existing storage classes")) + } + } + + for _, sc := range existingStorageClasses.Items { + if sc.Annotations["storageclass.kubernetes.io/is-default-class"] == "true" { + foundDefaultStorageClass = true + } + } + + } + + if !foundDefaultStorageClass { + addonsToDeploy = append(addonsToDeploy, addonAction{ + name: resources.AddonDefaultStorageClass, + }) + } + return addonsToDeploy } @@ -162,6 +190,7 @@ func Ensure(s *state.State) error { return err } } + if err := EnsureAddonByName(s, add.name); err != nil { return err } @@ -224,7 +253,7 @@ func EnsureUserAddons(s *state.State) error { // NB: We can't migrate StorageClass when applying the CSI driver because // CSI driver is deployed only for Kubernetes 1.23+ clusters, but this // issue affects older clusters as well. - if addonName == defaultStorageClassAddonName && s.Cluster.CloudProvider.GCE != nil { + if addonName == resources.AddonDefaultStorageClass && s.Cluster.CloudProvider.GCE != nil { if err := migrateGCEStandardStorageClass(s); err != nil { return err } diff --git a/pkg/localhelm/helm3.go b/pkg/localhelm/helm3.go index 62bc6d831..c1bbbe8e4 100644 --- a/pkg/localhelm/helm3.go +++ b/pkg/localhelm/helm3.go @@ -38,7 +38,6 @@ import ( "helm.sh/helm/v3/pkg/registry" helmrelease "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/storage/driver" - "sigs.k8s.io/yaml" kubeoneapi "k8c.io/kubeone/pkg/apis/kubeone" "k8c.io/kubeone/pkg/fail" @@ -52,6 +51,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/yaml" ) const ( diff --git a/pkg/tasks/kubeconfig.go b/pkg/tasks/kubeconfig.go index a2e3f894e..c3aedc1ba 100644 --- a/pkg/tasks/kubeconfig.go +++ b/pkg/tasks/kubeconfig.go @@ -44,7 +44,7 @@ func saveKubeconfig(st *state.State) error { } func removeSuperKubeconfig(st *state.State) error { - st.Logger.Info("Removing %s...", superAdminConfPath) + st.Logger.Infof("Removing %s...", superAdminConfPath) host, err := st.Cluster.Leader() if err != nil { diff --git a/pkg/templates/resources/resources.go b/pkg/templates/resources/resources.go index 958573508..9065a593b 100644 --- a/pkg/templates/resources/resources.go +++ b/pkg/templates/resources/resources.go @@ -55,6 +55,7 @@ const ( AddonMetricsServer = "metrics-server" AddonNodeLocalDNS = "nodelocaldns" AddonOperatingSystemManager = "operating-system-manager" + AddonDefaultStorageClass = "default-storage-class" ) func CloudAddons() []string { From a18f8a619016602d48538c7d620f6db4b87e5ed2 Mon Sep 17 00:00:00 2001 From: Artiom Diomin Date: Tue, 6 Aug 2024 17:50:33 +0300 Subject: [PATCH 2/2] Fix linter Signed-off-by: Artiom Diomin --- pkg/addons/ensure.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/addons/ensure.go b/pkg/addons/ensure.go index c1142979e..45941b056 100644 --- a/pkg/addons/ensure.go +++ b/pkg/addons/ensure.go @@ -159,7 +159,6 @@ func collectAddons(s *state.State) []addonAction { foundDefaultStorageClass = true } } - } if !foundDefaultStorageClass {