Skip to content

Commit

Permalink
Merge pull request #678 from akrejcir/remove-proxy-namespace-0.18
Browse files Browse the repository at this point in the history
[release-v0.18] refactor(CNV-31248): remove namespace annotation
  • Loading branch information
kubevirt-bot authored Sep 1, 2023
2 parents 0f25225 + 4f0aaa0 commit a6ee5f8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 230 deletions.
1 change: 0 additions & 1 deletion automation/e2e-upgrade-functests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,5 @@ export SKIP_CLEANUP_AFTER_TESTS="true"
export TEST_EXISTING_CR_NAME="${SSP_NAME}"
export TEST_EXISTING_CR_NAMESPACE="${SSP_NAMESPACE}"
export IS_UPGRADE_LANE="true"
export VM_CONSOLE_PROXY_NAMESPACE="kubevirt"

make deploy functest
2 changes: 0 additions & 2 deletions config/samples/ssp_v1beta2_ssp.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
apiVersion: ssp.kubevirt.io/v1beta2
kind: SSP
metadata:
annotations:
ssp.kubevirt.io/vm-console-proxy-namespace: "kubevirt"
name: ssp-sample
namespace: kubevirt
spec:
Expand Down
13 changes: 5 additions & 8 deletions data/olm-catalog/ssp-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ metadata:
"apiVersion": "ssp.kubevirt.io/v1beta2",
"kind": "SSP",
"metadata": {
"annotations": {
"ssp.kubevirt.io/vm-console-proxy-namespace": "kubevirt"
},
"name": "ssp-sample",
"namespace": "kubevirt"
},
Expand Down Expand Up @@ -268,13 +265,13 @@ spec:
- list
- watch
- apiGroups:
- ""
- ""
resources:
- persistentvolumes
- persistentvolumes
verbs:
- get
- list
- watch
- get
- list
- watch
- apiGroups:
- ""
resources:
Expand Down
42 changes: 18 additions & 24 deletions internal/operands/vm-console-proxy/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
)

const (
VmConsoleProxyNamespaceAnnotation = "ssp.kubevirt.io/vm-console-proxy-namespace"

operandName = "vm-console-proxy"
operandComponent = "vm-console-proxy"

Expand Down Expand Up @@ -54,11 +52,16 @@ func WatchClusterTypes() []operands.WatchType {
return []operands.WatchType{
{Object: &rbac.ClusterRole{}},
{Object: &rbac.ClusterRoleBinding{}},
{Object: &apiregv1.APIService{}},
}
}

func WatchTypes() []operands.WatchType {
return []operands.WatchType{
{Object: &core.ServiceAccount{}},
{Object: &core.Service{}},
{Object: &apps.Deployment{}, WatchFullObject: true},
{Object: &core.ConfigMap{}},
{Object: &apiregv1.APIService{}},
{Object: &routev1.Route{}},
}
}
Expand Down Expand Up @@ -94,7 +97,7 @@ func (v *vmConsoleProxy) Name() string {
}

func (v *vmConsoleProxy) WatchTypes() []operands.WatchType {
return nil
return WatchTypes()
}

func (v *vmConsoleProxy) WatchClusterTypes() []operands.WatchType {
Expand Down Expand Up @@ -228,9 +231,9 @@ func (v *vmConsoleProxy) deleteRoute(request *common.Request) ([]common.CleanupR

func reconcileServiceAccount(serviceAccount core.ServiceAccount) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
serviceAccount.Namespace = getVmConsoleProxyNamespace(request)
serviceAccount.Namespace = request.Instance.Namespace
return common.CreateOrUpdate(request).
ClusterResource(&serviceAccount).
NamespacedResource(&serviceAccount).
WithAppLabels(operandName, operandComponent).
Reconcile()
}
Expand All @@ -247,6 +250,7 @@ func reconcileClusterRole(clusterRole rbac.ClusterRole) common.ReconcileFunc {

func reconcileClusterRoleBinding(clusterRoleBinding rbac.ClusterRoleBinding) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
clusterRoleBinding.Subjects[0].Namespace = request.Instance.Namespace
return common.CreateOrUpdate(request).
ClusterResource(&clusterRoleBinding).
WithAppLabels(operandName, operandComponent).
Expand All @@ -256,6 +260,7 @@ func reconcileClusterRoleBinding(clusterRoleBinding rbac.ClusterRoleBinding) com

func reconcileRoleBinding(roleBinding *rbac.RoleBinding) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
roleBinding.Subjects[0].Namespace = request.Instance.Namespace
return common.CreateOrUpdate(request).
ClusterResource(roleBinding).
WithAppLabels(operandName, operandComponent).
Expand All @@ -265,38 +270,38 @@ func reconcileRoleBinding(roleBinding *rbac.RoleBinding) common.ReconcileFunc {

func reconcileConfigMap(configMap core.ConfigMap) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
configMap.Namespace = getVmConsoleProxyNamespace(request)
configMap.Namespace = request.Instance.Namespace
return common.CreateOrUpdate(request).
ClusterResource(&configMap).
NamespacedResource(&configMap).
WithAppLabels(operandName, operandComponent).
Reconcile()
}
}

func reconcileService(service core.Service) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
service.Namespace = getVmConsoleProxyNamespace(request)
service.Namespace = request.Instance.Namespace
return common.CreateOrUpdate(request).
ClusterResource(&service).
NamespacedResource(&service).
WithAppLabels(operandName, operandComponent).
Reconcile()
}
}

