Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of namespace watching: #1011

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions cmd/tink-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"github.com/tinkerbell/tink/internal/deprecated/controller"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

Expand All @@ -25,6 +27,7 @@
type Config struct {
K8sAPI string
Kubeconfig string // only applies to out of cluster
Namespace string
MetricsAddr string
ProbeAddr string
EnableLeaderElection bool
Expand All @@ -43,6 +46,7 @@
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
fs.IntVar(&c.LogLevel, "log-level", 0, "Log level (0: info, 1: debug)")
fs.StringVar(&c.Namespace, "namespace", "", "The namespace to watch for resources. Use empty string (with a ClusterRole) to watch all namespaces.")

Check warning on line 49 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L49

Added line #L49 was not covered by tests
}

func main() {
Expand Down Expand Up @@ -85,16 +89,7 @@
logger := zapr.NewLogger(zlog).WithName("github.com/tinkerbell/tink")
logger.Info("Starting controller version " + version)

ccfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: config.Kubeconfig},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: config.K8sAPI}})

cfg, err := ccfg.ClientConfig()
if err != nil {
return err
}

namespace, _, err := ccfg.Namespace()
cfg, namespace, err := config.getClient()

Check warning on line 92 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L92

Added line #L92 was not covered by tests
if err != nil {
return err
}
Expand All @@ -109,6 +104,9 @@
},
HealthProbeBindAddress: config.ProbeAddr,
}
if config.Namespace != "" {
options.Cache = cache.Options{DefaultNamespaces: map[string]cache.Config{namespace: {}}}

Check warning on line 108 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L107-L108

Added lines #L107 - L108 were not covered by tests
}

ctrl.SetLogger(logger)

Expand All @@ -124,6 +122,32 @@
return cmd
}

func (c *Config) getClient() (*rest.Config, string, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()

Check warning on line 126 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L125-L126

Added lines #L125 - L126 were not covered by tests

overrides := &clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: c.K8sAPI,
},
Context: clientcmdapi.Context{
Namespace: c.Namespace,
},

Check warning on line 134 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L128-L134

Added lines #L128 - L134 were not covered by tests
}
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)

Check warning on line 136 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L136

Added line #L136 was not covered by tests

rc, err := loader.ClientConfig()
if err != nil {
return nil, "", fmt.Errorf("getting client config: %w", err)

Check warning on line 140 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L138-L140

Added lines #L138 - L140 were not covered by tests
}

ns, _, err := loader.Namespace()
if err != nil {
return nil, "", fmt.Errorf("getting namespace: %w", err)

Check warning on line 145 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L143-L145

Added lines #L143 - L145 were not covered by tests
}

return rc, ns, nil

Check warning on line 148 in cmd/tink-controller/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/tink-controller/main.go#L148

Added line #L148 was not covered by tests
}

func createViper(logger logr.Logger) (*viper.Viper, error) {
v := viper.New()
v.AutomaticEnv()
Expand Down
Loading