From e29ddb89768d89db59bb3d000b765d7b8b085ec8 Mon Sep 17 00:00:00 2001 From: yy Date: Mon, 22 Jan 2024 14:57:55 +0800 Subject: [PATCH] chore: support terminal using random service name. Signed-off-by: yy --- controllers/terminal/api/v1/terminal_types.go | 1 + .../crd/bases/terminal.sealos.io_terminals.yaml | 17 +++-------------- .../terminal/config/manager/kustomization.yaml | 2 +- controllers/terminal/config/rbac/role.yaml | 14 -------------- .../terminal/controllers/terminal_controller.go | 12 ++++++++++-- .../terminal/deploy/manifests/deploy.yaml.tmpl | 17 +++++++++++++++++ 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/controllers/terminal/api/v1/terminal_types.go b/controllers/terminal/api/v1/terminal_types.go index eb0e4a46277..49701dd8bcf 100644 --- a/controllers/terminal/api/v1/terminal_types.go +++ b/controllers/terminal/api/v1/terminal_types.go @@ -55,6 +55,7 @@ type TerminalSpec struct { // TerminalStatus defines the observed state of Terminal type TerminalStatus struct { AvailableReplicas int32 `json:"availableReplicas"` + ServiceName string `json:"serviceName"` Domain string `json:"domain"` } diff --git a/controllers/terminal/config/crd/bases/terminal.sealos.io_terminals.yaml b/controllers/terminal/config/crd/bases/terminal.sealos.io_terminals.yaml index f0d45325507..418987b8a82 100644 --- a/controllers/terminal/config/crd/bases/terminal.sealos.io_terminals.yaml +++ b/controllers/terminal/config/crd/bases/terminal.sealos.io_terminals.yaml @@ -1,17 +1,3 @@ -# Copyright © 2023 sealos. -# -# 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. - --- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition @@ -103,9 +89,12 @@ spec: type: integer domain: type: string + serviceName: + type: string required: - availableReplicas - domain + - serviceName type: object type: object served: true diff --git a/controllers/terminal/config/manager/kustomization.yaml b/controllers/terminal/config/manager/kustomization.yaml index 4c03a9cddd8..7d4d5945300 100644 --- a/controllers/terminal/config/manager/kustomization.yaml +++ b/controllers/terminal/config/manager/kustomization.yaml @@ -27,4 +27,4 @@ kind: Kustomization images: - name: controller newName: ghcr.io/labring/sealos-terminal-controller - newTag: dev + newTag: latest diff --git a/controllers/terminal/config/rbac/role.yaml b/controllers/terminal/config/rbac/role.yaml index ac95f56f8ff..5a23e6c1857 100644 --- a/controllers/terminal/config/rbac/role.yaml +++ b/controllers/terminal/config/rbac/role.yaml @@ -1,17 +1,3 @@ -# Copyright © 2023 sealos. -# -# 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. - --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole diff --git a/controllers/terminal/controllers/terminal_controller.go b/controllers/terminal/controllers/terminal_controller.go index addd0bc56fa..a6b7f600eaa 100644 --- a/controllers/terminal/controllers/terminal_controller.go +++ b/controllers/terminal/controllers/terminal_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "k8s.io/apimachinery/pkg/util/rand" "os" "time" @@ -129,6 +130,13 @@ func (r *TerminalReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c return ctrl.Result{}, nil } + if terminal.Status.ServiceName == "" { + terminal.Status.ServiceName = terminal.Name + "-svc" + rand.String(5) + if err := r.Status().Update(ctx, terminal); err != nil { + return ctrl.Result{}, err + } + } + var hostname string if err := r.syncDeployment(ctx, terminal, &hostname); err != nil { logger.Error(err, "create deployment failed") @@ -194,7 +202,7 @@ func (r *TerminalReconciler) syncService(ctx context.Context, terminal *terminal labelsMap := buildLabelsMap(terminal) expectService := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: terminal.Name, + Name: terminal.Status.ServiceName, Namespace: terminal.Namespace, }, Spec: corev1.ServiceSpec{ @@ -208,7 +216,7 @@ func (r *TerminalReconciler) syncService(ctx context.Context, terminal *terminal service := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: terminal.Name, + Name: terminal.Status.ServiceName, Namespace: terminal.Namespace, }, } diff --git a/controllers/terminal/deploy/manifests/deploy.yaml.tmpl b/controllers/terminal/deploy/manifests/deploy.yaml.tmpl index aafb0416887..08916cfc873 100644 --- a/controllers/terminal/deploy/manifests/deploy.yaml.tmpl +++ b/controllers/terminal/deploy/manifests/deploy.yaml.tmpl @@ -91,9 +91,12 @@ spec: type: integer domain: type: string + serviceName: + type: string required: - availableReplicas - domain + - serviceName type: object type: object served: true @@ -321,6 +324,20 @@ subjects: apiVersion: v1 data: controller_manager_config.yaml: | + # Copyright © 2023 sealos. + # + # 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. + apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 kind: ControllerManagerConfig health: