Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: standardize the code for getting Helm values #500

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pkg/handlers/generic/lifecycle/ccm/aws/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"fmt"

"github.com/blang/semver/v4"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
Expand Down Expand Up @@ -58,11 +58,8 @@ func (a *AWSCCM) Apply(
ctx context.Context,
cluster *clusterv1.Cluster,
_ *v1alpha1.ClusterConfigSpec,
log logr.Logger,
) error {
log := ctrl.LoggerFrom(ctx).WithValues(
"cluster",
cluster.Name,
)
log.Info("Creating AWS CCM ConfigMap for Cluster")
version, err := semver.ParseTolerant(cluster.Spec.Topology.Version)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions pkg/handlers/generic/lifecycle/ccm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strings"

"github.com/go-logr/logr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -25,7 +26,12 @@ const (
)

type CCMProvider interface {
Apply(context.Context, *clusterv1.Cluster, *v1alpha1.ClusterConfigSpec) error
Apply(
context.Context,
*clusterv1.Cluster,
*v1alpha1.ClusterConfigSpec,
logr.Logger,
) error
}

type CCMHandler struct {
Expand Down Expand Up @@ -120,7 +126,7 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
return
}

err = handler.Apply(ctx, &req.Cluster, &clusterConfigVar)
err = handler.Apply(ctx, &req.Cluster, &clusterConfigVar, log)
if err != nil {
log.Error(
err,
Expand Down
13 changes: 5 additions & 8 deletions pkg/handlers/generic/lifecycle/ccm/nutanix/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"fmt"
"text/template"

"github.com/go-logr/logr"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand Down Expand Up @@ -73,21 +73,23 @@ func (p *provider) Apply(
ctx context.Context,
cluster *clusterv1.Cluster,
clusterConfig *v1alpha1.ClusterConfigSpec,
log logr.Logger,
) error {
// No need to check for nil values in the struct, this function will only be called if CCM is not nil
if clusterConfig.Addons.CCM.Credentials == nil {
return ErrMissingCredentials
}

valuesTemplateConfigMap, err := lifecycleutils.RetrieveValuesTemplateConfigMap(
log.Info("Retrieving Nutanix CCM installation values template for cluster")
values, err := lifecycleutils.RetrieveValuesTemplate(
ctx,
p.client,
p.config.defaultValuesTemplateConfigMapName,
p.config.DefaultsNamespace(),
)
if err != nil {
return fmt.Errorf(
"failed to retrieve Nutanix CCM installation values template ConfigMap for cluster: %w",
"failed to retrieve Nutanix CCM installation values template for cluster: %w",
err,
)
}
Expand Down Expand Up @@ -115,16 +117,11 @@ func (p *provider) Apply(
}
}

log := ctrl.LoggerFrom(ctx).WithValues(
"cluster",
ctrlclient.ObjectKeyFromObject(cluster),
)
helmChart, err := p.helmChartInfoGetter.For(ctx, log, config.NutanixCCM)
if err != nil {
return fmt.Errorf("failed to get values for nutanix-ccm-config %w", err)
}

values := valuesTemplateConfigMap.Data["values.yaml"]
// The configMap will contain the Helm values, but templated with fields that need to be filled in.
values, err = templateValues(clusterConfig, values)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@ import (
)

type crsConfig struct {
defaultsNamespace string

defaultClusterAutoscalerConfigMap string
}

func (c *crsConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
flags.StringVar(
&c.defaultsNamespace,
prefix+".defaults-namespace",
corev1.NamespaceDefault,
"namespace of the ConfigMap used to deploy cluster-autoscaler",
)

flags.StringVar(
&c.defaultClusterAutoscalerConfigMap,
prefix+".default-cluster-autoscaler-configmap-name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
)

const (
Expand Down Expand Up @@ -52,27 +52,23 @@ func (s helmAddonStrategy) apply(
log logr.Logger,
) error {
log.Info("Retrieving cluster-autoscaler installation values template for cluster")
valuesTemplateConfigMap, err := utils.RetrieveValuesTemplateConfigMap(
values, err := lifecycleutils.RetrieveValuesTemplate(
ctx,
s.client,
s.config.defaultValuesTemplateConfigMapName,
defaultsNamespace,
)
if err != nil {
return fmt.Errorf(
"failed to retrieve cluster-autoscaler installation values template ConfigMap for cluster: %w",
"failed to retrieve cluster-autoscaler installation values template for cluster: %w",
err,
)
}

cluster := &req.Cluster

values := valuesTemplateConfigMap.Data["values.yaml"]

// The cluster-autoscaler is different from other addons.
// It requires all resources to be created in the management cluster,
// which means creating the HelmChartProxy always targeting the management cluster.
targetCluster, err := findTargetCluster(ctx, s.client, cluster)
targetCluster, err := findTargetCluster(ctx, s.client, &req.Cluster)
if err != nil {
return err
}
Expand Down
37 changes: 6 additions & 31 deletions pkg/handlers/generic/lifecycle/cni/calico/strategy_helmaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-logr/logr"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
Expand All @@ -19,6 +18,7 @@ import (
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
)

const (
Expand Down Expand Up @@ -68,14 +68,15 @@ func (s helmAddonStrategy) apply(
}

log.Info("Retrieving Calico installation values template for cluster")
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(
values, err := lifecycleutils.RetrieveValuesTemplate(
ctx,
defaultsNamespace,
s.client,
defaultInstallationConfigMapName,
defaultsNamespace,
)
if err != nil {
return fmt.Errorf(
"failed to retrieve Calico installation values template ConfigMap for cluster: %w",
"failed to retrieve Calico installation values template for cluster: %w",
err,
)
}
Expand All @@ -98,7 +99,7 @@ func (s helmAddonStrategy) apply(
ReleaseNamespace: defaultTigerOperatorNamespace,
ReleaseName: defaultTigeraOperatorReleaseName,
Version: s.helmChart.Version,
ValuesTemplate: valuesTemplateConfigMap.Data["values.yaml"],
ValuesTemplate: values,
},
}

Expand All @@ -115,29 +116,3 @@ func (s helmAddonStrategy) apply(

return nil
}

func (s helmAddonStrategy) retrieveValuesTemplateConfigMap(
ctx context.Context,
defaultsNamespace string,
configMapName string,
) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: defaultsNamespace,
Name: configMapName,
},
}
configMapObjName := ctrlclient.ObjectKeyFromObject(
configMap,
)
err := s.client.Get(ctx, configMapObjName, configMap)
if err != nil {
return nil, fmt.Errorf(
"failed to retrieve installation values template ConfigMap %q: %w",
configMapObjName,
err,
)
}

return configMap, nil
}
38 changes: 9 additions & 29 deletions pkg/handlers/generic/lifecycle/cni/cilium/strategy_helmaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-logr/logr"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
Expand All @@ -19,6 +18,7 @@ import (
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
)

const (
Expand Down Expand Up @@ -52,10 +52,15 @@ func (s helmAddonStrategy) apply(
log logr.Logger,
) error {
log.Info("Retrieving Cilium installation values template for cluster")
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(ctx, defaultsNamespace)
values, err := lifecycleutils.RetrieveValuesTemplate(
ctx,
s.client,
s.config.defaultValuesTemplateConfigMapName,
defaultsNamespace,
)
if err != nil {
return fmt.Errorf(
"failed to retrieve Cilium installation values template ConfigMap for cluster: %w",
"failed to retrieve Cilium installation values template for cluster: %w",
err,
)
}
Expand All @@ -78,7 +83,7 @@ func (s helmAddonStrategy) apply(
ReleaseNamespace: defaultCiliumNamespace,
ReleaseName: defaultCiliumReleaseName,
Version: s.helmChart.Version,
ValuesTemplate: valuesTemplateConfigMap.Data["values.yaml"],
ValuesTemplate: values,
},
}

Expand All @@ -95,28 +100,3 @@ func (s helmAddonStrategy) apply(

return nil
}

func (s helmAddonStrategy) retrieveValuesTemplateConfigMap(
ctx context.Context,
defaultsNamespace string,
) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: defaultsNamespace,
Name: s.config.defaultValuesTemplateConfigMapName,
},
}
configMapObjName := ctrlclient.ObjectKeyFromObject(
configMap,
)
err := s.client.Get(ctx, configMapObjName, configMap)
if err != nil {
return nil, fmt.Errorf(
"failed to retrieve installation values template ConfigMap %q: %w",
configMapObjName,
err,
)
}

return configMap, nil
}
2 changes: 2 additions & 0 deletions pkg/handlers/generic/lifecycle/csi/aws-ebs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"

"github.com/go-logr/logr"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -60,6 +61,7 @@ func (a *AWSEBS) Apply(
provider v1alpha1.CSIProvider,
defaultStorageConfig *v1alpha1.DefaultStorage,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
_ logr.Logger,
) error {
strategy := provider.Strategy
switch strategy {
Expand Down
3 changes: 3 additions & 0 deletions pkg/handlers/generic/lifecycle/csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"

"github.com/go-logr/logr"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -28,6 +29,7 @@ type CSIProvider interface {
v1alpha1.CSIProvider,
*v1alpha1.DefaultStorage,
*runtimehooksv1.AfterControlPlaneInitializedRequest,
logr.Logger,
) error
}

Expand Down Expand Up @@ -124,6 +126,7 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
provider,
csiProviders.DefaultStorage,
req,
log,
)
if err != nil {
log.Error(
Expand Down
Loading
Loading