feat: support manually managed cert issuers on k3s

The `cert-issuer-name` and `cert-issuer-kind` properties point an app's generated `Certificate` at a cert-manager issuer created outside of Dokku, allowing certificates to be issued through solvers the built-in letsencrypt integration cannot use, such as `dns01` for wildcard certificates. Setting an issuer enables https on its own, as a manually managed issuer has no email for Dokku to configure. An imported certificate still takes precedence, and `letsencrypt-server false` remains the single off switch. Dokku warns before a build starts when the referenced issuer is absent from the cluster, without blocking the deploy. Wildcard domains no longer collide with their apex domain when generating ingress names, and `letsencrypt-server` values are now validated when set rather than at deploy time.
This commit is contained in:
Jose Diaz-Gonzalez
2026-08-08 15:16:12 -04:00
parent 2da48f4f86
commit d134e75371
14 changed files with 980 additions and 42 deletions

View File

@@ -1,8 +1,10 @@
package scheduler_k3s
import (
"strings"
"testing"
"github.com/dokku/dokku/plugins/common"
corev1 "k8s.io/api/core/v1"
)
@@ -357,3 +359,325 @@ func TestParseSysctls(t *testing.T) {
})
}
}
func TestNormalizeCertIssuerKind(t *testing.T) {
cases := []struct {
name string
value string
want string
wantErr bool
}{
{name: "empty is left unset", value: "", want: ""},
{name: "canonical issuer", value: "Issuer", want: CertIssuerKindIssuer},
{name: "canonical cluster issuer", value: "ClusterIssuer", want: CertIssuerKindClusterIssuer},
{name: "lowercase issuer is canonicalized", value: "issuer", want: CertIssuerKindIssuer},
{name: "lowercase cluster issuer is canonicalized", value: "clusterissuer", want: CertIssuerKindClusterIssuer},
{name: "uppercase issuer is canonicalized", value: "ISSUER", want: CertIssuerKindIssuer},
{name: "unknown kind", value: "Certificate", wantErr: true},
{name: "typo", value: "ClusterIssue", wantErr: true},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := normalizeCertIssuerKind(tc.value)
if tc.wantErr {
if err == nil {
t.Fatalf("normalizeCertIssuerKind(%q) expected an error, got %q", tc.value, got)
}
return
}
if err != nil {
t.Fatalf("normalizeCertIssuerKind(%q) unexpected error: %v", tc.value, err)
}
if got != tc.want {
t.Errorf("normalizeCertIssuerKind(%q) = %q, want %q", tc.value, got, tc.want)
}
})
}
}
func TestValidateCertIssuerName(t *testing.T) {
cases := []struct {
name string
value string
wantErr bool
}{
{name: "empty unsets the property", value: ""},
{name: "reserved disabled value", value: CertIssuerNameDisabled},
{name: "simple name", value: "acme-dns"},
{name: "dotted name", value: "my.issuer.example"},
{name: "underscores are not legal kubernetes names", value: "my_own_issuer_name", wantErr: true},
{name: "uppercase is not a legal kubernetes name", value: "AcmeDNS", wantErr: true},
{name: "leading dash", value: "-acme-dns", wantErr: true},
{name: "trailing dash", value: "acme-dns-", wantErr: true},
{name: "too long", value: strings.Repeat("a", 254), wantErr: true},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := validateCertIssuerName(tc.value)
if tc.wantErr && err == nil {
t.Fatalf("validateCertIssuerName(%q) expected an error", tc.value)
}
if !tc.wantErr && err != nil {
t.Fatalf("validateCertIssuerName(%q) unexpected error: %v", tc.value, err)
}
})
}
}
func TestValidateLetsencryptServer(t *testing.T) {
cases := []struct {
name string
value string
wantErr bool
}{
{name: "empty unsets the property", value: ""},
{name: "prod", value: "prod"},
{name: "production", value: "production"},
{name: "stag", value: "stag"},
{name: "staging", value: "staging"},
{name: "disabled", value: LetsencryptServerDisabled},
{name: "typo", value: "prodd", wantErr: true},
{name: "issuer name is not a server", value: "my-own-issuer", wantErr: true},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := validateLetsencryptServer(tc.value)
if tc.wantErr && err == nil {
t.Fatalf("validateLetsencryptServer(%q) expected an error", tc.value)
}
if !tc.wantErr && err != nil {
t.Fatalf("validateLetsencryptServer(%q) unexpected error: %v", tc.value, err)
}
})
}
}
func TestDomainSlug(t *testing.T) {
cases := []struct {
name string
domain string
want string
}{
{name: "plain domain", domain: "example.com", want: "example-com"},
{name: "wildcard domain keeps a distinct slug", domain: "*.example.com", want: "wildcard-example-com"},
{name: "subdomain", domain: "app.example.com", want: "app-example-com"},
{name: "wildcard subdomain", domain: "*.app.example.com", want: "wildcard-app-example-com"},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := domainSlug(tc.domain); got != tc.want {
t.Errorf("domainSlug(%q) = %q, want %q", tc.domain, got, tc.want)
}
})
}
}
func TestDomainSlugDistinguishesWildcardFromApex(t *testing.T) {
// A kubernetes wildcard host matches exactly one label, so *.example.com does not
// cover example.com and both are commonly added to the same app. The generated
// ingress name is derived from this slug, so a collision would render two objects
// with the same name and fail the helm install.
if domainSlug("*.example.com") == domainSlug("example.com") {
t.Fatalf("domainSlug collides for *.example.com and example.com: %q", domainSlug("example.com"))
}
}
func TestResolveAppTLSConfig(t *testing.T) {
cases := []struct {
name string
appProperties map[string]string
globalProperties map[string]string
importedCertExists bool
want AppTLSConfig
wantErr bool
}{
{
name: "defaults to the shared production ClusterIssuer once a global email is set",
globalProperties: map[string]string{
"letsencrypt-email-prod": "ops@dokku.me",
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindClusterIssuer,
IssuerName: "letsencrypt-prod",
},
},
{
name: "no letsencrypt email leaves tls disabled",
want: AppTLSConfig{
IssuerKind: CertIssuerKindClusterIssuer,
IssuerName: "letsencrypt-prod",
},
},
{
name: "an app level email renders a namespaced Issuer",
appProperties: map[string]string{
"letsencrypt-server": "staging",
"letsencrypt-email-stag": "app@dokku.me",
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindIssuer,
IssuerName: "myapp-letsencrypt-stag",
Issuer: AppIssuer{
Email: "app@dokku.me",
Enabled: true,
IngressClass: DefaultIngressClass,
Name: "myapp-letsencrypt-stag",
Server: LetsencryptServerStag,
},
},
},
{
name: "an imported certificate wins over a custom issuer",
importedCertExists: true,
appProperties: map[string]string{
"cert-issuer-name": "acme-dns",
},
want: AppTLSConfig{
Enabled: true,
UseImportedCert: true,
},
},
{
name: "letsencrypt-server false disables a globally configured custom issuer",
appProperties: map[string]string{
"letsencrypt-server": LetsencryptServerDisabled,
},
globalProperties: map[string]string{
"cert-issuer-name": "acme-dns",
},
want: AppTLSConfig{},
},
{
name: "a custom issuer enables tls with no letsencrypt email anywhere",
appProperties: map[string]string{
"cert-issuer-name": "acme-dns",
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindClusterIssuer,
IssuerName: "acme-dns",
UsesCustomIssuer: true,
},
},
{
name: "a namespaced custom issuer renders no Dokku managed issuer",
appProperties: map[string]string{
"cert-issuer-name": "acme-dns",
"cert-issuer-kind": CertIssuerKindIssuer,
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindIssuer,
IssuerName: "acme-dns",
UsesCustomIssuer: true,
},
},
{
name: "a custom issuer beats a fully configured letsencrypt setup",
appProperties: map[string]string{
"cert-issuer-name": "acme-dns",
"letsencrypt-email-prod": "app@dokku.me",
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindClusterIssuer,
IssuerName: "acme-dns",
UsesCustomIssuer: true,
},
},
{
name: "an app level custom issuer overrides the global one",
appProperties: map[string]string{
"cert-issuer-name": "app-issuer",
},
globalProperties: map[string]string{
"cert-issuer-name": "global-issuer",
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindClusterIssuer,
IssuerName: "app-issuer",
UsesCustomIssuer: true,
},
},
{
name: "the reserved disabled value falls back to letsencrypt",
appProperties: map[string]string{
"cert-issuer-name": CertIssuerNameDisabled,
},
globalProperties: map[string]string{
"cert-issuer-name": "global-issuer",
"letsencrypt-email-prod": "ops@dokku.me",
},
want: AppTLSConfig{
Enabled: true,
IssuerKind: CertIssuerKindClusterIssuer,
IssuerName: "letsencrypt-prod",
},
},
{
name: "an invalid letsencrypt server still errors",
appProperties: map[string]string{
"letsencrypt-server": "prodd",
},
wantErr: true,
},
{
name: "an invalid stored cert issuer kind errors",
appProperties: map[string]string{
"cert-issuer-name": "acme-dns",
"cert-issuer-kind": "Certificate",
},
wantErr: true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
setupReportTest(t, "myapp")
for key, value := range tc.appProperties {
if err := common.PropertyWrite("scheduler-k3s", "myapp", key, value); err != nil {
t.Fatalf("PropertyWrite(%q): %v", key, err)
}
}
for key, value := range tc.globalProperties {
if err := common.PropertyWrite("scheduler-k3s", "--global", key, value); err != nil {
t.Fatalf("PropertyWrite(--global, %q): %v", key, err)
}
}
got, err := resolveAppTLSConfig(ResolveAppTLSConfigInput{
AppName: "myapp",
ImportedCertExists: tc.importedCertExists,
})
if tc.wantErr {
if err == nil {
t.Fatalf("resolveAppTLSConfig() expected an error, got %+v", got)
}
return
}
if err != nil {
t.Fatalf("resolveAppTLSConfig() unexpected error: %v", err)
}
if got != tc.want {
t.Errorf("resolveAppTLSConfig() = %+v, want %+v", got, tc.want)
}
})
}
}