func reconcileDeployment(deployment apps.Deployment) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
deployment.Namespace = getVmConsoleProxyNamespace(request)
deployment.Namespace = request.Instance.Namespace
deployment.Spec.Template.Spec.Containers[0].Image = getVmConsoleProxyImage()
return common.CreateOrUpdate(request).
ClusterResource(&deployment).
NamespacedResource(&deployment).
WithAppLabels(operandName, operandComponent).
Reconcile()
}
}

func reconcileApiService(apiService *apiregv1.APIService) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
apiService.Spec.Service.Namespace = getVmConsoleProxyNamespace(request)
apiService.Spec.Service.Namespace = request.Instance.Namespace
return common.CreateOrUpdate(request).
ClusterResource(apiService).
WithAppLabels(operandName, operandComponent).
Expand All @@ -313,17 +318,6 @@ func reconcileApiService(apiService *apiregv1.APIService) common.ReconcileFunc {
}
}

func getVmConsoleProxyNamespace(request *common.Request) string {
const defaultNamespace = "kubevirt"
if request.Instance.GetAnnotations() == nil {
return defaultNamespace
}
if namespace, isFound := request.Instance.GetAnnotations()[VmConsoleProxyNamespaceAnnotation]; isFound {
return namespace
}
return defaultNamespace
}

func getVmConsoleProxyImage() string {
return common.EnvOrDefault(common.VmConsoleProxyImageKey, defaultVmConsoleProxyImage)
}
Expand Down
145 changes: 1 addition & 144 deletions internal/operands/vm-console-proxy/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ var _ = Describe("VM Console Proxy Operand", func() {
}
})

It("should delete resources when enabled annotation is removed", func() {
It("should delete resources when feature gate was disabled", func() {
_, err := operand.Reconcile(&request)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -268,146 +268,6 @@ var _ = Describe("VM Console Proxy Operand", func() {
ExpectResourceNotExists(bundle.Deployment, request)
ExpectResourceNotExists(bundle.ApiService, request)
})

Context("with namespace annotation", func() {
const otherNamespace = "some-namespace"

var (
serviceAccount *core.ServiceAccount
configMap *core.ConfigMap
service *core.Service
deployment *apps.Deployment
)

BeforeEach(func() {
serviceAccount = bundle.ServiceAccount.DeepCopy()
serviceAccount.Namespace = otherNamespace

configMap = bundle.ConfigMap.DeepCopy()
configMap.Namespace = otherNamespace

service = bundle.Service.DeepCopy()
service.Namespace = otherNamespace

deployment = bundle.Deployment.DeepCopy()
deployment.Namespace = otherNamespace

request.Instance.GetAnnotations()[VmConsoleProxyNamespaceAnnotation] = otherNamespace
})

It("should deploy resources in namespace provided by annotation", func() {
_, err := operand.Reconcile(&request)
Expect(err).ToNot(HaveOccurred())

ExpectResourceExists(serviceAccount, request)
ExpectResourceExists(bundle.ClusterRole, request)
ExpectResourceExists(bundle.ClusterRoleBinding, request)
ExpectResourceExists(bundle.RoleBinding, request)
ExpectResourceExists(configMap, request)
ExpectResourceExists(service, request)
ExpectResourceExists(deployment, request)
ExpectResourceExists(bundle.ApiService, request)
})

It("should cleanup resources from namespace provided by annotation", func() {
_, err := operand.Reconcile(&request)
Expect(err).ToNot(HaveOccurred())

ExpectResourceExists(serviceAccount, request)
ExpectResourceExists(bundle.ClusterRole, request)
ExpectResourceExists(bundle.ClusterRoleBinding, request)
ExpectResourceExists(bundle.RoleBinding, request)
ExpectResourceExists(configMap, request)
ExpectResourceExists(service, request)
ExpectResourceExists(deployment, request)
ExpectResourceExists(bundle.ApiService, request)

_, err = operand.Cleanup(&request)
Expect(err).ToNot(HaveOccurred())

ExpectResourceNotExists(serviceAccount, request)
ExpectResourceNotExists(bundle.ClusterRole, request)
ExpectResourceNotExists(bundle.ClusterRoleBinding, request)
ExpectResourceNotExists(bundle.RoleBinding, request)
ExpectResourceNotExists(configMap, request)
ExpectResourceNotExists(service, request)
ExpectResourceNotExists(deployment, request)
ExpectResourceNotExists(bundle.ApiService, request)
})

It("should deploy APIService with ServiceReference pointing to the right namespace", func() {
_, err := operand.Reconcile(&request)
Expect(err).ToNot(HaveOccurred())

apiService := &apiregv1.APIService{}
key := client.ObjectKeyFromObject(bundle.ApiService)
Expect(request.Client.Get(request.Context, key, apiService)).To(Succeed())

Expect(apiService.Spec.Service.Namespace).To(Equal(otherNamespace))
})

It("should remove resources from the namespace", func() {
_, err := operand.Reconcile(&request)
Expect(err).ToNot(HaveOccurred())

ExpectResourceExists(serviceAccount, request)
ExpectResourceExists(bundle.ClusterRole, request)
ExpectResourceExists(bundle.ClusterRoleBinding, request)
ExpectResourceExists(bundle.RoleBinding, request)
ExpectResourceExists(configMap, request)
ExpectResourceExists(service, request)
ExpectResourceExists(deployment, request)
ExpectResourceExists(bundle.ApiService, request)

request.Instance.Spec.FeatureGates.DeployVmConsoleProxy = false

delete(request.Instance.Annotations, VmConsoleProxyNamespaceAnnotation)

_, err = operand.Reconcile(&request)
Expect(err).ToNot(HaveOccurred())

ExpectResourceNotExists(serviceAccount, request)
ExpectResourceNotExists(bundle.ClusterRole, request)
ExpectResourceNotExists(bundle.ClusterRoleBinding, request)
ExpectResourceNotExists(bundle.RoleBinding, request)
ExpectResourceNotExists(configMap, request)
ExpectResourceNotExists(service, request)
ExpectResourceNotExists(deployment, request)
ExpectResourceNotExists(bundle.ApiService, request)
})

DescribeTable("should delete Route leftover from previous version", func(op func() error) {
route := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: routeName,
Namespace: otherNamespace,
Annotations: map[string]string{
libhandler.TypeAnnotation: "SSP.ssp.kubevirt.io",
libhandler.NamespacedNameAnnotation: namespace + "/" + name,
},
Labels: map[string]string{
common.AppKubernetesNameLabel: operandName,
common.AppKubernetesComponentLabel: operandComponent,
common.AppKubernetesManagedByLabel: common.AppKubernetesManagedByValue,
},
},
Spec: routev1.RouteSpec{},
}

Expect(request.Client.Create(request.Context, route)).To(Succeed())
Expect(op()).To(Succeed())
ExpectResourceNotExists(route, request)
},
Entry("on reconcile", func() error {
_, err := operand.Reconcile(&request)
return err
}),
Entry("on cleanup", func() error {
_, err := operand.Cleanup(&request)
return err
}),
)
})
})

