Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replacing of namespace annotation when openshift-cnv namespace is set #683

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions data/tekton-pipelines/pipelines-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: RoleBinding
metadata:
name: windows-pipelines-modify-data-object
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
roleRef:
kind: ClusterRole
name: modify-data-object-task
Expand All @@ -18,7 +18,7 @@ kind: RoleBinding
metadata:
name: windows-pipelines-create-vm
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
roleRef:
kind: ClusterRole
name: create-vm-from-manifest-task
Expand All @@ -32,7 +32,7 @@ kind: RoleBinding
metadata:
name: windows-pipelines-wait-for-vmi
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
roleRef:
kind: ClusterRole
name: modify-data-object-task
Expand All @@ -46,7 +46,7 @@ kind: RoleBinding
metadata:
name: windows-pipelines-cleanup-vm
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
roleRef:
kind: ClusterRole
name: cleanup-vm-task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: ConfigMap
metadata:
name: windows10-bios-autounattend
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
data:
autounattend.xml: |
<?xml version="1.0" encoding="utf-8"?>
Expand Down
8 changes: 4 additions & 4 deletions data/tekton-pipelines/windows-customize-configmaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: ConfigMap
metadata:
name: windows-sqlserver
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
data:
unattend.xml: |
<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -63,7 +63,7 @@ kind: ConfigMap
metadata:
name: windows-vs-code
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
data:
unattend.xml: |
<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -122,7 +122,7 @@ kind: ConfigMap
metadata:
name: windows10-unattend
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
data:
unattend.xml: |
<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -195,7 +195,7 @@ kind: ConfigMap
metadata:
name: windows11-unattend
annotations:
"kubevirt.io/deploy-namespace": "kubevirt-os-images"
"kubevirt.io/tekton-piplines-deploy-namespace": "kubevirt-os-images"
data:
unattend.xml: |
<?xml version="1.0" encoding="utf-8"?>
Expand Down
26 changes: 16 additions & 10 deletions internal/operands/tekton-pipelines/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const (
operandName = "tekton-pipelines"
operandComponent = common.AppComponentTektonPipelines
tektonCrd = "tasks.tekton.dev"
deployNamespaceAnnotation = "kubevirt.io/deploy-namespace"
deployNamespaceAnnotation = "kubevirt.io/tekton-piplines-deploy-namespace"
pipelineServiceAccountName = "pipeline"
kubevirtNamespace = "kubevirt"
)

