Skip to content

Commit 57b6035

Browse files
Add kubectl-nfd
kubectl-nfd is a kubectl plugin for debbuging NodeFeatureRules Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent 1a5659f commit 57b6035

File tree

16 files changed

+981
-11
lines changed

16 files changed

+981
-11
lines changed

Makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: all test templates yamls
1+
.PHONY: all test templates yamls build build-%
22
.FORCE:
33

44
GO_CMD ?= go
@@ -90,12 +90,17 @@ IMAGE_BUILD_ARGS_MINIMAL = --target minimal \
9090

9191
all: image
9292

93-
build:
94-
@mkdir -p bin
95-
$(GO_CMD) build -v -o bin $(BUILD_FLAGS) ./cmd/...
93+
BUILD_BINARIES := nfd-master nfd-worker nfd-topology-updater nfd-gc kubectl-nfd
9694

97-
install:
98-
$(GO_CMD) install -v $(BUILD_FLAGS) ./cmd/...
95+
build-%:
96+
$(GO_CMD) build -v -o bin $(BUILD_FLAGS) ./cmd/$*
97+
98+
build: $(foreach bin, $(BUILD_BINARIES), build-$(bin))
99+
100+
install-%:
101+
$(GO_CMD) install -v $(BUILD_FLAGS) ./cmd/$*
102+
103+
install: $(foreach bin, $(BUILD_BINARIES), install-$(bin))
99104

100105
image: yamls
101106
$(IMAGE_BUILD_CMD) $(IMAGE_BUILD_ARGS) $(IMAGE_BUILD_ARGS_FULL)

cmd/kubectl-nfd/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import "sigs.k8s.io/node-feature-discovery/cmd/kubectl-nfd/subcmd"
20+
21+
const (
22+
// ProgramName is the canonical name of this program
23+
ProgramName = "kubectl-nfd"
24+
)
25+
26+
func main() {
27+
subcmd.Execute()
28+
}

cmd/kubectl-nfd/subcmd/dryrun.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package subcmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
kubectlnfd "sigs.k8s.io/node-feature-discovery/pkg/kubectl-nfd"
25+
)
26+
27+
var dryrunCmd = &cobra.Command{
28+
Use: "dryrun",
29+
Short: "Process a NodeFeatureRule file against a NodeFeature file",
30+
Long: `Process a NodeFeatureRule file against a local NodeFeature file to dry run the rule against a node before applying it to a cluster`,
31+
Run: func(cmd *cobra.Command, args []string) {
32+
fmt.Printf("Evaluating NodeFeatureRule %q against NodeFeature %q\n", nodefeaturerule, nodefeature)
33+
err := kubectlnfd.DryRun(nodefeaturerule, nodefeature)
34+
if len(err) > 0 {
35+
fmt.Printf("NodeFeatureRule %q is not valid for NodeFeature %q\n", nodefeaturerule, nodefeature)
36+
for _, e := range err {
37+
cmd.PrintErrln(e)
38+
}
39+
// Return non-zero exit code to indicate failure
40+
os.Exit(1)
41+
}
42+
fmt.Printf("NodeFeatureRule %q is valid for NodeFeature %q\n", nodefeaturerule, nodefeature)
43+
},
44+
}
45+
46+
func init() {
47+
RootCmd.AddCommand(dryrunCmd)
48+
49+
dryrunCmd.Flags().StringVarP(&nodefeaturerule, "nodefeaturerule-file", "f", "", "Path to the NodeFeatureRule file to validate")
50+
dryrunCmd.Flags().StringVarP(&nodefeature, "nodefeature-file", "n", "", "Path to the NodeFeature file to validate against")
51+
err := dryrunCmd.MarkFlagRequired("nodefeaturerule-file")
52+
if err != nil {
53+
panic(err)
54+
}
55+
err = dryrunCmd.MarkFlagRequired("nodefeature-file")
56+
if err != nil {
57+
panic(err)
58+
}
59+
}

cmd/kubectl-nfd/subcmd/root.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package subcmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var (
27+
// Path to the NodeFeatureRule file to validate
28+
nodefeaturerule string
29+
// Path to the NodeFeature file to run against the NodeFeatureRule
30+
nodefeature string
31+
// Node to validate against
32+
node string
33+
// kubeconfig file to use
34+
kubeconfig string
35+
)
36+
37+
// RootCmd represents the base command when called without any subcommands
38+
var RootCmd = &cobra.Command{
39+
Use: "kubectl-nfd",
40+
Short: "NFD kubectl plugin",
41+
Long: `kubectl plugin for NFD
42+
Debug tool to validate/dryrun/test NodeFeatureRules
43+
for more information see:
44+
https://kubernetes-sigs.github.io/node-feature-discovery/v0.14/usage/customization-guide.html#nodefeaturerule-custom-resource`,
45+
}
46+
47+
// Execute adds all child commands to the root command and sets flags appropriately.
48+
// This is called by main.main(). It only needs to happen once to the rootCmd.
49+
func Execute() {
50+
if err := RootCmd.Execute(); err != nil {
51+
fmt.Println(err)
52+
os.Exit(1)
53+
}
54+
}

