Skip to content

Commit

Permalink
Add webhook to replicate operandrequest in partial watched namespace (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
Daniel-Fan authored Jun 23, 2024
1 parent 59e8aed commit a974e22
Show file tree
Hide file tree
Showing 14 changed files with 1,586 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ spec:
- get
- list
- watch
- update
- apiGroups:
- operator.ibm.com
resources:
Expand Down Expand Up @@ -618,6 +619,18 @@ spec:
- patch
- update
- watch
- apiGroups:
- admissionregistration.k8s.io
resources:
- mutatingwebhookconfigurations
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
serviceAccountName: operand-deployment-lifecycle-manager
deployments:
- label:
Expand Down Expand Up @@ -705,9 +718,15 @@ spec:
privileged: false
readOnlyRootFilesystem: true
runAsNonRoot: true
volumeMounts:
- mountPath: /etc/ssl/certs/webhook
name: webhook-certs
serviceAccount: operand-deployment-lifecycle-manager
serviceAccountName: operand-deployment-lifecycle-manager
terminationGracePeriodSeconds: 10
volumes:
- emptyDir: {}
name: webhook-certs
permissions:
- rules:
- apiGroups:
Expand Down
6 changes: 6 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,11 @@ spec:
privileged: false
readOnlyRootFilesystem: true
runAsNonRoot: true
volumeMounts:
- mountPath: /etc/ssl/certs/webhook
name: webhook-certs
terminationGracePeriodSeconds: 10
serviceAccount: operand-deployment-lifecycle-manager
volumes:
- emptyDir: {}
name: webhook-certs
13 changes: 13 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
- get
- list
- watch
- update
apiGroups:
- operator.ibm.com
resources:
Expand Down Expand Up @@ -62,6 +63,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- admissionregistration.k8s.io
resources:
- mutatingwebhookconfigurations
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand Down
8 changes: 8 additions & 0 deletions controllers/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
)

const (
//OperatorName is the name of operator
OperatorName string = "operand-deployment-lifecycle-manager"

//CSVName is the name of Operand Deployment Lifecycle Manager CSV
CSVName string = "operand-deployment-lifecycle-manager.v1.21.13"

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

//OdlmManagedLabel is the label used to label the webhook managed by ODLM
OdlmManagedLabel string = "operator.ibm.com/managedBy-odlm"

//HashedData is the key for checking the checksum of data section
HashedData string = "hashedData"

Expand Down
65 changes: 65 additions & 0 deletions controllers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package util

import (
"context"
"errors"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"

rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// GetOperatorNamespace returns the Namespace of the operator
Expand Down Expand Up @@ -70,6 +75,23 @@ func GetoperatorCheckerMode() bool {
return false
}

func GetPartialWatchNamespace() string {
ns, found := os.LookupEnv("PARTIAL_WATCH_NAMESPACE")
if !found {
return ""
}
return ns
}

func PartialWatchNamespaceEnabled() bool {
// If it is not found, it is enabled by default
isEnabled, found := os.LookupEnv("ENABLE_PARTIAL_WATCH_NAMESPACE")
if !found || isEnabled == "true" {
return true
}
return false
}

// ResourceExists returns true if the given resource kind exists
// in the given api groupversion
func ResourceExists(dc discovery.DiscoveryInterface, apiGroupVersion, kind string) (bool, error) {
Expand Down Expand Up @@ -188,3 +210,46 @@ func Contains(list []string, s string) bool {
}
return false
}

// returns error, roleName, roleUID
func GetClusterRoleDetails(kube client.Client, ns string, csvName string) (string, types.UID, error) {
existingResource := &rbacv1.ClusterRoleList{}
opts := []client.ListOption{
client.MatchingLabels(map[string]string{
"olm.owner.namespace": ns,
"olm.owner": csvName,
}),
}
err := kube.List(context.TODO(), existingResource, opts...)
if err != nil {
return "", "", err
}
switch len(existingResource.Items) {
case 0:
return "", "", errors.New("unable to find ClusterRole for operator " + csvName)
default:
// 1 or more ClusterRole returned so index first one
return existingResource.Items[0].Name, existingResource.Items[0].UID, nil
}
}

func GetClusterRole(kube client.Client, ns string, csvName string) (*rbacv1.ClusterRole, error) {
existingResource := &rbacv1.ClusterRoleList{}
opts := []client.ListOption{
client.MatchingLabels(map[string]string{
"olm.owner.namespace": ns,
"olm.owner": csvName,
}),
}
err := kube.List(context.TODO(), existingResource, opts...)
if err != nil {
return nil, err
}
switch len(existingResource.Items) {
case 0:
return nil, errors.New("unable to find ClusterRole for operator " + csvName)
default:
// 1 or more ClusterRole returned so index first one
return &existingResource.Items[0], nil
}
}
Loading

0 comments on commit a974e22

Please sign in to comment.