Skip to content

Commit

Permalink
chore: optimize terminal config.
Browse files Browse the repository at this point in the history
Signed-off-by: yy <[email protected]>

Signed-off-by: yy <[email protected]>
  • Loading branch information
lingdie committed Apr 19, 2024
1 parent c3fa80d commit ab77273
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 49 deletions.
42 changes: 42 additions & 0 deletions controllers/pkg/config/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"gopkg.in/yaml.v3"
"io/ioutil"
)

type Config struct {
Global GlobalConfig `yaml:"global"`
}

type GlobalConfig struct {
CloudDomain string `yaml:"cloudDomain"`
CloudPort string `yaml:"cloudPort"`
RegionUid string `yaml:"regionUid"`
CertSecretName string `yaml:"certSecretName"`
Common Common `yaml:"common"`
Database Database `yaml:"database"`
}

type Common struct {
GuildEnabled string `yaml:"guildEnabled"`
ApiEnabled string `yaml:"apiEnabled"`
}

type Database struct {
MongodbUri string `yaml:"mongodbUri"`
GlobalCockroachdbUri string `yaml:"globalCockroachdbUri"`
RegionalCockroachdbUri string `yaml:"regionalCockroachdbUri"`
}

func LoadConfig(path string, target interface{}) error {
configData, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(configData, target)
if err != nil {
return err
}
return nil
}
18 changes: 9 additions & 9 deletions controllers/terminal/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ spec:
- /manager
args:
- --leader-elect
- --config-file-path=/config.yaml
image: controller:latest
name: manager
imagePullPolicy: Always
Expand Down Expand Up @@ -82,14 +83,13 @@ spec:
requests:
cpu: 10m
memory: 64Mi
env:
- name: USER_NAMESPACE
value: "user-system"
- name: DOMAIN
value: "cloud.sealos.io"
- name: SECRET_NAME
value: "wildcard-cloud-sealos-io-cert"
- name: SECRET_NAMESPACE
value: "sealos-system"
volumeMounts:
- name: terminal-controller-volume
mountPath: /config.yaml
subPath: config.yaml
serviceAccountName: controller-manager
terminationGracePeriodSeconds: 10
volumes:
- name: terminal-controller-volume
configMap:
name: terminal-controller-config
14 changes: 14 additions & 0 deletions controllers/terminal/controllers/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package controllers

import (
"github.com/labring/sealos/controllers/pkg/config"
)

type Config struct {
Global config.GlobalConfig `yaml:"global"`
TerminalConfig TerminalConfig `yaml:"terminalController"`
}