cmd/kubectl-nfd/subcmd/test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package subcmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
kubectlnfd "sigs.k8s.io/node-feature-discovery/pkg/kubectl-nfd"
25+
)
26+
27+
var testCmd = &cobra.Command{
28+
Use: "test",
29+
Short: "Test a NodeFeatureRule file against a Node",
30+
Long: `Test a NodeFeatureRule file against a Node to ensure it is valid before applying it to a cluster`,
31+
Run: func(cmd *cobra.Command, args []string) {
32+
fmt.Printf("Evaluating NodeFeatureRule against Node %s\n", node)
33+
err := kubectlnfd.Test(nodefeaturerule, node, kubeconfig)
34+
if len(err) > 0 {
35+
fmt.Printf("NodeFeatureRule is not valid for Node %s\n", node)
36+
for _, e := range err {
37+
cmd.PrintErrln(e)
38+
}
39+
// Return non-zero exit code to indicate failure
40+
os.Exit(1)
41+
}
42+
fmt.Printf("NodeFeatureRule is valid for Node %s\n", node)
43+
},
44+
}
45+
46+
func init() {
47+
RootCmd.AddCommand(testCmd)
48+
49+
testCmd.Flags().StringVarP(&nodefeaturerule, "nodefeaturerule-file", "f", "", "Path to the NodeFeatureRule file to validate")
50+
testCmd.Flags().StringVarP(&node, "nodename", "n", "", "Node to validate against")
51+
testCmd.Flags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "kubeconfig file to use")
52+
err := testCmd.MarkFlagRequired("nodefeaturerule-file")
53+
if err != nil {
54+
panic(err)
55+
}
56+
}

cmd/kubectl-nfd/subcmd/validate.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package subcmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
kubectlnfd "sigs.k8s.io/node-feature-discovery/pkg/kubectl-nfd"
25+
)
26+
27+
var validateCmd = &cobra.Command{
28+
Use: "validate",
29+
Short: "Validate a NodeFeatureRule file",
30+
Long: `Validate a NodeFeatureRule file to ensure it is valid before applying it to a cluster`,
31+
Run: func(cmd *cobra.Command, args []string) {
32+
fmt.Printf("Validating NodeFeatureRule %s\n", nodefeaturerule)
33+
err := kubectlnfd.ValidateNFR(nodefeaturerule)
34+
if len(err) > 0 {
35+
fmt.Printf("NodeFeatureRule %s is not valid\n", nodefeaturerule)
36+
for _, e := range err {
37+
cmd.PrintErrln(e)
38+
}
39+
// Return non-zero exit code to indicate failure
40+
os.Exit(1)
41+
}
42+
fmt.Printf("NodeFeatureRule %s is valid\n", nodefeaturerule)
43+
},
44+
}
45+
46+
func init() {
47+
RootCmd.AddCommand(validateCmd)
48+
49+
validateCmd.Flags().StringVarP(&nodefeaturerule, "nodefeaturerule-file", "f", "", "Path to the NodeFeatureRule file to validate")
50+
err := validateCmd.MarkFlagRequired("nodefeaturerule-file")
51+
if err != nil {
52+
panic(err)
53+
}
54+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: "Kubectl plugin cmdline reference"
3+
layout: default
4+
sort: 8
5+
---
6+
7+
# Commandline flags of kubectl-nfd (plugin)
8+
{: .no_toc}
9+
10+
## Table of contents
11+
{: .no_toc .text-delta}
12+
13+
1. TOC
14+
{:toc}
15+
16+
---
17+
18+
To quickly view available command line flags execute `kubectl nfd -help`.
19+
20+
### -h, -help
21+
22+
Print usage and exit.
23+
24+
## Validate
25+
26+
Validate a NodeFeatureRule file.
27+
28+
### -f / --nodefeature-file
29+
30+
The `--nodefeature-file` flag specifies the path to the NodeFeatureRule file
31+
to validate.
32+
33+
## Test
34+
35+
Test a NodeFeatureRule file against a node without applying it.
36+
37+
### -k, --kubeconfig
38+
39+
The `--kubeconfig` flag specifies the path to the kubeconfig file to use for
40+
CLI requests.
41+
42+
### -s, --namespace
43+
44+
The `--namespace` flag specifies the namespace to use for CLI requests.
45+
Default: `default`.
46+
47+
### -n, --nodename
48+
49+
The `--nodename` flag specifies the name of the node to test the
50+
NodeFeatureRule against.
51+
52+
### -f, --nodefeaturerule-file
53+
54+
The `--nodefeaturerule-file` flag specifies the path to the NodeFeatureRule file
55+
to test.
56+
57+
## DryRun
58+
59+
Process a NodeFeatureRule file against a NodeFeature file.
60+
61+
### -f, --nodefeaturerule-file
62+
63+
The `--nodefeaturerule-file` flag specifies the path to the NodeFeatureRule file
64+
to test.
65+
66+
### -n, --nodefeature-file
67+
68+
The `--nodefeature-file` flag specifies the path to the NodeFeature file to test.

docs/reference/versions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Versions"
33
layout: default
4-
sort: 8
4+
sort: 9
55
---
66

77
# Versions and deprecation

0 commit comments

Comments
 (0)