Skip to content

Commit db74976

Browse files
committed
(feat) allow configurable namespace for Sveltos
Historically, the Sveltos namespace has been hardcoded/fixed to projectsveltos. This PR is part of series that will remove this limitation.
1 parent db5d372 commit db74976

26 files changed

Lines changed: 302 additions & 64 deletions

.github/workflows/main.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,18 @@ jobs:
5353
go-version: 1.26.3
5454
- name: fv
5555
run: make create-cluster fv
56+
env:
57+
FV: true
58+
FV-NAMESPACE:
59+
runs-on: ubuntu-latest
60+
steps:
61+
- name: checkout
62+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
63+
- name: Set up Go
64+
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
65+
with:
66+
go-version: 1.26.3
67+
- name: fv
68+
run: make create-cluster-infra fv-namespace
5669
env:
5770
FV: true

Makefile

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ARCH ?= amd64
3636
OS ?= $(shell uname -s | tr A-Z a-z)
3737
K8S_LATEST_VER ?= $(shell curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)
3838
export CONTROLLER_IMG ?= $(REGISTRY)/$(IMAGE_NAME)
39-
TAG ?= v1.10.0
39+
TAG ?= main
4040

4141
## Tool Binaries
4242
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
@@ -50,7 +50,7 @@ KUBECTL := $(TOOLS_BIN_DIR)/kubectl
5050
CLUSTERCTL := $(TOOLS_BIN_DIR)/clusterctl
5151

5252
GOLANGCI_LINT_VERSION := "v2.11.4"
53-
CLUSTERCTL_VERSION := v1.13.1
53+
CLUSTERCTL_VERSION := v1.13.2
5454

5555
KUSTOMIZE_VER := v5.8.0
5656
KUSTOMIZE_BIN := kustomize
@@ -176,27 +176,50 @@ kind-test: test create-cluster fv ## Build docker image; start kind cluster; loa
176176
fv: $(GINKGO) ## Run Sveltos Controller tests using existing cluster
177177
cd test/fv; $(GINKGO) -nodes $(NUM_NODES) --label-filter='FV' --v --trace --randomize-all
178178

179+
.PHONY: kind-test-namespace
180+
kind-test-namespace: test create-cluster-infra fv-namespace ## Build image; create cluster infra; deploy in random namespace and run fv
181+
182+
.PHONY: fv-namespace
183+
fv-namespace: $(GINKGO) $(KUBECTL) $(KUSTOMIZE) $(ENVSUBST) ## Deploy Sveltos in a randomly named namespace and run fv
184+
$(MAKE) load-image
185+
$(MAKE) deploy-crds
186+
@SVELTOS_NS="sveltos-$$(openssl rand -hex 4)"; \
187+
echo "Deploying in namespace: $$SVELTOS_NS"; \
188+
curl -s https://raw.githubusercontent.com/projectsveltos/addon-controller/$(TAG)/manifest/manifest.yaml | \
189+
sed -E 's/^([[:space:]]+)(name|namespace): projectsveltos$$/\1\2: '"$$SVELTOS_NS"'/' | \
190+
$(KUBECTL) apply -f-; \
191+
$(KUBECTL) wait --for=condition=Available deployment/addon-controller -n "$$SVELTOS_NS" --timeout=$(TIMEOUT); \
192+
$(KUSTOMIZE) build config/default | $(ENVSUBST) | \
193+
sed -E 's/^([[:space:]]+)(name|namespace): projectsveltos$$/\1\2: '"$$SVELTOS_NS"'/' | \
194+
$(KUBECTL) apply -f-; \
195+
$(KUBECTL) wait --for=condition=Available deployment/shard-controller -n "$$SVELTOS_NS" --timeout=$(TIMEOUT); \
196+
cd test/fv && SVELTOS_NAMESPACE="$$SVELTOS_NS" $(GINKGO) -nodes $(NUM_NODES) --label-filter='FV' --v --trace --randomize-all
197+
179198
.PHONY: test
180199
test: manifests generate fmt vet $(SETUP_ENVTEST) ## Run uts.
181200
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test $(shell go list ./... |grep -v test/fv |grep -v test/helpers) $(TEST_ARGS) -coverprofile cover.out
182201

183-
.PHONY: create-cluster
184-
create-cluster: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create a new kind cluster designed for development
202+
.PHONY: create-cluster-infra
203+
create-cluster-infra: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create cluster infrastructure without deploying Sveltos
185204
$(MAKE) create-control-cluster
186205

187206
@echo wait for capd-system pod
188207
$(KUBECTL) wait --for=condition=Available deployment/capd-controller-manager -n capd-system --timeout=$(TIMEOUT)
189-
$(KUBECTL) wait --for=condition=Available deployment/capi-kubeadm-control-plane-controller-manager -n capi-kubeadm-control-plane-system --timeout=$(TIMEOUT)
208+
209+
@echo wait for capi-kubeadm-bootstrap-system pod
190210
$(KUBECTL) wait --for=condition=Available deployment/capi-kubeadm-bootstrap-controller-manager -n capi-kubeadm-bootstrap-system --timeout=$(TIMEOUT)
191211

