From 6803afc098750e5b52987aedd69f6786d715b66f Mon Sep 17 00:00:00 2001 From: Hardik Dodiya Date: Mon, 2 Sep 2024 13:45:22 +0000 Subject: [PATCH] Support ServerSelectors for MetalMachines --- api/v1alpha1/common_types.go | 12 --- api/v1alpha1/metalmachine_types.go | 2 +- api/v1alpha1/zz_generated.deepcopy.go | 46 +++++------ ...ucture.cluster.x-k8s.io_metalmachines.yaml | 40 +++++++++- ...luster.x-k8s.io_metalmachinetemplates.yaml | 40 +++++++++- docs/api-reference/api.md | 76 ++++++++++--------- .../controller/metalmachine_controller.go | 16 +--- 7 files changed, 139 insertions(+), 93 deletions(-) delete mode 100644 api/v1alpha1/common_types.go diff --git a/api/v1alpha1/common_types.go b/api/v1alpha1/common_types.go deleted file mode 100644 index d34b8d8..0000000 --- a/api/v1alpha1/common_types.go +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 - -package v1alpha1 - -// ServerSelector specifies matching criteria for labels on Server. -// This is used to claim specific Server types for a Machine -type ServerSelector struct { - // Key/value pairs of labels that must exist on a chosen Server - // +optional - MatchLabels map[string]string `json:"matchLabels,omitempty"` -} diff --git a/api/v1alpha1/metalmachine_types.go b/api/v1alpha1/metalmachine_types.go index 11b180a..e6e2970 100644 --- a/api/v1alpha1/metalmachine_types.go +++ b/api/v1alpha1/metalmachine_types.go @@ -32,7 +32,7 @@ type MetalMachineSpec struct { // ServerSelector specifies matching criteria for labels on Servers. // This is used to claim specific Server types for a MetalMachine. // +optional - ServerSelector ServerSelector `json:"serverSelector,omitempty"` + ServerSelector *metav1.LabelSelector `json:"serverSelector,omitempty"` } // MetalMachineStatus defines the observed state of MetalMachine diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index b868f3e..0787cdf 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -1,13 +1,27 @@ //go:build !ignore_autogenerated -// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 +/* +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. +*/ // Code generated by controller-gen. DO NOT EDIT. package v1alpha1 import ( + "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/errors" @@ -177,7 +191,11 @@ func (in *MetalMachineSpec) DeepCopyInto(out *MetalMachineSpec) { *out = new(string) **out = **in } - in.ServerSelector.DeepCopyInto(&out.ServerSelector) + if in.ServerSelector != nil { + in, out := &in.ServerSelector, &out.ServerSelector + *out = new(v1.LabelSelector) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MetalMachineSpec. @@ -305,25 +323,3 @@ func (in *MetalMachineTemplateSpec) DeepCopy() *MetalMachineTemplateSpec { in.DeepCopyInto(out) return out } - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ServerSelector) DeepCopyInto(out *ServerSelector) { - *out = *in - if in.MatchLabels != nil { - in, out := &in.MatchLabels, &out.MatchLabels - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ServerSelector. -func (in *ServerSelector) DeepCopy() *ServerSelector { - if in == nil { - return nil - } - out := new(ServerSelector) - in.DeepCopyInto(out) - return out -} diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachines.yaml index 5edd0a5..cbbb383 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachines.yaml @@ -51,13 +51,49 @@ spec: ServerSelector specifies matching criteria for labels on Servers. This is used to claim specific Server types for a MetalMachine. properties: + matchExpressions: + description: matchExpressions is a list of label selector requirements. + The requirements are ANDed. + items: + description: |- + A label selector requirement is a selector that contains values, a key, and an operator that + relates the key and values. + properties: + key: + description: key is the label key that the selector applies + to. + type: string + operator: + description: |- + operator represents a key's relationship to a set of values. + Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: |- + values is an array of string values. If the operator is In or NotIn, + the values array must be non-empty. If the operator is Exists or DoesNotExist, + the values array must be empty. This array is replaced during a strategic + merge patch. + items: + type: string + type: array + x-kubernetes-list-type: atomic + required: + - key + - operator + type: object + type: array + x-kubernetes-list-type: atomic matchLabels: additionalProperties: type: string - description: Key/value pairs of labels that must exist on a chosen - Server + description: |- + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, whose key field is "key", the + operator is "In", and the values array contains only "value". The requirements are ANDed. type: object type: object + x-kubernetes-map-type: atomic required: - image type: object diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachinetemplates.yaml index 069bf25..e0427c8 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_metalmachinetemplates.yaml @@ -84,13 +84,49 @@ spec: ServerSelector specifies matching criteria for labels on Servers. This is used to claim specific Server types for a MetalMachine. properties: + matchExpressions: + description: matchExpressions is a list of label selector + requirements. The requirements are ANDed. + items: + description: |- + A label selector requirement is a selector that contains values, a key, and an operator that + relates the key and values. + properties: + key: + description: key is the label key that the selector + applies to. + type: string + operator: + description: |- + operator represents a key's relationship to a set of values. + Valid operators are In, NotIn, Exists and DoesNotExist. + type: string + values: + description: |- + values is an array of string values. If the operator is In or NotIn, + the values array must be non-empty. If the operator is Exists or DoesNotExist, + the values array must be empty. This array is replaced during a strategic + merge patch. + items: + type: string + type: array + x-kubernetes-list-type: atomic + required: + - key + - operator + type: object + type: array + x-kubernetes-list-type: atomic matchLabels: additionalProperties: type: string - description: Key/value pairs of labels that must exist - on a chosen Server + description: |- + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, whose key field is "key", the + operator is "In", and the values array contains only "value". The requirements are ANDed. type: object type: object + x-kubernetes-map-type: atomic required: - image type: object diff --git a/docs/api-reference/api.md b/docs/api-reference/api.md index 9ecac63..ab1210c 100644 --- a/docs/api-reference/api.md +++ b/docs/api-reference/api.md @@ -205,10 +205,21 @@ string +image
+ +string + + + +

