Skip to content

Commit 6c7e8aa

Browse files
committed
feat: Set "openshift.io/required-scc" annotation on deployments
The annotation pins required SCC. Signed-off-by: Andrej Krejcir <[email protected]>
1 parent c485385 commit 6c7e8aa

File tree

8 files changed

+85
-1
lines changed

8 files changed

+85
-1
lines changed

config/manager/manager.template.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ kind: Deployment
33
metadata:
44
name: operator
55
namespace: kubevirt
6+
annotations:
7+
openshift.io/required-scc: restricted-v2
68
labels:
79
control-plane: ssp-operator
810
name: ssp-operator
@@ -14,6 +16,7 @@ spec:
1416
template:
1517
metadata:
1618
annotations:
19+
openshift.io/required-scc: restricted-v2
1720
kubectl.kubernetes.io/default-container: manager
1821
labels:
1922
control-plane: ssp-operator

data/olm-catalog/ssp-operator.clusterserviceversion.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ spec:
368368
metadata:
369369
annotations:
370370
kubectl.kubernetes.io/default-container: manager
371+
openshift.io/required-scc: restricted-v2
371372
labels:
372373
control-plane: ssp-operator
373374
name: ssp-operator

internal/common/labels.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const (
1515
AppKubernetesManagedByValue string = "ssp-operator"
1616
)
1717

18+
const (
19+
RequiredSCCAnnotationValue = "restricted-v2"
20+
)
21+
1822
type AppComponent string
1923

2024
func (a AppComponent) String() string {

internal/operands/template-validator/reconcile_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9+
securityv1 "github.com/openshift/api/security/v1"
910

1011
admission "k8s.io/api/admissionregistration/v1"
1112
apps "k8s.io/api/apps/v1"
@@ -396,6 +397,18 @@ var _ = Describe("Template validator operand", func() {
396397
Entry("with specific nodeAffinity, podAffinity and podAntiAffinity", []func(*common.Request){setNodeAffinity, setPodAffinity, setPodAntiAffinity}, nodeAffinity, podAffinity, mergedPodAntiAffinity),
397398
)
398399
})
400+
401+
It("should add openshift.io/required-scc annotation to deployment", func() {
402+
_, err := operand.Reconcile(&request)
403+
Expect(err).ToNot(HaveOccurred())
404+
405+
key := client.ObjectKeyFromObject(newDeployment(namespace, replicas, "test-img"))
406+
deployment := &apps.Deployment{}
407+
Expect(request.Client.Get(request.Context, key, deployment)).To(Succeed())
408+
409+
Expect(deployment.Annotations).To(HaveKeyWithValue(securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue))
410+
Expect(deployment.Spec.Template.Annotations).To(HaveKeyWithValue(securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue))
411+
})
399412
})
400413

