Skip to content

Commit 7ed390a

Browse files
committed
refactor: standardize the code for getting Helm values
1 parent 97df77f commit 7ed390a

File tree

12 files changed

+72
-131
lines changed

12 files changed

+72
-131
lines changed

pkg/handlers/generic/lifecycle/ccm/aws/handler.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"fmt"
99

1010
"github.com/blang/semver/v4"
11+
"github.com/go-logr/logr"
1112
"github.com/spf13/pflag"
1213
corev1 "k8s.io/api/core/v1"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
15-
ctrl "sigs.k8s.io/controller-runtime"
1616
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1717

1818
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
@@ -58,11 +58,8 @@ func (a *AWSCCM) Apply(
5858
ctx context.Context,
5959
cluster *clusterv1.Cluster,
6060
_ *v1alpha1.ClusterConfigSpec,
61+
log logr.Logger,
6162
) error {
62-
log := ctrl.LoggerFrom(ctx).WithValues(
63-
"cluster",
64-
cluster.Name,
65-
)
6663
log.Info("Creating AWS CCM ConfigMap for Cluster")
6764
version, err := semver.ParseTolerant(cluster.Spec.Topology.Version)
6865
if err != nil {

pkg/handlers/generic/lifecycle/ccm/handler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"strings"
1010

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

2728
type CCMProvider interface {
28-
Apply(context.Context, *clusterv1.Cluster, *v1alpha1.ClusterConfigSpec) error
29+
Apply(
30+
context.Context,
31+
*clusterv1.Cluster,
32+
*v1alpha1.ClusterConfigSpec,
33+
logr.Logger,
34+
) error
2935
}
3036

3137
type CCMHandler struct {
@@ -120,7 +126,7 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
120126
return
121127
}
122128

123-
err = handler.Apply(ctx, &req.Cluster, &clusterConfigVar)
129+
err = handler.Apply(ctx, &req.Cluster, &clusterConfigVar, log)
124130
if err != nil {
125131
log.Error(
126132
err,

pkg/handlers/generic/lifecycle/ccm/nutanix/handler.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"fmt"
1111
"text/template"
1212

13+
"github.com/go-logr/logr"
1314
"github.com/spf13/pflag"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
16-
ctrl "sigs.k8s.io/controller-runtime"
1717
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1818
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1919

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

82-
valuesTemplateConfigMap, err := lifecycleutils.RetrieveValuesTemplateConfigMap(
83+
log.Info("Retrieving Nutanix CCM installation values template for cluster")
84+
values, err := lifecycleutils.RetrieveValuesTemplate(
8385
ctx,
8486
p.client,
8587
p.config.defaultValuesTemplateConfigMapName,
8688
p.config.DefaultsNamespace(),
8789
)
8890
if err != nil {
8991
return fmt.Errorf(
90-
"failed to retrieve Nutanix CCM installation values template ConfigMap for cluster: %w",
92+
"failed to retrieve Nutanix CCM installation values template for cluster: %w",
9193
err,
9294
)
9395
}
@@ -115,16 +117,11 @@ func (p *provider) Apply(
115117
}
116118
}
117119

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

127-
values := valuesTemplateConfigMap.Data["values.yaml"]
128125
// The configMap will contain the Helm values, but templated with fields that need to be filled in.
129126
values, err = templateValues(clusterConfig, values)
130127
if err != nil {

pkg/handlers/generic/lifecycle/clusterautoscaler/strategy_crs.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,10 @@ import (
2020
)
2121

2222
type crsConfig struct {
23-
defaultsNamespace string
24-
2523
defaultClusterAutoscalerConfigMap string
2624
}
2725

2826
func (c *crsConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
29-
flags.StringVar(
30-
&c.defaultsNamespace,
31-
prefix+".defaults-namespace",
32-
corev1.NamespaceDefault,
33-
"namespace of the ConfigMap used to deploy cluster-autoscaler",
34-
)
35-
3627
flags.StringVar(
3728
&c.defaultClusterAutoscalerConfigMap,
3829
prefix+".default-cluster-autoscaler-configmap-name",

pkg/handlers/generic/lifecycle/clusterautoscaler/strategy_helmaddon.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
1919
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
2020
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
21-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
21+
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
2222
)
2323

2424
const (
@@ -52,27 +52,23 @@ func (s helmAddonStrategy) apply(
5252
log logr.Logger,
5353
) error {
5454
log.Info("Retrieving cluster-autoscaler installation values template for cluster")
55-
valuesTemplateConfigMap, err := utils.RetrieveValuesTemplateConfigMap(
55+
values, err := lifecycleutils.RetrieveValuesTemplate(
5656
ctx,
5757
s.client,
5858
s.config.defaultValuesTemplateConfigMapName,
5959
defaultsNamespace,
6060
)
6161
if err != nil {
6262
return fmt.Errorf(
63-
"failed to retrieve cluster-autoscaler installation values template ConfigMap for cluster: %w",
63+
"failed to retrieve cluster-autoscaler installation values template for cluster: %w",
6464
err,
6565
)
6666
}
6767

68-
cluster := &req.Cluster
69-
70-
values := valuesTemplateConfigMap.Data["values.yaml"]
71-
7268
// The cluster-autoscaler is different from other addons.
7369
// It requires all resources to be created in the management cluster,
7470
// which means creating the HelmChartProxy always targeting the management cluster.
75-
targetCluster, err := findTargetCluster(ctx, s.client, cluster)
71+
targetCluster, err := findTargetCluster(ctx, s.client, &req.Cluster)
7672
if err != nil {
7773
return err
7874
}

pkg/handlers/generic/lifecycle/cni/calico/strategy_helmaddon.go

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/go-logr/logr"
1111
"github.com/spf13/pflag"
12-
corev1 "k8s.io/api/core/v1"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1514
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -19,6 +18,7 @@ import (
1918
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
2019
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
2120
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
21+
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
2222
)
2323

2424
const (
@@ -68,14 +68,15 @@ func (s helmAddonStrategy) apply(
6868
}
6969

7070
log.Info("Retrieving Calico installation values template for cluster")
71-
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(
71+
values, err := lifecycleutils.RetrieveValuesTemplate(
7272
ctx,
73-
defaultsNamespace,
73+
s.client,
7474
defaultInstallationConfigMapName,
75+
defaultsNamespace,
7576
)
7677
if err != nil {
7778
return fmt.Errorf(
78-
"failed to retrieve Calico installation values template ConfigMap for cluster: %w",
79+
"failed to retrieve Calico installation values template for cluster: %w",
7980
err,
8081
)
8182
}
@@ -98,7 +99,7 @@ func (s helmAddonStrategy) apply(
9899
ReleaseNamespace: defaultTigerOperatorNamespace,
99100
ReleaseName: defaultTigeraOperatorReleaseName,
100101
Version: s.helmChart.Version,
101-
ValuesTemplate: valuesTemplateConfigMap.Data["values.yaml"],
102+
ValuesTemplate: values,
102103
},
103104
}
104105

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

116117
return nil
117118
}
118-
119-
func (s helmAddonStrategy) retrieveValuesTemplateConfigMap(
120-
ctx context.Context,
121-
defaultsNamespace string,
122-
configMapName string,
123-
) (*corev1.ConfigMap, error) {
124-
configMap := &corev1.ConfigMap{
125-
ObjectMeta: metav1.ObjectMeta{
126-
Namespace: defaultsNamespace,
127-
Name: configMapName,
128-
},
129-
}
130-
configMapObjName := ctrlclient.ObjectKeyFromObject(
131-
configMap,
132-
)
133-
err := s.client.Get(ctx, configMapObjName, configMap)
134-
if err != nil {
135-
return nil, fmt.Errorf(
136-
"failed to retrieve installation values template ConfigMap %q: %w",
137-
configMapObjName,
138-
err,
139-
)
140-
}
141-
142-
return configMap, nil
143-
}

pkg/handlers/generic/lifecycle/cni/cilium/strategy_helmaddon.go

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/go-logr/logr"
1111
"github.com/spf13/pflag"
12-
corev1 "k8s.io/api/core/v1"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1514
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -19,6 +18,7 @@ import (
1918
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
2019
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
2120
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
21+
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
2222
)
2323

2424
const (
@@ -52,10 +52,15 @@ func (s helmAddonStrategy) apply(
5252
log logr.Logger,
5353
) error {
5454
log.Info("Retrieving Cilium installation values template for cluster")
55-
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(ctx, defaultsNamespace)
55+
values, err := lifecycleutils.RetrieveValuesTemplate(
56+
ctx,
57+
s.client,
58+
s.config.defaultValuesTemplateConfigMapName,
59+
defaultsNamespace,
60+
)
5661
if err != nil {
5762
return fmt.Errorf(
58-
"failed to retrieve Cilium installation values template ConfigMap for cluster: %w",
63+
"failed to retrieve Cilium installation values template for cluster: %w",
5964
err,
6065
)
6166
}
@@ -78,7 +83,7 @@ func (s helmAddonStrategy) apply(
7883
ReleaseNamespace: defaultCiliumNamespace,
7984
ReleaseName: defaultCiliumReleaseName,
8085
Version: s.helmChart.Version,
81-
ValuesTemplate: valuesTemplateConfigMap.Data["values.yaml"],
86+
ValuesTemplate: values,
8287
},
8388
}
8489

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

96101
return nil
97102
}
98-
99-
func (s helmAddonStrategy) retrieveValuesTemplateConfigMap(
100-
ctx context.Context,
101-
defaultsNamespace string,
102-
) (*corev1.ConfigMap, error) {
103-
configMap := &corev1.ConfigMap{
104-
ObjectMeta: metav1.ObjectMeta{
105-
Namespace: defaultsNamespace,
106-
Name: s.config.defaultValuesTemplateConfigMapName,
107-
},
108-
}
109-
configMapObjName := ctrlclient.ObjectKeyFromObject(
110-
configMap,
111-
)
112-
err := s.client.Get(ctx, configMapObjName, configMap)
113-
if err != nil {
114-
return nil, fmt.Errorf(
115-
"failed to retrieve installation values template ConfigMap %q: %w",
116-
configMapObjName,
117-
err,
118-
)
119-
}
120-
121-
return configMap, nil
122-
}

pkg/handlers/generic/lifecycle/csi/aws-ebs/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/go-logr/logr"
1011
"github.com/spf13/pflag"
1112
corev1 "k8s.io/api/core/v1"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -60,6 +61,7 @@ func (a *AWSEBS) Apply(
6061
provider v1alpha1.CSIProvider,
6162
defaultStorageConfig *v1alpha1.DefaultStorage,
6263
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
64+
_ logr.Logger,
6365
) error {
6466
strategy := provider.Strategy
6567
switch strategy {

pkg/handlers/generic/lifecycle/csi/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/go-logr/logr"
1011
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1112
ctrl "sigs.k8s.io/controller-runtime"
1213
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -28,6 +29,7 @@ type CSIProvider interface {
2829
v1alpha1.CSIProvider,
2930
*v1alpha1.DefaultStorage,
3031
*runtimehooksv1.AfterControlPlaneInitializedRequest,
32+
logr.Logger,
3133
) error
3234
}
3335

@@ -124,6 +126,7 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
124126
provider,
125127
csiProviders.DefaultStorage,
126128
req,
129+
log,
127130
)
128131
if err != nil {
129132
log.Error(

0 commit comments

Comments
 (0)