From dd8f1a35a44906f71606ffa48eabc2b94bd92d0d Mon Sep 17 00:00:00 2001 From: faiq Date: Mon, 8 Apr 2024 12:26:05 -0600 Subject: [PATCH] feat: add helm chart field --- api/v1alpha1/addon_types.go | 6 ++++ api/v1alpha1/constants.go | 2 ++ api/v1alpha1/zz_generated.deepcopy.go | 5 +++ .../lifecycle/clusterautoscaler/handler.go | 1 + .../generic/lifecycle/cni/calico/handler.go | 2 +- .../generic/lifecycle/cni/cilium/handler.go | 2 +- pkg/handlers/generic/lifecycle/config/cm.go | 36 +++++++++++++++---- .../lifecycle/csi/nutanix-csi/handler.go | 9 +++-- pkg/handlers/generic/lifecycle/nfd/handler.go | 2 +- 9 files changed, 54 insertions(+), 11 deletions(-) diff --git a/api/v1alpha1/addon_types.go b/api/v1alpha1/addon_types.go index 27f08077d..b1eee7bff 100644 --- a/api/v1alpha1/addon_types.go +++ b/api/v1alpha1/addon_types.go @@ -9,6 +9,7 @@ import ( "k8s.io/utils/ptr" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/openapi/patterns" "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables" ) @@ -24,6 +25,7 @@ const ( ) type Addons struct { + HelmChartRepository *string `json:"helmChartRepository,omitempty"` // +optional CNI *CNI `json:"cni,omitempty"` @@ -46,6 +48,10 @@ func (Addons) VariableSchema() clusterv1.VariableSchema { Description: "Cluster configuration", Type: "object", Properties: map[string]clusterv1.JSONSchemaProps{ + "helmChartRepository": { + Pattern: patterns.DNS1123Label, + Description: "Optional OCI registry used to pull helm charts for adons", + }, "cni": CNI{}.VariableSchema().OpenAPIV3Schema, "nfd": NFD{}.VariableSchema().OpenAPIV3Schema, "clusterAutoscaler": ClusterAutoscaler{}.VariableSchema().OpenAPIV3Schema, diff --git a/api/v1alpha1/constants.go b/api/v1alpha1/constants.go index de4b8b1f6..0d9b3d06f 100644 --- a/api/v1alpha1/constants.go +++ b/api/v1alpha1/constants.go @@ -14,4 +14,6 @@ const ( AWSVariableName = "aws" // NutanixVariableName is the Nutanix config patch variable name. NutanixVariableName = "nutanix" + // HelmChartRepository is the variable name for the OCI registry for addons. + HelmChartRepository = "HelmChartRepository" ) diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 533f9e54a..e175a3c45 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -184,6 +184,11 @@ func (in AdditionalSecurityGroup) DeepCopy() AdditionalSecurityGroup { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Addons) DeepCopyInto(out *Addons) { *out = *in + if in.HelmChartRepository != nil { + in, out := &in.HelmChartRepository, &out.HelmChartRepository + *out = new(string) + **out = **in + } if in.CNI != nil { in, out := &in.CNI, &out.CNI *out = new(CNI) diff --git a/pkg/handlers/generic/lifecycle/clusterautoscaler/handler.go b/pkg/handlers/generic/lifecycle/clusterautoscaler/handler.go index d9b6c68a1..a348eb5d9 100644 --- a/pkg/handlers/generic/lifecycle/clusterautoscaler/handler.go +++ b/pkg/handlers/generic/lifecycle/clusterautoscaler/handler.go @@ -123,6 +123,7 @@ func (n *DefaultClusterAutoscaler) AfterControlPlaneInitialized( case v1alpha1.AddonStrategyHelmAddon: helmChart, err := n.helmChartInfoGetter.For( ctx, + &req.Cluster, log, config.Autoscaler, ) diff --git a/pkg/handlers/generic/lifecycle/cni/calico/handler.go b/pkg/handlers/generic/lifecycle/cni/calico/handler.go index ec0dfa431..ac5e4c10b 100644 --- a/pkg/handlers/generic/lifecycle/cni/calico/handler.go +++ b/pkg/handlers/generic/lifecycle/cni/calico/handler.go @@ -130,7 +130,7 @@ func (c *CalicoCNI) AfterControlPlaneInitialized( case v1alpha1.AddonStrategyHelmAddon: // this is tigera and not calico because we deploy calico via operataor log.Info("fetching settings for tigera-operator-config") - helmChart, err := c.helmChartInfoGetter.For(ctx, log, config.Tigera) + helmChart, err := c.helmChartInfoGetter.For(ctx, &req.Cluster, log, config.Tigera) if err != nil { log.Error( err, diff --git a/pkg/handlers/generic/lifecycle/cni/cilium/handler.go b/pkg/handlers/generic/lifecycle/cni/cilium/handler.go index 8abcb6091..f8fa53306 100644 --- a/pkg/handlers/generic/lifecycle/cni/cilium/handler.go +++ b/pkg/handlers/generic/lifecycle/cni/cilium/handler.go @@ -128,7 +128,7 @@ func (c *CiliumCNI) AfterControlPlaneInitialized( client: c.client, } case v1alpha1.AddonStrategyHelmAddon: - helmChart, err := c.helmChartInfoGetter.For(ctx, log, config.Cilium) + helmChart, err := c.helmChartInfoGetter.For(ctx, &req.Cluster, log, config.Cilium) if err != nil { log.Error( err, diff --git a/pkg/handlers/generic/lifecycle/config/cm.go b/pkg/handlers/generic/lifecycle/config/cm.go index 89b44fa20..d57b19261 100644 --- a/pkg/handlers/generic/lifecycle/config/cm.go +++ b/pkg/handlers/generic/lifecycle/config/cm.go @@ -11,7 +11,12 @@ import ( "gopkg.in/yaml.v2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1" + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables" + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" ) type Component string @@ -27,9 +32,10 @@ const ( ) type HelmChartGetter struct { - cl ctrlclient.Reader - cmName string - cmNamespace string + cl ctrlclient.Reader + cmName string + cmNamespace string + variablePath []string } type HelmChart struct { @@ -46,6 +52,7 @@ func NewHelmChartGetterFromConfigMap( cl, cmName, cmNamespace, + []string{"addons", v1alpha1.HelmChartRepository}, } } @@ -72,6 +79,7 @@ func (h *HelmChartGetter) get( func (h *HelmChartGetter) For( ctx context.Context, + cluster *clusterv1.Cluster, log logr.Logger, name Component, ) (*HelmChart, error) { @@ -81,6 +89,19 @@ func (h *HelmChartGetter) For( h.cmName, h.cmNamespace), ) + varMap := variables.ClusterVariablesToVariablesMap(cluster.Spec.Topology.Variables) + + helmChartRepo, found, err := variables.Get[string]( + varMap, + clusterconfig.MetaVariableName, + h.variablePath...) + if err != nil { + return nil, fmt.Errorf( + "failed to get helmChartRepo variable from %s: %w", + clusterconfig.MetaVariableName, + err, + ) + } cm, err := h.get(ctx) if err != nil { return nil, err @@ -89,7 +110,10 @@ func (h *HelmChartGetter) For( if !ok { return nil, fmt.Errorf("did not find key %s in %v", name, cm.Data) } - var settings HelmChart - err = yaml.Unmarshal([]byte(d), &settings) - return &settings, err + var chart HelmChart + err = yaml.Unmarshal([]byte(d), &chart) + if found { + chart.Repository = helmChartRepo + } + return &chart, err } diff --git a/pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go b/pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go index dcaf9da59..cda324b82 100644 --- a/pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go +++ b/pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go @@ -148,7 +148,7 @@ func (n *NutanixCSI) handleHelmAddonApply( "cluster", ctrlclient.ObjectKeyFromObject(&req.Cluster), ) - helmChart, err := n.helmChartInfoGetter.For(ctx, log, config.NutanixStorageCSI) + helmChart, err := n.helmChartInfoGetter.For(ctx, &req.Cluster, log, config.NutanixStorageCSI) if err != nil { return fmt.Errorf("failed to get values for nutanix-csi-config %w", err) } @@ -186,7 +186,12 @@ func (n *NutanixCSI) handleHelmAddonApply( return fmt.Errorf("failed to apply nutanix-csi installation HelmChartProxy: %w", err) } - snapshotHelmChart, err := n.helmChartInfoGetter.For(ctx, log, config.NutanixSnapshotCSI) + snapshotHelmChart, err := n.helmChartInfoGetter.For( + ctx, + &req.Cluster, + log, + config.NutanixSnapshotCSI, + ) if err != nil { return fmt.Errorf("failed to get values for nutanix-csi-config %w", err) } diff --git a/pkg/handlers/generic/lifecycle/nfd/handler.go b/pkg/handlers/generic/lifecycle/nfd/handler.go index f4c42966e..8fb9a42a0 100644 --- a/pkg/handlers/generic/lifecycle/nfd/handler.go +++ b/pkg/handlers/generic/lifecycle/nfd/handler.go @@ -116,7 +116,7 @@ func (n *DefaultNFD) AfterControlPlaneInitialized( client: n.client, } case v1alpha1.AddonStrategyHelmAddon: - helmChart, err := n.helmChartInfoGetter.For(ctx, log, config.NFD) + helmChart, err := n.helmChartInfoGetter.For(ctx, &req.Cluster, log, config.NFD) if err != nil { log.Error( err,