Skip to content

Commit e35c776

Browse files
committed
conversion: CAPI2MAPI set openshift labels for zone, region and instance-type
1 parent 69085f0 commit e35c776

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

pkg/conversion/capi2mapi/aws.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,12 @@ func (m machineAndAWSMachineAndAWSCluster) ToMachine() (*mapiv1beta1.Machine, []
221221

222222
warnings = append(warnings, warn...)
223223

224-
mapiMachine, err := fromCAPIMachineToMAPIMachine(m.machine)
224+
additionalMachineAPILabels := map[string]string{
225+
"machine.openshift.io/instance-type": m.awsMachine.Spec.InstanceType,
226+
"machine.openshift.io/region": m.awsCluster.Spec.Region,
227+
}
228+
229+
mapiMachine, err := fromCAPIMachineToMAPIMachine(m.machine, additionalMachineAPILabels)
225230
if err != nil {
226231
errors = append(errors, err...)
227232
}

pkg/conversion/capi2mapi/common.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ func RawExtensionFromProviderSpec(spec interface{}) (*runtime.RawExtension, erro
4545

4646
func convertCAPIMachineSetSelectorToMAPI(capiSelector metav1.LabelSelector) metav1.LabelSelector {
4747
mapiSelector := capiSelector.DeepCopy()
48-
mapiSelector.MatchLabels = convertCAPILabelsToMAPILabels(capiSelector.MatchLabels)
48+
mapiSelector.MatchLabels = convertCAPILabelsToMAPILabels(capiSelector.MatchLabels, nil)
4949

5050
return *mapiSelector
5151
}
5252

53-
func convertCAPILabelsToMAPILabels(capiLabels map[string]string) map[string]string {
54-
if len(capiLabels) == 0 {
53+
func convertCAPILabelsToMAPILabels(capiLabels map[string]string, machineAPILabels map[string]string) map[string]string {
54+
if len(capiLabels) == 0 && len(machineAPILabels) == 0 {
5555
return nil
5656
}
5757

@@ -77,6 +77,15 @@ func convertCAPILabelsToMAPILabels(capiLabels map[string]string) map[string]stri
7777
mapiLabels[k] = v
7878
}
7979

80+
for k, v := range machineAPILabels {
81+
// Ignore empty labels to ensure to not overwrite potentially existing labels with empty values.
82+
if v == "" {
83+
continue
84+
}
85+
86+
mapiLabels[k] = v
87+
}
88+
8089
// On the original MAPI object some label fields are nil rather than empty.
8190
// So return nil instead to avoid unnecessary diff being picked up by the diff checker.
8291
if len(mapiLabels) == 0 {
@@ -155,5 +164,8 @@ func convertCAPIAnnotationsToMAPIAnnotations(capiAnnotations map[string]string)
155164
mapiAnnotations[k] = v
156165
}
157166

167+
// TODO
168+
// - machine.openshift.io/instance-state
169+
158170
return mapiAnnotations
159171
}

pkg/conversion/capi2mapi/machine.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const (
3434
)
3535

3636
// fromCAPIMachineToMAPIMachine translates a core CAPI Machine to its MAPI Machine correspondent.
37-
func fromCAPIMachineToMAPIMachine(capiMachine *clusterv1.Machine) (*mapiv1beta1.Machine, field.ErrorList) {
37+
func fromCAPIMachineToMAPIMachine(capiMachine *clusterv1.Machine, additionalMachineAPILabels map[string]string) (*mapiv1beta1.Machine, field.ErrorList) {
3838
errs := field.ErrorList{}
3939

4040
lifecycleHooks, capiMachineNonHookAnnotations := convertCAPILifecycleHookAnnotationsToMAPILifecycleHooksAndAnnotations(capiMachine.Annotations)
@@ -44,11 +44,13 @@ func fromCAPIMachineToMAPIMachine(capiMachine *clusterv1.Machine) (*mapiv1beta1.
4444
errs = append(errs, machineStatusErrs...)
4545
}
4646

47+
additionalMachineAPILabels["machine.openshift.io/zone"] = ptr.Deref(capiMachine.Spec.FailureDomain, "")
48+
4749
mapiMachine := &mapiv1beta1.Machine{
4850
ObjectMeta: metav1.ObjectMeta{
4951
Name: capiMachine.Name,
5052
Namespace: mapiNamespace,
51-
Labels: convertCAPILabelsToMAPILabels(capiMachine.Labels),
53+
Labels: convertCAPILabelsToMAPILabels(capiMachine.Labels, additionalMachineAPILabels),
5254
Annotations: convertCAPIAnnotationsToMAPIAnnotations(capiMachineNonHookAnnotations),
5355
Finalizers: []string{mapiv1beta1.MachineFinalizer},
5456
OwnerReferences: nil, // OwnerReferences not populated here. They are added later by the machineSync controller.

pkg/conversion/capi2mapi/machineset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func fromCAPIMachineSetToMAPIMachineSet(capiMachineSet *clusterv1.MachineSet) (*
3232
ObjectMeta: metav1.ObjectMeta{
3333
Name: capiMachineSet.Name,
3434
Namespace: capiMachineSet.Namespace,
35-
Labels: convertCAPILabelsToMAPILabels(capiMachineSet.Labels),
35+
Labels: convertCAPILabelsToMAPILabels(capiMachineSet.Labels, nil),
3636
Annotations: convertCAPIAnnotationsToMAPIAnnotations(capiMachineSet.Annotations),
3737
Finalizers: nil, // MAPI MachineSet does not have finalizers.
3838
OwnerReferences: nil, // OwnerReferences not populated here. They are added later by the machineSetSync controller.
@@ -44,7 +44,7 @@ func fromCAPIMachineSetToMAPIMachineSet(capiMachineSet *clusterv1.MachineSet) (*
4444
DeletePolicy: capiMachineSet.Spec.DeletePolicy,
4545
Template: mapiv1beta1.MachineTemplateSpec{
4646
ObjectMeta: mapiv1beta1.ObjectMeta{
47-
Labels: convertCAPILabelsToMAPILabels(capiMachineSet.Spec.Template.Labels),
47+
Labels: convertCAPILabelsToMAPILabels(capiMachineSet.Spec.Template.Labels, nil),
4848
Annotations: convertCAPIAnnotationsToMAPIAnnotations(capiMachineSet.Spec.Template.Annotations),
4949
},
5050
},

pkg/conversion/capi2mapi/openstack.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/runtime"
2929
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3030
"k8s.io/apimachinery/pkg/util/validation/field"
31+
"k8s.io/utils/ptr"
3132
openstackv1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
3233
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3334
)
@@ -201,7 +202,14 @@ func (m machineAndOpenStackMachineAndOpenStackCluster) ToMachine() (*mapiv1beta1
201202

202203
warnings = append(warnings, warns...)
203204

204-
mapiMachine, errs := fromCAPIMachineToMAPIMachine(m.machine)
205+
additionalMachineAPILabels := map[string]string{
206+
// Unable to determine the region without fetching the identity resources as done at:
207+
// https://github.com/openshift/machine-api-provider-openstack/blob/2defb131bd0836beba0a9790de7c9a63137a5cec/pkg/machine/actuator.go#L85-L89
208+
// "machine.openshift.io/region":
209+
"machine.openshift.io/instance-type": ptr.Deref(m.openstackMachine.Spec.Flavor, ""),
210+
}
211+
212+
mapiMachine, errs := fromCAPIMachineToMAPIMachine(m.machine, additionalMachineAPILabels)
205213
if errs != nil {
206214
errors = append(errors, errs...)
207215
}

pkg/conversion/capi2mapi/powervs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ func (m machineAndPowerVSMachineAndPowerVSCluster) ToMachine() (*mapiv1beta1.Mac
100100
return nil, nil, fmt.Errorf("unable to convert PowerVS providerSpec to raw extension: %w", errRaw)
101101
}
102102

103-
mapiMachine, err := fromCAPIMachineToMAPIMachine(m.machine)
103+
additionalMachineAPILabels := map[string]string{
104+
"machine.openshift.io/instance-type": m.powerVSMachine.Spec.SystemType,
105+
"machine.openshift.io/region": ptr.Deref(m.powerVSMachine.Status.Region, ""),
106+
}
107+
108+
mapiMachine, err := fromCAPIMachineToMAPIMachine(m.machine, additionalMachineAPILabels)
104109
if err != nil {
105110
errors = append(errors, err...)
106111
}

0 commit comments

Comments
 (0)