diff --git a/controllers/pkg/config/global.go b/controllers/pkg/config/global.go new file mode 100644 index 00000000000..52011b230d1 --- /dev/null +++ b/controllers/pkg/config/global.go @@ -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 +} diff --git a/controllers/terminal/config/manager/manager.yaml b/controllers/terminal/config/manager/manager.yaml index 84ef1fc5718..042d7f1e56c 100644 --- a/controllers/terminal/config/manager/manager.yaml +++ b/controllers/terminal/config/manager/manager.yaml @@ -52,6 +52,7 @@ spec: - /manager args: - --leader-elect + - --config-file-path=/config.yaml image: controller:latest name: manager imagePullPolicy: Always @@ -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 \ No newline at end of file diff --git a/controllers/terminal/controllers/config.go b/controllers/terminal/controllers/config.go new file mode 100644 index 00000000000..413951773a6 --- /dev/null +++ b/controllers/terminal/controllers/config.go @@ -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"` +} diff --git a/controllers/terminal/controllers/ingress.go b/controllers/terminal/controllers/ingress.go index 562607ed066..3e341f1f34c 100644 --- a/controllers/terminal/controllers/ingress.go +++ b/controllers/terminal/controllers/ingress.go @@ -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, @@ -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{ diff --git a/controllers/terminal/controllers/terminal_controller.go b/controllers/terminal/controllers/terminal_controller.go index 8707f56645e..2a989e70fda 100644 --- a/controllers/terminal/controllers/terminal_controller.go +++ b/controllers/terminal/controllers/terminal_controller.go @@ -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 @@ -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/controller-runtime@v0.12.1/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{} @@ -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) @@ -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 { @@ -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{}). diff --git a/controllers/terminal/deploy/manifests/deploy.yaml.tmpl b/controllers/terminal/deploy/manifests/deploy.yaml.tmpl index 2a693192fa8..b6e7b28b8d2 100644 --- a/controllers/terminal/deploy/manifests/deploy.yaml.tmpl +++ b/controllers/terminal/deploy/manifests/deploy.yaml.tmpl @@ -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 @@ -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 @@ -470,4 +471,8 @@ spec: operator: In values: - controller-manager - topologyKey: "kubernetes.io/hostname" \ No newline at end of file + topologyKey: "kubernetes.io/hostname" + volumes: + - name: terminal-controller-volume + configMap: + name: terminal-controller-config \ No newline at end of file diff --git a/controllers/terminal/main.go b/controllers/terminal/main.go index 8fde2254209..c54a11ca5ad 100644 --- a/controllers/terminal/main.go +++ b/controllers/terminal/main.go @@ -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 @@ -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, } @@ -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)