Skip to content

Commit fde18b8

Browse files
authored
refactor: Reusable HelmAddon strategy (#735)
This commit refactors some of the existing HelmAddon code used in various handlers to a common applier, thus reducing code maintenance. There are a number of other places where this could be applied, but they currently have specific behaviour that is harder to generalise and as such I have not refactored those handlers yet. Depends on #734.
1 parent 69f4b17 commit fde18b8

File tree

26 files changed

+311
-643
lines changed

26 files changed

+311
-643
lines changed

charts/cluster-api-runtime-extensions-nutanix/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ A Helm chart for cluster-api-runtime-extensions-nutanix
6363
| hooks.csi.aws-ebs.helmAddonStrategy.defaultValueTemplateConfigMap.create | bool | `true` | |
6464
| hooks.csi.aws-ebs.helmAddonStrategy.defaultValueTemplateConfigMap.name | string | `"default-aws-ebs-csi-helm-values-template"` | |
6565
| hooks.csi.local-path.helmAddonStrategy.defaultValueTemplateConfigMap.create | bool | `true` | |
66-
| hooks.csi.local-path.helmAddonStrategy.defaultValueTemplateConfigMap.name | string | `"default-local-path-csi-helm-values-template"` | |
66+
| hooks.csi.local-path.helmAddonStrategy.defaultValueTemplateConfigMap.name | string | `"default-local-path-provisioner-csi-helm-values-template"` | |
6767
| hooks.csi.nutanix.helmAddonStrategy.defaultValueTemplateConfigMap.create | bool | `true` | |
6868
| hooks.csi.nutanix.helmAddonStrategy.defaultValueTemplateConfigMap.name | string | `"default-nutanix-csi-helm-values-template"` | |
6969
| hooks.csi.snapshot-controller.helmAddonStrategy.defaultValueTemplateConfigMap.create | bool | `true` | |

charts/cluster-api-runtime-extensions-nutanix/templates/csi/local-path/manifests/local-path-provisioner-csi-configmap.yaml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ data:
1313
kind: ServiceAccount
1414
metadata:
1515
labels:
16-
app.kubernetes.io/instance: local-path-provisioner
16+
app.kubernetes.io/instance: local-path-provisioner-csi
1717
app.kubernetes.io/managed-by: Helm
1818
app.kubernetes.io/name: local-path-provisioner
1919
app.kubernetes.io/version: v0.0.27
2020
helm.sh/chart: local-path-provisioner-0.0.29
21-
name: local-path-provisioner
21+
name: local-path-provisioner-csi
2222
namespace: kube-system
2323
---
2424
apiVersion: rbac.authorization.k8s.io/v1
2525
kind: Role
2626
metadata:
2727
labels:
28-
app.kubernetes.io/instance: local-path-provisioner
28+
app.kubernetes.io/instance: local-path-provisioner-csi
2929
app.kubernetes.io/managed-by: Helm
3030
app.kubernetes.io/name: local-path-provisioner
3131
app.kubernetes.io/version: v0.0.27
3232
helm.sh/chart: local-path-provisioner-0.0.29
33-
name: local-path-provisioner
33+
name: local-path-provisioner-csi
3434
namespace: kube-system
3535
rules:
3636
- apiGroups:
@@ -50,12 +50,12 @@ data:
5050
kind: ClusterRole
5151
metadata:
5252
labels:
53-
app.kubernetes.io/instance: local-path-provisioner
53+
app.kubernetes.io/instance: local-path-provisioner-csi
5454
app.kubernetes.io/managed-by: Helm
5555
app.kubernetes.io/name: local-path-provisioner
5656
app.kubernetes.io/version: v0.0.27
5757
helm.sh/chart: local-path-provisioner-0.0.29
58-
name: local-path-provisioner
58+
name: local-path-provisioner-csi
5959
rules:
6060
- apiGroups:
6161
- ""
@@ -101,39 +101,39 @@ data:
101101
kind: RoleBinding
102102
metadata:
103103
labels:
104-
app.kubernetes.io/instance: local-path-provisioner
104+
app.kubernetes.io/instance: local-path-provisioner-csi
105105
app.kubernetes.io/managed-by: Helm
106106
app.kubernetes.io/name: local-path-provisioner
107107
app.kubernetes.io/version: v0.0.27
108108
helm.sh/chart: local-path-provisioner-0.0.29
109-
name: local-path-provisioner
109+
name: local-path-provisioner-csi
110110
namespace: kube-system
111111
roleRef:
112112
apiGroup: rbac.authorization.k8s.io
113113
kind: Role
114-
name: local-path-provisioner
114+
name: local-path-provisioner-csi
115115
subjects:
116116
- kind: ServiceAccount
117-
name: local-path-provisioner
117+
name: local-path-provisioner-csi
118118
namespace: kube-system
119119
---
120120
apiVersion: rbac.authorization.k8s.io/v1
121121
kind: ClusterRoleBinding
122122
metadata:
123123
labels:
124-
app.kubernetes.io/instance: local-path-provisioner
124+
app.kubernetes.io/instance: local-path-provisioner-csi
125125
app.kubernetes.io/managed-by: Helm
126126
app.kubernetes.io/name: local-path-provisioner
127127
app.kubernetes.io/version: v0.0.27
128128
helm.sh/chart: local-path-provisioner-0.0.29
129-
name: local-path-provisioner
129+
name: local-path-provisioner-csi
130130
roleRef:
131131
apiGroup: rbac.authorization.k8s.io
132132
kind: ClusterRole
133-
name: local-path-provisioner
133+
name: local-path-provisioner-csi
134134
subjects:
135135
- kind: ServiceAccount
136-
name: local-path-provisioner
136+
name: local-path-provisioner-csi
137137
namespace: kube-system
138138
---
139139
apiVersion: v1
@@ -178,7 +178,7 @@ data:
178178
kind: ConfigMap
179179
metadata:
180180
labels:
181-
app.kubernetes.io/instance: local-path-provisioner
181+
app.kubernetes.io/instance: local-path-provisioner-csi
182182
app.kubernetes.io/managed-by: Helm
183183
app.kubernetes.io/name: local-path-provisioner
184184
app.kubernetes.io/version: v0.0.27
@@ -190,23 +190,23 @@ data:
190190
kind: Deployment
191191
metadata:
192192
labels:
193-
app.kubernetes.io/instance: local-path-provisioner
193+
app.kubernetes.io/instance: local-path-provisioner-csi
194194
app.kubernetes.io/managed-by: Helm
195195
app.kubernetes.io/name: local-path-provisioner
196196
app.kubernetes.io/version: v0.0.27
197197
helm.sh/chart: local-path-provisioner-0.0.29
198-
name: local-path-provisioner
198+
name: local-path-provisioner-csi
199199
namespace: kube-system
200200
spec:
201201
replicas: 1
202202
selector:
203203
matchLabels:
204-
app.kubernetes.io/instance: local-path-provisioner
204+
app.kubernetes.io/instance: local-path-provisioner-csi
205205
app.kubernetes.io/name: local-path-provisioner
206206
template:
207207
metadata:
208208
labels:
209-
app.kubernetes.io/instance: local-path-provisioner
209+
app.kubernetes.io/instance: local-path-provisioner-csi
210210
app.kubernetes.io/managed-by: Helm
211211
app.kubernetes.io/name: local-path-provisioner
212212
app.kubernetes.io/version: v0.0.27
@@ -220,7 +220,7 @@ data:
220220
- --config
221221
- /etc/config/config.json
222222
- --service-account-name
223-
- local-path-provisioner
223+
- local-path-provisioner-csi
224224
- --provisioner-name
225225
- rancher.io/local-path
226226
- --helper-image
@@ -241,7 +241,7 @@ data:
241241
- mountPath: /etc/config/
242242
name: config-volume
243243
securityContext: {}
244-
serviceAccountName: local-path-provisioner
244+
serviceAccountName: local-path-provisioner-csi
245245
tolerations:
246246
- key: CriticalAddonsOnly
247247
operator: Exists

charts/cluster-api-runtime-extensions-nutanix/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ hooks:
5858
helmAddonStrategy:
5959
defaultValueTemplateConfigMap:
6060
create: true
61-
name: default-local-path-csi-helm-values-template
61+
name: default-local-path-provisioner-csi-helm-values-template
6262
snapshot-controller:
6363
helmAddonStrategy:
6464
defaultValueTemplateConfigMap:

hack/addons/kustomize/local-path-provisioner-csi/kustomization.yaml.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace: kube-system
1212
helmCharts:
1313
- name: local-path-provisioner
1414
repo: https://charts.containeroo.ch
15-
releaseName: local-path-provisioner
15+
releaseName: local-path-provisioner-csi
1616
version: ${LOCAL_PATH_CSI_CHART_VERSION}
1717
valuesFile: helm-values.yaml
1818
includeCRDs: true

hack/addons/kustomize/nutanix-storage-csi/kustomization.yaml.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace: kube-system
1212
helmCharts:
1313
- name: nutanix-csi-storage
1414
repo: https://nutanix.github.io/helm-releases/
15-
releaseName: nutanix-csi-storage
15+
releaseName: nutanix-csi
1616
version: ${NUTANIX_STORAGE_CSI_CHART_VERSION}
1717
includeCRDs: true
1818
skipTests: true

hack/addons/kustomize/snapshot-controller/helm-values.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Copyright 2024 Nutanix. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
# Copyright 2024 D2iQ, Inc. All rights reserved.
5-
# SPDX-License-Identifier: Apache-2.0
6-
74
controller:
85
priorityClassName: system-cluster-critical
96
tolerations:
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package addons
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/go-logr/logr"
11+
"github.com/spf13/pflag"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
16+
17+
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
18+
k8sclient "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
19+
lifecycleconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
20+
handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
21+
)
22+
23+
type HelmAddonConfig struct {
24+
defaultValuesTemplateConfigMapName string
25+
26+
defaultHelmReleaseNamespace string
27+
defaultHelmReleaseName string
28+
}
29+
30+
func NewHelmAddonConfig(
31+
defaultValuesTemplateConfigMapName string,
32+
defaultHelmReleaseNamespace string,
33+
defaultHelmReleaseName string,
34+
) *HelmAddonConfig {
35+
return &HelmAddonConfig{
36+
defaultValuesTemplateConfigMapName: defaultValuesTemplateConfigMapName,
37+
defaultHelmReleaseNamespace: defaultHelmReleaseNamespace,
38+
defaultHelmReleaseName: defaultHelmReleaseName,
39+
}
40+
}
41+
42+
func (c *HelmAddonConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
43+
flags.StringVar(
44+
&c.defaultValuesTemplateConfigMapName,
45+
prefix+".default-values-template-configmap-name",
46+
c.defaultValuesTemplateConfigMapName,
47+
"default values ConfigMap name",
48+
)
49+
}
50+
51+
type helmAddonApplier struct {
52+
config *HelmAddonConfig
53+
client ctrlclient.Client
54+
helmChart *lifecycleconfig.HelmChart
55+
}
56+
57+
var _ Applier = &helmAddonApplier{}
58+
59+
func NewHelmAddonApplier(
60+
config *HelmAddonConfig,
61+
client ctrlclient.Client,
62+
helmChart *lifecycleconfig.HelmChart,
63+
) *helmAddonApplier {
64+
return &helmAddonApplier{
65+
config: config,
66+
client: client,
67+
helmChart: helmChart,
68+
}
69+
}
70+
71+
func (a *helmAddonApplier) Apply(
72+
ctx context.Context,
73+
cluster *clusterv1.Cluster,
74+
defaultsNamespace string,
75+
log logr.Logger,
76+
) error {
77+
log.Info("Retrieving installation values template for cluster")
78+
values, err := handlersutils.RetrieveValuesTemplate(
79+
ctx,
80+
a.client,
81+
a.config.defaultValuesTemplateConfigMapName,
82+
defaultsNamespace,
83+
)
84+
if err != nil {
85+
return fmt.Errorf(
86+
"failed to retrieve installation values template for cluster: %w",
87+
err,
88+
)
89+
}
90+
91+
chartProxy := &caaphv1.HelmChartProxy{
92+
TypeMeta: metav1.TypeMeta{
93+
APIVersion: caaphv1.GroupVersion.String(),
94+
Kind: "HelmChartProxy",
95+
},
96+
ObjectMeta: metav1.ObjectMeta{
97+
Namespace: cluster.Namespace,
98+
Name: a.config.defaultHelmReleaseName + "-" + cluster.Name,
99+
},
100+
Spec: caaphv1.HelmChartProxySpec{
101+
RepoURL: a.helmChart.Repository,
102+
ChartName: a.helmChart.Name,
103+
ClusterSelector: metav1.LabelSelector{
104+
MatchLabels: map[string]string{clusterv1.ClusterNameLabel: cluster.Name},
105+
},
106+
ReleaseNamespace: a.config.defaultHelmReleaseNamespace,
107+
ReleaseName: a.config.defaultHelmReleaseName,
108+
Version: a.helmChart.Version,
109+
ValuesTemplate: values,
110+
},
111+
}
112+
handlersutils.SetTLSConfigForHelmChartProxyIfNeeded(chartProxy)
113+
if err = controllerutil.SetOwnerReference(cluster, chartProxy, a.client.Scheme()); err != nil {
114+
return fmt.Errorf(
115+
"failed to set owner reference on HelmChartProxy %q: %w",
116+
chartProxy.Name,
117+
err,
118+
)
119+
}
120+
121+
if err = k8sclient.ServerSideApply(ctx, a.client, chartProxy, k8sclient.ForceOwnership); err != nil {
122+
return fmt.Errorf("failed to apply HelmChartProxy %q: %w", chartProxy.Name, err)
123+
}
124+
125+
return nil
126+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package addons
5+
6+
import (
7+
"context"
8+
9+
"github.com/go-logr/logr"
10+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
11+
)
12+
13+
type Applier interface {
14+
Apply(
15+
ctx context.Context,
16+
cluster *clusterv1.Cluster,
17+
defaultsNamespace string,
18+
log logr.Logger,
19+
) error
20+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (c *CalicoCNI) apply(
161161
)
162162
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
163163
resp.SetMessage(
164-
fmt.Sprintf("failed to get configration to create helm addon: %v",
164+
fmt.Sprintf("failed to get configuration to create helm addon: %v",
165165
err,
166166
),
167167
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (c *CiliumCNI) apply(
158158
)
159159
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
160160
resp.SetMessage(
161-
fmt.Sprintf("failed to get configration to create helm addon: %v",
161+
fmt.Sprintf("failed to get configuration to create helm addon: %v",
162162
err,
163163
),
164164
)

0 commit comments

Comments
 (0)