var namespaceRegex = regexp.MustCompile(namespacePattern)
Expand Down Expand Up @@ -133,9 +134,14 @@ func (t *tektonPipelines) Cleanup(request *common.Request) ([]common.CleanupResu
o := sa.DeepCopy()
objects = append(objects, o)
}
namespace, _ := getTektonPipelinesNamespace(request)
for i := range objects {
objects[i].SetNamespace(namespace)

namespace, isUserDefinedNamespace := getTektonPipelinesNamespace(request)
for i, o := range objects {
objectNamespace := namespace
if value, ok := o.GetAnnotations()[deployNamespaceAnnotation]; ok && !isUserDefinedNamespace {
objectNamespace = value
}
objects[i].SetNamespace(objectNamespace)
}

for _, cr := range t.clusterRoles {
Expand Down Expand Up @@ -180,12 +186,12 @@ func reconcileTektonPipelinesFuncs(pipelines []pipeline.Pipeline) []common.Recon

func reconcileConfigMapsFuncs(configMaps []v1.ConfigMap) []common.ReconcileFunc {
funcs := make([]common.ReconcileFunc, 0, len(configMaps))
var userDefinedNamespace bool
var isUserDefinedNamespace bool
for i := range configMaps {
configMap := &configMaps[i]
funcs = append(funcs, func(request *common.Request) (common.ReconcileResult, error) {
configMap.Namespace, userDefinedNamespace = getTektonPipelinesNamespace(request)
if value, ok := configMap.Annotations[deployNamespaceAnnotation]; ok && !userDefinedNamespace {
configMap.Namespace, isUserDefinedNamespace = getTektonPipelinesNamespace(request)
if value, ok := configMap.Annotations[deployNamespaceAnnotation]; ok && !isUserDefinedNamespace {
configMap.Namespace = value
}
return common.CreateOrUpdate(request).
Expand Down Expand Up @@ -261,8 +267,8 @@ func reconcileRoleBindingsFuncs(rolebindings []rbac.RoleBinding) []common.Reconc
for i := range rolebindings {
roleBinding := &rolebindings[i]
funcs = append(funcs, func(request *common.Request) (common.ReconcileResult, error) {
namespace, userDefinedNamespace := getTektonPipelinesNamespace(request)
if value, ok := roleBinding.Annotations[deployNamespaceAnnotation]; ok && !userDefinedNamespace {
namespace, isUserDefinedNamespace := getTektonPipelinesNamespace(request)
if value, ok := roleBinding.Annotations[deployNamespaceAnnotation]; ok && !isUserDefinedNamespace {
roleBinding.Namespace = value
} else {
roleBinding.Namespace = namespace
Expand All @@ -281,7 +287,7 @@ func reconcileRoleBindingsFuncs(rolebindings []rbac.RoleBinding) []common.Reconc
}

func getTektonPipelinesNamespace(request *common.Request) (string, bool) {
if request.Instance.Spec.TektonPipelines != nil && request.Instance.Spec.TektonPipelines.Namespace != "" {
if request.Instance.Spec.TektonPipelines != nil && request.Instance.Spec.TektonPipelines.Namespace != "" && request.Instance.Spec.TektonPipelines.Namespace != kubevirtNamespace {
return request.Instance.Spec.TektonPipelines.Namespace, true
}
return request.Instance.Namespace, false
Expand Down
51 changes: 47 additions & 4 deletions internal/operands/tekton-pipelines/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ var _ = Describe("environments", func() {
}
})

It("kubevirt.io/deploy-namespace annotation in configMaps should be replaced by user defined namespace", func() {
It("kubevirt.io/tekton-piplines-deploy-namespace annotation in configMaps should be replaced by user defined namespace", func() {
_, err := operand.Reconcile(request)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -188,7 +188,7 @@ var _ = Describe("environments", func() {
}
})

It("kubevirt.io/deploy-namespace annotation in roleBindings should be replaced by user defined namespace", func() {
It("kubevirt.io/tekton-piplines-deploy-namespace annotation in roleBindings should be replaced by user defined namespace", func() {
_, err := operand.Reconcile(request)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -202,13 +202,56 @@ var _ = Describe("environments", func() {
})
})

Context("With kubevirt namespace in ssp CR for pipelines", func() {
BeforeEach(func() {
request.Instance.Spec.FeatureGates.DeployTektonTaskResources = true
request.Instance.Spec.TektonPipelines = &ssp.TektonPipelines{
Namespace: kubevirtNamespace,
}
})

It("kubevirt.io/tekton-piplines-deploy-namespace annotation in configMaps should not be replaced", func() {
_, err := operand.Reconcile(request)
Expect(err).ToNot(HaveOccurred())

for _, configMap := range bundle.ConfigMaps {
expectedNamespace := namespace
if annotationNamespace, ok := configMap.Annotations[deployNamespaceAnnotation]; ok {
expectedNamespace = annotationNamespace
}

key := client.ObjectKeyFromObject(&configMap)
cm := &v1.ConfigMap{}
Expect(request.Client.Get(request.Context, key, cm)).ToNot(HaveOccurred())
Expect(cm.Namespace).To(Equal(expectedNamespace), cm.Name+" configMap namespace should equal")
}
})

It("kubevirt.io/tekton-piplines-deploy-namespace annotation in roleBindings should not be replaced", func() {
_, err := operand.Reconcile(request)
Expect(err).ToNot(HaveOccurred())

for _, roleBinding := range bundle.RoleBindings {
expectedNamespace := namespace
if annotationNamespace, ok := roleBinding.Annotations[deployNamespaceAnnotation]; ok {
expectedNamespace = annotationNamespace
}

key := client.ObjectKeyFromObject(&roleBinding)
rb := &rbac.RoleBinding{}
Expect(request.Client.Get(request.Context, key, rb)).ToNot(HaveOccurred())
Expect(rb.Namespace).To(Equal(expectedNamespace), rb.Name+" roleBinding namespace should equal")
}
})
})

Context("Without user defined namespace in ssp CR for pipelines", func() {
BeforeEach(func() {
request.Instance.Spec.FeatureGates.DeployTektonTaskResources = true
request.Instance.Spec.TektonPipelines = nil
})

It("kubevirt.io/deploy-namespace annotation in configMaps should replace default namespace", func() {
It("kubevirt.io/tekton-piplines-deploy-namespace annotation in configMaps should replace default namespace", func() {
_, err := operand.Reconcile(request)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -227,7 +270,7 @@ var _ = Describe("environments", func() {
}
})

It("kubevirt.io/deploy-namespace annotation in roleBindings should replace default namespace", func() {
It("kubevirt.io/tekton-piplines-deploy-namespace annotation in roleBindings should replace default namespace", func() {
_, err := operand.Reconcile(request)
Expect(err).ToNot(HaveOccurred())

Expand Down
Loading