Skip to content

Commit f5099cb

Browse files
committed
Let MHC to remediate any machine owned by a controller
Fix https://bugzilla.redhat.com/show_bug.cgi?id=1816398 We want to relax the hard dependency on machineSets and let remediation to happen for machines that are owned by any controller. This will enable a possible integration for a self healing control plane.
1 parent a353204 commit f5099cb

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

pkg/controller/machinehealthcheck/machinehealthcheck_controller.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
const (
3434
machineAnnotationKey = "machine.openshift.io/machine"
3535
machineExternalAnnotationKey = "host.metal3.io/external-remediation"
36-
ownerControllerKind = "MachineSet"
3736
nodeMasterLabel = "node-role.kubernetes.io/master"
3837
machineRoleLabel = "machine.openshift.io/cluster-api-machine-role"
3938
machineMasterRole = "master"
@@ -436,8 +435,8 @@ func (r *ReconcileMachineHealthCheck) mhcRequestsFromMachine(o handler.MapObject
436435

437436
func (t *target) remediate(r *ReconcileMachineHealthCheck) error {
438437
glog.Infof(" %s: start remediation logic", t.string())
439-
if !t.hasMachineSetOwner() {
440-
glog.Infof("%s: no machineSet controller owner, skipping remediation", t.string())
438+
if !t.hasControllerOwner() {
439+
glog.Infof("%s: no controller owner, skipping remediation", t.string())
441440
return nil
442441
}
443442

@@ -595,14 +594,8 @@ func (t *target) needsRemediation(timeoutForMachineToHaveNode time.Duration) (bo
595594
return false, minDuration(nextCheckTimes), nil
596595
}
597596

598-
func (t *target) hasMachineSetOwner() bool {
599-
ownerRefs := t.Machine.ObjectMeta.GetOwnerReferences()
600-
for _, or := range ownerRefs {
601-
if or.Kind == ownerControllerKind {
602-
return true
603-
}
604-
}
605-
return false
597+
func (t *target) hasControllerOwner() bool {
598+
return metav1.GetControllerOf(&t.Machine) != nil
606599
}
607600

608601
func derefStringPointer(stringPointer *string) string {

pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"k8s.io/apimachinery/pkg/util/intstr"
2121
"k8s.io/client-go/kubernetes/scheme"
2222
"k8s.io/client-go/tools/record"
23+
"k8s.io/utils/pointer"
2324
"sigs.k8s.io/controller-runtime/pkg/client"
2425
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2526
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -364,11 +365,27 @@ func TestReconcile(t *testing.T) {
364365
}
365366
}
366367

367-
func TestHasMachineSetOwner(t *testing.T) {
368+
func TestHasControllerOwner(t *testing.T) {
368369
machineWithMachineSet := maotesting.NewMachine("machineWithMachineSet", "node")
370+
369371
machineWithNoMachineSet := maotesting.NewMachine("machineWithNoMachineSet", "node")
370372
machineWithNoMachineSet.OwnerReferences = nil
371373

374+
machineWithAnyControllerOwner := maotesting.NewMachine("machineWithAnyControllerOwner", "node")
375+
machineWithAnyControllerOwner.OwnerReferences = []metav1.OwnerReference{
376+
{
377+
Kind: "Any",
378+
Controller: pointer.BoolPtr(true),
379+
},
380+
}
381+
382+
machineWithNoControllerOwner := maotesting.NewMachine("machineWithNoControllerOwner", "node")
383+
machineWithNoControllerOwner.OwnerReferences = []metav1.OwnerReference{
384+
{
385+
Kind: "Any",
386+
},
387+
}
388+
372389
testsCases := []struct {
373390
target target
374391
expected bool
@@ -385,10 +402,22 @@ func TestHasMachineSetOwner(t *testing.T) {
385402
},
386403
expected: true,
387404
},
405+
{
406+
target: target{
407+
Machine: *machineWithAnyControllerOwner,
408+
},
409+
expected: true,
410+
},
411+
{
412+
target: target{
413+
Machine: *machineWithNoControllerOwner,
414+
},
415+
expected: false,
416+
},
388417
}
389418

390419
for _, tc := range testsCases {
391-
if got := tc.target.hasMachineSetOwner(); got != tc.expected {
420+
if got := tc.target.hasControllerOwner(); got != tc.expected {
392421
t.Errorf("Test case: Machine %s. Expected: %t, got: %t", tc.target.Machine.Name, tc.expected, got)
393422
}
394423
}
@@ -1820,11 +1849,16 @@ func TestRemediate(t *testing.T) {
18201849
APIVersion: "machine.openshift.io/v1beta1",
18211850
},
18221851
ObjectMeta: metav1.ObjectMeta{
1823-
Annotations: make(map[string]string),
1824-
Name: "test",
1825-
Namespace: namespace,
1826-
Labels: map[string]string{"foo": "bar"},
1827-
OwnerReferences: []metav1.OwnerReference{{Kind: "MachineSet"}},
1852+
Annotations: make(map[string]string),
1853+
Name: "test",
1854+
Namespace: namespace,
1855+
Labels: map[string]string{"foo": "bar"},
1856+
OwnerReferences: []metav1.OwnerReference{
1857+
{
1858+
Kind: "MachineSet",
1859+
Controller: pointer.BoolPtr(true),
1860+
},
1861+
},
18281862
},
18291863
Spec: mapiv1beta1.MachineSpec{},
18301864
Status: mapiv1beta1.MachineStatus{},
@@ -1858,12 +1892,17 @@ func TestRemediate(t *testing.T) {
18581892
APIVersion: "machine.openshift.io/v1beta1",
18591893
},
18601894
ObjectMeta: metav1.ObjectMeta{
1861-
Annotations: make(map[string]string),
1862-
Name: "test",
1863-
Namespace: namespace,
1864-
Labels: map[string]string{"foo": "bar"},
1865-
OwnerReferences: []metav1.OwnerReference{{Kind: "MachineSet"}},
1866-
UID: "uid",
1895+
Annotations: make(map[string]string),
1896+
Name: "test",
1897+
Namespace: namespace,
1898+
Labels: map[string]string{"foo": "bar"},
1899+
OwnerReferences: []metav1.OwnerReference{
1900+
{
1901+
Kind: "MachineSet",
1902+
Controller: pointer.BoolPtr(true),
1903+
},
1904+
},
1905+
UID: "uid",
18671906
},
18681907
//Spec: mapiv1beta1.MachineSpec{},
18691908
//Status: mapiv1beta1.MachineStatus{},
@@ -1905,7 +1944,12 @@ func TestRemediate(t *testing.T) {
19051944
Labels: map[string]string{
19061945
machineRoleLabel: machineMasterRole,
19071946
},
1908-
OwnerReferences: []metav1.OwnerReference{{Kind: "MachineSet"}},
1947+
OwnerReferences: []metav1.OwnerReference{
1948+
{
1949+
Kind: "MachineSet",
1950+
Controller: pointer.BoolPtr(true),
1951+
},
1952+
},
19091953
},
19101954
Spec: mapiv1beta1.MachineSpec{},
19111955
Status: mapiv1beta1.MachineStatus{},

pkg/util/testing/testing.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
corev1 "k8s.io/api/core/v1"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/util/uuid"
11+
"k8s.io/utils/pointer"
1112
)
1213

1314
const (
@@ -74,12 +75,17 @@ func NewMachine(name string, nodeName string) *mapiv1.Machine {
7475
m := &mapiv1.Machine{
7576
TypeMeta: metav1.TypeMeta{Kind: "Machine"},
7677
ObjectMeta: metav1.ObjectMeta{
77-
Annotations: make(map[string]string),
78-
Name: name,
79-
Namespace: Namespace,
80-
Labels: FooBar(),
81-
UID: uuid.NewUUID(),
82-
OwnerReferences: []metav1.OwnerReference{{Kind: "MachineSet"}},
78+
Annotations: make(map[string]string),
79+
Name: name,
80+
Namespace: Namespace,
81+
Labels: FooBar(),
82+
UID: uuid.NewUUID(),
83+
OwnerReferences: []metav1.OwnerReference{
84+
{
85+
Kind: "MachineSet",
86+
Controller: pointer.BoolPtr(true),
87+
},
88+
},
8389
},
8490
Spec: mapiv1.MachineSpec{},
8591
}

0 commit comments

Comments
 (0)