func TestVmConsoleProxyBundle(t *testing.T) {
Expand Down Expand Up @@ -436,9 +296,6 @@ func getMockedRequest() common.Request {
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
VmConsoleProxyNamespaceAnnotation: namespace,
},
},
Spec: ssp.SSPSpec{
FeatureGates: &ssp.FeatureGates{
Expand Down
27 changes: 11 additions & 16 deletions tests/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import (
)

const (
envExistingCrName = "TEST_EXISTING_CR_NAME"
envExistingCrNamespace = "TEST_EXISTING_CR_NAMESPACE"
envSkipUpdateSspTests = "SKIP_UPDATE_SSP_TESTS"
envSkipCleanupAfterTests = "SKIP_CLEANUP_AFTER_TESTS"
envTimeout = "TIMEOUT_MINUTES"
envShortTimeout = "SHORT_TIMEOUT_MINUTES"
envTopologyMode = "TOPOLOGY_MODE"
envIsUpgradeLane = "IS_UPGRADE_LANE"
envSspDeploymentName = "SSP_DEPLOYMENT_NAME"
envSspDeploymentNamespace = "SSP_DEPLOYMENT_NAMESPACE"
envSspWebhookServiceName = "SSP_WEBHOOK_SERVICE_NAME"
envVmConsoleProxyNamespace = "VM_CONSOLE_PROXY_NAMESPACE"
envExistingCrName = "TEST_EXISTING_CR_NAME"
envExistingCrNamespace = "TEST_EXISTING_CR_NAMESPACE"
envSkipUpdateSspTests = "SKIP_UPDATE_SSP_TESTS"
envSkipCleanupAfterTests = "SKIP_CLEANUP_AFTER_TESTS"
envTimeout = "TIMEOUT_MINUTES"
envShortTimeout = "SHORT_TIMEOUT_MINUTES"
envTopologyMode = "TOPOLOGY_MODE"
envIsUpgradeLane = "IS_UPGRADE_LANE"
envSspDeploymentName = "SSP_DEPLOYMENT_NAME"
envSspDeploymentNamespace = "SSP_DEPLOYMENT_NAMESPACE"
envSspWebhookServiceName = "SSP_WEBHOOK_SERVICE_NAME"
)

const (
Expand Down Expand Up @@ -97,10 +96,6 @@ func SspWebhookServiceName() string {
return os.Getenv(envSspWebhookServiceName)
}

func VmConsoleProxyNamespace() string {
return os.Getenv(envVmConsoleProxyNamespace)
}

func getBoolEnv(envName string) bool {
envVal := os.Getenv(envName)
if envVal == "" {
Expand Down
Loading

0 comments on commit a6ee5f8

Please sign in to comment.