Skip to content

Commit

Permalink
fix helm e2e failures
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed Jan 25, 2024
1 parent 336d9ad commit d013ad4
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 17 deletions.
88 changes: 86 additions & 2 deletions hack/generate/samples/internal/helm/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (mh *Memcached) Run() {
pkg.CheckError("enabling prometheus metrics", err)

log.Infof("adding customized roles")
err = kbutil.InsertCode(filepath.Join(mh.ctx.Dir, "config", "rbac", "role.yaml"),
"rules:", policyRolesFragment)
err = kbutil.ReplaceInFile(filepath.Join(mh.ctx.Dir, "config", "rbac", "role.yaml"),
rolesFragmentReplaceTarget, policyRolesFragment)
pkg.CheckError("adding customized roles", err)

log.Infof("creating the bundle")
Expand All @@ -122,7 +122,91 @@ func (mh *Memcached) Run() {

const createdAt = `createdAt: "2022-11-08T17:26:37Z"`

const rolesFragmentReplaceTarget = `
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
`

const policyRolesFragment = `
##
## Base operator rules
##
# We need to get namespaces so the operator can read namespaces to ensure they exist
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
# We need to manage Helm release secrets
- apiGroups:
- ""
resources:
- secrets
verbs:
- "*"
# We need to create events on CRs about things happening during reconciliation
- apiGroups:
- ""
resources:
- events
verbs:
- create
##
## Rules for cache.example.com/v1alpha1, Kind: Memcached
##
- apiGroups:
- cache.example.com
resources:
- memcacheds
- memcacheds/status
- memcacheds/finalizers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
- services
- services/finalizers
- endpoints
- persistentvolumeclaims
- events
- configmaps
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
- deployments
- daemonsets
- replicasets
- statefulsets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
##
## Rules customized for cache.example.com/v1alpha1, Kind: Memcached
##
Expand Down
21 changes: 14 additions & 7 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func getWatchNamespaces(namespaces map[string]cache.Config) []string {
log.Info("Watching single namespace.")
return []string{namespace}
}
if len(namespaces) == 0 {
if namespaces == nil || len(namespaces) == 0 {

Check failure on line 247 in internal/cmd/helm-operator/run/cmd.go

View workflow job for this annotation

GitHub Actions / sanity

S1009: should omit nil check; len() for map[string]sigs.k8s.io/controller-runtime/pkg/cache.Config is defined as zero (gosimple)
log.Info(fmt.Sprintf("Watch namespaces not configured by environment variable %s or file. "+
"Watching all namespaces.", k8sutil.WatchNamespaceEnvVar))
return []string{metav1.NamespaceAll}
Expand Down Expand Up @@ -283,13 +283,20 @@ func buildNewCacheFunc(watchNamespaces []string, ws []watches.Watch, sch *apimac
}
defaultSelector := labels.NewSelector().Add(*req)

return func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
defaultNamespaces := map[string]cache.Config{}
for _, ns := range watchNamespaces {
// Don't specify any config values so the defaults specified
// a couple lines later are used
defaultNamespaces[ns] = cache.Config{}
defaultNamespaces := map[string]cache.Config{}
for _, ns := range watchNamespaces {
// Don't specify any config values so the defaults specified
// a couple lines later are used
defaultNamespaces[ns] = cache.Config{}
if ns == metav1.NamespaceAll {
defaultNamespaces[ns] = cache.Config{
LabelSelector: labels.Everything(),
}
}
}
fmt.Println("XXX Debug", "defaultNamespaces", defaultNamespaces)

return func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.ByObject = selectorsByObject
opts.DefaultLabelSelector = defaultSelector
opts.DefaultNamespaces = defaultNamespaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,29 @@ spec:
clusterPermissions:
- rules:
- apiGroups:
- policy
- ""
resources:
- namespaces
verbs:
- get
- apiGroups:
- ""
resources:
- secrets
verbs:
- '*'
- apiGroups:
- ""
resources:
- events
- poddisruptionbudgets
verbs:
- create
- apiGroups:
- cache.example.com
resources:
- memcacheds
- memcacheds/status
- memcacheds/finalizers
verbs:
- create
- delete
Expand All @@ -91,8 +110,42 @@ spec:
- apiGroups:
- ""
resources:
- serviceaccounts
- pods
- services
- services/finalizers
- endpoints
- persistentvolumeclaims
- events
- configmaps
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
- deployments
- daemonsets
- replicasets
- statefulsets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- policy
resources:
- events
- poddisruptionbudgets
verbs:
- create
- delete
Expand All @@ -104,10 +157,15 @@ spec:
- apiGroups:
- ""
resources:
- pods
- serviceaccounts
- services
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- authentication.k8s.io
Expand Down
82 changes: 78 additions & 4 deletions testdata/helm/memcached-operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,84 @@ metadata:
app.kubernetes.io/managed-by: kustomize
name: manager-role
rules:
##
## Base operator rules
##
# We need to get namespaces so the operator can read namespaces to ensure they exist
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
# We need to manage Helm release secrets
- apiGroups:
- ""
resources:
- secrets
verbs:
- "*"
# We need to create events on CRs about things happening during reconciliation
- apiGroups:
- ""
resources:
- events
verbs:
- create

##
## Rules for cache.example.com/v1alpha1, Kind: Memcached
##
- apiGroups:
- cache.example.com
resources:
- memcacheds
- memcacheds/status
- memcacheds/finalizers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
- services
- services/finalizers
- endpoints
- persistentvolumeclaims
- events
- configmaps
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
- deployments
- daemonsets
- replicasets
- statefulsets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch


##
## Rules customized for cache.example.com/v1alpha1, Kind: Memcached
##
Expand Down Expand Up @@ -41,7 +119,3 @@ rules:
- watch

#+kubebuilder:scaffold:rules

- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]

0 comments on commit d013ad4

Please sign in to comment.