Skip to content

Commit

Permalink
small adjustments
Browse files Browse the repository at this point in the history
- make some functions private
- improve logs message

Signed-off-by: Marc Sluiter <[email protected]>
  • Loading branch information
slintes committed Jul 9, 2024
1 parent 6f52c77 commit 444a703
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
44 changes: 22 additions & 22 deletions controllers/owner_and_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,30 @@ import (
"github.com/medik8s/self-node-remediation/api/v1alpha1"
)

// GetNodeName gets the node name:
// 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
}

// 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) {
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 {
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 {
if ownedByMachine, ref := isOwnedByMachine(snr); ownedByMachine {
return getNodeNameFromMachine(ctx, c, ref, snr.GetNamespace(), log)
}
// fallback: annotation or name
Expand All @@ -39,8 +53,8 @@ func getNodeNameDirect(snr *v1alpha1.SelfNodeRemediation) string {
return snr.GetName()
}

// IsOwnedByNHC checks if the SNR CR is owned by a NodeHealthCheck CR.
func IsOwnedByNHC(snr *v1alpha1.SelfNodeRemediation) (bool, *metav1.OwnerReference) {
// 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
Expand All @@ -49,8 +63,8 @@ func IsOwnedByNHC(snr *v1alpha1.SelfNodeRemediation) (bool, *metav1.OwnerReferen
return false, nil
}

// IsOwnedByMachine checks if the SNR CR is owned by a Machine CR.
func IsOwnedByMachine(snr *v1alpha1.SelfNodeRemediation) (bool, *metav1.OwnerReference) {
// 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
Expand All @@ -59,20 +73,6 @@ func IsOwnedByMachine(snr *v1alpha1.SelfNodeRemediation) (bool, *metav1.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{
Expand Down
2 changes: 1 addition & 1 deletion controllers/selfnoderemediation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func (r *SelfNodeRemediationReconciler) setTimeAssumedRebooted(ctx context.Conte

// getNodeFromSnr returns the unhealthy node reported in the given snr
func (r *SelfNodeRemediationReconciler) getNodeFromSnr(ctx context.Context, snr *v1alpha1.SelfNodeRemediation) (*v1.Node, error) {
nodeName, err := GetNodeName(ctx, r.Client, snr, r.logger)
nodeName, err := getNodeName(ctx, r.Client, snr, r.logger)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/peerhealth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *Server) Start(ctx context.Context) error {

// IsHealthy checks if the given node is healthy
func (s *Server) IsHealthy(ctx context.Context, request *HealthRequest) (*HealthResponse, error) {
s.log.Info("IsHealthy", "node", request.GetNodeName(), "machine", request.GetMachineName())
s.log.Info("checking health for peer", "node", request.GetNodeName(), "machine", request.GetMachineName())

nodeName := request.GetNodeName()
if nodeName == "" {
Expand Down

0 comments on commit 444a703

Please sign in to comment.