Skip to content

Commit dfd5360

Browse files
authored
(chore) add fv for Sveltos running in random namespace (#1803)
1 parent 834c972 commit dfd5360

12 files changed

Lines changed: 135 additions & 30 deletions

File tree

.github/workflows/main.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,33 @@ jobs:
208208
swap-storage: true
209209
- name: new-fv-pullmode
210210
run: make create-cluster-pullmode new-fv-pullmode
211+
env:
212+
FV: true
213+
FV-NAMESPACE:
214+
runs-on: ubuntu-latest
215+
steps:
216+
- name: checkout
217+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
218+
- name: Set up Go
219+
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
220+
with:
221+
go-version: 1.26.3
222+
- name: Free Disk Space (Ubuntu)
223+
uses: jlumbroso/free-disk-space@main
224+
with:
225+
# this might remove tools that are actually needed,
226+
# if set to "true" but frees about 6 GB
227+
tool-cache: false
228+
229+
# all of these default to true, but feel free to set to
230+
# "false" if necessary for your workflow
231+
android: true
232+
dotnet: true
233+
haskell: true
234+
large-packages: true
235+
docker-images: true
236+
swap-storage: true
237+
- name: fv
238+
run: make create-cluster-infra fv-namespace
211239
env:
212240
FV: true

Makefile

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,9 @@ fv-agentless: $(KUBECTL) $(GINKGO) ## Run Sveltos Controller tests using existin
267267
$(KUBECTL) wait --for=condition=Available deployment/addon-controller -n projectsveltos --timeout=$(TIMEOUT)
268268
cd test/fv; $(GINKGO) -nodes $(NUM_NODES) --label-filter='FV' --v --trace --randomize-all
269269

270-
.PHONY: create-cluster
271-
create-cluster: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create a new kind cluster designed for development
270+
.PHONY: create-cluster-infra
271+
create-cluster-infra: $(KIND) $(CLUSTERCTL) $(KUBECTL) ## Create cluster infrastructure without deploying Sveltos
272272
$(MAKE) create-control-cluster
273-
274-
@echo "Start projectsveltos"
275-
$(MAKE) deploy-projectsveltos
276-
277273
$(MAKE) create-workload-cluster
278274

279275
@echo "prepare configMap with kustomize files"
@@ -282,9 +278,36 @@ create-cluster: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create a new kin
282278
@echo "prepare configMap with flux resources"
283279
$(KUBECTL) create configmap install-flux --from-file=test/flux-install.yaml
284280

285-
@echo apply reloader CRD to managed cluster
281+
@echo "apply reloader CRD to managed cluster"
286282
$(KUBECTL) --kubeconfig=./test/fv/workload_kubeconfig apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_reloaders.lib.projectsveltos.io.yaml
287283

284+
.PHONY: fv-namespace
285+
fv-namespace: $(GINKGO) $(KUBECTL) $(KUSTOMIZE) $(ENVSUBST) ## Run FV tests with addon-controller deployed in a random namespace
286+
$(MAKE) load-image
287+
$(MAKE) deploy-crds
288+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
289+
SVELTOS_NS="sveltos-$$(openssl rand -hex 4)"; \
290+
echo "Deploying addon-controller in namespace: $$SVELTOS_NS"; \
291+
$(KUSTOMIZE) build config/default | $(ENVSUBST) | \
292+
sed -E 's/^([[:space:]]+)(name|namespace): projectsveltos$$/\1\2: '"$$SVELTOS_NS"'/' | \
293+
$(KUBECTL) apply -f-; \
294+
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/configuration/default-debuggingconfiguration.yaml; \
295+
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/sveltoscluster-manager/$(TAG)/manifest/manifest.yaml; \
296+
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/register-mgmt-cluster/$(TAG)/manifest/manifest.yaml; \
297+
echo "Waiting for addon-controller to be available in namespace $$SVELTOS_NS..."; \
298+
$(KUBECTL) wait --for=condition=Available deployment/addon-controller -n "$$SVELTOS_NS" --timeout=$(TIMEOUT); \
299+
cd test/fv && SVELTOS_NAMESPACE="$$SVELTOS_NS" $(GINKGO) -nodes $(NUM_NODES) --label-filter='FV' --v --trace --randomize-all
300+
301+
.PHONY: kind-test-namespace
302+
kind-test-namespace: test create-cluster-infra fv-namespace ## Build docker image; start kind cluster; run fv in a random namespace
303+
304+
.PHONY: create-cluster
305+
create-cluster: $(KIND) $(CLUSTERCTL) $(KUBECTL) $(ENVSUBST) ## Create a new kind cluster designed for development
306+
$(MAKE) create-cluster-infra
307+
308+
@echo "Start projectsveltos"
309+
$(MAKE) deploy-projectsveltos
310+
288311
.PHONY: delete-cluster
289312
delete-cluster: $(KIND) ## Deletes the kind cluster $(CONTROL_CLUSTER_NAME)
290313
$(KIND) delete cluster --name $(CONTROL_CLUSTER_NAME)
@@ -419,11 +442,8 @@ create-cluster-pullmode: $(KIND) $(KUBECTL) $(ENVSUBST) $(KUSTOMIZE)
419442
@echo "Switching to cluster1..."
420443
$(KUBECTL) config use-context kind-$(CONTROL_CLUSTER_NAME)
421444

422-
deploy-projectsveltos: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL)
423-
# Load projectsveltos image into cluster
424-
@echo 'Load projectsveltos image into cluster'
425-
$(MAKE) load-image
426-
445+
.PHONY: deploy-crds
446+
deploy-crds: $(KUBECTL) ## Install libsveltos CRDs
427447
@echo 'Install libsveltos CRDs'
428448
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_debuggingconfigurations.lib.projectsveltos.io.yaml
429449
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_classifiers.lib.projectsveltos.io.yaml
@@ -436,6 +456,13 @@ deploy-projectsveltos: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL)
436456
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_configurationbundles.lib.projectsveltos.io.yaml
437457
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_sveltoslicenses.lib.projectsveltos.io.yaml
438458