212+
@echo wait for capi-kubeadm-control-plane-system pod
213+
$(KUBECTL) wait --for=condition=Available deployment/capi-kubeadm-control-plane-controller-manager -n capi-kubeadm-control-plane-system --timeout=$(TIMEOUT)
214+
192215
@echo "sleep allowing webhook to be ready"
193216
sleep 10
194217

195218
@echo "Create a workload cluster"
196219
$(KUBECTL) apply -f $(KIND_CLUSTER_YAML)
197220

198-
@echo "Start projectsveltos event-manager"
199-
$(MAKE) deploy-projectsveltos
221+
@echo "wait for cluster to be provisioned"
222+
$(KUBECTL) wait cluster $(WORKLOAD_CLUSTER_NAME) -n default --for=jsonpath='{.status.phase}'=Provisioned --timeout=$(TIMEOUT)
200223

201224
@echo "sleep allowing control plane to be ready"
202225
sleep 60
@@ -210,6 +233,11 @@ create-cluster: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create a new kin
210233
@echo wait for calico pod
211234
$(KUBECTL) --kubeconfig=./test/fv/workload_kubeconfig wait --for=condition=Available deployment/calico-kube-controllers -n kube-system --timeout=$(TIMEOUT)
212235

236+
.PHONY: create-cluster
237+
create-cluster: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create a new kind cluster designed for development
238+
$(MAKE) create-cluster-infra
239+
$(MAKE) deploy-projectsveltos
240+
213241
.PHONY: delete-cluster
214242
delete-cluster: $(KIND) ## Deletes the kind cluster $(CONTROL_CLUSTER_NAME)
215243
$(KIND) delete cluster --name $(CONTROL_CLUSTER_NAME)
@@ -301,11 +329,7 @@ create-control-cluster:
301329
@echo "Create control cluster with docker as infrastructure provider"
302330
CLUSTER_TOPOLOGY=true $(CLUSTERCTL) init --infrastructure docker
303331

304-
deploy-projectsveltos: $(KUSTOMIZE)
305-
# Load projectsveltos image into cluster
306-
@echo 'Load projectsveltos image into cluster'
307-
$(MAKE) load-image
308-
332+
deploy-crds:
309333
@echo 'Install libsveltos CRDs'
310334
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/config/crd/bases/lib.projectsveltos.io_accessrequests.yaml
311335
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/config/crd/bases/lib.projectsveltos.io_classifierreports.yaml
@@ -324,6 +348,12 @@ deploy-projectsveltos: $(KUSTOMIZE)
324348
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/config/crd/bases/lib.projectsveltos.io_rolerequests.yaml
325349
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/config/crd/bases/lib.projectsveltos.io_sveltosclusters.yaml
326350

351+
deploy-projectsveltos: $(KUSTOMIZE)
352+
# Load projectsveltos image into cluster
353+
@echo 'Load projectsveltos image into cluster'
354+
$(MAKE) load-image
355+
$(MAKE) deploy-crds
356+
327357
@echo "Deploying addon-controller"
328358
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/addon-controller/$(TAG)/manifest/manifest.yaml
329359

cmd/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func main() {
115115
os.Exit(1)
116116
}
117117

118+
sveltosNamespace := getSveltosNamespace()
119+
controller.SetSveltosNamespace(sveltosNamespace)
120+
118121
// Setup the context that's going to be used in controllers and for the manager.
119122
ctx := ctrl.SetupSignalHandler()
120123

@@ -289,3 +292,12 @@ func getDiagnosticsOptions() metricsserver.Options {
289292
FilterProvider: filters.WithAuthenticationAndAuthorization,
290293
}
291294
}
295+
296+
func getSveltosNamespace() string {
297+
sveltosNamespace := os.Getenv("NAMESPACE")
298+
if sveltosNamespace == "" {
299+
setupLog.V(logs.LogInfo).Error(nil, "Missing required environment variables NAMESPACE")
300+
os.Exit(1)
301+
}
302+
return sveltosNamespace
303+
}

