Skip to content

Commit

Permalink
Add Kubernetes namespace override flag (#275)
Browse files Browse the repository at this point in the history
Add Kubernetes namespace override. See tinkerbell/hegel#106 for more details.
  • Loading branch information
mergify[bot] authored May 23, 2022
2 parents ab716aa + a068097 commit 625c6bc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/cmd/boots/boots
/cmd/boots/boots-*-*
coverage.txt
.vscode
23 changes: 13 additions & 10 deletions client/kubernetes/hardware_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,31 @@ type Finder struct {
clientFunc func() crclient.Client
cacheStarter func(context.Context) error
logger log.Logger
namespace string
}

// NewFinder returns a HardwareFinder that discovers hardware from Kubernetes.
//
// Callers must instantiate the client-side cache by calling Start() before use.
func NewFinder(logger log.Logger, k8sAPI, kubeconfig string) (*Finder, error) {
func NewFinder(logger log.Logger, k8sAPI, kubeconfig, kubeNamespace string) (*Finder, error) {
ccfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: k8sAPI}})
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: kubeconfig,
},
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: k8sAPI,
},
Context: clientcmdapi.Context{
Namespace: kubeNamespace,
},
},
)

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

namespace, _, err := ccfg.Namespace()
if err != nil {
return nil, errors.WithStack(err)
}

cluster, err := NewCluster(config)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -51,7 +55,6 @@ func NewFinder(logger log.Logger, k8sAPI, kubeconfig string) (*Finder, error) {
clientFunc: cluster.GetClient,
cacheStarter: cluster.Start,
logger: logger,
namespace: namespace,
}, nil
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/boots/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type config struct {
kubeconfig string
// kubeAPI is the Kubernetes API URL
kubeAPI string
// kubeNamespace is an override for the namespace the kubernetes client will watch.
kubeNamespace string
}

func main() {
Expand Down Expand Up @@ -256,7 +258,7 @@ func getFinders(l log.Logger, c *config, reporter client.Reporter) (client.Workf
return nil, nil, err
}
case "kubernetes":
kf, err := kubernetes.NewFinder(l, c.kubeAPI, c.kubeconfig)
kf, err := kubernetes.NewFinder(l, c.kubeAPI, c.kubeconfig, c.kubeNamespace)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -372,6 +374,7 @@ func newCLI(cfg *config, fs *flag.FlagSet) *ffcli.Command {
fs.StringVar(&cfg.extraKernelArgs, "extra-kernel-args", "", "Extra set of kernel args (k=v k=v) that are appended to the kernel cmdline when booting via iPXE.")
fs.StringVar(&cfg.kubeconfig, "kubeconfig", "", "The Kubernetes config file location. Only applies if DATA_MODEL_VERSION=kubernetes.")
fs.StringVar(&cfg.kubeAPI, "kubernetes", "", "The Kubernetes API URL, used for in-cluster client construction. Only applies if DATA_MODEL_VERSION=kubernetes.")
fs.StringVar(&cfg.kubeNamespace, "kube-namespace", "", "An optional Kubernetes namespace override to query hardware data from.")

return &ffcli.Command{
Name: name,
Expand Down
1 change: 1 addition & 0 deletions cmd/boots/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ FLAGS
-ipxe-remote-tftp-addr remote IP where iPXE binaries are served via TFTP. Overrides -tftp-addr.
-ipxe-tftp-addr local IP and port to listen on for serving iPXE binaries via TFTP (port must be 69). (default "0.0.0.0:69")
-ipxe-tftp-timeout local iPXE TFTP server requests timeout. (default "5s")
-kube-namespace An optional Kubernetes namespace override to query hardware data from.
-kubeconfig The Kubernetes config file location. Only applies if DATA_MODEL_VERSION=kubernetes.
-kubernetes The Kubernetes API URL, used for in-cluster client construction. Only applies if DATA_MODEL_VERSION=kubernetes.
-log-level log level. (default "info")
Expand Down

0 comments on commit 625c6bc

Please sign in to comment.