459+
deploy-projectsveltos: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL)
460+
# Load projectsveltos image into cluster
461+
@echo 'Load projectsveltos image into cluster'
462+
$(MAKE) load-image
463+
464+
$(MAKE) deploy-crds
465+
439466
# Install projectsveltos addon-controller components
440467
@echo 'Install projectsveltos addon-controller components'
441468
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}

controllers/management_cluster.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2324
"k8s.io/apimachinery/pkg/types"
2425
"k8s.io/client-go/discovery"
2526
memory "k8s.io/client-go/discovery/cached"
@@ -128,6 +129,39 @@ func getSveltosNamespace() string {
128129
return sveltosNamespace
129130
}
130131

132+
func updateResourceNamespace(policy *unstructured.Unstructured, namespace string) error {
133+
if policy.GetKind() == namespaceKind {
134+
policy.SetName(namespace)
135+
return nil
136+
}
137+
138+
if policy.GetNamespace() != "" {
139+
policy.SetNamespace(namespace)
140+
}
141+
142+
if policy.GetKind() != clusterRoleBindingKind {
143+
return nil
144+
}
145+
146+
subjects, found, err := unstructured.NestedSlice(policy.Object, "subjects")
147+
if err != nil || !found {
148+
return err
149+
}
150+
151+
for i := range subjects {
152+
subject, ok := subjects[i].(map[string]interface{})
153+
if !ok {
154+
continue
155+
}
156+
if subject["kind"] == "ServiceAccount" {
157+
subject["namespace"] = namespace
158+
subjects[i] = subject
159+
}
160+
}
161+
162+
return unstructured.SetNestedSlice(policy.Object, subjects, "subjects")
163+
}
164+
131165
func resetManagementClusterMapper() {
132166
managementClusterMapper.Reset()
133167
}

controllers/resourcesummary.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ import (
4949
)
5050

5151
const (
52-
deploymentKind = "Deployment"
52+
deploymentKind = "Deployment"
53+
namespaceKind = "Namespace"
54+
clusterRoleBindingKind = "ClusterRoleBinding"
5355

5456
driftDetectionClusterNamespaceLabel = "cluster-namespace"
5557
driftDetectionClusterNameLabel = "cluster-name"
@@ -337,8 +339,9 @@ func deployDriftDetectionManagerResources(ctx context.Context, restConfig *rest.
337339
}
338340
}
339341