type TerminalConfig struct {
IngressTlsSecretName string `yaml:"ingressTlsSecretName"`
}
4 changes: 2 additions & 2 deletions controllers/terminal/controllers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if ($flag = '02'){ return 403; }`
)

func (r *TerminalReconciler) createNginxIngress(terminal *terminalv1.Terminal, host string) *networkingv1.Ingress {
cors := fmt.Sprintf("https://%s,https://*.%s", r.terminalDomain+r.getPort(), r.terminalDomain+r.getPort())
cors := fmt.Sprintf("https://%s,https://*.%s", r.CtrConfig.Global.CloudDomain+r.getPort(), r.CtrConfig.Global.CloudDomain+r.getPort())

objectMeta := metav1.ObjectMeta{
Name: terminal.Name,
Expand Down Expand Up @@ -78,7 +78,7 @@ func (r *TerminalReconciler) createNginxIngress(terminal *terminalv1.Terminal, h

tls := networkingv1.IngressTLS{
Hosts: []string{host},
SecretName: r.secretName,
SecretName: r.CtrConfig.TerminalConfig.IngressTlsSecretName,
}

ingress := &networkingv1.Ingress{
Expand Down
30 changes: 7 additions & 23 deletions controllers/terminal/controllers/terminal_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@ const (
// TerminalReconciler reconciles a Terminal object
type TerminalReconciler struct {
client.Client
Scheme *runtime.Scheme
recorder record.EventRecorder
Config *rest.Config
terminalDomain string
terminalPort string
secretName string
secretNamespace string
Scheme *runtime.Scheme
recorder record.EventRecorder
Config *rest.Config
CtrConfig *Config
}

//+kubebuilder:rbac:groups=terminal.sealos.io,resources=terminals,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -90,15 +87,6 @@ type TerminalReconciler struct {
//+kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Terminal object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *TerminalReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx, "terminal", req.NamespacedName)
terminal := &terminalv1.Terminal{}
Expand Down Expand Up @@ -172,7 +160,7 @@ func (r *TerminalReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

func (r *TerminalReconciler) syncIngress(ctx context.Context, terminal *terminalv1.Terminal, hostname string, recLabels map[string]string) error {
var err error
host := hostname + "." + r.terminalDomain
host := hostname + "." + r.CtrConfig.Global.CloudDomain
switch terminal.Spec.IngressType {
case terminalv1.Nginx:
err = r.syncNginxIngress(ctx, terminal, host, recLabels)
Expand Down Expand Up @@ -417,10 +405,10 @@ func getSecretNamespace() string {
}

func (r *TerminalReconciler) getPort() string {
if r.terminalPort == "" || r.terminalPort == "80" || r.terminalPort == "443" {
if r.CtrConfig.Global.CloudPort == "" || r.CtrConfig.Global.CloudPort == "80" || r.CtrConfig.Global.CloudPort == "443" {
return ""
}
return ":" + r.terminalPort
return ":" + r.CtrConfig.Global.CloudPort
}

func NewCache() cache.NewCacheFunc {
Expand All @@ -443,10 +431,6 @@ func NewCache() cache.NewCacheFunc {
// SetupWithManager sets up the controller with the Manager.
func (r *TerminalReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.recorder = mgr.GetEventRecorderFor("sealos-terminal-controller")
r.terminalDomain = getDomain()
r.terminalPort = getPort()
r.secretName = getSecretName()
r.secretNamespace = getSecretNamespace()
r.Config = mgr.GetConfig()
return ctrl.NewControllerManagedBy(mgr).
For(&terminalv1.Terminal{}).
Expand Down
31 changes: 18 additions & 13 deletions controllers/terminal/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,15 @@ data:
# leaderElectionReleaseOnCancel: true
kind: ConfigMap
metadata:
name: terminal-manager-config
name: terminal-controller-config
namespace: terminal-system
data:
config.yaml: |
global:
cloudDomain: {{ .cloudDomain }}
cloudPort: {{ if .cloudPort }}:{{ .cloudPort }}{{ end }}
terminalController:
ingressTlsSecretName: {{ .wildcardCertSecretName }}
---
apiVersion: v1
kind: Service
Expand Down Expand Up @@ -414,21 +421,15 @@ spec:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
- --leader-elect
- --config-file-path=/config.yaml
command:
- /manager
env:
- name: USER_NAMESPACE
value: {{ .userNamespace }}
- name: DOMAIN
value: {{ .cloudDomain }}
- name: PORT
value: '{{ .cloudPort }}'
- name: SECRET_NAME
value: {{ .wildcardCertSecretName }}
- name: SECRET_NAMESPACE
value: {{ .wildcardCertSecretNamespace }}
image: ghcr.io/labring/sealos-terminal-controller:latest
imagePullPolicy: Always
volumeMounts:
- name: terminal-controller-volume
mountPath: /config.yaml
subPath: config.yaml
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -470,4 +471,8 @@ spec:
operator: In
values:
- controller-manager
topologyKey: "kubernetes.io/hostname"
topologyKey: "kubernetes.io/hostname"
volumes:
- name: terminal-controller-volume
configMap:
name: terminal-controller-config
15 changes: 13 additions & 2 deletions controllers/terminal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

configpkg "github.com/labring/sealos/controllers/pkg/config"
terminalv1 "github.com/labring/sealos/controllers/terminal/api/v1"
"github.com/labring/sealos/controllers/terminal/controllers"
//+kubebuilder:scaffold:imports
Expand All @@ -51,11 +52,13 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var configFilePath string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&configFilePath, "config-file-path", "/config.yaml", "The path of the config file")
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -91,9 +94,17 @@ func main() {
os.Exit(1)
}

// Load the configuration file
config := &controllers.Config{}
if err := configpkg.LoadConfig(configFilePath, config); err != nil {
setupLog.Error(err, "unable to load configuration file")
os.Exit(1)
}

if err = (&controllers.TerminalReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
CtrConfig: config,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Terminal")
os.Exit(1)
Expand Down

0 comments on commit ab77273

Please sign in to comment.