|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 16 | + "k8s.io/client-go/kubernetes/fake" |
| 17 | + k8stesting "k8s.io/client-go/testing" |
| 18 | + |
| 19 | + "github.com/skyhook-io/radar/internal/k8s" |
| 20 | +) |
| 21 | + |
| 22 | +func TestDrainOptionsFromRequestDefaults(t *testing.T) { |
| 23 | + no := false |
| 24 | + yes := true |
| 25 | + grace := int64(5) |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + req DrainRequest |
| 30 | + emptyDirDefault bool |
| 31 | + wantEmptyDir bool |
| 32 | + wantForce bool |
| 33 | + wantTimeout time.Duration |
| 34 | + }{ |
| 35 | + {name: "drain keeps its historical emptyDir default", req: DrainRequest{}, emptyDirDefault: true, wantEmptyDir: true, wantTimeout: 60 * time.Second}, |
| 36 | + {name: "plan never includes emptyDir data unless asked", req: DrainRequest{}, emptyDirDefault: false, wantEmptyDir: false, wantTimeout: 60 * time.Second}, |
| 37 | + {name: "explicit false overrides the drain default", req: DrainRequest{DeleteEmptyDirData: &no}, emptyDirDefault: true, wantEmptyDir: false, wantTimeout: 60 * time.Second}, |
| 38 | + {name: "explicit true overrides the plan default", req: DrainRequest{DeleteEmptyDirData: &yes, Force: true, Timeout: 120}, emptyDirDefault: false, wantEmptyDir: true, wantForce: true, wantTimeout: 120 * time.Second}, |
| 39 | + } |
| 40 | + for _, tt := range tests { |
| 41 | + t.Run(tt.name, func(t *testing.T) { |
| 42 | + got := drainOptionsFromRequest(tt.req, tt.emptyDirDefault) |
| 43 | + if !got.IgnoreDaemonSets { |
| 44 | + t.Fatalf("DaemonSets must always be ignored") |
| 45 | + } |
| 46 | + if got.DeleteEmptyDirData != tt.wantEmptyDir || got.Force != tt.wantForce || got.Timeout != tt.wantTimeout { |
| 47 | + t.Fatalf("got %+v", got) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + got := drainOptionsFromRequest(DrainRequest{GracePeriodSeconds: &grace}, false) |
| 53 | + if got.GracePeriodSeconds == nil || *got.GracePeriodSeconds != 5 { |
| 54 | + t.Fatalf("grace period must pass through, got %+v", got.GracePeriodSeconds) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func drainPlanTestClient() *fake.Clientset { |
| 59 | + yes := true |
| 60 | + return fake.NewSimpleClientset( |
| 61 | + &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "worker-1"}}, |
| 62 | + &corev1.Pod{ |
| 63 | + ObjectMeta: metav1.ObjectMeta{Name: "web-1", Namespace: "shop", OwnerReferences: []metav1.OwnerReference{{Kind: "ReplicaSet", Name: "web", Controller: &yes}}}, |
| 64 | + Spec: corev1.PodSpec{NodeName: "worker-1"}, |
| 65 | + Status: corev1.PodStatus{Phase: corev1.PodRunning}, |
| 66 | + }, |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +func TestWriteDrainPlanRespondsWithPlanAndNeverMutates(t *testing.T) { |
| 71 | + client := drainPlanTestClient() |
| 72 | + s := &Server{} |
| 73 | + rr := httptest.NewRecorder() |
| 74 | + req := httptest.NewRequest(http.MethodPost, "/api/nodes/worker-1/drain-plan", nil) |
| 75 | + |
| 76 | + s.writeDrainPlan(rr, req, client, "worker-1", k8s.DrainOptions{IgnoreDaemonSets: true}) |
| 77 | + |
| 78 | + if rr.Code != http.StatusOK { |
| 79 | + t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String()) |
| 80 | + } |
| 81 | + var plan k8s.DrainPlan |
| 82 | + if err := json.Unmarshal(rr.Body.Bytes(), &plan); err != nil { |
| 83 | + t.Fatalf("decode plan: %v", err) |
| 84 | + } |
| 85 | + if !plan.Estimate || plan.Node != "worker-1" || plan.Summary.Evict != 1 || len(plan.Pods) != 1 { |
| 86 | + t.Fatalf("unexpected plan: %+v", plan) |
| 87 | + } |
| 88 | + for _, a := range client.Actions() { |
| 89 | + if a.GetVerb() != "get" && a.GetVerb() != "list" { |
| 90 | + t.Fatalf("plan endpoint must not mutate the cluster, saw %s %s", a.GetVerb(), a.GetResource().Resource) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestWriteDrainPlanStatusMapping(t *testing.T) { |
| 96 | + t.Run("unknown node is 404", func(t *testing.T) { |
| 97 | + s := &Server{} |
| 98 | + rr := httptest.NewRecorder() |
| 99 | + s.writeDrainPlan(rr, httptest.NewRequest(http.MethodPost, "/", nil), drainPlanTestClient(), "ghost", k8s.DrainOptions{}) |
| 100 | + if rr.Code != http.StatusNotFound { |
| 101 | + t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String()) |
| 102 | + } |
| 103 | + }) |
| 104 | + t.Run("forbidden pod list is 403", func(t *testing.T) { |
| 105 | + client := drainPlanTestClient() |
| 106 | + client.PrependReactor("list", "pods", func(k8stesting.Action) (bool, runtime.Object, error) { |
| 107 | + return true, nil, apierrors.NewForbidden(schema.GroupResource{Resource: "pods"}, "", nil) |
| 108 | + }) |
| 109 | + s := &Server{} |
| 110 | + rr := httptest.NewRecorder() |
| 111 | + s.writeDrainPlan(rr, httptest.NewRequest(http.MethodPost, "/", nil), client, "worker-1", k8s.DrainOptions{}) |
| 112 | + if rr.Code != http.StatusForbidden { |
| 113 | + t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String()) |
| 114 | + } |
| 115 | + }) |
| 116 | + t.Run("forbidden PDB list degrades to an unevaluated plan, not an error", func(t *testing.T) { |
| 117 | + client := drainPlanTestClient() |
| 118 | + client.PrependReactor("list", "poddisruptionbudgets", func(k8stesting.Action) (bool, runtime.Object, error) { |
| 119 | + return true, nil, apierrors.NewForbidden(schema.GroupResource{Group: "policy", Resource: "poddisruptionbudgets"}, "", nil) |
| 120 | + }) |
| 121 | + s := &Server{} |
| 122 | + rr := httptest.NewRecorder() |
| 123 | + s.writeDrainPlan(rr, httptest.NewRequest(http.MethodPost, "/", nil), client, "worker-1", k8s.DrainOptions{}) |
| 124 | + var plan k8s.DrainPlan |
| 125 | + if rr.Code != http.StatusOK || json.Unmarshal(rr.Body.Bytes(), &plan) != nil || plan.PDBsEvaluated || plan.PDBError == "" { |
| 126 | + t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String()) |
| 127 | + } |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +func TestDrainPlanOptionsFromHTTP(t *testing.T) { |
| 132 | + t.Run("empty body applies the plan defaults", func(t *testing.T) { |
| 133 | + opts, err := drainPlanOptionsFromHTTP(httptest.NewRequest(http.MethodPost, "/", nil)) |
| 134 | + if err != nil || opts.DeleteEmptyDirData || opts.Force || !opts.IgnoreDaemonSets { |
| 135 | + t.Fatalf("got %+v, %v", opts, err) |
| 136 | + } |
| 137 | + }) |
| 138 | + t.Run("explicit options are honoured", func(t *testing.T) { |
| 139 | + body := strings.NewReader(`{"deleteEmptyDirData":true,"force":true,"gracePeriodSeconds":7}`) |
| 140 | + opts, err := drainPlanOptionsFromHTTP(httptest.NewRequest(http.MethodPost, "/", body)) |
| 141 | + if err != nil || !opts.DeleteEmptyDirData || !opts.Force || opts.GracePeriodSeconds == nil || *opts.GracePeriodSeconds != 7 { |
| 142 | + t.Fatalf("got %+v, %v", opts, err) |
| 143 | + } |
| 144 | + }) |
| 145 | + t.Run("malformed body is rejected", func(t *testing.T) { |
| 146 | + if _, err := drainPlanOptionsFromHTTP(httptest.NewRequest(http.MethodPost, "/", strings.NewReader("{"))); err == nil { |
| 147 | + t.Fatalf("expected a decode error") |
| 148 | + } |
| 149 | + }) |
| 150 | +} |
0 commit comments