From 9b3df6660e657fe1e12d5baafb9913fcfc39ff6b Mon Sep 17 00:00:00 2001 From: Varsha Prasad Narsing Date: Mon, 18 Sep 2023 15:47:00 -0400 Subject: [PATCH] [Add] add default namespace if not specified If the bundle objects do not specify a namespace, use the default namespace to deploy them. Signed-off-by: Varsha Prasad Narsing --- Makefile | 8 +------- api/v1alpha2/bundledeployment_types.go | 4 +++- internal/controllers/v1alpha2/deployer/deploy.go | 11 +++++++++-- .../apis/crds/core.rukpak.io_bundledeployments.yaml | 2 ++ 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 0754bd7b..c72dd246 100644 --- a/Makefile +++ b/Makefile @@ -84,11 +84,6 @@ generate: $(CONTROLLER_GEN) ## Generate code and manifests paths=./internal/uploadmgr/... \ output:stdout > ./manifests/base/core/resources/cluster_role.yaml $(Q)$(CONTROLLER_GEN) rbac:roleName=webhooks-admin paths=./internal/webhook/... output:stdout > ./manifests/base/apis/webhooks/resources/cluster_role.yaml - $(Q)$(CONTROLLER_GEN) rbac:roleName=helm-provisioner-admin \ - paths=./internal/controllers/bundle/... \ - paths=./internal/controllers/bundledeployment/... \ - paths=./internal/provisioner/helm/... \ - output:stdout > ./manifests/base/provisioners/helm/resources/cluster_role.yaml verify: tidy fmt generate ## Verify the current code generation and lint git diff --exit-code @@ -144,7 +139,6 @@ install-manifests: wait: $(KUBECTL) wait --for=condition=Available --namespace=$(RUKPAK_NAMESPACE) deployment/core --timeout=60s $(KUBECTL) wait --for=condition=Available --namespace=$(RUKPAK_NAMESPACE) deployment/rukpak-webhooks --timeout=60s - $(KUBECTL) wait --for=condition=Available --namespace=$(RUKPAK_NAMESPACE) deployment/helm-provisioner --timeout=60s $(KUBECTL) wait --for=condition=Available --namespace=crdvalidator-system deployment/crd-validation-webhook --timeout=60s run: build-container kind-cluster kind-load install ## Build image, stop/start a local kind cluster, and run operator in that cluster @@ -171,7 +165,7 @@ uninstall: ## Remove all rukpak resources from the cluster ##@ build/load: -BINARIES=core helm unpack webhooks crdvalidator rukpakctl +BINARIES=core unpack webhooks crdvalidator rukpakctl LINUX_BINARIES=$(join $(addprefix linux/,$(BINARIES)), ) .PHONY: build $(BINARIES) $(LINUX_BINARIES) build-container kind-load kind-load-bundles kind-cluster registry-load-bundles diff --git a/api/v1alpha2/bundledeployment_types.go b/api/v1alpha2/bundledeployment_types.go index 67a7d71c..dee5c232 100644 --- a/api/v1alpha2/bundledeployment_types.go +++ b/api/v1alpha2/bundledeployment_types.go @@ -18,7 +18,7 @@ package v1alpha2 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime" ) var ( @@ -87,6 +87,8 @@ type BundleDeploymentSpec struct { // Should move to helm deployer configuration. // +kubebuilder:pruning:PreserveUnknownFields Config runtime.RawExtension `json:"config,omitempty"` + // +optional + DefaultNamespace string `json:"defaultnamespace,omitempty"` } // BundleDeploymentStatus defines the observed state of BundleDeployment diff --git a/internal/controllers/v1alpha2/deployer/deploy.go b/internal/controllers/v1alpha2/deployer/deploy.go index 1a3459de..3566805e 100644 --- a/internal/controllers/v1alpha2/deployer/deploy.go +++ b/internal/controllers/v1alpha2/deployer/deploy.go @@ -123,6 +123,7 @@ func (hd *helmDeployer) Deploy(ctx context.Context, fs afero.Fs, bundleDeploymen util.CoreOwnerKindKey: v1alpha2.BundleDeploymentKind, util.CoreOwnerNameKey: bundleDeployment.GetName(), }, + defaultNamespace: bundleDeployment.Spec.DefaultNamespace, } rel, state, err := hd.getReleaseState(cl, bundleDeployment, chrt, values, post) @@ -320,8 +321,9 @@ func getChartFromPlainBundle(chartfs afero.Fs, bd *v1alpha2.BundleDeployment) (* } type postrenderer struct { - labels map[string]string - cascade postrender.PostRenderer + labels map[string]string + defaultNamespace string + cascade postrender.PostRenderer } func (p *postrenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) { @@ -337,6 +339,11 @@ func (p *postrenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, erro return nil, err } obj.SetLabels(util.MergeMaps(obj.GetLabels(), p.labels)) + // If the default namespace is not already created in the cluster, there + // would be an error since we do not create a ns if it doesn't exist. + if obj.GetNamespace() == "" && p.defaultNamespace != "" { + obj.SetNamespace(p.defaultNamespace) + } b, err := obj.MarshalJSON() if err != nil { return nil, err diff --git a/manifests/base/apis/crds/core.rukpak.io_bundledeployments.yaml b/manifests/base/apis/crds/core.rukpak.io_bundledeployments.yaml index 221a4a68..ec1725a6 100644 --- a/manifests/base/apis/crds/core.rukpak.io_bundledeployments.yaml +++ b/manifests/base/apis/crds/core.rukpak.io_bundledeployments.yaml @@ -392,6 +392,8 @@ spec: configuration.' type: object x-kubernetes-preserve-unknown-fields: true + defaultnamespace: + type: string format: description: Format refers to the bundle type which is being passed through the bundle deployment API.