Skip to content

Commit

Permalink
Replace deprecated method use for CR v0.15.0
Browse files Browse the repository at this point in the history
This change replaces deprecated method usage with the new standards
defined by controller-runtime v0.15.0.

The changes are noted in the release notes:
https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Specifically we are changing:
    1. Usage of options.Namespace replaced by options.Cache.Namespaces
    2. Usage of MultiNamespacedCacheBuilder replaced by
       options.Cache{Namespaces} struct.
    3. wait.ErrWaitTimeout replaced by wait.Interrupted(err)

Signed-off-by: Brendan Shephard <bshephar@redhat.com>
  • Loading branch information
bshephar committed Jun 29, 2023
1 parent 3e140c4 commit 1a2ffd2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
10 changes: 5 additions & 5 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,20 @@ func run(cmd *cobra.Command, f *flags.Flags) {
log.V(1).Info(fmt.Sprintf("Setting namespace with value in %s", helmmgr.WatchNamespaceEnvVar))
if namespace == metav1.NamespaceAll {
log.Info("Watching all namespaces.")
options.Namespace = metav1.NamespaceAll
options.Cache.Namespaces = []string{metav1.NamespaceAll}
} else {
if strings.Contains(namespace, ",") {
log.Info("Watching multiple namespaces.")
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
options.Cache = cache.Options{Namespaces: strings.Split(namespace, ",")}
} else {
log.Info("Watching single namespace.")
options.Namespace = namespace
options.Cache.Namespaces = []string{namespace}
}
}
} else if options.Namespace == "" {
} else if len(options.Cache.Namespaces) == 0 {
log.Info(fmt.Sprintf("Watch namespaces not configured by environment variable %s or file. "+
"Watching all namespaces.", helmmgr.WatchNamespaceEnvVar))
options.Namespace = metav1.NamespaceAll
options.Cache.Namespaces = []string{metav1.NamespaceAll}
}

mgr, err := manager.New(cfg, options)
Expand Down
3 changes: 2 additions & 1 deletion internal/sdk/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllerutil_test

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -62,7 +63,7 @@ var _ = Describe("Controllerutil", func() {

It("should be cancellable", func() {
cancel()
Expect(WaitForDeletion(ctx, client, pod)).To(MatchError(wait.ErrWaitTimeout))
Expect(WaitForDeletion(ctx, client, pod)).To(MatchError(wait.Interrupted(fmt.Errorf("timed out waiting for the condition"))))
})

It("should succeed after pod is deleted", func() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func ConfigureWatchNamespaces(options *manager.Options, log logr.Logger) {
if len(namespaces) != 0 {
log.Info("watching namespaces", "namespaces", namespaces)
if len(namespaces) > 1 {
options.NewCache = cache.MultiNamespacedCacheBuilder(namespaces)
options.Cache = cache.Options{Namespaces: namespaces}
} else {
options.Namespace = namespaces[0]
options.Cache = cache.Options{Namespaces: []string{}}
}
return
}
log.Info("watching all namespaces")
options.Namespace = v1.NamespaceAll
options.Cache.Namespaces = []string{v1.NamespaceAll}
}

func lookupEnv() []string {
Expand Down
8 changes: 4 additions & 4 deletions pkg/manager/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ var _ = Describe("ConfigureWatchNamespaces", func() {

It("should watch all namespaces when no env set", func() {
ConfigureWatchNamespaces(&opts, log)
Expect(opts.Namespace).To(Equal(""))
Expect(opts.Cache.Namespaces).To(Equal(""))
Expect(opts.NewCache).To(BeNil())
})

It("should watch all namespaces when WATCH_NAMESPACE is empty", func() {
Expect(os.Setenv(WatchNamespaceEnvVar, ""))
ConfigureWatchNamespaces(&opts, log)
Expect(opts.Namespace).To(Equal(""))
Expect(opts.Cache.Namespaces).To(Equal(""))
Expect(opts.NewCache).To(BeNil())
})

It("should watch one namespace when WATCH_NAMESPACE is has one namespace", func() {
Expect(os.Setenv(WatchNamespaceEnvVar, "watch"))
ConfigureWatchNamespaces(&opts, log)
Expect(opts.Namespace).To(Equal("watch"))
Expect(opts.Cache.Namespaces).To(Equal("watch"))
Expect(opts.NewCache).To(BeNil())
})

Expand All @@ -81,7 +81,7 @@ var _ = Describe("ConfigureWatchNamespaces", func() {
ConfigureWatchNamespaces(&opts, log)

By("checking that a single-namespace watch is not configured")
Expect(opts.Namespace).To(Equal(""))
Expect(opts.Cache.Namespaces).To(Equal(""))

By("using the options NewCache function to create a cache")
c, err := opts.NewCache(cfg, cache.Options{})
Expand Down

0 comments on commit 1a2ffd2

Please sign in to comment.