cli/cmd/cluster: add ensureComponent() function to controlplaneUpdater
So we can use it to make sure that given controlplane component is
installed and properly configured without upgrading.
Refs
@@ -21,6 +21,7 @@ import (
log "github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
+ "helm.sh/helm/v3/pkg/storage/driver"
"sigs.k8s.io/yaml"
"github.com/kinvolk/lokomotive/pkg/backend"
@@ -252,3 +253,73 @@ func (c controlplaneUpdater) upgradeComponent(component, namespace string) error
return nil
}
+
+// ensureComponent makes sure that given controlplane component is installed and properly configured.
+//
+// Configuration is ensured by forcing a Helm rollback to the latest known version, which should revert
+// all changes done manually to managed resources (restore removed resources etc.).
+//
+//nolint:funlen
+func (c controlplaneUpdater) ensureComponent(component, namespace string) error {
+ actionConfig, err := util.HelmActionConfig(namespace, c.kubeconfig)
+ if err != nil {
+ return fmt.Errorf("initializing Helm action: %w", err)
+ }
+
+ histClient := action.NewHistory(actionConfig)
+ histClient.Max = 1
+
+ history, err := histClient.Run(component)
+ if err != nil && err != driver.ErrReleaseNotFound {
+ return fmt.Errorf("checking for chart history: %w", err)
+ }
+
+ exists := err != driver.ErrReleaseNotFound
+
+ if !exists {
+ fmt.Printf("Controlplane component '%s' is missing, reinstalling...", component)
+
+ helmChart, err := c.getControlplaneChart(component)
+ if err != nil {
+ return fmt.Errorf("loading chart from assets: %w", err)
+ }
+
+ values, err := c.getControlplaneValues(component)
+ if err != nil {
+ return fmt.Errorf("getting chart values from Terraform: %w", err)
+ }
+
+ install := action.NewInstall(actionConfig)
+ install.ReleaseName = component
+ install.Namespace = namespace
+ install.Atomic = true
+ install.CreateNamespace = true
+
+ if _, err := install.Run(helmChart, values); err != nil {
+ fmt.Println("Failed!")
+
+ return fmt.Errorf("installing controlplane component: %w", err)
+ }
+
+ fmt.Println("Done.")
+
+ return nil
+ }
+
+ rollback := action.NewRollback(actionConfig)
+
+ rollback.Wait = true
+ rollback.Version = history[0].Version
+
+ fmt.Printf("Ensuring controlplane component '%s' is properly configured... ", component)
+
+ if err := rollback.Run(component); err != nil {
+ fmt.Println("Failed!")
+
+ return fmt.Errorf("ensuring controlplane component: %w", err)
+ }
+
+ fmt.Println("Done.")
+
+ return nil
+}
