|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 13 | +) |
| 14 | + |
| 15 | +func TestIsControlPlaneReady(t *testing.T) { |
| 16 | + controlPlaneLabels := map[string]string{ |
| 17 | + "node-role.kubernetes.io/master": "", |
| 18 | + "node-role.kubernetes.io/control-plane": "", |
| 19 | + "beta.kubernetes.io/arch": "amd64", |
| 20 | + "beta.kubernetes.io/os": "linux", |
| 21 | + "kubernetes.io/arch": "amd64", |
| 22 | + "kubernetes.io/hostname": "kind-control-plane", |
| 23 | + "kubernetes.io/os": "linux", |
| 24 | + } |
| 25 | + |
| 26 | + nodeTests := []struct { |
| 27 | + name string |
| 28 | + labels map[string]string |
| 29 | + conditions []corev1.NodeCondition |
| 30 | + wantReady bool |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "control plane not ready", |
| 34 | + labels: controlPlaneLabels, |
| 35 | + conditions: makeConditions( |
| 36 | + corev1.NodeCondition{Type: corev1.NodeReady, Status: corev1.ConditionFalse, LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "KubeletNotReady", Message: "container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized"}, |
| 37 | + ), |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "control plane ready", |
| 41 | + labels: controlPlaneLabels, |
| 42 | + conditions: makeConditions( |
| 43 | + corev1.NodeCondition{Type: "NetworkUnavailable", Status: "False", LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "CalicoIsUp", Message: "Calico is running on this node"}, |
| 44 | + corev1.NodeCondition{Type: "Ready", Status: "True", LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "KubeletReady", Message: "kubelet is posting ready status"}, |
| 45 | + ), |
| 46 | + wantReady: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "no control plane", |
| 50 | + labels: map[string]string{}, |
| 51 | + conditions: makeConditions( |
| 52 | + corev1.NodeCondition{Type: corev1.NodeReady, Status: corev1.ConditionFalse, LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "KubeletNotReady", Message: "container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized"}, |
| 53 | + ), |
| 54 | + }, |
| 55 | + } |
| 56 | + for _, tt := range nodeTests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + cl := makeClient(makeNode(tt.labels, tt.conditions...)) |
| 59 | + |
| 60 | + ready, err := IsControlPlaneReady(context.TODO(), cl) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + if ready != tt.wantReady { |
| 66 | + t.Fatalf("IsControlPlaneReady() got %v, want %v", ready, tt.wantReady) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func makeNode(labels map[string]string, conds ...corev1.NodeCondition) *corev1.Node { |
| 73 | + return &corev1.Node{ |
| 74 | + ObjectMeta: metav1.ObjectMeta{ |
| 75 | + Name: "test-control-plane", |
| 76 | + Labels: labels, |
| 77 | + UID: types.UID("f046e20e-df55-40d0-ab3a-76ff56617575"), |
| 78 | + }, |
| 79 | + Spec: corev1.NodeSpec{}, |
| 80 | + Status: corev1.NodeStatus{ |
| 81 | + Conditions: conds, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func makeClient(objs ...runtime.Object) client.Client { |
| 87 | + return fake.NewClientBuilder().WithRuntimeObjects(objs...).Build() |
| 88 | +} |
| 89 | + |
| 90 | +func makeConditions(conds ...corev1.NodeCondition) []corev1.NodeCondition { |
| 91 | + base := []corev1.NodeCondition{ |
| 92 | + corev1.NodeCondition{Type: corev1.NodeMemoryPressure, Status: corev1.ConditionFalse, LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "KubeletHasSufficientMemory", Message: "kubelet has sufficient memory available"}, |
| 93 | + corev1.NodeCondition{Type: corev1.NodeDiskPressure, Status: corev1.ConditionFalse, LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "KubeletHasNoDiskPressure", Message: "kubelet has no disk pressure"}, |
| 94 | + corev1.NodeCondition{Type: corev1.NodePIDPressure, Status: corev1.ConditionFalse, LastHeartbeatTime: metav1.Now(), LastTransitionTime: metav1.Now(), Reason: "KubeletHasSufficientPID", Message: "kubelet has sufficient PID available"}, |
| 95 | + } |
| 96 | + return append(conds, base...) |
| 97 | +} |
0 commit comments