340-
if policy.GetNamespace() != "" {
341-
policy.SetNamespace(getSveltosNamespace())
342+
if err := updateResourceNamespace(policy, getSveltosNamespace()); err != nil {
343+
logger.V(logs.LogInfo).Error(err, "failed to update resource namespace")
344+
return err
342345
}
343346

344347
var referencedUnstructured []*unstructured.Unstructured
@@ -612,7 +615,9 @@ func removeDriftDetectionManagerFromManagementCluster(ctx context.Context,
612615
return err
613616
}
614617

615-
if policy.GetNamespace() != "" {
618+
if policy.GetKind() == namespaceKind {
619+
policy.SetName(getSveltosNamespace())
620+
} else if policy.GetNamespace() != "" {
616621
policy.SetNamespace(getSveltosNamespace())
617622
}
618623

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/gdexlab/go-render v1.0.1
1616
github.com/go-logr/logr v1.4.3
1717
github.com/hexops/gotextdiff v1.0.3
18-
github.com/onsi/ginkgo/v2 v2.28.3
18+
github.com/onsi/ginkgo/v2 v2.29.0
1919
github.com/onsi/gomega v1.41.0
2020
github.com/pkg/errors v0.9.1
2121
github.com/projectsveltos/libsveltos v1.10.1-0.20260521153750-a1f348424b3f

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
256256
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
257257
github.com/onsi/ginkgo/v2 v2.28.3 h1:4JvMdwtFU0imd8fHx25OJXoDMRexnf8v5NHKYSTTji4=
258258
github.com/onsi/ginkgo/v2 v2.28.3/go.mod h1:+aXOY+vzZ5mu2iI2HpTZUPmM//oQfsNFX6gU9kNcA44=
259+
github.com/onsi/ginkgo/v2 v2.29.0 h1:rfh+ZFjgJhYWRoIqVf3Uwx/W20yLrcrE2h2GmYVRaag=
260+
github.com/onsi/ginkgo/v2 v2.29.0/go.mod h1:+aXOY+vzZ5mu2iI2HpTZUPmM//oQfsNFX6gU9kNcA44=
259261
github.com/onsi/gomega v1.41.0 h1:OwKp4pXNgVxf6sCplzYo794OFNuoL2q2SBMU5NSWOjA=
260262
github.com/onsi/gomega v1.41.0/go.mod h1:M/Uqpu/8qTjtzCLUA2zJHX9Iilrau25x1PdoSRbWh5A=
261263
github.com/opencontainers/go-digest v1.0.1-0.20260423074420-acc66fb5367c h1:dTJQx6HDrRNmA3p5JlfVYw81R3g2RfWdG0+ZBaJeqcc=

hack/tools/go.mod

Lines changed: 1 addition & 1 deletion
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.3
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

hack/tools/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
132132
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
133133
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
134134
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
135-
github.com/onsi/ginkgo/v2 v2.28.3 h1:4JvMdwtFU0imd8fHx25OJXoDMRexnf8v5NHKYSTTji4=
136-
github.com/onsi/ginkgo/v2 v2.28.3/go.mod h1:+aXOY+vzZ5mu2iI2HpTZUPmM//oQfsNFX6gU9kNcA44=
135+
github.com/onsi/ginkgo/v2 v2.29.0 h1:rfh+ZFjgJhYWRoIqVf3Uwx/W20yLrcrE2h2GmYVRaag=
136+
github.com/onsi/ginkgo/v2 v2.29.0/go.mod h1:+aXOY+vzZ5mu2iI2HpTZUPmM//oQfsNFX6gU9kNcA44=
137137
github.com/onsi/gomega v1.40.0 h1:Vtol0e1MghCD2ZVIilPDIg44XSL9l2QAn8ZNaljWcJc=
138138
github.com/onsi/gomega v1.40.0/go.mod h1:M/Uqpu/8qTjtzCLUA2zJHX9Iilrau25x1PdoSRbWh5A=
139139
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=

test/fv/capi_onboard_annotation_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ var _ = Describe("Helm", Serial, func() {
4242
namePrefix = "onboard-"
4343
onboardAnnotation = "onboard-capi"
4444

45-
addonDeplNamespace = "projectsveltos"
46-
addonDeplName = "addon-controller"
45+
addonDeplName = "addon-controller"
4746
)
4847

4948
BeforeEach(func() {
50-
Byf("Set capi-onboard-annotation for deployment %s/%s", addonDeplNamespace, addonDeplName)
51-
updateOnboardAnnotationArg(addonDeplNamespace, addonDeplName, onboardAnnotation)
49+
Byf("Set capi-onboard-annotation for deployment %s/%s", sveltosNamespace, addonDeplName)
50+
updateOnboardAnnotationArg(sveltosNamespace, addonDeplName, onboardAnnotation)
5251

5352
removeAnnotationFromCluster(onboardAnnotation)
5453
})
5554

5655
AfterEach(func() {
57-
Byf("Reset capi-onboard-annotation for deployment %s/%s", addonDeplNamespace, addonDeplName)
58-
updateOnboardAnnotationArg(addonDeplNamespace, addonDeplName, "")
56+
Byf("Reset capi-onboard-annotation for deployment %s/%s", sveltosNamespace, addonDeplName)
57+
updateOnboardAnnotationArg(sveltosNamespace, addonDeplName, "")
5958

6059
removeAnnotationFromCluster(onboardAnnotation)
6160
})

test/fv/drift_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ reportsController:
311311
Eventually(func() bool {
312312
depl := &appsv1.Deployment{}
313313
err = workloadClient.Get(context.TODO(),
314-
types.NamespacedName{Namespace: "projectsveltos", Name: "drift-detection-manager"}, depl)
314+
types.NamespacedName{Namespace: sveltosNamespace, Name: "drift-detection-manager"}, depl)
315315
if err != nil {
316316
return false
317317
}
@@ -529,7 +529,7 @@ func isAgentLessMode() bool {
529529
By("Getting addon-controller pod")
530530
addonControllerDepl := &appsv1.Deployment{}
531531
Expect(k8sClient.Get(context.TODO(),
532-
types.NamespacedName{Namespace: "projectsveltos", Name: "addon-controller"},
532+
types.NamespacedName{Namespace: sveltosNamespace, Name: "addon-controller"},
533533
addonControllerDepl)).To(Succeed())
534534

535535
Expect(len(addonControllerDepl.Spec.Template.Spec.Containers)).To(Equal(1))

0 commit comments

Comments
 (0)