@@ -12,14 +12,11 @@ import (
12
12
13
13
"github.com/go-logr/logr"
14
14
"github.com/spf13/pflag"
15
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
15
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
17
16
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
18
- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
19
17
20
- caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
21
18
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
22
- "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/ pkg/k8s/client "
19
+ "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/addons "
23
20
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
24
21
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
25
22
handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
@@ -80,25 +77,11 @@ func (p *provider) Apply(
80
77
return ErrMissingCredentials
81
78
}
82
79
83
- log .Info ("Retrieving Nutanix CCM installation values template for cluster" )
84
- values , err := handlersutils .RetrieveValuesTemplate (
85
- ctx ,
86
- p .client ,
87
- p .config .defaultValuesTemplateConfigMapName ,
88
- p .config .DefaultsNamespace (),
89
- )
90
- if err != nil {
91
- return fmt .Errorf (
92
- "failed to retrieve Nutanix CCM installation values template for cluster: %w" ,
93
- err ,
94
- )
95
- }
96
-
97
80
// It's possible to have the credentials Secret be created by the Helm chart.
98
81
// However, that would leave the credentials visible in the HelmChartProxy.
99
82
// Instead, we'll create the Secret on the remote cluster and reference it in the Helm values.
100
83
if clusterConfig .Addons .CCM .Credentials != nil {
101
- err = handlersutils .EnsureOwnerReferenceForSecret (
84
+ err : = handlersutils .EnsureOwnerReferenceForSecret (
102
85
ctx ,
103
86
p .client ,
104
87
clusterConfig .Addons .CCM .Credentials .SecretRef .Name ,
@@ -134,77 +117,56 @@ func (p *provider) Apply(
134
117
return fmt .Errorf ("failed to get values for nutanix-ccm-config: %w" , err )
135
118
}
136
119
137
- // The configMap will contain the Helm values, but templated with fields that need to be filled in.
138
- values , err = templateValues (clusterConfig , values )
139
- if err != nil {
140
- return fmt .Errorf ("failed to template Helm values read from ConfigMap: %w" , err )
141
- }
142
-
143
- hcp := & caaphv1.HelmChartProxy {
144
- TypeMeta : metav1.TypeMeta {
145
- APIVersion : caaphv1 .GroupVersion .String (),
146
- Kind : "HelmChartProxy" ,
147
- },
148
- ObjectMeta : metav1.ObjectMeta {
149
- Namespace : cluster .Namespace ,
150
- Name : "nutanix-ccm-" + cluster .Name ,
151
- },
152
- Spec : caaphv1.HelmChartProxySpec {
153
- RepoURL : helmChart .Repository ,
154
- ChartName : helmChart .Name ,
155
- ClusterSelector : metav1.LabelSelector {
156
- MatchLabels : map [string ]string {clusterv1 .ClusterNameLabel : cluster .Name },
157
- },
158
- ReleaseNamespace : defaultHelmReleaseNamespace ,
159
- ReleaseName : defaultHelmReleaseName ,
160
- Version : helmChart .Version ,
161
- ValuesTemplate : values ,
162
- },
163
- }
164
- handlersutils .SetTLSConfigForHelmChartProxyIfNeeded (hcp )
165
- if err = controllerutil .SetOwnerReference (cluster , hcp , p .client .Scheme ()); err != nil {
166
- return fmt .Errorf (
167
- "failed to set owner reference on nutanix-ccm installation HelmChartProxy: %w" ,
168
- err ,
169
- )
170
- }
120
+ applier := addons .NewHelmAddonApplier (
121
+ addons .NewHelmAddonConfig (
122
+ p .config .defaultValuesTemplateConfigMapName ,
123
+ defaultHelmReleaseNamespace ,
124
+ defaultHelmReleaseName ,
125
+ ),
126
+ p .client ,
127
+ helmChart ,
128
+ ).WithValueTemplater (templateValuesFunc (clusterConfig ))
171
129
172
- if err = client . ServerSideApply (ctx , p . client , hcp , client . ForceOwnership ); err != nil {
130
+ if err = applier . Apply (ctx , cluster , p . config . DefaultsNamespace (), log ); err != nil {
173
131
return fmt .Errorf ("failed to apply nutanix-ccm installation HelmChartProxy: %w" , err )
174
132
}
175
133
176
134
return nil
177
135
}
178
136
179
- func templateValues (clusterConfig * apivariables.ClusterConfigSpec , text string ) (string , error ) {
180
- helmValuesTemplate , err := template .New ("" ).Parse (text )
181
- if err != nil {
182
- return "" , fmt .Errorf ("failed to parse Helm values template: %w" , err )
183
- }
137
+ func templateValuesFunc (
138
+ clusterConfig * apivariables.ClusterConfigSpec ,
139
+ ) func (* clusterv1.Cluster , string ) (string , error ) {
140
+ return func (_ * clusterv1.Cluster , valuesTemplate string ) (string , error ) {
141
+ helmValuesTemplate , err := template .New ("" ).Parse (valuesTemplate )
142
+ if err != nil {
143
+ return "" , fmt .Errorf ("failed to parse Helm values template: %w" , err )
144
+ }
184
145
185
- type input struct {
186
- PrismCentralHost string
187
- PrismCentralPort int32
188
- PrismCentralInsecure bool
189
- PrismCentralAdditionalTrustBundle string
190
- }
146
+ type input struct {
147
+ PrismCentralHost string
148
+ PrismCentralPort int32
149
+ PrismCentralInsecure bool
150
+ PrismCentralAdditionalTrustBundle string
151
+ }
191
152
192
- address , port , err := clusterConfig .Nutanix .PrismCentralEndpoint .ParseURL ()
193
- if err != nil {
194
- return "" , err
195
- }
196
- templateInput := input {
197
- PrismCentralHost : address ,
198
- PrismCentralPort : port ,
199
- PrismCentralInsecure : clusterConfig .Nutanix .PrismCentralEndpoint .Insecure ,
200
- PrismCentralAdditionalTrustBundle : clusterConfig .Nutanix .PrismCentralEndpoint .AdditionalTrustBundle ,
201
- }
153
+ address , port , err := clusterConfig .Nutanix .PrismCentralEndpoint .ParseURL ()
154
+ if err != nil {
155
+ return "" , err
156
+ }
157
+ templateInput := input {
158
+ PrismCentralHost : address ,
159
+ PrismCentralPort : port ,
160
+ PrismCentralInsecure : clusterConfig .Nutanix .PrismCentralEndpoint .Insecure ,
161
+ PrismCentralAdditionalTrustBundle : clusterConfig .Nutanix .PrismCentralEndpoint .AdditionalTrustBundle ,
162
+ }
202
163
203
- var b bytes.Buffer
204
- err = helmValuesTemplate .Execute (& b , templateInput )
205
- if err != nil {
206
- return "" , fmt .Errorf ("failed setting PrismCentral configuration in template: %w" , err )
207
- }
164
+ var b bytes.Buffer
165
+ err = helmValuesTemplate .Execute (& b , templateInput )
166
+ if err != nil {
167
+ return "" , fmt .Errorf ("failed setting PrismCentral configuration in template: %w" , err )
168
+ }
208
169
209
- return b .String (), nil
170
+ return b .String (), nil
171
+ }
210
172
}
0 commit comments