Skip to content

Commit

Permalink
Update handling of namespace watching:
Browse files Browse the repository at this point in the history
CLI flag to watch one or all namespaces.

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Oct 15, 2024
1 parent 8c7a9c8 commit 348742d
Showing 1 changed file with 34 additions and 10 deletions.
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 @@ import (
"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 @@ var version = "devel"
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 @@ func (c *Config) AddFlags(fs *pflag.FlagSet) {
"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 @@ func NewRootCommand() *cobra.Command {
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 @@ func NewRootCommand() *cobra.Command {
},
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 @@ func NewRootCommand() *cobra.Command {
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

0 comments on commit 348742d

Please sign in to comment.