From 5f0862115b8e8a1deea0322a5bd5693181c9a6e2 Mon Sep 17 00:00:00 2001 From: JameKeal <413621396@qq.com> Date: Mon, 12 Jun 2023 17:22:51 +0800 Subject: [PATCH] add unit test --- .../openyurt/templates/yurthub-staticpod.yaml | 107 ----------------- .../yurthub-cloud-yurtstaticset.yaml | 3 + .../templates/yurthub-yurtstaticset.yaml | 3 + pkg/util/kubeconfig/kubeconfig_test.go | 2 + .../kubeadm/app/util/apiclient/idempotency.go | 25 ---- pkg/yurtadm/cmd/join/join.go | 3 + pkg/yurtadm/cmd/join/join_test.go | 12 +- pkg/yurtadm/cmd/join/phases/postcheck.go | 13 +-- pkg/yurtadm/util/yurthub/yurthub_test.go | 109 ++++++++++++++++++ 9 files changed, 132 insertions(+), 145 deletions(-) delete mode 100644 charts/openyurt/templates/yurthub-staticpod.yaml diff --git a/charts/openyurt/templates/yurthub-staticpod.yaml b/charts/openyurt/templates/yurthub-staticpod.yaml deleted file mode 100644 index 64596bf6e79..00000000000 --- a/charts/openyurt/templates/yurthub-staticpod.yaml +++ /dev/null @@ -1,107 +0,0 @@ -apiVersion: apps.openyurt.io/v1alpha1 -kind: StaticPod -metadata: - name: yurt-hub - namespace: {{ include "openyurt.namespace" . }} -spec: - staticPodManifest: yurt-hub - template: - metadata: - labels: - k8s-app: yurt-hub - name: yurt-hub - namespace: {{ include "openyurt.namespace" . }} - spec: - volumes: - - name: hub-dir - hostPath: - path: /var/lib/yurthub - type: DirectoryOrCreate - - name: kubernetes - hostPath: - path: /etc/kubernetes - type: Directory - - name: pem-dir - hostPath: - path: /var/lib/kubelet/pki - type: Directory - containers: - - name: yurt-hub - image: "{{ .Values.yurtHub.Image.registry }}/{{ .Values.yurtHub.Image.repository }}:{{ .Values.yurtHub.Image.tag }}" - imagePullPolicy: IfNotPresent - volumeMounts: - - name: hub-dir - mountPath: /var/lib/yurthub - - name: kubernetes - mountPath: /etc/kubernetes - - name: pem-dir - mountPath: /var/lib/kubelet/pki - command: - - yurthub - - --v=2 - - --bind-address={{ .Values.yurtHub.yurthubServerAddr | quote }} - - --server-addr={{`{{.kubernetesServerAddr}}`}} - - --node-name=$(NODE_NAME) - - --bootstrap-file={{ .Values.yurtHub.bootstrapFile | quote }} - - --working-mode={{`{{.workingMode}}`}} - - --namespace={{ include "openyurt.namespace" . }} - {{`{{if .organizations }}`}} - - --hub-cert-organizations={{`{{.organizations}}`}} - {{`{{end}}`}} - {{`{{if .nodePoolName }}`}} - - --nodepool-name={{`{{.nodePoolName}}`}} - {{`{{end}}`}} - livenessProbe: - httpGet: - host: {{ .Values.yurtHub.yurthubServerAddr | quote }} - path: /v1/healthz - port: 10267 - initialDelaySeconds: 300 - periodSeconds: 5 - failureThreshold: 3 - resources: - requests: - cpu: 150m - memory: 150Mi - limits: - memory: 300Mi - securityContext: - capabilities: - add: [ "NET_ADMIN", "NET_RAW" ] - env: - - name: NODE_NAME - valueFrom: - fieldRef: - fieldPath: spec.nodeName - hostNetwork: true - priorityClassName: system-node-critical - priority: 2000001000 ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRole -metadata: - name: read-static-pod-cm -rules: - - apiGroups: - - "" - resources: - - configmaps - resourceNames: - - static-pod-cm-{{ include "openyurt.namespace" . }}-yurt-hub - verbs: - - get ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRoleBinding -metadata: - name: read-static-pod-cm-bootstrap -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: read-static-pod-cm -subjects: - - apiGroup: rbac.authorization.k8s.io - kind: Group - name: system:bootstrappers - - diff --git a/charts/yurthub/templates/yurthub-cloud-yurtstaticset.yaml b/charts/yurthub/templates/yurthub-cloud-yurtstaticset.yaml index a287fac1aa0..b65890a1075 100644 --- a/charts/yurthub/templates/yurthub-cloud-yurtstaticset.yaml +++ b/charts/yurthub/templates/yurthub-cloud-yurtstaticset.yaml @@ -42,6 +42,9 @@ spec: {{- if .Values.organizations }} - --hub-cert-organizations={{ .Values.organizations }} {{- end }} + {{- if .nodePoolName }} + - --nodepool-name={{ .Values.nodePoolName }} + {{- end }} livenessProbe: httpGet: host: {{ .Values.yurthubBindingAddr }} diff --git a/charts/yurthub/templates/yurthub-yurtstaticset.yaml b/charts/yurthub/templates/yurthub-yurtstaticset.yaml index 172dc91576e..7b58e770e8a 100644 --- a/charts/yurthub/templates/yurthub-yurtstaticset.yaml +++ b/charts/yurthub/templates/yurthub-yurtstaticset.yaml @@ -42,6 +42,9 @@ spec: {{- if .Values.organizations }} - --hub-cert-organizations={{ .Values.organizations }} {{- end }} + {{- if .nodePoolName }} + - --nodepool-name={{ .Values.nodePoolName }} + {{- end }} livenessProbe: httpGet: host: {{ .Values.yurthubBindingAddr }} diff --git a/pkg/util/kubeconfig/kubeconfig_test.go b/pkg/util/kubeconfig/kubeconfig_test.go index b1cce66144a..11d1a075e65 100644 --- a/pkg/util/kubeconfig/kubeconfig_test.go +++ b/pkg/util/kubeconfig/kubeconfig_test.go @@ -183,6 +183,8 @@ func TestWriteKubeconfigToDisk(t *testing.T) { newFile, ) } + client, err := ToYurtClientSet(c) + t.Log(client, err) }) } } diff --git a/pkg/util/kubernetes/kubeadm/app/util/apiclient/idempotency.go b/pkg/util/kubernetes/kubeadm/app/util/apiclient/idempotency.go index 3603d510d94..e8d85e2372e 100644 --- a/pkg/util/kubernetes/kubeadm/app/util/apiclient/idempotency.go +++ b/pkg/util/kubernetes/kubeadm/app/util/apiclient/idempotency.go @@ -154,28 +154,3 @@ func GetNodePoolInfoWithRetry(client yurtclientset.Interface, name string) (*nod } return nil, lastError } - -func JoinNodeInSpecifiedNodePool(client clientset.Interface, nodeName, nodePoolName string) error { - var node *v1.Node - var lastError error - err := wait.ExponentialBackoff(clientsetretry.DefaultBackoff, func() (bool, error) { - var err error - node, err = client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{}) - if err != nil { - lastError = err - return false, nil - } - - node.Labels[nodepoolv1alpha1.LabelDesiredNodePool] = nodePoolName - _, err = client.CoreV1().Nodes().Update(context.TODO(), node, metav1.UpdateOptions{}) - if err != nil { - lastError = err - return false, nil - } - return true, nil - }) - if err == nil { - return nil - } - return lastError -} diff --git a/pkg/yurtadm/cmd/join/join.go b/pkg/yurtadm/cmd/join/join.go index 974fc66d4ff..05e711914ec 100644 --- a/pkg/yurtadm/cmd/join/join.go +++ b/pkg/yurtadm/cmd/join/join.go @@ -38,6 +38,7 @@ import ( yurtconstants "github.com/openyurtio/openyurt/pkg/yurtadm/constants" "github.com/openyurtio/openyurt/pkg/yurtadm/util/edgenode" yurtadmutil "github.com/openyurtio/openyurt/pkg/yurtadm/util/kubernetes" + nodepoolv1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/apis/apps/v1alpha1" ) type joinOptions struct { @@ -341,6 +342,8 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) { // the specified nodePool not exist, return return nil, errors.Errorf("when --nodepool-name is specified, the specified nodePool should be exist.") } + // add nodePool label for node by kubelet + data.nodeLabels[nodepoolv1alpha1.LabelDesiredNodePool] = opt.nodePoolName } klog.Infof("node join data info: %#+v", *data) diff --git a/pkg/yurtadm/cmd/join/join_test.go b/pkg/yurtadm/cmd/join/join_test.go index 0d486d536db..0536d163607 100644 --- a/pkg/yurtadm/cmd/join/join_test.go +++ b/pkg/yurtadm/cmd/join/join_test.go @@ -235,6 +235,10 @@ func TestRun(t *testing.T) { func TestNewJoinData(t *testing.T) { jo := newJoinOptions() + jo2 := newJoinOptions() + jo2.token = "v22u0b.17490yh3xp8azpr0" + jo2.unsafeSkipCAVerification = true + jo2.nodePoolName = "nodePool2" tests := []struct { name string @@ -244,10 +248,16 @@ func TestNewJoinData(t *testing.T) { }{ { "normal", - []string{}, + []string{"localhost:8080"}, jo, nil, }, + { + "norma2", + []string{"localhost:8080"}, + jo2, + nil, + }, } for _, tt := range tests { diff --git a/pkg/yurtadm/cmd/join/phases/postcheck.go b/pkg/yurtadm/cmd/join/phases/postcheck.go index 304f02766e5..a70cae1cb52 100644 --- a/pkg/yurtadm/cmd/join/phases/postcheck.go +++ b/pkg/yurtadm/cmd/join/phases/postcheck.go @@ -19,14 +19,12 @@ package phases import ( "k8s.io/klog/v2" - "github.com/openyurtio/openyurt/pkg/util/kubernetes/kubeadm/app/util/apiclient" "github.com/openyurtio/openyurt/pkg/yurtadm/cmd/join/joindata" "github.com/openyurtio/openyurt/pkg/yurtadm/util/kubernetes" "github.com/openyurtio/openyurt/pkg/yurtadm/util/yurthub" ) -// RunPostCheck executes the node health check and clean process, -// if specified nodePool, it will join node in specified nodePool. +// RunPostCheck executes the node health check and clean process. func RunPostCheck(data joindata.YurtJoinData) error { klog.V(1).Infof("check kubelet status.") if err := kubernetes.CheckKubeletStatus(); err != nil { @@ -40,15 +38,6 @@ func RunPostCheck(data joindata.YurtJoinData) error { } klog.V(1).Infof("hub agent is ready") - if len(data.NodeRegistration().NodePoolName) != 0 { - klog.V(1).Infof("starting join node in specified nodePool.") - if err := apiclient.JoinNodeInSpecifiedNodePool(data.BootstrapClient(), - data.NodeRegistration().Name, data.NodeRegistration().NodePoolName); err != nil { - return err - } - klog.V(1).Infof("join node in specified nodePool successful.") - } - if err := yurthub.CleanHubBootstrapConfig(); err != nil { return err } diff --git a/pkg/yurtadm/util/yurthub/yurthub_test.go b/pkg/yurtadm/util/yurthub/yurthub_test.go index 78c005e805d..8736162dc6e 100644 --- a/pkg/yurtadm/util/yurthub/yurthub_test.go +++ b/pkg/yurtadm/util/yurthub/yurthub_test.go @@ -20,6 +20,11 @@ import ( "testing" "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/util/sets" + clientset "k8s.io/client-go/kubernetes" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + + "github.com/openyurtio/openyurt/pkg/yurtadm/cmd/join/joindata" ) var ( @@ -225,3 +230,107 @@ func Test_useRealServerAddr(t *testing.T) { }) } } + +type testData struct { + joinNodeData *joindata.NodeRegistration +} + +func (j *testData) ServerAddr() string { + return "" +} + +func (j *testData) JoinToken() string { + return "" +} + +func (j *testData) PauseImage() string { + return "" +} + +func (j *testData) YurtHubImage() string { + return "" +} + +func (j *testData) YurtHubServer() string { + return "" +} + +func (j *testData) YurtHubTemplate() string { + return "" +} + +func (j *testData) YurtHubManifest() string { + return "" +} + +func (j *testData) KubernetesVersion() string { + return "" +} + +func (j *testData) TLSBootstrapCfg() *clientcmdapi.Config { + return nil +} + +func (j *testData) BootstrapClient() *clientset.Clientset { + return nil +} + +func (j *testData) NodeRegistration() *joindata.NodeRegistration { + return j.joinNodeData +} + +func (j *testData) IgnorePreflightErrors() sets.String { + return nil +} + +func (j *testData) CaCertHashes() []string { + return nil +} + +func (j *testData) NodeLabels() map[string]string { + return nil +} + +func (j *testData) KubernetesResourceServer() string { + return "" +} + +func (j *testData) ReuseCNIBin() bool { + return false +} + +func (j *testData) Namespace() string { + return "" +} + +func TestAddYurthubStaticYaml(t *testing.T) { + xdata := testData{ + joinNodeData: &joindata.NodeRegistration{ + Name: "name1", + NodePoolName: "nodePool1", + CRISocket: "", + WorkingMode: "edge", + Organizations: "", + }} + + tests := []struct { + name string + data testData + podManifestPath string + wantErr bool + }{ + { + name: "test", + data: xdata, + podManifestPath: "/etc/kubernetes/manifest", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := AddYurthubStaticYaml(&tt.data, tt.podManifestPath); (err != nil) != tt.wantErr { + t.Errorf("AddYurthubStaticYaml() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}