Skip to content

Commit

Permalink
Merge pull request #1011 from jacobweinstock/iso-mounting
Browse files Browse the repository at this point in the history
Update handling of namespace watching:

## Description

<!--- Please describe what this PR is going to change -->
CLI flag to watch one or all namespaces.

## Why is this needed

<!--- Link to issue you have raised -->

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->


## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
jacobweinstock authored Oct 15, 2024
2 parents 8c7a9c8 + 348742d commit 1df09e2
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.")
}

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()
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: {}}}
}

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()

overrides := &clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: c.K8sAPI,
},
Context: clientcmdapi.Context{
Namespace: c.Namespace,
},
}
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)

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

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

return rc, ns, nil
}

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

0 comments on commit 1df09e2

Please sign in to comment.