Skip to content

Commit 1df09e2

Browse files
Merge pull request #1011 from jacobweinstock/iso-mounting
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
2 parents 8c7a9c8 + 348742d commit 1df09e2

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

cmd/tink-controller/main.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
"github.com/tinkerbell/tink/internal/deprecated/controller"
1414
"go.uber.org/zap"
1515
"go.uber.org/zap/zapcore"
16+
"k8s.io/client-go/rest"
1617
"k8s.io/client-go/tools/clientcmd"
1718
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1819
ctrl "sigs.k8s.io/controller-runtime"
20+
"sigs.k8s.io/controller-runtime/pkg/cache"
1921
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
2022
)
2123

@@ -25,6 +27,7 @@ var version = "devel"
2527
type Config struct {
2628
K8sAPI string
2729
Kubeconfig string // only applies to out of cluster
30+
Namespace string
2831
MetricsAddr string
2932
ProbeAddr string
3033
EnableLeaderElection bool
@@ -43,6 +46,7 @@ func (c *Config) AddFlags(fs *pflag.FlagSet) {
4346
"Enable leader election for controller manager. "+
4447
"Enabling this will ensure there is only one active controller manager.")
4548
fs.IntVar(&c.LogLevel, "log-level", 0, "Log level (0: info, 1: debug)")
49+
fs.StringVar(&c.Namespace, "namespace", "", "The namespace to watch for resources. Use empty string (with a ClusterRole) to watch all namespaces.")
4650
}
4751

4852
func main() {
@@ -85,16 +89,7 @@ func NewRootCommand() *cobra.Command {
8589
logger := zapr.NewLogger(zlog).WithName("github.com/tinkerbell/tink")
8690
logger.Info("Starting controller version " + version)
8791

88-
ccfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
89-
&clientcmd.ClientConfigLoadingRules{ExplicitPath: config.Kubeconfig},
90-
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: config.K8sAPI}})
91-
92-
cfg, err := ccfg.ClientConfig()
93-
if err != nil {
94-
return err
95-
}
96-
97-
namespace, _, err := ccfg.Namespace()
92+
cfg, namespace, err := config.getClient()
9893
if err != nil {
9994
return err
10095
}
@@ -109,6 +104,9 @@ func NewRootCommand() *cobra.Command {
109104
},
110105
HealthProbeBindAddress: config.ProbeAddr,
111106
}
107+
if config.Namespace != "" {
108+
options.Cache = cache.Options{DefaultNamespaces: map[string]cache.Config{namespace: {}}}
109+
}
112110

113111
ctrl.SetLogger(logger)
114112

@@ -124,6 +122,32 @@ func NewRootCommand() *cobra.Command {
124122
return cmd
125123
}
126124

125+
func (c *Config) getClient() (*rest.Config, string, error) {
126+
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
127+
128+
overrides := &clientcmd.ConfigOverrides{
129+
ClusterInfo: clientcmdapi.Cluster{
130+
Server: c.K8sAPI,
131+
},
132+
Context: clientcmdapi.Context{
133+
Namespace: c.Namespace,
134+
},
135+
}
136+
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
137+
138+
rc, err := loader.ClientConfig()
139+
if err != nil {
140+
return nil, "", fmt.Errorf("getting client config: %w", err)
141+
}
142+
143+
ns, _, err := loader.Namespace()
144+
if err != nil {
145+
return nil, "", fmt.Errorf("getting namespace: %w", err)
146+
}
147+
148+
return rc, ns, nil
149+
}
150+
127151
func createViper(logger logr.Logger) (*viper.Viper, error) {
128152
v := viper.New()
129153
v.AutomaticEnv()

0 commit comments

Comments
 (0)