Skip to content

Commit a974e22

Browse files
authored
Add webhook to replicate operandrequest in partial watched namespace (#1059)
* Add webhook to replicate operandrequest in partial watched namespace Signed-off-by: Daniel Fan <[email protected]> * update setup-envtest Signed-off-by: Daniel Fan <[email protected]> * Add test cases Signed-off-by: Daniel Fan <[email protected]> * Update GetFilteredOpreqSpec name Signed-off-by: Daniel Fan <[email protected]> * wait for OperandRegistry before annotating OperandRequest Signed-off-by: Daniel Fan <[email protected]> --------- Signed-off-by: Daniel Fan <[email protected]>
1 parent 59e8aed commit a974e22

File tree

14 files changed

+1586
-0
lines changed

14 files changed

+1586
-0
lines changed

bundle/manifests/operand-deployment-lifecycle-manager.clusterserviceversion.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ spec:
573573
- get
574574
- list
575575
- watch
576+
- update
576577
- apiGroups:
577578
- operator.ibm.com
578579
resources:
@@ -618,6 +619,18 @@ spec:
618619
- patch
619620
- update
620621
- watch
622+
- apiGroups:
623+
- admissionregistration.k8s.io
624+
resources:
625+
- mutatingwebhookconfigurations
626+
verbs:
627+
- create
628+
- delete
629+
- get
630+
- list
631+
- patch
632+
- update
633+
- watch
621634
serviceAccountName: operand-deployment-lifecycle-manager
622635
deployments:
623636
- label:
@@ -705,9 +718,15 @@ spec:
705718
privileged: false
706719
readOnlyRootFilesystem: true
707720
runAsNonRoot: true
721+
volumeMounts:
722+
- mountPath: /etc/ssl/certs/webhook
723+
name: webhook-certs
708724
serviceAccount: operand-deployment-lifecycle-manager
709725
serviceAccountName: operand-deployment-lifecycle-manager
710726
terminationGracePeriodSeconds: 10
727+
volumes:
728+
- emptyDir: {}
729+
name: webhook-certs
711730
permissions:
712731
- rules:
713732
- apiGroups:

config/manager/manager.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,11 @@ spec:
9292
privileged: false
9393
readOnlyRootFilesystem: true
9494
runAsNonRoot: true
95+
volumeMounts:
96+
- mountPath: /etc/ssl/certs/webhook
97+
name: webhook-certs
9598
terminationGracePeriodSeconds: 10
9699
serviceAccount: operand-deployment-lifecycle-manager
100+
volumes:
101+
- emptyDir: {}
102+
name: webhook-certs

config/rbac/role.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rules:
1010
- get
1111
- list
1212
- watch
13+
- update
1314
apiGroups:
1415
- operator.ibm.com
1516
resources:
@@ -62,6 +63,18 @@ rules:
6263
- patch
6364
- update
6465
- watch
66+
- apiGroups:
67+
- admissionregistration.k8s.io
68+
resources:
69+
- mutatingwebhookconfigurations
70+
verbs:
71+
- create
72+
- delete
73+
- get
74+
- list
75+
- patch
76+
- update
77+
- watch
6578
---
6679
apiVersion: rbac.authorization.k8s.io/v1
6780
kind: Role

controllers/constant/constant.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
)
2222

2323
const (
24+
//OperatorName is the name of operator
25+
OperatorName string = "operand-deployment-lifecycle-manager"
26+
27+
//CSVName is the name of Operand Deployment Lifecycle Manager CSV
28+
CSVName string = "operand-deployment-lifecycle-manager.v1.21.13"
2429

2530
//ClusterOperatorNamespace is the namespace of cluster operators
2631
ClusterOperatorNamespace string = "openshift-operators"
@@ -52,6 +57,9 @@ const (
5257
//FindOperandRegistry is the key for checking if the OperandRegistry is found
5358
FindOperandRegistry string = "operator.ibm.com/operandregistry-is-not-found"
5459

60+
//OdlmManagedLabel is the label used to label the webhook managed by ODLM
61+
OdlmManagedLabel string = "operator.ibm.com/managedBy-odlm"
62+
5563
//HashedData is the key for checking the checksum of data section
5664
HashedData string = "hashedData"
5765

controllers/util/util.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
package util
1818

1919
import (
20+
"context"
21+
"errors"
2022
"os"
2123
"sort"
2224
"strconv"
2325
"strings"
2426
"sync"
2527
"time"
2628

29+
rbacv1 "k8s.io/api/rbac/v1"
30+
"k8s.io/apimachinery/pkg/types"
2731
"k8s.io/client-go/discovery"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
2833
)
2934

3035
// GetOperatorNamespace returns the Namespace of the operator
@@ -70,6 +75,23 @@ func GetoperatorCheckerMode() bool {
7075
return false
7176
}
7277

78+
func GetPartialWatchNamespace() string {
79+
ns, found := os.LookupEnv("PARTIAL_WATCH_NAMESPACE")
80+
if !found {
81+
return ""
82+
}
83+
return ns
84+
}
85+
86+
func PartialWatchNamespaceEnabled() bool {
87+
// If it is not found, it is enabled by default
88+
isEnabled, found := os.LookupEnv("ENABLE_PARTIAL_WATCH_NAMESPACE")
89+
if !found || isEnabled == "true" {
90+
return true
91+
}
92+
return false
93+
}
94+
7395
// ResourceExists returns true if the given resource kind exists
7496
// in the given api groupversion
7597
func ResourceExists(dc discovery.DiscoveryInterface, apiGroupVersion, kind string) (bool, error) {
@@ -188,3 +210,46 @@ func Contains(list []string, s string) bool {
188210
}
189211
return false
190212
}
213+
214+
// returns error, roleName, roleUID
215+
func GetClusterRoleDetails(kube client.Client, ns string, csvName string) (string, types.UID, error) {
216+
existingResource := &rbacv1.ClusterRoleList{}
217+
opts := []client.ListOption{
218+
client.MatchingLabels(map[string]string{
219+
"olm.owner.namespace": ns,
220+
"olm.owner": csvName,
221+
}),
222+
}
223+
err := kube.List(context.TODO(), existingResource, opts...)
224+
if err != nil {
225+
return "", "", err
226+
}
227+
switch len(existingResource.Items) {
228+
case 0:
229+
return "", "", errors.New("unable to find ClusterRole for operator " + csvName)
230+
default:
231+
// 1 or more ClusterRole returned so index first one
232+
return existingResource.Items[0].Name, existingResource.Items[0].UID, nil
233+
}
234+
}
235+
236+
func GetClusterRole(kube client.Client, ns string, csvName string) (*rbacv1.ClusterRole, error) {
237+
existingResource := &rbacv1.ClusterRoleList{}
238+
opts := []client.ListOption{
239+
client.MatchingLabels(map[string]string{
240+
"olm.owner.namespace": ns,
241+
"olm.owner": csvName,
242+
}),
243+
}
244+
err := kube.List(context.TODO(), existingResource, opts...)
245+
if err != nil {
246+
return nil, err
247+
}
248+
switch len(existingResource.Items) {
249+
case 0:
250+
return nil, errors.New("unable to find ClusterRole for operator " + csvName)
251+
default:
252+
// 1 or more ClusterRole returned so index first one
253+
return &existingResource.Items[0], nil
254+
}
255+
}

0 commit comments

Comments
 (0)