-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterdeployment_controller.go
377 lines (318 loc) · 14 KB
/
clusterdeployment_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
"encoding/json"
"fmt"
"regexp"
"slices"
"strings"
controlplanev1alpha2 "github.com/openshift-assisted/cluster-api-agent/controlplane/api/v1alpha2"
"github.com/openshift-assisted/cluster-api-agent/controlplane/internal/imageregistry"
"github.com/openshift-assisted/cluster-api-agent/controlplane/internal/release"
"github.com/openshift-assisted/cluster-api-agent/util"
logutil "github.com/openshift-assisted/cluster-api-agent/util/log"
configv1 "github.com/openshift/api/config/v1"
hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1"
hivev1 "github.com/openshift/hive/apis/hive/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capiutil "sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
)
const (
InstallConfigOverrides = aiv1beta1.Group + "/install-config-overrides"
defaultBaremetalBaselineCapability = "None"
defaultBaselineCapability = "vCurrent"
)
var (
defaultBaremetalAdditionalCapabilities = []configv1.ClusterVersionCapability{"baremetal", "Console", "Insights", "OperatorLifecycleManager", "Ingress"}
)
// ClusterDeploymentReconciler reconciles a ClusterDeployment object
type ClusterDeploymentReconciler struct {
client.Client
Scheme *runtime.Scheme
}
// SetupWithManager sets up the controller with the Manager.
func (r *ClusterDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&hivev1.ClusterDeployment{}).
Watches(&controlplanev1alpha2.OpenshiftAssistedControlPlane{}, &handler.EnqueueRequestForObject{}).
Watches(&clusterv1.MachineDeployment{}, &handler.EnqueueRequestForObject{}).
Complete(r)
}
func (r *ClusterDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
clusterDeployment := &hivev1.ClusterDeployment{}
if err := r.Client.Get(ctx, req.NamespacedName, clusterDeployment); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log.WithValues("cluster_deployment", clusterDeployment.Name, "cluster_deployment_namespace", clusterDeployment.Namespace)
log.V(logutil.TraceLevel).Info("Reconciling ClusterDeployment")
acp := controlplanev1alpha2.OpenshiftAssistedControlPlane{}
if err := util.GetTypedOwner(ctx, r.Client, clusterDeployment, &acp); err != nil {
log.V(logutil.TraceLevel).Info("Cluster deployment is not owned by OpenshiftAssistedControlPlane")
return ctrl.Result{}, nil
}
log.WithValues("openshiftassisted_control_plane", acp.Name, "openshiftassisted_control_plane_namespace", acp.Namespace)
arch, err := getArchitectureFromBootstrapConfigs(ctx, r.Client, &acp)
if err != nil {
return ctrl.Result{}, err
}
if err = ensureClusterImageSet(ctx, r.Client, clusterDeployment.Name, getReleaseImage(acp, arch)); err != nil {
log.Error(err, "failed creating ClusterImageSet")
return ctrl.Result{}, err
}
if acp.Spec.Config.ImageRegistryRef != nil {
if err := r.createImageRegistry(ctx, acp.Spec.Config.ImageRegistryRef.Name, acp.Namespace); err != nil {
log.Error(err, "failed to create image registry config manifest")
return ctrl.Result{}, err
}
}
if err := r.ensureAgentClusterInstall(ctx, clusterDeployment, acp); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, r.updateClusterDeploymentRef(ctx, clusterDeployment)
}
func (r *ClusterDeploymentReconciler) ensureAgentClusterInstall(
ctx context.Context,
clusterDeployment *hivev1.ClusterDeployment,
oacp controlplanev1alpha2.OpenshiftAssistedControlPlane,
) error {
log := ctrl.LoggerFrom(ctx)
cluster, err := capiutil.GetOwnerCluster(ctx, r.Client, oacp.ObjectMeta)
if err != nil {
log.Error(err, "failed to retrieve owner Cluster from the API Server")
return err
}
workerNodes := r.getWorkerNodesCount(ctx, cluster)
clusterNetwork, serviceNetwork := getClusterNetworks(cluster)
additionalManifests := getClusterAdditionalManifestRefs(oacp)
aci := &hiveext.AgentClusterInstall{
ObjectMeta: metav1.ObjectMeta{
Name: clusterDeployment.Name,
Namespace: clusterDeployment.Namespace,
},
}
mutate := func() error {
aci.ObjectMeta.Labels = util.ControlPlaneMachineLabelsForCluster(
&oacp,
clusterDeployment.Labels[clusterv1.ClusterNameLabel],
)
aci.ObjectMeta.Labels[hiveext.ClusterConsumerLabel] = openshiftAssistedControlPlaneKind
aci.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
*metav1.NewControllerRef(&oacp, controlplanev1alpha2.GroupVersion.WithKind(openshiftAssistedControlPlaneKind)),
}
aci.Spec.ClusterDeploymentRef = corev1.LocalObjectReference{Name: clusterDeployment.Name}
aci.Spec.PlatformType = hiveext.PlatformType(configv1.NonePlatformType)
aci.Spec.ProvisionRequirements = hiveext.ProvisionRequirements{
ControlPlaneAgents: int(oacp.Spec.Replicas),
WorkerAgents: workerNodes,
}
aci.Spec.DiskEncryption = oacp.Spec.Config.DiskEncryption
aci.Spec.MastersSchedulable = oacp.Spec.Config.MastersSchedulable
aci.Spec.Proxy = oacp.Spec.Config.Proxy
aci.Spec.SSHPublicKey = oacp.Spec.Config.SSHAuthorizedKey
aci.Spec.ImageSetRef = &hivev1.ClusterImageSetReference{Name: clusterDeployment.Name}
aci.Spec.Networking.ClusterNetwork = clusterNetwork
aci.Spec.Networking.ServiceNetwork = serviceNetwork
aci.Spec.ManifestsConfigMapRefs = additionalManifests
if len(oacp.Spec.Config.APIVIPs) > 0 && len(oacp.Spec.Config.IngressVIPs) > 0 {
aci.Spec.APIVIPs = oacp.Spec.Config.APIVIPs
aci.Spec.IngressVIPs = oacp.Spec.Config.IngressVIPs
aci.Spec.PlatformType = hiveext.PlatformType(configv1.BareMetalPlatformType)
}
if err := setACICapabilities(&oacp, aci); err != nil {
return err
}
return nil
}
if _, err = controllerutil.CreateOrPatch(ctx, r.Client, aci, mutate); err != nil {
log.Error(err, "failed to create or update AgentClusterInstall")
return err
}
return nil
}
// Returns release image from OpenshiftAssistedControlPlane. It will compute it starting from Spec.DistributionVersion and
// possibly cluster.x-k8s.io/release-image-repository-override annotation.
// Expected patterns:
// quay.io/openshift-release-dev/ocp-release:4.17.0-rc.2-x86_64
// quay.io/okd/scos-release:4.18.0-okd-scos.ec.1
// Can be overridden with annotation: cluster.x-k8s.io/release-image-repository-override=quay.io/myorg/myrepo
func getReleaseImage(oacp controlplanev1alpha2.OpenshiftAssistedControlPlane, architecture string) string {
releaseImageRepository, ok := oacp.Annotations[release.ReleaseImageRepositoryOverrideAnnotation]
if !ok {
releaseImageRepository = ""
}
return release.GetReleaseImage(oacp.Spec.DistributionVersion, releaseImageRepository, architecture)
}
func (r *ClusterDeploymentReconciler) getWorkerNodesCount(ctx context.Context, cluster *clusterv1.Cluster) int {
log := ctrl.LoggerFrom(ctx)
count := 0
mdList := clusterv1.MachineDeploymentList{}
if err := r.Client.List(ctx, &mdList, client.MatchingLabels{clusterv1.ClusterNameLabel: cluster.Name}); err != nil {
log.Error(err, "failed to list MachineDeployments", "cluster", cluster.Name)
return count
}
for _, md := range mdList.Items {
count += int(*md.Spec.Replicas)
}
return count
}
func (r *ClusterDeploymentReconciler) updateClusterDeploymentRef(
ctx context.Context,
cd *hivev1.ClusterDeployment,
) error {
cd.Spec.ClusterInstallRef = &hivev1.ClusterInstallLocalReference{
Group: hiveext.Group,
Version: hiveext.Version,
Kind: "AgentClusterInstall",
Name: cd.Name,
}
return r.Client.Update(ctx, cd)
}
func ensureClusterImageSet(ctx context.Context, c client.Client, imageSetName string, releaseImage string) error {
imageSet := &hivev1.ClusterImageSet{
ObjectMeta: metav1.ObjectMeta{
Name: imageSetName,
},
}
_, err := controllerutil.CreateOrPatch(ctx, c, imageSet, func() error {
imageSet.Spec.ReleaseImage = releaseImage
return nil
})
return err
}
func getClusterNetworks(cluster *clusterv1.Cluster) ([]hiveext.ClusterNetworkEntry, []string) {
var clusterNetwork []hiveext.ClusterNetworkEntry
if cluster.Spec.ClusterNetwork != nil && cluster.Spec.ClusterNetwork.Pods != nil {
for _, cidrBlock := range cluster.Spec.ClusterNetwork.Pods.CIDRBlocks {
clusterNetwork = append(clusterNetwork, hiveext.ClusterNetworkEntry{CIDR: cidrBlock, HostPrefix: 23})
}
}
var serviceNetwork []string
if cluster.Spec.ClusterNetwork != nil && cluster.Spec.ClusterNetwork.Services != nil {
serviceNetwork = cluster.Spec.ClusterNetwork.Services.CIDRBlocks
}
return clusterNetwork, serviceNetwork
}
func getClusterAdditionalManifestRefs(acp controlplanev1alpha2.OpenshiftAssistedControlPlane) []hiveext.ManifestsConfigMapReference {
var additionalManifests []hiveext.ManifestsConfigMapReference
if len(acp.Spec.Config.ManifestsConfigMapRefs) > 0 {
additionalManifests = append(additionalManifests, acp.Spec.Config.ManifestsConfigMapRefs...)
}
if acp.Spec.Config.ImageRegistryRef != nil {
additionalManifests = append(additionalManifests, hiveext.ManifestsConfigMapReference{Name: imageregistry.ImageConfigMapName})
}
return additionalManifests
}
func (r *ClusterDeploymentReconciler) createImageRegistry(ctx context.Context, registryName, registryNamespace string) error {
registryConfigmap := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, client.ObjectKey{Name: registryName, Namespace: registryNamespace}, registryConfigmap); err != nil {
return err
}
spokeImageRegistryData, err := imageregistry.GenerateImageRegistryData(registryConfigmap, registryNamespace)
if err != nil {
return err
}
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: imageregistry.ImageConfigMapName,
Namespace: registryNamespace,
},
}
_, err = controllerutil.CreateOrPatch(ctx, r.Client, cm, func() error {
cm.Data = spokeImageRegistryData
return nil
})
return err
}
type InstallConfigOverride struct {
Capability configv1.ClusterVersionCapabilitiesSpec `json:"capabilities,omitempty"`
}
// setACICapabilities will set the install config override annotation in the AgentClusterInstall if there
// are additional enabled capabilities that need to be defined.
// For SNO (single node openshift) and non-baremetal platform MNO (multi-node openshift), this is set if
// the OpenshiftAssistedControlPlane has additional capabilities set.
// For MNO (multi-node openshift), this is set to the default list for baremetal platform,
// but the OpenshiftAssistedControlPlane can specify additional capabilities to be appended to this list.
func setACICapabilities(oacp *controlplanev1alpha2.OpenshiftAssistedControlPlane, aci *hiveext.AgentClusterInstall) error {
isBaremetalPlatform := aci.Spec.PlatformType == hiveext.BareMetalPlatformType
if isCapabilitiesEmpty(oacp.Spec.Config.Capabilities) && !isBaremetalPlatform {
return nil
}
var installCfgOverride InstallConfigOverride
baselineCapability, err := getBaselineCapability(oacp.Spec.Config.Capabilities.BaselineCapability, isBaremetalPlatform)
if err != nil {
return err
}
installCfgOverride.Capability.BaselineCapabilitySet = configv1.ClusterVersionCapabilitySet(baselineCapability)
additionalEnabledCapabilities := getAdditionalCapabilities(oacp.Spec.Config.Capabilities.AdditionalEnabledCapabilities, isBaremetalPlatform)
installCfgOverride.Capability.AdditionalEnabledCapabilities = additionalEnabledCapabilities
installCfgOverrideStr, err := json.Marshal(installCfgOverride)
if err != nil {
return err
}
if aci.Annotations == nil {
aci.Annotations = make(map[string]string)
}
aci.Annotations[InstallConfigOverrides] = string(installCfgOverrideStr)
return nil
}
func getBaselineCapability(capability string, isBaremetalPlatform bool) (string, error) {
baselineCapability := capability
if baselineCapability == "None" || baselineCapability == "vCurrent" {
return baselineCapability, nil
}
if baselineCapability == "" {
baselineCapability = defaultBaselineCapability
if isBaremetalPlatform {
baselineCapability = defaultBaremetalBaselineCapability
}
return baselineCapability, nil
}
var baselineCapabilityRegexp = regexp.MustCompile(`v4\.[0-9]+`)
if !baselineCapabilityRegexp.MatchString(baselineCapability) {
return "",
fmt.Errorf("invalid baseline capability set, must be one of: None, vCurrent, or v4.x. Got: [%s]", baselineCapability)
}
return baselineCapability, nil
}
func getAdditionalCapabilities(specifiedAdditionalCapabilities []string, isBaremetalPlatform bool) []configv1.ClusterVersionCapability {
additionalCapabilitiesList := []configv1.ClusterVersionCapability{}
if isBaremetalPlatform {
additionalCapabilitiesList = append([]configv1.ClusterVersionCapability{}, defaultBaremetalAdditionalCapabilities...)
}
for _, capability := range specifiedAdditionalCapabilities {
// Ignore MAPI for baremetal MNO clusters and ignore duplicates
if (strings.EqualFold(capability, "MachineAPI") && isBaremetalPlatform) || slices.Contains(additionalCapabilitiesList, configv1.ClusterVersionCapability(capability)) {
continue
}
additionalCapabilitiesList = append(additionalCapabilitiesList, configv1.ClusterVersionCapability(capability))
}
if len(additionalCapabilitiesList) < 1 {
return nil
}
return additionalCapabilitiesList
}
func isCapabilitiesEmpty(capabilities controlplanev1alpha2.Capabilities) bool {
return equality.Semantic.DeepEqual(capabilities, controlplanev1alpha2.Capabilities{})
}