handler_test.go 12.4 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 KubeSphere Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

17 18 19 20
package v1alpha1

import (
	"bytes"
Z
zryfish 已提交
21 22
	"encoding/json"
	"fmt"
23 24 25 26
	"github.com/google/go-cmp/cmp"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/printers"
Z
zryfish 已提交
27
	k8sfake "k8s.io/client-go/kubernetes/fake"
28 29 30
	"kubesphere.io/kubesphere/pkg/apis/cluster/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
	"kubesphere.io/kubesphere/pkg/informers"
Z
zryfish 已提交
31 32 33 34 35
	"kubesphere.io/kubesphere/pkg/version"
	"net/http"
	"net/http/httptest"
	"net/url"
	"sigs.k8s.io/controller-runtime/pkg/envtest"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	"testing"
)

const (
	proxyAddress = "http://139.198.121.121:8080"
	agentImage   = "kubesphere/tower:v1.0"
	proxyService = "tower.kubesphere-system.svc"
)

var cluster = &v1alpha1.Cluster{
	ObjectMeta: metav1.ObjectMeta{
		Name: "gondor",
	},
	Spec: v1alpha1.ClusterSpec{
		Connection: v1alpha1.Connection{
			Type:  v1alpha1.ConnectionTypeProxy,
			Token: "randomtoken",
		},
	},
}

var service = &corev1.Service{
	ObjectMeta: metav1.ObjectMeta{
		Name:      "tower",
		Namespace: "kubesphere-system",
	},
	Spec: corev1.ServiceSpec{
		Ports: []corev1.ServicePort{
			{
				Port:     8080,
				Protocol: corev1.ProtocolTCP,
			},
		},
	},
	Status: corev1.ServiceStatus{
		LoadBalancer: corev1.LoadBalancerStatus{
			Ingress: []corev1.LoadBalancerIngress{
				{
					IP:       "139.198.121.121",
					Hostname: "foo.bar",
				},
			},
		},
	},
}

var expected = `apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  name: cluster-agent
Z
zryfish 已提交
87
  namespace: kubesphere-system
88
spec:
Z
zryfish 已提交
89 90 91 92
  selector:
    matchLabels:
      app: agent
      app.kubernetes.io/part-of: tower
93 94 95 96
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
Z
zryfish 已提交
97 98 99
      labels:
        app: agent
        app.kubernetes.io/part-of: tower
100 101 102 103 104 105 106
    spec:
      containers:
      - command:
        - /agent
        - --name=gondor
        - --token=randomtoken
        - --proxy-server=http://139.198.121.121:8080
107
        - --keepalive=10s
108 109
        - --kubesphere-service=ks-apiserver.kubesphere-system.svc:80
        - --kubernetes-service=kubernetes.default.svc:443
110
        - --v=0
111 112 113 114 115 116 117 118 119 120 121 122 123 124
        image: kubesphere/tower:v1.0
        name: agent
        resources:
          limits:
            cpu: "1"
            memory: 200M
          requests:
            cpu: 100m
            memory: 100M
      serviceAccountName: kubesphere
status: {}
`

func TestGeranteAgentDeployment(t *testing.T) {
Z
zryfish 已提交
125
	k8sclient := k8sfake.NewSimpleClientset(service)
126 127
	ksclient := fake.NewSimpleClientset(cluster)

Z
Zack Zhang 已提交
128
	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil)
129 130 131 132

	informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Informer().GetIndexer().Add(service)
	informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster)

Z
zryfish 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	directConnectionCluster := cluster.DeepCopy()
	directConnectionCluster.Spec.Connection.Type = v1alpha1.ConnectionTypeDirect

	var testCases = []struct {
		description    string
		expectingError bool
		expectedError  error
		cluster        *v1alpha1.Cluster
		expected       string
	}{
		{
			description:    "test normal case",
			expectingError: false,
			expected:       expected,
			cluster:        cluster,
		},
		{
			description:    "test direct connection cluster",
			expectingError: true,
Z
zryfish 已提交
152
			expectedError:  errClusterConnectionIsNotProxy,
Z
zryfish 已提交
153 154
			cluster:        directConnectionCluster,
		},
155 156
	}

Z
zryfish 已提交
157 158 159
	for _, testCase := range testCases {

		t.Run(testCase.description, func(t *testing.T) {
160
			h := newHandler(informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Lister(),
Z
zryfish 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
				informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Lister(),
				proxyService,
				"",
				agentImage)

			var buf bytes.Buffer

			err := h.populateProxyAddress()
			if err != nil {
				t.Error(err)
			}

			err = h.generateDefaultDeployment(testCase.cluster, &buf)
			if testCase.expectingError {
				if err == nil {
					t.Fatalf("expecting error %v, got nil", testCase.expectedError)
				} else if err != testCase.expectedError {
					t.Fatalf("expecting error %v, got %v", testCase.expectedError, err)
				}
			}
Z
zryfish 已提交
181 182 183 184

			if diff := cmp.Diff(testCase.expected, buf.String()); len(diff) != 0 {
				t.Errorf("%T, got +, expected -, %s", testCase.expected, diff)
			}
Z
zryfish 已提交
185
		})
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}
}

func TestInnerGenerateAgentDeployment(t *testing.T) {
	h := &handler{
		proxyAddress: proxyAddress,
		agentImage:   agentImage,
		yamlPrinter:  &printers.YAMLPrinter{},
	}

	var buf bytes.Buffer

	err := h.generateDefaultDeployment(cluster, &buf)

	if err != nil {
		t.Error(err)
	}

	if diff := cmp.Diff(buf.String(), expected); len(diff) != 0 {
		t.Error(diff)
	}

}
Z
zryfish 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

