|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package patchnode |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "net/http" |
| 23 | + "net/http/httptest" |
| 24 | + "testing" |
| 25 | + |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + clientset "k8s.io/client-go/kubernetes" |
| 29 | + restclient "k8s.io/client-go/rest" |
| 30 | + kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" |
| 31 | +) |
| 32 | + |
| 33 | +func TestAnnotateCRISocket(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + currentCRISocketAnnotation string |
| 37 | + newCRISocketAnnotation string |
| 38 | + expectedPatch string |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "CRI-socket annotation missing", |
| 42 | + currentCRISocketAnnotation: "", |
| 43 | + newCRISocketAnnotation: "/run/containerd/containerd.sock", |
| 44 | + expectedPatch: `{"metadata":{"annotations":{"kubeadm.alpha.kubernetes.io/cri-socket":"/run/containerd/containerd.sock"}}}`, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "CRI-socket annotation already exists", |
| 48 | + currentCRISocketAnnotation: "/run/containerd/containerd.sock", |
| 49 | + newCRISocketAnnotation: "/run/containerd/containerd.sock", |
| 50 | + expectedPatch: `{}`, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "CRI-socket annotation needs to be updated", |
| 54 | + currentCRISocketAnnotation: "/var/run/dockershim.sock", |
| 55 | + newCRISocketAnnotation: "/run/containerd/containerd.sock", |
| 56 | + expectedPatch: `{"metadata":{"annotations":{"kubeadm.alpha.kubernetes.io/cri-socket":"/run/containerd/containerd.sock"}}}`, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tc := range tests { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + |
| 63 | + nodename := "node01" |
| 64 | + node := &v1.Node{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: nodename, |
| 67 | + Labels: map[string]string{ |
| 68 | + v1.LabelHostname: nodename, |
| 69 | + }, |
| 70 | + Annotations: map[string]string{}, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + if tc.currentCRISocketAnnotation != "" { |
| 75 | + node.ObjectMeta.Annotations[kubeadmconstants.AnnotationKubeadmCRISocket] = tc.currentCRISocketAnnotation |
| 76 | + } |
| 77 | + |
| 78 | + jsonNode, err := json.Marshal(node) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("unexpected encoding error: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + var patchRequest string |
| 84 | + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 85 | + w.Header().Set("Content-Type", "application/json") |
| 86 | + |
| 87 | + if req.URL.Path != "/api/v1/nodes/"+nodename { |
| 88 | + t.Errorf("request for unexpected HTTP resource: %v", req.URL.Path) |
| 89 | + http.Error(w, "", http.StatusNotFound) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + switch req.Method { |
| 94 | + case "GET": |
| 95 | + case "PATCH": |
| 96 | + buf := new(bytes.Buffer) |
| 97 | + buf.ReadFrom(req.Body) |
| 98 | + patchRequest = buf.String() |
| 99 | + default: |
| 100 | + t.Errorf("request for unexpected HTTP verb: %v", req.Method) |
| 101 | + http.Error(w, "", http.StatusNotFound) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + w.WriteHeader(http.StatusOK) |
| 106 | + w.Write(jsonNode) |
| 107 | + })) |
| 108 | + defer s.Close() |
| 109 | + |
| 110 | + cs, err := clientset.NewForConfig(&restclient.Config{Host: s.URL}) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("unexpected error building clientset: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if err := AnnotateCRISocket(cs, nodename, tc.newCRISocketAnnotation); err != nil { |
| 116 | + t.Errorf("unexpected error: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + if tc.expectedPatch != patchRequest { |
| 120 | + t.Errorf("expected patch %v, got %v", tc.expectedPatch, patchRequest) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments