handler_test.go 17.3 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"
Y
yuswift 已提交
21
	"context"
Z
zryfish 已提交
22 23
	"encoding/json"
	"fmt"
Y
yuswift 已提交
24 25 26 27 28
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

29
	"github.com/google/go-cmp/cmp"
Y
yuswift 已提交
30
	appsv1 "k8s.io/api/apps/v1"
31
	corev1 "k8s.io/api/core/v1"
Y
yuswift 已提交
32
	"k8s.io/apimachinery/pkg/api/errors"
33 34
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/printers"
Y
yuswift 已提交
35
	k8s "k8s.io/client-go/kubernetes"
Z
zryfish 已提交
36
	k8sfake "k8s.io/client-go/kubernetes/fake"
Y
yuswift 已提交
37 38 39
	"k8s.io/client-go/tools/clientcmd"
	"sigs.k8s.io/controller-runtime/pkg/envtest"

40 41 42
	"kubesphere.io/kubesphere/pkg/apis/cluster/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
	"kubesphere.io/kubesphere/pkg/informers"
Z
zryfish 已提交
43
	"kubesphere.io/kubesphere/pkg/version"
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 87 88
)

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",
				},
			},
		},
	},
}

Y
yuswift 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
var hostMap = map[string]string{
	"kubesphere.yaml": `
monitoring:
  endpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
authentication:
  jwtSecret: sQh3JOqNbmci6Gu94TeV10AY7ipltwjp
  oauthOptions:
    accessTokenMaxAge: 0s
    accessTokenInactivityTimeout: 0s
`,
}

var memberMap = map[string]string{
	"kubesphere.yaml": `
monitoring:
  endpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
authentication:
  jwtSecret: sQh3JOqNbmci6Gu94TeV10AY7ipltwj
  oauthOptions:
    accessTokenMaxAge: 0s
    accessTokenInactivityTimeout: 0s
`,
}

var hostCm = &corev1.ConfigMap{
	ObjectMeta: metav1.ObjectMeta{
		Namespace: KubesphereNamespace,
		Name:      KubeSphereConfigName,
	},
	Data: hostMap,
}

var memberCm = &corev1.ConfigMap{
	ObjectMeta: metav1.ObjectMeta{
		Namespace: KubesphereNamespace,
		Name:      KubeSphereConfigName,
	},
	Data: memberMap,
}

var ksApiserverDeploy = `
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "labels": {
            "app": "ks-apiserver",
            "tier": "backend",
            "version": "v3.0.0"
        },
        "name": "ks-apiserver",
        "namespace": "kubesphere-system"
    },
    "spec": {
        "replicas": 1,
        "selector": {
            "matchLabels": {
                "app": "ks-apiserver",
                "tier": "backend",
                "version": "v3.0.0"
            }
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app": "ks-apiserver",
                    "tier": "backend",
                    "version": "v3.0.0"
                }
            },
            "spec": {
                "containers": [
                    {
                        "command": [
                            "ks-apiserver",
                            "--logtostderr=true"
                        ],
                        "image": "kubesphere/ks-apiserver:v3.0.0",
                        "name": "ks-apiserver"
                    }
                ]
            }
        }
    }
}`

176 177 178 179 180
var expected = `apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  name: cluster-agent
Z
zryfish 已提交
181
  namespace: kubesphere-system
182
spec:
Z
zryfish 已提交
183 184 185 186
  selector:
    matchLabels:
      app: agent
      app.kubernetes.io/part-of: tower
187 188 189 190
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
Z
zryfish 已提交
191 192 193
      labels:
        app: agent
        app.kubernetes.io/part-of: tower
194 195 196 197 198 199 200
    spec:
      containers:
      - command:
        - /agent
        - --name=gondor
        - --token=randomtoken
        - --proxy-server=http://139.198.121.121:8080
201
        - --keepalive=10s
202 203
        - --kubesphere-service=ks-apiserver.kubesphere-system.svc:80
        - --kubernetes-service=kubernetes.default.svc:443
204
        - --v=0
205 206 207 208 209 210 211 212 213 214 215 216 217 218
        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 已提交
219
	k8sclient := k8sfake.NewSimpleClientset(service)
220 221
	ksclient := fake.NewSimpleClientset(cluster)

J
junotx 已提交
222
	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil, nil)
223 224 225 226

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

Z
zryfish 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	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 已提交
246
			expectedError:  errClusterConnectionIsNotProxy,
Z
zryfish 已提交
247 248
			cluster:        directConnectionCluster,
		},
249 250
	}

Z
zryfish 已提交
251 252 253
	for _, testCase := range testCases {

		t.Run(testCase.description, func(t *testing.T) {
Y
yuswift 已提交
254 255
			h := newHandler(informersFactory.KubernetesSharedInformerFactory(),
				informersFactory.KubeSphereSharedInformerFactory(),
Z
zryfish 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
				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 已提交
275 276 277 278

			if diff := cmp.Diff(testCase.expected, buf.String()); len(diff) != 0 {
				t.Errorf("%T, got +, expected -, %s", testCase.expected, diff)
			}
Z
zryfish 已提交
279
		})
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	}
}

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 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

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: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJZkcyYVlKYnRYT2N3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWE53YUdWeVpUQWVGdzB5TURBMU1qY3dOekl4TkRCYUZ3MHpNREExTWpVd056SXhOREJhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlh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FKdlp0YkFlZll2RXQ4c3JETVZsb1FOWVVKTURxWnBLenpqR0w5S3FqdlFwbjJOL2EwNmsKUzNsMndWWExXNy8xb0RMV2gxZGZLbUJlUlJhZUxJMXIvS3FyeTRjUStiVEIzTzBwS2R3WjFFYXNQajBDTnlaUAp0YnFkOEFWa09pRkFNYlRWaVZuR3RzSnJ3c1V2N1NSTUU4ckloMVJva0ZtRGRpN0J1UVl5emc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
`

