-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fix agent reconciler to respect SNR owned by Machines - fix peer server SNR check - align name / owner checks into own file - reduce noisy logging Signed-off-by: Marc Sluiter <[email protected]>
- Loading branch information
Showing
9 changed files
with
145 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/go-logr/logr" | ||
commonAnnotations "github.com/medik8s/common/pkg/annotations" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/openshift/api/machine/v1beta1" | ||
|
||
"github.com/medik8s/self-node-remediation/api/v1alpha1" | ||
) | ||
|
||
// GetNodeName gets the node name: | ||
// - if owned by NHC, or as fallback, from annotation or CR name | ||
// - if owned by a Machine, from the Machine's node reference | ||
func GetNodeName(ctx context.Context, c client.Client, snr *v1alpha1.SelfNodeRemediation, log logr.Logger) (string, error) { | ||
// NHC has priority, so check it first: in case the SNR is owned by NHC, get the node name from annotation or CR name | ||
if ownedByNHC, _ := IsOwnedByNHC(snr); ownedByNHC { | ||
return getNodeNameDirect(snr), nil | ||
} | ||
// in case the SNR is owned by a Machine, we need to check the Machine's nodeRef | ||
if ownedByMachine, ref := IsOwnedByMachine(snr); ownedByMachine { | ||
return getNodeNameFromMachine(ctx, c, ref, snr.GetNamespace(), log) | ||
} | ||
// fallback: annotation or name | ||
return getNodeNameDirect(snr), nil | ||
} | ||
|
||
func getNodeNameDirect(snr *v1alpha1.SelfNodeRemediation) string { | ||
nodeName, isNodeNameAnnotationExist := snr.GetAnnotations()[commonAnnotations.NodeNameAnnotation] | ||
if isNodeNameAnnotationExist { | ||
return nodeName | ||
} | ||
return snr.GetName() | ||
} | ||
|
||
// IsOwnedByNHC checks if the SNR CR is owned by a NodeHealthCheck CR. | ||
func IsOwnedByNHC(snr *v1alpha1.SelfNodeRemediation) (bool, *metav1.OwnerReference) { | ||
for _, ownerRef := range snr.OwnerReferences { | ||
if ownerRef.Kind == "NodeHealthCheck" { | ||
return true, &ownerRef | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
// IsOwnedByMachine checks if the SNR CR is owned by a Machine CR. | ||
func IsOwnedByMachine(snr *v1alpha1.SelfNodeRemediation) (bool, *metav1.OwnerReference) { | ||
for _, ownerRef := range snr.OwnerReferences { | ||
if ownerRef.Kind == "Machine" { | ||
return true, &ownerRef | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
// IsSNRMatching checks if the SNR CR is matching the node or machine name, | ||
// and additionally returns the node name for the SNR in case machineName is empty | ||
func IsSNRMatching(ctx context.Context, c client.Client, snr *v1alpha1.SelfNodeRemediation, nodeName string, machineName string, log logr.Logger) (bool, string, error) { | ||
if isOwnedByMachine, ref := IsOwnedByMachine(snr); isOwnedByMachine && machineName == ref.Name { | ||
return true, "", nil | ||
} | ||
snrNodeName, err := GetNodeName(ctx, c, snr, log) | ||
if err != nil { | ||
log.Error(err, "failed to get node name from machine") | ||
return false, "", err | ||
} | ||
return snrNodeName == nodeName, snrNodeName, nil | ||
} | ||
|
||
func getNodeNameFromMachine(ctx context.Context, c client.Client, ref *metav1.OwnerReference, ns string, log logr.Logger) (string, error) { | ||
machine := &v1beta1.Machine{} | ||
machineKey := client.ObjectKey{ | ||
Name: ref.Name, | ||
Namespace: ns, | ||
} | ||
|
||
if err := c.Get(ctx, machineKey, machine); err != nil { | ||
log.Error(err, "failed to get machine from SelfNodeRemediation CR owner ref", | ||
"machine name", machineKey.Name, "namespace", machineKey.Namespace) | ||
return "", err | ||
} | ||
|
||
if machine.Status.NodeRef == nil { | ||
err := errors.New("nodeRef is nil") | ||
log.Error(err, "failed to retrieve node from the unhealthy machine") | ||
return "", err | ||
} | ||
|
||
return machine.Status.NodeRef.Name, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.