Skip to content

Commit

Permalink
chore: support terminal using random service name. (labring#4499)
Browse files Browse the repository at this point in the history
* chore: support terminal using random service name.

Signed-off-by: yy <[email protected]>

* fix license

* fix fmt

* chore: fix makefile.
Signed-off-by: yy <[email protected]>

Signed-off-by: yy <[email protected]>

* chore: fix ingress backend service name.
Signed-off-by: yy <[email protected]>

Signed-off-by: yy <[email protected]>

* chore: delete duplicate license in deploy.yaml.
Signed-off-by: yy <[email protected]>

Signed-off-by: yy <[email protected]>

---------

Signed-off-by: yy <[email protected]>
  • Loading branch information
lingdie authored Jan 22, 2024
1 parent 0495264 commit 09434a6
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 8 deletions.
8 changes: 6 additions & 2 deletions controllers/terminal/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# Image URL to use all building/pushing image targets
IMG ?= ghcr.io/labring/sealos-terminal-controller:latest
TARGETARCH ?= amd64

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24.1

Expand Down Expand Up @@ -63,15 +65,17 @@ test: manifests generate fmt vet envtest ## Run tests.

.PHONY: build
build: ## Build manager binary.
CGO_ENABLED=0 go build -o bin/manager main.go
CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
mv bin/manager bin/controller-terminal-${TARGETARCH}
chmod +x bin/controller-terminal-${TARGETARCH}
docker build -t ${IMG} . --build-arg TARGETARCH=${TARGETARCH}

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down
1 change: 1 addition & 0 deletions controllers/terminal/api/v1/terminal_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2023 sealos.
# Copyright © 2024 sealos.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,9 +103,12 @@ spec:
type: integer
domain:
type: string
serviceName:
type: string
required:
- availableReplicas
- domain
- serviceName
type: object
type: object
served: true
Expand Down
2 changes: 1 addition & 1 deletion controllers/terminal/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ kind: Kustomization
images:
- name: controller
newName: ghcr.io/labring/sealos-terminal-controller
newTag: dev
newTag: latest
2 changes: 1 addition & 1 deletion controllers/terminal/config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2023 sealos.
# Copyright © 2024 sealos.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion controllers/terminal/controllers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *TerminalReconciler) createNginxIngress(terminal *terminalv1.Terminal, h
Path: "/",
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: terminal.Name,
Name: terminal.Status.ServiceName,
Port: networkingv1.ServiceBackendPort{
Number: 8080,
},
Expand Down
12 changes: 10 additions & 2 deletions controllers/terminal/controllers/terminal_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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{
Expand All @@ -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,
},
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/terminal/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ spec:
type: integer
domain:
type: string
serviceName:
type: string
required:
- availableReplicas
- domain
- serviceName
type: object
type: object
served: true
Expand Down

0 comments on commit 09434a6

Please sign in to comment.