401414
func updateDeploymentStatus(key client.ObjectKey, request *common.Request, updateFunc func(deploymentStatus *apps.DeploymentStatus)) {

internal/operands/template-validator/resources.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package template_validator
33
import (
44
"fmt"
55

6+
securityv1 "github.com/openshift/api/security/v1"
67
templatev1 "github.com/openshift/api/template/v1"
78
admission "k8s.io/api/admissionregistration/v1"
89
apps "k8s.io/api/apps/v1"
@@ -15,6 +16,7 @@ import (
1516
kubevirt "kubevirt.io/api/core"
1617
kubevirtv1 "kubevirt.io/api/core/v1"
1718

19+
"kubevirt.io/ssp-operator/internal/common"
1820
"kubevirt.io/ssp-operator/internal/env"
1921
common_templates "kubevirt.io/ssp-operator/internal/operands/common-templates"
2022
metrics "kubevirt.io/ssp-operator/internal/operands/metrics"
@@ -160,6 +162,9 @@ func newDeployment(namespace string, replicas int32, image string) *apps.Deploym
160162
ObjectMeta: metav1.ObjectMeta{
161163
Name: DeploymentName,
162164
Namespace: namespace,
165+
Annotations: map[string]string{
166+
securityv1.RequiredSCCAnnotation: common.RequiredSCCAnnotationValue,
167+
},
163168
Labels: map[string]string{
164169
"name": DeploymentName,
165170
},
@@ -171,7 +176,10 @@ func newDeployment(namespace string, replicas int32, image string) *apps.Deploym
171176
},
172177
Template: core.PodTemplateSpec{
173178
ObjectMeta: metav1.ObjectMeta{
174-
Name: VirtTemplateValidator,
179+
Name: VirtTemplateValidator,
180+
Annotations: map[string]string{
181+
securityv1.RequiredSCCAnnotation: common.RequiredSCCAnnotationValue,
182+
},
175183
Labels: podLabels,
176184
},
177185
Spec: core.PodSpec{

internal/operands/vm-console-proxy/reconcile.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
ocpv1 "github.com/openshift/api/config/v1"
88
routev1 "github.com/openshift/api/route/v1"
9+
securityv1 "github.com/openshift/api/security/v1"
910
"github.com/openshift/library-go/pkg/crypto"
1011
apps "k8s.io/api/apps/v1"
1112
core "k8s.io/api/core/v1"
@@ -347,6 +348,8 @@ func reconcileDeployment(deployment apps.Deployment) common.ReconcileFunc {
347348
return func(request *common.Request) (common.ReconcileResult, error) {
348349
deployment.Namespace = request.Instance.Namespace
349350
deployment.Spec.Template.Spec.Containers[0].Image = getVmConsoleProxyImage()
351+
metav1.SetMetaDataAnnotation(&deployment.ObjectMeta, securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue)
352+
metav1.SetMetaDataAnnotation(&deployment.Spec.Template.ObjectMeta, securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue)
350353
common.AddAppLabels(request.Instance, operandName, operandComponent, &deployment.Spec.Template.ObjectMeta)
351354
return common.CreateOrUpdate(request).
352355
NamespacedResource(&deployment).

internal/operands/vm-console-proxy/reconcile_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
. "github.com/onsi/ginkgo/v2"
99
. "github.com/onsi/gomega"
10+
securityv1 "github.com/openshift/api/security/v1"
1011
"k8s.io/apimachinery/pkg/api/errors"
1112

1213
ocpv1 "github.com/openshift/api/config/v1"
@@ -354,6 +355,18 @@ var _ = Describe("VM Console Proxy Operand", func() {
354355
ExpectResourceNotExists(bundle.Deployment, request)
355356
ExpectResourceNotExists(bundle.ApiService, request)
356357
})
358+
359+
It("should add openshift.io/required-scc annotation to deployment", func() {
360+
_, err := operand.Reconcile(&request)
361+
Expect(err).ToNot(HaveOccurred())
362+
363+
key := client.ObjectKeyFromObject(bundle.Deployment)
364+
deployment := &apps.Deployment{}
365+
Expect(request.Client.Get(request.Context, key, deployment)).To(Succeed())
366+
367+
Expect(deployment.Annotations).To(HaveKeyWithValue(securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue))
368+
Expect(deployment.Spec.Template.Annotations).To(HaveKeyWithValue(securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue))
369+
})
357370
})
358371

359372
func TestVmConsoleProxyBundle(t *testing.T) {

tests/scc_annotation_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tests
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
7+
securityv1 "github.com/openshift/api/security/v1"
8+
apps "k8s.io/api/apps/v1"
9+
core "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/types"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
14+
"kubevirt.io/ssp-operator/internal/common"
15+
)
16+
17+
var _ = Describe("Required SCC annotation", func() {
18+
It("[test_id:TODO] SSP pods should have 'openshift.io/required-scc' annotation", func() {
19+
deployment := &apps.Deployment{}
20+
Expect(apiClient.Get(ctx, types.NamespacedName{
21+
Name: strategy.GetSSPDeploymentName(),
22+
Namespace: strategy.GetSSPDeploymentNameSpace(),
23+
}, deployment)).To(Succeed())
24+
25+
selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
26+
Expect(err).ToNot(HaveOccurred())
27+
28+
pods := &core.PodList{}
29+
Expect(apiClient.List(ctx, pods, client.MatchingLabelsSelector{Selector: selector})).To(Succeed())
30+
Expect(pods.Items).ToNot(BeEmpty())
31+
32+
for _, pod := range pods.Items {
33+
Expect(pod.Annotations).To(HaveKeyWithValue(securityv1.RequiredSCCAnnotation, common.RequiredSCCAnnotationValue),
34+
"SSP pod %s/%s does not have required annotation",
35+
pod.Namespace, pod.Name,
36+
)
37+
}
38+
})
39+
})

0 commit comments

Comments
 (0)