feat(kubernetes): implement Kubernetes client wrapper #137
No reviewers
Labels
No labels
Compat/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Status
Abandoned
Status
Blocked
Status
Need More Info
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
thwap/thwap-pagesd!137
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/kubernetes-wrapper"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Implement the
KubernetesServiceinterface from task #32 onClientininternal/kubernetesusing client-gonetworking/v1Ingress resources:CustomDomainwhen set, otherwise<name>.<base_domain>; path/→ backend service<name>; setsingressClassName./preview/<branch>to the<name>-<branch>service on the site host (path-based routing).New(clientset, namespace, ingressClass, baseDomain)takes an injected clientset for testability;var _ KubernetesService = (*Client)(nil)assertion.Config: added
base_domainandingress_classtoKubernetesConfig(defaultspages.example.com/nginx).Dependencies: promoted
k8s.io/apiandk8s.io/apimachineryto direct deps; vendored the client-go fake package (needed for tests).Tests: client-go
fake.NewSimpleClientset()(no cluster needed) — default vs custom host, ingress class, backend service, preview path/backend, upsert behavior, delete idempotency, andPublishSitenot-implemented.Why
Phase 4 task #51 — the Kubernetes layer used by site registration (#67), the webhook receiver (#73), and preview deployments (Phase 6).
Testing
go build ./...go vet ./...go test -race ./internal/kubernetes/— 9 tests passgofmt -l ./cmd ./internal ./pkgcleangolangci-lint run— 0 issuesBreaking Changes
None.
Notes
PublishSiteintentionally returns not-implemented until the Kubernetes resource templates task (#55) defines the Deployment/Service templates.Closes #47
Closes #48
Closes #49
Closes #50
Closes #51
Review Summary
Verified locally on the PR head (
afea3b2):go build,go vet,go mod verify,gofmt(incl../pkg),golangci-lint runall clean; all 9 kubernetes tests pass with-race. The injected-clientset design and upsert/idempotent-delete patterns are correct, and config wiring (base_domain,ingress_class) is consistent with defaults and tests.Blocking Issue
internal/kubernetes/kubernetes.go— preview resource names embed the raw branch name, producing invalid Kubernetes object names for thetest/*branch pattern this feature targets (roadmap task #49). For branchtest/foo:blog-preview-test/foo(previewIngressName, used by bothCreatePreviewIngressandDeletePreview).blog-test/foo(previewIngress→serviceBackend).Both fail
validation.IsDNS1123Subdomain(verified empirically — the/is invalid). The fake clientset performs no name validation, so the tests pass while a real API server rejects the create.Fix: sanitize the branch when deriving resource names — e.g. replace
/(and any other non-[a-z0-9-]characters) with-inpreviewIngressNameand the preview service name. The ingress path (/preview/test/foo) can keep the raw branch and is fine. Add a test using a slashed branch liketest/foo.Non-blocking Suggestions
stringPtr/pathTypePtrreinventptr.To(stdlib, Go 1.18+) — minor cleanup.CreateSiteIngress/CreatePreviewIngressset no labels or owner references on the Ingress — fine now, but labels would help future cleanup/selection.<name>.<base_domain>) and ingress names assumesite.Nameis already DNS-safe — worth a validation note at registration time (Phase 5).The blocking item is a small fix in the name-derivation helpers.
Review changes addressed in
3d2323e:Blocking issue — added
sanitizeName, which replaces characters outside[a-z0-9-]with-(and lowercases), applied when deriving the preview Ingress name and backend Service name. The routing path still preserves the raw branch (/preview/test/foo). Regression tests added:TestCreatePreviewIngressSlashedBranch,TestDeletePreviewSlashedBranch, andTestSanitizeName.Non-blocking #1 — replaced
stringPtr/pathTypePtrhelpers withk8s.io/utils/ptr.To.Verified:
go build,go vet,gofmt,golangci-lint runclean; all 11 kubernetes tests pass with-race.k8s.io/utilspromoted to a direct dependency.Review Summary
The blocking issue from the previous review is resolved.
sanitizeNamenow lowercases and replaces any character outside[a-z0-9-]with-, applied to bothpreviewIngressNameand the preview backend Service name. Verified: branchtest/foonow yields valid k8s object namesblog-preview-test-fooandblog-test-foo(passIsDNS1123Subdomain), while the ingress path/preview/test/fookeeps the raw branch for routing.TestCreatePreviewIngressSlashedBranch,TestDeletePreviewSlashedBranch, and table-drivenTestSanitizeNameexercise the previously-broken path.ptr.Tocleanup —stringPtr/pathTypePtrreplaced with stdlib-styleptr.To(k8s.io/utils promoted to a direct dep), as suggested.Verified locally on the PR head (
3d2323e):go build,go vet,go mod verify,gofmt,golangci-lint runall clean; all 10 kubernetes tests pass with-race.No blocking issues. Approving.