Files
cozystack/internal/controller/tenantgateway/crdcel_test.go
T
Aleksei Sviridkin d624049a3a feat(gateway): allow tlsPassthroughListeners under dns01 and existingSecret
The refusal existed because Cilium 1.19.5 served every Gateway port
from one Envoy listener matched by SNI alone, so the "*.<apex>"
terminate listener of the wildcard modes and a passthrough listener
under the apex could reach each other's backend. The shipped Cilium
1.20.2 gives each port its own Envoy listener when SNI overlaps across
ports, and Gateway API treats listeners on different ports as
compatible, so the pair is served as declared. edge still refuses the
field: it renders no TLS listener for an entry to bind to.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-09 17:53:25 +03:00

918 lines
37 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tenantgateway
import (
"context"
"fmt"
"os"
"strings"
"testing"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextvalidation "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/validation"
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
schemacel "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel"
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema/listtype"
apiservervalidation "k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
celconfig "k8s.io/apiserver/pkg/apis/cel"
"sigs.k8s.io/yaml"
gatewayv1alpha1 "github.com/cozystack/cozystack/api/gateway/v1alpha1"
)
// crdPath is the generated CRD the controller ships. The CEL rules under
// test are compiled from the +kubebuilder:validation:XValidation markers
// on TenantGatewaySpec, so reading the generated file (rather than
// hand-writing the rules here) is what makes this test detect a marker
// that silently failed to generate.
const crdPath = "../../../packages/system/cozystack-controller/definitions/gateway.cozystack.io_tenantgateways.yaml"
// admissionCheck bundles the two validators the apiserver applies to a
// spec: the structural schema (types, pattern, minimum/maximum,
// maxItems) and the compiled CEL rules. Both must be run to answer "is
// this write accepted" — the constraints under test are split across
// the two, and checking only one silently ignores half of them.
type admissionCheck struct {
cel *schemacel.Validator
structural *schema.Structural
schema apiservervalidation.SchemaValidator
}
// v1alpha1SpecSchema returns the generated CRD's v1alpha1 spec schema,
// failing the test when no such version carries one.
//
// Every caller reads a marker off this schema to prove the marker
// generated. Searching for the version inline instead puts the
// assertions inside the search loop, where a renamed or dropped version
// skips the body and the test reports green having checked nothing —
// the precise regression these tests exist to catch. Returning through
// one helper that fails on absence makes that outcome unreachable.
func v1alpha1SpecSchema(t *testing.T) *apiextensionsv1.JSONSchemaProps {
t.Helper()
raw, err := os.ReadFile(crdPath)
if err != nil {
t.Fatalf("read CRD: %v", err)
}
var crd apiextensionsv1.CustomResourceDefinition
if err := yaml.Unmarshal(raw, &crd); err != nil {
t.Fatalf("unmarshal CRD: %v", err)
}
for i := range crd.Spec.Versions {
v := &crd.Spec.Versions[i]
if v.Name != "v1alpha1" || v.Schema == nil || v.Schema.OpenAPIV3Schema == nil {
continue
}
if p, ok := v.Schema.OpenAPIV3Schema.Properties["spec"]; ok {
return &p
}
}
t.Fatal("v1alpha1 spec schema not found in CRD")
return nil
}
// declaredCertModes returns the certMode values the shipped CRD's enum
// declares. A test that must hold for every mode reads the set from the
// CRD rather than restating it, so a mode added to the API is put
// through that test without anyone remembering to add it.
func declaredCertModes(t *testing.T) map[gatewayv1alpha1.CertMode]struct{} {
t.Helper()
specProps := v1alpha1SpecSchema(t)
certMode, ok := specProps.Properties["certMode"]
if !ok {
t.Fatal("spec schema carries no certMode property")
}
if len(certMode.Enum) == 0 {
t.Fatal("certMode carries no enum; the Enum marker did not generate")
}
declared := map[gatewayv1alpha1.CertMode]struct{}{}
for _, raw := range certMode.Enum {
var mode string
if err := yaml.Unmarshal(raw.Raw, &mode); err != nil {
t.Fatalf("unmarshal certMode enum value %q: %v", string(raw.Raw), err)
}
declared[gatewayv1alpha1.CertMode(mode)] = struct{}{}
}
return declared
}
// specValidator compiles the spec schema the way the apiserver
// evaluates an incoming object: structural validation plus the CEL
// rules against the per-request runtime budget.
//
// This does NOT check whether the apiserver would accept the CRD in the
// first place. Install-time cost estimation is a separate code path
// with a separate budget, and a CRD can pass everything here while
// being refused on install. TestCRDPassesInstallTimeValidation covers
// that.
func specValidator(t *testing.T) *admissionCheck {
t.Helper()
specProps := v1alpha1SpecSchema(t)
if len(specProps.XValidations) == 0 {
t.Fatal("spec schema carries no x-kubernetes-validations; the XValidation markers did not generate")
}
return checkFromProps(t, specProps)
}
// checkFromProps compiles one spec schema into the validators admission
// runs. Split out from specValidator so a test can compile a modified
// copy of the shipped schema and compare the two behaviours.
func checkFromProps(t *testing.T, specProps *apiextensionsv1.JSONSchemaProps) *admissionCheck {
t.Helper()
var internal apiextensions.JSONSchemaProps
if err := apiextensionsv1.Convert_v1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(specProps, &internal, nil); err != nil {
t.Fatalf("convert schema: %v", err)
}
structural, err := schema.NewStructural(&internal)
if err != nil {
t.Fatalf("structural schema: %v", err)
}
schemaValidator, _, err := apiservervalidation.NewSchemaValidator(&internal)
if err != nil {
t.Fatalf("schema validator: %v", err)
}
return &admissionCheck{
cel: schemacel.NewValidator(structural, true, celconfig.PerCallLimit),
structural: structural,
schema: schemaValidator,
}
}
// rejects reports whether the apiserver would refuse this spec, running
// the structural schema and the CEL rules the same way admission does.
func (a *admissionCheck) rejects(t *testing.T, spec map[string]any) bool {
t.Helper()
if errs := apiservervalidation.ValidateCustomResource(field.NewPath("spec"), spec, a.schema); len(errs) > 0 {
return true
}
errs, _ := a.cel.Validate(context.TODO(), field.NewPath("spec"), a.structural, spec, nil, celconfig.RuntimeCELCostBudget)
return len(errs) > 0
}
// TestSpecCELMatchesControllerValidation pins that the admission-time
// CEL rules and the controller's validateTLSPassthroughListeners agree
// on every case the CEL rules cover: reserved ports, duplicate ports,
// out-of-apex hostnames, names colliding with tlsPassthroughServices, and
// the cert modes that refuse the field outright.
//
// The two must not drift. CEL keeps a bad spec out of etcd so it never
// aborts the reconcile chain; the Go check still has to reject objects
// admitted before the rules existed. If one side stops rejecting a case
// the other rejects, this test says so.
//
// The check runs the structural schema alongside the CEL rules, because
// the constraints are split across both layers and the split is an
// implementation detail the caller should not have to know: hostname
// format rides on the field's pattern, the reserved-port and apex rules
// on CEL. Asserting only one layer is how the hostname-format gap went
// unnoticed — every malformed hostname is inside the apex, so the CEL
// rule waves it through and only the pattern stops it.
func TestSpecCELMatchesControllerValidation(t *testing.T) {
a := specValidator(t)
const apex = "foo.example.com"
type listener struct {
name string
port int32
host string
}
tests := []struct {
name string
listeners []listener
services []string
certMode gatewayv1alpha1.CertMode
wantReject bool
}{
{
name: "http01 accepts a passthrough listener",
listeners: []listener{{"postgres", 5432, "postgres.foo.example.com"}},
certMode: gatewayv1alpha1.CertModeHTTP01,
},
{
name: "dns01 accepts a passthrough listener",
listeners: []listener{{"postgres", 5432, "postgres.foo.example.com"}},
certMode: gatewayv1alpha1.CertModeDNS01,
},
{
name: "existingSecret accepts a passthrough listener",
listeners: []listener{{"postgres", 5432, "postgres.foo.example.com"}},
certMode: gatewayv1alpha1.CertModeExistingSecret,
},
{
name: "edge refuses a passthrough listener",
listeners: []listener{{"postgres", 5432, "postgres.foo.example.com"}},
certMode: gatewayv1alpha1.CertModeEdge,
wantReject: true,
},
{
name: "distinct native ports within apex",
listeners: []listener{{"postgres", 5432, "postgres.foo.example.com"}, {"mysql", 3306, "mysql.foo.example.com"}},
},
{
name: "wildcard hostname under apex",
listeners: []listener{{"kafka", 9092, "*.kafka.foo.example.com"}},
},
{
name: "hostname equal to apex",
listeners: []listener{{"pg", 5432, apex}},
},
{
name: "port 443 is reserved",
listeners: []listener{{"pg", 443, "pg.foo.example.com"}},
wantReject: true,
},
{
name: "port 80 is reserved",
listeners: []listener{{"pg", 80, "pg.foo.example.com"}},
wantReject: true,
},
{
name: "duplicate port across listeners",
listeners: []listener{{"pg", 5432, "pg.foo.example.com"}, {"pg2", 5432, "pg2.foo.example.com"}},
wantReject: true,
},
{
name: "hostname outside the apex",
listeners: []listener{{"pg", 5432, "pg.evil.example.com"}},
wantReject: true,
},
{
name: "sibling domain must not pass the suffix test",
listeners: []listener{{"pg", 5432, "pg.evilfoo.example.com"}},
wantReject: true,
},
{
name: "name collides with a passthrough service",
listeners: []listener{{"api", 5432, "api2.foo.example.com"}},
services: []string{"api"},
wantReject: true,
},
// The service entry itself is the subject of these two rows.
// Every other row passes services well-formed or nil, so a
// disagreement about THIS field's syntax was invisible to the
// parity check the test exists to be.
{
// A multi-label entry renders sectionName tls-s3.storage,
// which Gateway API's own SectionName pattern accepts, so
// nothing here may refuse it.
name: "multi-label passthrough service is accepted",
listeners: nil,
services: []string{"s3.storage"},
wantReject: false,
},
{
name: "malformed passthrough service is rejected",
listeners: nil,
services: []string{"API_X"},
wantReject: true,
},
{
// 250 characters: valid as a DNS subdomain and inside
// SectionName's own 253, but the entry is not what becomes
// the SectionName — "tls-" + entry is, and that is 254.
// Accepting it renders a listener name the apiserver
// refuses, and the refusal takes the whole Gateway with it.
name: "passthrough service too long once the tls- prefix is added",
listeners: nil,
services: []string{strings.Repeat("a", 250)},
wantReject: true,
},
// Hostname format is carried by the Pattern on the field, not
// by CEL, but it belongs in this table for the same reason the
// CEL rules do: the apex rule is a plain suffix test, so
// without the pattern each of these typos is within the apex,
// passes admission, and is caught only by the controller —
// after the object is already in etcd and the reconcile chain
// behind it has aborted.
{
name: "underscore in hostname",
listeners: []listener{{"pg", 5432, "pg_main.foo.example.com"}},
wantReject: true,
},
{
name: "upper-case in hostname",
listeners: []listener{{"pg", 5432, "PG.foo.example.com"}},
wantReject: true,
},
{
name: "leading dash in hostname label",
listeners: []listener{{"pg", 5432, "-pg.foo.example.com"}},
wantReject: true,
},
{
name: "wildcard not in the left-most label",
listeners: []listener{{"pg", 5432, "*.*.foo.example.com"}},
wantReject: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
celList := make([]any, 0, len(tc.listeners))
goList := make([]gatewayv1alpha1.TLSPassthroughListener, 0, len(tc.listeners))
for _, l := range tc.listeners {
celList = append(celList, map[string]any{
"name": l.name, "port": int64(l.port), "hostname": l.host,
})
goList = append(goList, gatewayv1alpha1.TLSPassthroughListener{
Name: l.name, Port: l.port, Hostname: l.host,
})
}
spec := map[string]any{
"apex": apex,
"tlsPassthroughListeners": celList,
}
if tc.services != nil {
svcs := make([]any, 0, len(tc.services))
for _, s := range tc.services {
svcs = append(svcs, s)
}
spec["tlsPassthroughServices"] = svcs
}
// Left out when the row does not name one, so the rows that
// predate the cert-mode rule keep exercising the others with
// the field absent.
if tc.certMode != "" {
spec["certMode"] = string(tc.certMode)
}
gotCEL := a.rejects(t, spec)
if gotCEL != tc.wantReject {
t.Errorf("CEL rejected=%v, want %v", gotCEL, tc.wantReject)
}
gotGo := validateTLSPassthroughListeners(goList, tc.services, apex) != nil ||
validatePassthroughListenerCertMode(goList, tc.certMode) != nil
if gotGo != tc.wantReject {
t.Errorf("controller validation rejected=%v, want %v", gotGo, tc.wantReject)
}
if gotCEL != gotGo {
t.Errorf("CEL and controller disagree: CEL rejected=%v, controller rejected=%v", gotCEL, gotGo)
}
})
}
}
// TestEveryCertModeIsJudgedOnPassthroughListeners walks the certMode
// enum the shipped CRD declares and requires an explicit verdict for
// each value on whether that mode may carry tlsPassthroughListeners.
//
// The verdicts live in a table here rather than being read off either
// implementation, so a mode added to the enum without a decision fails
// this test instead of reaching a cluster. That is the failure mode the
// table exists for: both layers refuse the field by naming the modes
// that cannot carry it, and a mode nobody adds to those lists is
// admitted by default, silently dropped by the renderer, and reported
// Ready.
func TestEveryCertModeIsJudgedOnPassthroughListeners(t *testing.T) {
// true means the mode renders TLS listeners a passthrough entry can
// sit beside. The entry has a port of its own, so the wildcard
// terminate listener of dns01 and existingSecret on 443 SNI-overlaps
// it without sharing a port, which Gateway API treats as compatible.
// edge renders no TLS listener at all.
verdicts := map[gatewayv1alpha1.CertMode]bool{
gatewayv1alpha1.CertModeHTTP01: true,
gatewayv1alpha1.CertModeDNS01: true,
gatewayv1alpha1.CertModeExistingSecret: true,
gatewayv1alpha1.CertModeEdge: false,
}
declared := declaredCertModes(t)
for mode := range declared {
if _, ok := verdicts[mode]; !ok {
t.Errorf("certMode %q is in the CRD enum with no verdict here; decide whether it can carry tlsPassthroughListeners and refuse it in both layers if it cannot", mode)
}
}
for mode := range verdicts {
if _, ok := declared[mode]; !ok {
t.Errorf("certMode %q has a verdict here but is not in the CRD enum", mode)
}
}
a := specValidator(t)
const apex = "foo.example.com"
listeners := []gatewayv1alpha1.TLSPassthroughListener{{Name: "postgres", Port: 5432, Hostname: "postgres." + apex}}
for mode := range declared {
want, ok := verdicts[mode]
if !ok {
continue
}
t.Run(string(mode), func(t *testing.T) {
spec := map[string]any{
"apex": apex,
"certMode": string(mode),
"tlsPassthroughListeners": []any{
map[string]any{"name": "postgres", "port": int64(5432), "hostname": "postgres." + apex},
},
}
if gotCEL := !a.rejects(t, spec); gotCEL != want {
t.Errorf("CEL accepts certMode %q with a passthrough listener = %v, want %v", mode, gotCEL, want)
}
if gotGo := validatePassthroughListenerCertMode(listeners, mode) == nil; gotGo != want {
t.Errorf("controller accepts certMode %q with a passthrough listener = %v, want %v", mode, gotGo, want)
}
})
}
// An object written before certMode existed carries no value and is
// served by the http01 path, so both layers must treat the absent
// field the way they treat http01.
t.Run("absent certMode", func(t *testing.T) {
spec := map[string]any{
"apex": apex,
"tlsPassthroughListeners": []any{
map[string]any{"name": "postgres", "port": int64(5432), "hostname": "postgres." + apex},
},
}
if a.rejects(t, spec) {
t.Error("CEL refuses a passthrough listener on an object with no certMode; that object is served as http01")
}
if err := validatePassthroughListenerCertMode(listeners, ""); err != nil {
t.Errorf("controller refuses a passthrough listener with no certMode: %v", err)
}
})
}
// TestCRDPassesInstallTimeValidation runs the validation kube-apiserver
// applies when the CRD itself is written. It is a different budget from
// the per-request one the other tests exercise: the install-time
// estimator assumes the largest value each declared type permits, so an
// unbounded string or list inside a CEL rule blows the estimate even
// though every real object is small.
//
// What fails it is not an object but the CRD, on CREATE and on UPDATE
// alike. The CRD ships in the cozystack-controller chart, so that
// freezes the whole TenantGateway API across every cluster, fresh or
// upgraded. No other test in this suite touches that code path, which
// leaves the size bounds on spec.apex and spec.tlsPassthroughServices
// with no other guard.
func TestCRDPassesInstallTimeValidation(t *testing.T) {
raw, err := os.ReadFile(crdPath)
if err != nil {
t.Fatalf("read CRD: %v", err)
}
var v1crd apiextensionsv1.CustomResourceDefinition
if err := yaml.Unmarshal(raw, &v1crd); err != nil {
t.Fatalf("unmarshal CRD: %v", err)
}
// A CRD manifest on disk has no status; validation demands at least
// one stored version, so supply what the apiserver would have
// written. Without this the run fails on an unrelated status error
// and tells us nothing about cost.
v1crd.Status.StoredVersions = []string{"v1alpha1"}
var internal apiextensions.CustomResourceDefinition
if err := apiextensionsv1.Convert_v1_CustomResourceDefinition_To_apiextensions_CustomResourceDefinition(&v1crd, &internal, nil); err != nil {
t.Fatalf("convert CRD: %v", err)
}
for _, e := range apiextvalidation.ValidateCustomResourceDefinition(context.TODO(), &internal) {
t.Errorf("apiserver would reject this CRD: %v", e)
}
}
// TestTightenedConstraintsOnExistingObjects pins that every constraint
// added to an already-shipped field is one the apiserver ratchets, so an
// object whose stored value violates one keeps accepting writes that
// leave that value alone. The package README states this, and stating it
// in prose alone is how a claim drifts from the schema it describes.
//
// Ratcheting is on by default from Kubernetes 1.30 and the management
// cluster requires 1.33, so this holds on every supported cluster.
//
// It reaches list-type errors too, and at a coarser grain than the
// per-field ratchet: ValidateUpdate checks the incoming object's
// list-type semantics only when the stored object passes them, for the
// object as a whole. That is read from
// pkg/registry/customresource/strategy.go in apiextensions-apiserver
// v0.37.1, the version go.mod pins. So a stored duplicate does not block writes, it
// stops every list on the resource from being checked. The duplicate
// row below therefore stays admissible whether or not the field carries
// a marker, which is why the marker's absence is pinned by the schema
// assertion rather than by that row.
//
// The run pairs each field with the listtype validator as well, because
// the schema validator does not implement list semantics and would call
// a duplicate admissible whether or not the marker were there.
func TestTightenedConstraintsOnExistingObjects(t *testing.T) {
a := specValidator(t)
// One character past the apex bound. A bare run of letters rather
// than a plausible hostname, so the row can only fail on length —
// the field carries no format pattern to fail on instead, and
// pinning ratcheting is not the place to discover that.
overLongApex := strings.Repeat("a", 254)
// One past the count bound, so the row fails on maxItems and on
// nothing else: every entry is a well-formed DNS-1123 subdomain.
overCapServices := make([]string, 0, 63)
for i := range 63 {
overCapServices = append(overCapServices, fmt.Sprintf("svc%d", i))
}
spec := func(apex string, services ...string) map[string]any {
svcs := make([]any, 0, len(services))
for _, s := range services {
svcs = append(svcs, s)
}
return map[string]any{
"apex": apex,
"certMode": "http01",
"gatewayClassName": "cilium",
"tlsPassthroughServices": svcs,
}
}
// rejectsUpdate answers what the apiserver does to a write of new
// over old: the schema layer with ratcheting on, then the list-type
// layer. The list-type ratchet lives in the caller rather than in
// the validator: customresource.ValidateUpdate runs the check on the
// incoming object only when the stored one already passes it, so the
// guard is reproduced here rather than exercised. Dropping it would
// model an apiserver that refuses writes it accepts. Reproduced
// means it does not track upstream, so a bump of
// apiextensions-apiserver past the pinned v0.37.1 is where to
// re-read strategy.go and confirm the shape still holds.
rejectsUpdate := func(newSpec, oldSpec map[string]any) bool {
if errs := apiservervalidation.ValidateCustomResourceUpdate(
field.NewPath("spec"), newSpec, oldSpec, a.schema,
apiservervalidation.WithRatcheting(nil),
); len(errs) > 0 {
return true
}
if oldErrs := listtype.ValidateListSetsAndMaps(field.NewPath("spec"), a.structural, oldSpec); len(oldErrs) > 0 {
return false
}
return len(listtype.ValidateListSetsAndMaps(field.NewPath("spec"), a.structural, newSpec)) > 0
}
for _, tc := range []struct {
name string
newSpec, old map[string]any
wantRejected bool
}{{
name: "over-long apex left untouched",
newSpec: spec(overLongApex, "api"),
old: spec(overLongApex, "api"),
wantRejected: false,
}, {
name: "over-long apex introduced by this write",
newSpec: spec(overLongApex, "api"),
old: spec("foo.example.com", "api"),
wantRejected: true,
}, {
name: "over-long service list left untouched",
newSpec: spec("foo.example.com", overCapServices...),
old: spec("foo.example.com", overCapServices...),
wantRejected: false,
}, {
name: "over-long service list introduced by this write",
newSpec: spec("foo.example.com", overCapServices...),
old: spec("foo.example.com", "api"),
wantRejected: true,
}, {
name: "malformed service entry left untouched",
newSpec: spec("foo.example.com", "API_X"),
old: spec("foo.example.com", "API_X"),
wantRejected: false,
}, {
name: "malformed service entry introduced by this write",
newSpec: spec("foo.example.com", "API_X"),
old: spec("foo.example.com", "api"),
wantRejected: true,
}, {
name: "repeated service entry stays admissible",
newSpec: spec("foo.example.com", "api", "api"),
old: spec("foo.example.com", "api", "api"),
wantRejected: false,
}, {
name: "unique service entries",
newSpec: spec("foo.example.com", "api", "vm-exportproxy"),
old: spec("foo.example.com", "api", "vm-exportproxy"),
wantRejected: false,
}} {
t.Run(tc.name, func(t *testing.T) {
if got := rejectsUpdate(tc.newSpec, tc.old); got != tc.wantRejected {
t.Errorf("update rejected = %v, want %v", got, tc.wantRejected)
}
})
}
}
// TestListTypeSetWouldNotRefuseAStoredDuplicate pins why
// tlsPassthroughServices carries no listType marker, by compiling a copy
// of the shipped schema that does and showing what the apiserver would
// then do.
//
// The reason is not that a stored duplicate would be refused on every
// write. It would not: customresource.ValidateUpdate validates the
// incoming object's list-type semantics only when the stored object
// passes them, so a duplicate already in etcd is ratcheted through.
// That guard is byte-identical across apiextensions-apiserver v0.31.1,
// v0.35.0, v0.36.3 and the pinned v0.37.1, so it is settled behaviour
// rather than a version quirk, but it is upstream's to change. The
// reason is the grain of that ratchet. It is taken over the whole
// object, not per field, so one stored duplicate stops list-type
// validation for every list on the resource, silently and with nothing
// on status to say so, while the controller reports the duplicate by
// name.
//
// Compiled from a copy rather than asserted in prose because the claim
// is about apiserver behaviour under a marker this repo deliberately
// does not ship, which no other test can reach.
func TestListTypeSetWouldNotRefuseAStoredDuplicate(t *testing.T) {
shipped := v1alpha1SpecSchema(t)
if lt := shipped.Properties["tlsPassthroughServices"].XListType; lt != nil {
t.Fatalf("tlsPassthroughServices ships x-kubernetes-list-type=%q; the field is meant to stay unmarked", *lt)
}
withSet := shipped.DeepCopy()
svc := withSet.Properties["tlsPassthroughServices"]
setType := "set"
svc.XListType = &setType
withSet.Properties["tlsPassthroughServices"] = svc
a := checkFromProps(t, withSet)
spec := func(certMode string, services ...string) map[string]any {
svcs := make([]any, 0, len(services))
for _, e := range services {
svcs = append(svcs, e)
}
return map[string]any{
"apex": "foo.example.com",
"certMode": certMode,
"tlsPassthroughServices": svcs,
}
}
// The guard customresource.ValidateUpdate puts around the list-type
// check: the incoming object is checked only when the stored one
// passes. Reproduced rather than called, because the guard lives in
// the registry strategy and the validator it wraps is what this
// package can reach.
rejects := func(newSpec, oldSpec map[string]any) bool {
if errs := listtype.ValidateListSetsAndMaps(field.NewPath("spec"), a.structural, oldSpec); len(errs) > 0 {
return false
}
return len(listtype.ValidateListSetsAndMaps(field.NewPath("spec"), a.structural, newSpec)) > 0
}
if !rejects(spec("http01", "api", "api"), spec("http01", "api")) {
t.Error("a set marker accepted a write introducing a duplicate; the copy did not compile the marker")
}
if rejects(spec("dns01", "api", "api"), spec("http01", "api", "api")) {
t.Error("a stored duplicate blocked an unrelated write; the marker does not work the way the field comment used to say")
}
// The spillover that settles the decision: the guard is whole-object,
// so the same stored duplicate must also ratchet a duplicate name key
// through tlsPassthroughListeners, the listType=map list this
// resource cannot afford to lose — one bad entry in the unmarked
// list would switch every list's check off at once.
withListeners := func(s map[string]any, names ...string) map[string]any {
ls := make([]any, 0, len(names))
for _, n := range names {
ls = append(ls, map[string]any{"name": n, "port": int64(5432), "hostname": n + ".foo.example.com"})
}
s["tlsPassthroughListeners"] = ls
return s
}
if !rejects(withListeners(spec("http01", "api"), "pg", "pg"), spec("http01", "api")) {
t.Error("a duplicate tlsPassthroughListeners name over a clean stored object was not refused; the copy did not compile the listType=map marker")
}
if rejects(withListeners(spec("http01", "api", "api"), "pg", "pg"), spec("http01", "api", "api")) {
t.Error("a stored tlsPassthroughServices duplicate did not ratchet a duplicate tlsPassthroughListeners name through; the whole-object grain the field comment describes is not what the apiserver does")
}
}
// TestApexAcceptsUpperCaseAtCreate pins that spec.apex carries no
// format pattern, so a tenant whose host holds upper case can still
// have its TenantGateway created.
//
// The value arrives verbatim. The gateway chart writes spec.apex from
// the namespace.cozystack.io/host label, the tenant chart writes that
// label from spec.host, and the tenant values schema bounds host with
// nothing but its type, so an upper-case host reaches this field.
//
// Admitting it is not a claim that it works. renderGateway composes
// listener hostnames from the apex and Gateway API refuses upper case
// in them, so that tenant's Gateway does not render either way. What a
// pattern would change is only where the failure lands: refused at the
// write it fails the gateway HelmRelease, admitted it is Ready=False on
// the one Gateway. The bound is length alone for that reason, and the
// apex is judged by the controller when this field is in use.
//
// Create is the path that matters and the ratcheting test cannot reach
// it: ratcheting compares against a stored value, so it says nothing
// about the write that creates the object.
func TestApexAcceptsUpperCaseAtCreate(t *testing.T) {
a := specValidator(t)
if pattern := v1alpha1SpecSchema(t).Properties["apex"].Pattern; pattern != "" {
t.Errorf("spec.apex carries pattern %q; a mixed-case tenant host is refused at the write", pattern)
}
for _, spec := range []map[string]any{
{"apex": "Foo.Example.com"},
{"apex": "Case-Probe.foo.example.com", "tlsPassthroughServices": []any{"api"}},
} {
if a.rejects(t, spec) {
t.Errorf("spec %v rejected at create, want admitted", spec)
}
}
}
// TestPassthroughServiceCapFitsGatewayAPI pins that a tlsPassthroughServices
// list filled to the schema's maxItems is one the controller can render.
//
// The bound belongs to the same budget as the listener one and is read
// off the generated CRD for the same reason, but it needs its own run:
// the two fields are bounded by separate markers and only this one is
// reachable from chart values today, so a list the apiserver accepts
// and the renderer refuses is a shape an operator can actually reach.
// The rendered total is the port-80 listener plus one per entry, so a
// bound at the Gateway API cap itself is one over what a Gateway holds.
func TestPassthroughServiceCapFitsGatewayAPI(t *testing.T) {
const gatewayAPIListenerCap = 64
servicesSchema := v1alpha1SpecSchema(t).Properties["tlsPassthroughServices"]
if servicesSchema.MaxItems == nil {
t.Fatal("tlsPassthroughServices has no maxItems; the cap is unbounded")
}
maxItems := *servicesSchema.MaxItems
services := make([]string, 0, maxItems)
for i := range maxItems {
services = append(services, fmt.Sprintf("svc%d", i))
}
tgw := &gatewayv1alpha1.TenantGateway{
ObjectMeta: metav1.ObjectMeta{Name: "cozystack", Namespace: "tenant-foo"},
Spec: gatewayv1alpha1.TenantGatewaySpec{
Apex: "foo.example.com",
CertMode: gatewayv1alpha1.CertModeHTTP01,
GatewayClassName: "cilium",
TLSPassthroughServices: services,
},
}
r := &Reconciler{Scheme: newScheme(t)}
// One published app, matching the headroom the listener cap leaves,
// so the two fields are bounded against the same tenant.
gw, err := r.renderGateway(tgw, []string{"app.foo.example.com"}, nil)
if err != nil {
t.Fatalf("renderGateway at maxItems=%d: %v", maxItems, err)
}
if got := len(gw.Spec.Listeners); got > gatewayAPIListenerCap {
t.Errorf("maxItems=%d renders %d listeners, over the Gateway API cap of %d", maxItems, got, gatewayAPIListenerCap)
}
}
// TestPassthroughListenerNameUniquenessIsEnforcedBySchema pins the
// listType=map / listMapKey=name markers.
//
// Name uniqueness is the one rule with no CEL equivalent and no entry
// in the parity table — the field doc and the parity test both point at
// these markers as the reason. Nothing else asserts they are there:
// the per-object harness does not implement list-map semantics, so
// dropping the markers leaves the whole suite green while duplicate
// names become admissible, render two tls-<name> listeners, and get the
// Gateway rejected wholesale.
func TestPassthroughListenerNameUniquenessIsEnforcedBySchema(t *testing.T) {
listenersSchema := v1alpha1SpecSchema(t).Properties["tlsPassthroughListeners"]
if listenersSchema.XListType == nil || *listenersSchema.XListType != "map" {
t.Fatalf("tlsPassthroughListeners x-kubernetes-list-type=%v, want map; duplicate names would be admissible", listenersSchema.XListType)
}
if len(listenersSchema.XListMapKeys) != 1 || listenersSchema.XListMapKeys[0] != "name" {
t.Fatalf("tlsPassthroughListeners x-kubernetes-list-map-keys=%v, want [name]", listenersSchema.XListMapKeys)
}
}
// TestPassthroughListenerCapFitsGatewayAPI pins that a spec filled to
// the schema's maxItems, on a tenant publishing one app, still renders
// within Gateway API's 64-listener cap. The bound is read from the
// generated CRD so raising the marker without re-checking the
// arithmetic fails here rather than in a cluster.
//
// This is a bound on one field's contribution, not proof the total
// always fits — the rendered count also grows with published hostnames
// and passthrough services, and a tenant can exceed 64 with far fewer
// entries than the cap. TestRenderGatewayRejectsOverListenerCap covers
// the total.
func TestPassthroughListenerCapFitsGatewayAPI(t *testing.T) {
const gatewayAPIListenerCap = 64
listenersSchema := v1alpha1SpecSchema(t).Properties["tlsPassthroughListeners"]
if listenersSchema.MaxItems == nil {
t.Fatal("tlsPassthroughListeners has no maxItems; the cap is unbounded")
}
maxItems := *listenersSchema.MaxItems
listeners := make([]gatewayv1alpha1.TLSPassthroughListener, 0, maxItems)
for i := range maxItems {
listeners = append(listeners, gatewayv1alpha1.TLSPassthroughListener{
Name: fmt.Sprintf("db%d", i),
Port: int32(10000 + i),
Hostname: fmt.Sprintf("db%d.foo.example.com", i),
})
}
tgw := &gatewayv1alpha1.TenantGateway{
ObjectMeta: metav1.ObjectMeta{Name: "cozystack", Namespace: "tenant-foo"},
Spec: gatewayv1alpha1.TenantGatewaySpec{
Apex: "foo.example.com",
CertMode: gatewayv1alpha1.CertModeHTTP01,
GatewayClassName: "cilium",
TLSPassthroughListeners: listeners,
},
}
r := &Reconciler{Scheme: newScheme(t)}
gw, err := r.renderGateway(tgw, []string{"app.foo.example.com"}, nil)
if err != nil {
t.Fatalf("renderGateway: %v", err)
}
if got := len(gw.Spec.Listeners); got > gatewayAPIListenerCap {
t.Errorf("maxItems=%d renders %d listeners, over the Gateway API cap of %d", maxItems, got, gatewayAPIListenerCap)
}
}
// TestRenderGatewayRejectsOverListenerCap pins the total-count guard.
// Each individual field is bounded, but the Gateway's 64 slots are
// shared between the port-80 listener, per-hostname HTTPS listeners,
// passthrough services and passthrough listeners — so a spec where
// every field is within its own cap can still overflow. Without the
// guard the overflow surfaces as the apiserver rejecting the whole
// Gateway, which drops every app's HTTPS listener and reports nothing
// on the TenantGateway the tenant actually manages.
func TestRenderGatewayRejectsOverListenerCap(t *testing.T) {
listeners := make([]gatewayv1alpha1.TLSPassthroughListener, 0, 62)
for i := range 62 {
listeners = append(listeners, gatewayv1alpha1.TLSPassthroughListener{
Name: fmt.Sprintf("db%d", i),
Port: int32(10000 + i),
Hostname: fmt.Sprintf("db%d.foo.example.com", i),
})
}
tgw := &gatewayv1alpha1.TenantGateway{
ObjectMeta: metav1.ObjectMeta{Name: "cozystack", Namespace: "tenant-foo"},
Spec: gatewayv1alpha1.TenantGatewaySpec{
Apex: "foo.example.com",
CertMode: gatewayv1alpha1.CertModeHTTP01,
GatewayClassName: "cilium",
TLSPassthroughListeners: listeners,
},
}
r := &Reconciler{Scheme: newScheme(t)}
// 1 http + 62 passthrough + 2 published hostnames = 65.
_, err := r.renderGateway(tgw, []string{"a.foo.example.com", "b.foo.example.com"}, nil)
if err == nil {
t.Fatal("expected an error for a 65-listener Gateway, got nil")
}
if !strings.Contains(err.Error(), "over the Gateway API cap") {
t.Errorf("error does not name the budget: %v", err)
}
// One fewer published hostname lands exactly on the cap.
if _, err := r.renderGateway(tgw, []string{"a.foo.example.com"}, nil); err != nil {
t.Errorf("64 listeners should be accepted, got %v", err)
}
}
// TestSpecCELAcceptsEmptyPassthroughListeners guards the has() guards
// themselves: every rule is written to short-circuit when the optional
// field is absent, so a spec that never mentions tlsPassthroughListeners
// must pass. Dropping a "!has(...)" prefix would reject every existing
// TenantGateway in the cluster on its next write.
func TestSpecCELAcceptsEmptyPassthroughListeners(t *testing.T) {
a := specValidator(t)
for _, spec := range []map[string]any{
{"apex": "foo.example.com"},
{"apex": "foo.example.com", "tlsPassthroughServices": []any{"api"}},
{"apex": "foo.example.com", "tlsPassthroughListeners": []any{}},
} {
if a.rejects(t, spec) {
t.Errorf("spec %v was rejected, want accepted", spec)
}
}
}