-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some fixes and e2e test improvements:
- re-enable and fix api check log tests in e2e test - use service IP for killing API connection - kill API connection on SNR DS pod - add peer check server logs and use them for test which can't get logs from unhealthy node's SNR agent pod - wait for pod deletion only, not restart (restart is caused by reboot, not SNR) - refactor / cleanup e2e tests - fix owner check / node name / machine name in peer check server and agent reconciler - update sort-imports, which ignores generated files now
- Loading branch information
Showing
59 changed files
with
6,557 additions
and
617 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
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
Oops, something went wrong.