@@ -104,39 +104,11 @@ func (r *MachineDeploymentReconciler) Reconcile(req ctrl.Request) (ctrl.Result,
104
104
105
105
func (r * MachineDeploymentReconciler ) reconcile (ctx context.Context , d * clusterv1.MachineDeployment ) (ctrl.Result , error ) {
106
106
logger := r .Log .WithValues ("machinedeployment" , d .Name , "namespace" , d .Namespace )
107
+ logger .V (4 ).Info ("Reconcile MachineDeployment" )
107
108
109
+ // Reconcile defaults.
108
110
clusterv1 .PopulateDefaultsMachineDeployment (d )
109
111
110
- // Test for an empty LabelSelector and short circuit if that is the case
111
- // TODO: When we have validation webhooks, we should likely reject on an empty LabelSelector
112
- everything := metav1.LabelSelector {}
113
- if reflect .DeepEqual (d .Spec .Selector , & everything ) {
114
- if d .Status .ObservedGeneration < d .Generation {
115
- patch := client .MergeFrom (d .DeepCopy ())
116
- d .Status .ObservedGeneration = d .Generation
117
- if err := r .Client .Status ().Patch (ctx , d , patch ); err != nil {
118
- logger .Error (err , "Failed to patch status" )
119
- return ctrl.Result {}, err
120
- }
121
- }
122
- return ctrl.Result {}, nil
123
- }
124
-
125
- // Make sure that label selector can match the template's labels.
126
- // TODO(vincepri): Move to a validation (admission) webhook when supported.
127
- selector , err := metav1 .LabelSelectorAsSelector (& d .Spec .Selector )
128
- if err != nil {
129
- return ctrl.Result {}, errors .Wrapf (err , "failed to parse MachineDeployment %q label selector" , d .Name )
130
- }
131
-
132
- if ! selector .Matches (labels .Set (d .Spec .Template .Labels )) {
133
- return ctrl.Result {}, errors .Errorf ("failed validation on MachineDeployment %q label selector, cannot match Machine template labels" , d .Name )
134
- }
135
-
136
- // Copy label selector to its status counterpart in string format.
137
- // This is necessary for CRDs including scale subresources.
138
- d .Status .Selector = selector .String ()
139
-
140
112
// Reconcile and retrieve the Cluster object.
141
113
if d .Labels == nil {
142
114
d .Labels = make (map [string ]string )
@@ -163,6 +135,36 @@ func (r *MachineDeploymentReconciler) reconcile(ctx context.Context, d *clusterv
163
135
}
164
136
}
165
137
138
+ // Test for an empty LabelSelector and short circuit if that is the case
139
+ // TODO: When we have validation webhooks, we should likely reject on an empty LabelSelector
140
+ everything := metav1.LabelSelector {}
141
+ if reflect .DeepEqual (d .Spec .Selector , & everything ) {
142
+ if d .Status .ObservedGeneration < d .Generation {
143
+ patch := client .MergeFrom (d .DeepCopy ())
144
+ d .Status .ObservedGeneration = d .Generation
145
+ if err := r .Client .Status ().Patch (ctx , d , patch ); err != nil {
146
+ logger .Error (err , "Failed to patch status" )
147
+ return ctrl.Result {}, err
148
+ }
149
+ }
150
+ return ctrl.Result {}, nil
151
+ }
152
+
153
+ // Make sure selector and template to be in the same cluster.
154
+ d .Spec .Selector .MatchLabels [clusterv1 .ClusterLabelName ] = d .Spec .ClusterName
155
+ d .Spec .Template .Labels [clusterv1 .ClusterLabelName ] = d .Spec .ClusterName
156
+
157
+ // Make sure that label selector can match the template's labels.
158
+ // TODO(vincepri): Move to a validation (admission) webhook when supported.
159
+ selector , err := metav1 .LabelSelectorAsSelector (& d .Spec .Selector )
160
+ if err != nil {
161
+ return ctrl.Result {}, errors .Wrapf (err , "failed to parse MachineDeployment %q label selector" , d .Name )
162
+ }
163
+
164
+ if ! selector .Matches (labels .Set (d .Spec .Template .Labels )) {
165
+ return ctrl.Result {}, errors .Errorf ("failed validation on MachineDeployment %q label selector, cannot match Machine template labels" , d .Name )
166
+ }
167
+
166
168
msList , err := r .getMachineSetsForDeployment (d )
167
169
if err != nil {
168
170
return ctrl.Result {}, err
0 commit comments