diff --git a/plugins/scheduler-k3s/portmap.go b/plugins/scheduler-k3s/portmap.go index e4aa03807..6e012d888 100644 --- a/plugins/scheduler-k3s/portmap.go +++ b/plugins/scheduler-k3s/portmap.go @@ -25,27 +25,27 @@ func (p PortMap) IsAllowedHttps() bool { return p.Scheme == "https" || p.ContainerPort == 443 } -func getPortMaps(appName string) ([]PortMap, error) { +func getPortMaps(appName string) (map[string]PortMap, error) { portMaps := []PortMap{} + allowedMappings := map[string]PortMap{} output, err := common.PlugnTriggerOutputAsString("ports-get", []string{appName, "json"}...) if err != nil { - return portMaps, err + return allowedMappings, err } err = json.Unmarshal([]byte(output), &portMaps) if err != nil { - return portMaps, err + return allowedMappings, err } - allowedMappings := []PortMap{} for _, portMap := range portMaps { if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() { // todo: log warning continue } - allowedMappings = append(allowedMappings, portMap) + allowedMappings[portMap.String()] = portMap } return allowedMappings, nil diff --git a/plugins/scheduler-k3s/template.go b/plugins/scheduler-k3s/template.go index 766623326..5b0d94e81 100644 --- a/plugins/scheduler-k3s/template.go +++ b/plugins/scheduler-k3s/template.go @@ -139,11 +139,12 @@ type ProcessCron struct { } type ProcessPortMap struct { - ContainerPort int32 `yaml:"container_port"` - HostPort int32 `yaml:"host_port"` - Scheme string `yaml:"scheme"` - Protocol PortmapProtocol `yaml:"protocol"` - Name string `yaml:"name"` + ContainerPort int32 `yaml:"container_port"` + HostPort int32 `yaml:"host_port"` + Scheme string `yaml:"scheme"` + Protocol PortmapProtocol `yaml:"protocol"` + Name string `yaml:"name"` + RedirectToHttps bool `yaml:"redirect_to_https"` } type PortmapProtocol string diff --git a/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml b/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml new file mode 100644 index 000000000..4365c713c --- /dev/null +++ b/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml @@ -0,0 +1,15 @@ +--- +apiVersion: traefik.io/v1alpha1 +kind: Middleware +metadata: + annotations: + dokku.com/managed: "true" + labels: + app.kubernetes.io/instance: {{ $.Values.global.app_name }}-redirect-to-https + app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + name: {{ $.Values.global.app_name}}-redirect-to-https + namespace: {{ $.Values.global.namespace }} +spec: + redirectScheme: + scheme: https + permanent: true diff --git a/plugins/scheduler-k3s/templates/chart/ingress-route.yaml b/plugins/scheduler-k3s/templates/chart/ingress-route.yaml index 1efefa8e8..1061eb114 100644 --- a/plugins/scheduler-k3s/templates/chart/ingress-route.yaml +++ b/plugins/scheduler-k3s/templates/chart/ingress-route.yaml @@ -16,14 +16,20 @@ metadata: namespace: {{ $.Values.global.namespace }} spec: entryPoints: - - web - {{- if and $config.web.tls (eq $port_map.scheme "https") }} + {{- if and $config.web.tls.enabled (eq $port_map.scheme "https") }} - websecure + {{- else }} + - web {{- end }} routes: {{- range $ddx, $domain := $config.web.domains }} - kind: Rule match: Host(`{{ $domain }}`) + {{- if and $config.web.tls.enabled (and (eq $port_map.scheme "http") $port_map.redirect_to_https) }} + middlewares: + - name: {{ $.Values.global.app_name}}-redirect-to-https + namespace: traefik + {{- end }} services: - name: {{ $.Values.global.app_name }}-{{ $processName }} namespace: {{ $.Values.global.namespace }} @@ -31,7 +37,7 @@ spec: port: {{ $port_map.name }} scheme: http {{- end }} - {{- if and $config.web.tls (eq $port_map.scheme "https") }} + {{- if and $config.web.tls.enabled (eq $port_map.scheme "https") }} tls: secretName: tls-{{ $.Values.global.app_name }}-{{ $processName }} {{- end }} diff --git a/plugins/scheduler-k3s/triggers.go b/plugins/scheduler-k3s/triggers.go index da93f8037..aed80782e 100644 --- a/plugins/scheduler-k3s/triggers.go +++ b/plugins/scheduler-k3s/triggers.go @@ -372,18 +372,25 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e IssuerName: issuerName, }, } + processValues.ProcessType = ProcessType_Web for _, portMap := range portMaps { protocol := PortmapProtocol_TCP if portMap.Scheme == "udp" { protocol = PortmapProtocol_UDP } + + redirecToHttps := false + if _, ok := portMaps[fmt.Sprintf("https-443-%d", portMap.ContainerPort)]; ok && portMap.Scheme == "http" { + redirecToHttps = true + } processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{ - ContainerPort: portMap.ContainerPort, - HostPort: portMap.HostPort, - Name: portMap.String(), - Protocol: protocol, - Scheme: portMap.Scheme, + ContainerPort: portMap.ContainerPort, + HostPort: portMap.HostPort, + Name: portMap.String(), + Protocol: protocol, + RedirectToHttps: redirecToHttps, + Scheme: portMap.Scheme, }) } @@ -393,7 +400,11 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e values.Processes[processType] = processValues - for _, templateName := range []string{"deployment", "service", "certificate", "ingress-route"} { + templateFiles := []string{"deployment"} + if processType == "web" { + templateFiles = append(templateFiles, "service", "certificate", "ingress-route", "https-redirect-middleware") + } + for _, templateName := range templateFiles { b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName)) if err != nil { return fmt.Errorf("Error reading %s template: %w", templateName, err)