config/default/manager_auth_proxy_patch.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ spec:
1515
- "--v=5"
1616
- "--report-mode=0"
1717
- "--agent-in-mgmt-cluster=false"
18+
env:
19+
- name: GOMEMLIMIT
20+
valueFrom:
21+
resourceFieldRef:
22+
resource: limits.memory
23+
- name: GOMAXPROCS
24+
valueFrom:
25+
resourceFieldRef:
26+
resource: limits.cpu
27+
- name: NAMESPACE
28+
valueFrom:
29+
fieldRef:
30+
fieldPath: metadata.namespace

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ spec:
88
spec:
99
containers:
1010
# Change the value of image field below to your controller image URL
11-
- image: docker.io/projectsveltos/shard-controller:v1.10.0
11+
- image: docker.io/projectsveltos/shard-controller:main
1212
name: manager

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/onsi/ginkgo/v2 v2.28.3
1010
github.com/onsi/gomega v1.40.0
1111
github.com/pkg/errors v0.9.1
12-
github.com/projectsveltos/libsveltos v1.10.0
12+
github.com/projectsveltos/libsveltos v1.10.1-0.20260521153750-a1f348424b3f
1313
github.com/spf13/pflag v1.0.10
1414
golang.org/x/text v0.37.0
1515
k8s.io/api v0.36.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
118118
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
119119
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
120120
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
121-
github.com/projectsveltos/libsveltos v1.10.0 h1:+Gk34qCEOryHMT7xDFSIniiqk3VymhiKs/3EdK5gJ58=
122-
github.com/projectsveltos/libsveltos v1.10.0/go.mod h1:AzKBiyMTL3KSTLYMii5QdR3ieWyUKBHZCMFWIVCfm6A=
121+
github.com/projectsveltos/libsveltos v1.10.1-0.20260521153750-a1f348424b3f h1:XrgpnRRH59AwSkSEIcKPUDpAME0Sl4sXLeEhDIaYQMU=
122+
github.com/projectsveltos/libsveltos v1.10.1-0.20260521153750-a1f348424b3f/go.mod h1:AzKBiyMTL3KSTLYMii5QdR3ieWyUKBHZCMFWIVCfm6A=
123123
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
124124
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
125125
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=

hack/tools/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.26.3
44

55
require (
66
github.com/a8m/envsubst v1.4.3
7-
github.com/onsi/ginkgo/v2 v2.28.1
7+
github.com/onsi/ginkgo/v2 v2.29.0
88
golang.org/x/oauth2 v0.36.0
99
golang.org/x/tools v0.45.0
1010
k8s.io/client-go v0.36.1
@@ -39,7 +39,7 @@ require (
3939
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
4040
github.com/gobuffalo/flect v1.0.3 // indirect
4141
github.com/google/gnostic-models v0.7.1 // indirect
42-
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 // indirect
42+
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect
4343
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4444
github.com/json-iterator/go v1.1.12 // indirect
4545
github.com/mattn/go-colorable v0.1.14 // indirect

hack/tools/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
9696
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
9797
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 h1:z2ogiKUYzX5Is6zr/vP9vJGqPwcdqsWjOt+V8J7+bTc=
9898
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI=
99+
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 h1:EwtI+Al+DeppwYX2oXJCETMO23COyaKGP6fHVpkpWpg=
100+
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI=
99101
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
100102
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
101103
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -134,6 +136,8 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
134136
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
135137
github.com/onsi/ginkgo/v2 v2.28.1 h1:S4hj+HbZp40fNKuLUQOYLDgZLwNUVn19N3Atb98NCyI=
136138
github.com/onsi/ginkgo/v2 v2.28.1/go.mod h1:CLtbVInNckU3/+gC8LzkGUb9oF+e8W8TdUsxPwvdOgE=
139+
github.com/onsi/ginkgo/v2 v2.29.0 h1:rfh+ZFjgJhYWRoIqVf3Uwx/W20yLrcrE2h2GmYVRaag=
140+
github.com/onsi/ginkgo/v2 v2.29.0/go.mod h1:+aXOY+vzZ5mu2iI2HpTZUPmM//oQfsNFX6gU9kNcA44=
137141
github.com/onsi/gomega v1.40.0 h1:Vtol0e1MghCD2ZVIilPDIg44XSL9l2QAn8ZNaljWcJc=
138142
github.com/onsi/gomega v1.40.0/go.mod h1:M/Uqpu/8qTjtzCLUA2zJHX9Iilrau25x1PdoSRbWh5A=
139143
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=

internal/controller/controller_suite_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ import (
4646
const (
4747
timeout = 1 * time.Minute
4848
pollingInterval = 5 * time.Second
49-
projectsveltoNs = "projectsveltos"
5049
)
5150

5251
var (
53-
testEnv *helpers.TestEnvironment
54-
cancel context.CancelFunc
55-
ctx context.Context
56-
scheme *runtime.Scheme
52+
testEnv *helpers.TestEnvironment
53+
cancel context.CancelFunc
54+
ctx context.Context
55+
scheme *runtime.Scheme
56+
sveltosNamespace string
5757
)
5858

5959
func TestController(t *testing.T) {
@@ -93,9 +93,10 @@ var _ = BeforeSuite(func() {
9393
Expect(testEnv.Create(context.TODO(), sveltosCRD)).To(Succeed())
9494
Expect(waitForObject(context.TODO(), testEnv, sveltosCRD)).To(Succeed())
9595

96+
sveltosNamespace = randomString()
9697
namespace := &corev1.Namespace{
9798
ObjectMeta: metav1.ObjectMeta{
98-
Name: projectsveltoNs,
99+
Name: sveltosNamespace,
99100
},
100101
}
101102

0 commit comments

Comments
 (0)