Image specifies the boot image to be used for the server.

+ + + + serverSelector
- -ServerSelector + +Kubernetes meta/v1.LabelSelector @@ -265,10 +276,21 @@ string +image
+ +string + + + +

Image specifies the boot image to be used for the server.

+ + + + serverSelector
- -ServerSelector + +Kubernetes meta/v1.LabelSelector @@ -475,10 +497,21 @@ string +image
+ +string + + + +

Image specifies the boot image to be used for the server.

+ + + + serverSelector
- -ServerSelector + +Kubernetes meta/v1.LabelSelector @@ -523,37 +556,6 @@ MetalMachineTemplateResource -

ServerSelector -

-

-(Appears on:MetalMachineSpec) -

-
-

ServerSelector specifies matching criteria for labels on Server. -This is used to claim specific Server types for a Machine

-
- - - - - - - - - - - - - -
FieldDescription
-matchLabels
- -map[string]string - -
-(Optional) -

Key/value pairs of labels that must exist on a chosen Server

-

Generated with gen-crd-api-reference-docs diff --git a/internal/controller/metalmachine_controller.go b/internal/controller/metalmachine_controller.go index ae900e1..452556a 100644 --- a/internal/controller/metalmachine_controller.go +++ b/internal/controller/metalmachine_controller.go @@ -297,23 +297,11 @@ func (r *MetalMachineReconciler) applyServerClaim(ctx context.Context, log *logr IgnitionSecretRef: &corev1.LocalObjectReference{ Name: ignitionsecret.Name, }, - Image: metalmachine.Spec.Image, + Image: metalmachine.Spec.Image, + ServerSelector: metalmachine.Spec.ServerSelector, }, } - // TODO: Define proper contract for ServerSelectors. - serverSelector := metav1.LabelSelector{ - MatchLabels: map[string]string{ - "clusterapi-workload": "", - }, - } - if _, exists := metalmachine.Labels["cluster.x-k8s.io/control-plane"]; exists { - serverSelector.MatchLabels = map[string]string{ - "clusterapi-controlplane": "", - } - } - serverClaimObj.Spec.ServerSelector = &serverSelector - if err := controllerutil.SetControllerReference(metalmachine, serverClaimObj, r.Client.Scheme()); err != nil { return nil, fmt.Errorf("failed to set ControllerReference: %w", err) }