func TestValidateKubeConfig(t *testing.T) {
327 328 329
	k8sclient := k8sfake.NewSimpleClientset(service)
	ksclient := fake.NewSimpleClientset(cluster)

J
junotx 已提交
330
	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil, nil)
331 332 333 334

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

Y
yuswift 已提交
335 336
	h := newHandler(informersFactory.KubernetesSharedInformerFactory(),
		informersFactory.KubeSphereSharedInformerFactory(),
337 338 339 340
		proxyService,
		"",
		agentImage)

Z
zryfish 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354
	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 已提交
355 356
		ControlPlane: envtest.ControlPlane{
			APIServer: &envtest.APIServer{
Z
zryfish 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
				Args: envtest.DefaultKubeAPIServerFlags,
				URL:  u,
			},
		},
	}

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

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

373
	err = h.validateKubeConfig([]byte(base64EncodedKubeConfig))
Z
zryfish 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	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 已提交
390
	got, err := validateKubeSphereAPIServer(svr.URL, nil)
Z
zryfish 已提交
391 392 393 394 395 396 397 398 399
	if err != nil {
		t.Fatal(err)
	}

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

}
Y
yuswift 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

func TestValidateMemberClusterConfiguration(t *testing.T) {
	k8sclient := k8sfake.NewSimpleClientset(service)
	ksclient := fake.NewSimpleClientset(cluster)

	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil, nil)

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

	h := newHandler(informersFactory.KubernetesSharedInformerFactory(),
		informersFactory.KubeSphereSharedInformerFactory(),
		proxyService,
		"",
		agentImage)

	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,
		ControlPlane: envtest.ControlPlane{
			APIServer: &envtest.APIServer{
				Args: envtest.DefaultKubeAPIServerFlags,
				URL:  u,
			},
		},
	}

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

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

	addMemberClusterResource(hostCm, t)

	err = h.validateMemberClusterConfiguration([]byte(base64EncodedKubeConfig))
	if err != nil {
		t.Fatal(err)
	}

	addMemberClusterResource(memberCm, t)
	err = h.validateMemberClusterConfiguration([]byte(base64EncodedKubeConfig))
	if err == nil {
		t.Fatal()
	}
	t.Log(err)
}

func addMemberClusterResource(targetCm *corev1.ConfigMap, t *testing.T) {
	con, err := clientcmd.NewClientConfigFromBytes([]byte(base64EncodedKubeConfig))
	if err != nil {
		t.Fatal(err)
	}

	cli, err := con.ClientConfig()
	if err != nil {
		t.Fatal(err)
	}

	c, err := k8s.NewForConfig(cli)
	if err != nil {
		t.Fatal(err)
	}

	_, err = c.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: KubesphereNamespace}}, metav1.CreateOptions{})
	if err != nil && !errors.IsAlreadyExists(err) {
		t.Fatal(err)
	}

	_, err = c.CoreV1().ConfigMaps(KubesphereNamespace).Create(context.Background(), targetCm, metav1.CreateOptions{})
	if err != nil && errors.IsAlreadyExists(err) {
		_, err = c.CoreV1().ConfigMaps(KubesphereNamespace).Update(context.Background(), targetCm, metav1.UpdateOptions{})
		if err != nil {
			t.Fatal(err)
		}
	} else if err != nil {
		t.Fatal(err)
	}

	deploy := appsv1.Deployment{}
	err = json.Unmarshal([]byte(ksApiserverDeploy), &deploy)
	if err != nil {
		t.Fatal(err)
	}

	_, err = c.AppsV1().Deployments(KubesphereNamespace).Create(context.Background(), &deploy, metav1.CreateOptions{})
	if err != nil && !errors.IsAlreadyExists(err) {
		t.Fatal(err)
	}
}