var base64EncodedKubeConfig = `
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKYzNCb1pYSmxNQjRYRFRJd01EVXlOekEzTWpFME1Gb1hEVE13TURVeU5UQTNNakUwTUZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYTndhR1Z5WlRDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTWVxCjRwNldVQVJmeUtRcEpZM1ZGcEVhT3Y5REZBQkZhQXBpbUswQTBBL05KV25yaGl0MjhnL3ZXMno5bmxkeHQwZzgKc1J0ZEp4TUxmOHF5WkEramZudU5jUDBLUTJ5a3VxZE41c29MdE1TUmt1K1dHZFNkWlJvTVpEakdKbHRSUEdVRQpnOHd6OE9zdWRMcmZ5Zlcxdy8vUFRPWnRLNmsrNUhQZGtuWU5KcU9UZGJDTEVma2RZbFB1ZXNZTTFKamRacXlNClJQRDM1RXpUSEowR05jYlBzbUlyZ05WZGR4Nmh5RmIxTFZ0QXRqa0tWY2lNR1k1UlFTQWlQdVVGaGQreUcrOHUKUWlqQlgvV09ESlFOelJCQWFPdWRWdURqSkhIZ0lBV3FzcC9qSllWQkdRYnNad3djTTRUSEJPb2k3N1krYVR3SAphcHRaMitVMzgwbFY4d0tJR3NFQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFDVjY0VVpCZkcxc1d4ampJWTZ5Vm0rdzhxTFgKS0ZYanR3VElGb1Y3WENOT2RDbEQ0a2NsN1pyTnl5UEtUaTFOV3BDbUVzSXZPVjRSRXV6a2ZWZ2Rzc0tHL2dYVwpGNGV2dStZTFh1UEk1RHJFejNGaW5OMGxVcFNZMVg3b1Y5N2JyU1lmdE53aWJQbUVFTEVHbWJvMnNHS3VoL21BCjRJZ0tmZklqWVdSYjE3TlZLb2s0am1WTnhkMmNCL09GeFlsY2xndlc2THpxc1BDdnpWbDdDRWErRElyNHZLamgKRlhvOERXejMzclUySCs0RjNMOThzWGE1OWNITWZPb1kzZ3pzUE5LYnQwbWsxeTNOUmZocTZYTTJXemkrUFZkcAppbUVqUlY4UUl5c3Zhc25sTjFIY1FtcWtMaFdLYlRoOEc5MGhiTzlwdTFRNE0wMmZ3STg0ZVlSUWZNUT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: 127.0.0.1:6578
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJZkcyYVlKYnRYT2N3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWE53YUdWeVpUQWVGdzB5TURBMU1qY3dOekl4TkRCYUZ3MHpNREExTWpVd056SXhOREJhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXRiR3JpeFBxOWF6QU1wNGsKM0VsaElmanhOZXhkY2VORGJENkFMMXVGSWJUMk1uVnRXeFFXOU5ueVFwVU9NMzN4clNJMkJlVW1uU0U5RlR6cAo1bTBmaGVBOGht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S0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
`

func TestValidateKubeConfig(t *testing.T) {
233 234 235
	k8sclient := k8sfake.NewSimpleClientset(service)
	ksclient := fake.NewSimpleClientset(cluster)

Z
Zack Zhang 已提交
236
	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil)
237 238 239 240 241 242 243 244 245 246

	informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Informer().GetIndexer().Add(service)
	informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster)

	h := newHandler(informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Lister(),
		informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Lister(),
		proxyService,
		"",
		agentImage)

Z
zryfish 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260
	config, err := loadKubeConfigFromBytes([]byte(base64EncodedKubeConfig))
	if err != nil {
		t.Fatal(err)
	}

	// config.Host is schemaless, we need to add schema manually
	u, err := url.Parse(fmt.Sprintf("http://%s", config.Host))
	if err != nil {
		t.Fatal(err)
	}

	// we need to specify apiserver port to match above kubeconfig
	env := &envtest.Environment{
		Config: config,
H
hongming 已提交
261 262
		ControlPlane: envtest.ControlPlane{
			APIServer: &envtest.APIServer{
Z
zryfish 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
				Args: envtest.DefaultKubeAPIServerFlags,
				URL:  u,
			},
		},
	}

	cfg, err := env.Start()
	if err != nil {
		t.Log(cfg)
		t.Fatal(err)
	}

	defer func() {
		_ = env.Stop()
	}()

279
	err = h.validateKubeConfig([]byte(base64EncodedKubeConfig))
Z
zryfish 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	if err != nil {
		t.Fatal(err)
	}
}

var ver = version.Get()

func endpoint(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	_ = json.NewEncoder(w).Encode(ver)
}

func TestValidateKubeSphereEndpoint(t *testing.T) {
	svr := httptest.NewServer(http.HandlerFunc(endpoint))
	defer svr.Close()

Z
zryfish 已提交
296
	got, err := validateKubeSphereAPIServer(svr.URL, nil)
Z
zryfish 已提交
297 298 299 300 301 302 303 304 305
	if err != nil {
		t.Fatal(err)
	}

	if diff := cmp.Diff(&ver, got); len(diff) != 0 {
		t.Errorf("%T +got, -expected %v", ver, diff)
	}

}