Skip to content

Commit ba65372

Browse files
authored
Merge pull request #4728 from kersten/chore/external-plugin-flag-parse-error-handling
📖 : Improve sample external plugin by adding error handling for flag parsing failures.
2 parents 61f6641 + 4f9670c commit ba65372

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/cmd/flags.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19+
"fmt"
20+
1921
"v1/scaffolds"
2022

2123
"github.com/spf13/pflag"
@@ -43,7 +45,13 @@ func flagsCmd(pr *external.PluginRequest) external.PluginResponse {
4345
flagsToParse.Bool("api", false, "sets the api flag to true")
4446
flagsToParse.Bool("webhook", false, "sets the webhook flag to true")
4547

46-
flagsToParse.Parse(pr.Args)
48+
if err := flagsToParse.Parse(pr.Args); err != nil {
49+
pluginResponse.Error = true
50+
pluginResponse.ErrorMsgs = []string{
51+
fmt.Sprintf("failed to parse flags: %s", err.Error()),
52+
}
53+
return pluginResponse
54+
}
4755

4856
initFlag, _ := flagsToParse.GetBool("init")
4957
apiFlag, _ := flagsToParse.GetBool("api")

docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/cmd/metadata.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19+
"fmt"
20+
1921
"v1/scaffolds"
2022

2123
"github.com/spf13/pflag"
@@ -41,7 +43,13 @@ func metadataCmd(pr *external.PluginRequest) external.PluginResponse {
4143
flagsToParse.Bool("api", false, "sets the api flag to true")
4244
flagsToParse.Bool("webhook", false, "sets the webhook flag to true")
4345

44-
flagsToParse.Parse(pr.Args)
46+
if err := flagsToParse.Parse(pr.Args); err != nil {
47+
pluginResponse.Error = true
48+
pluginResponse.ErrorMsgs = []string{
49+
fmt.Sprintf("failed to parse flags: %s", err.Error()),
50+
}
51+
return pluginResponse
52+
}
4553

4654
initFlag, _ := flagsToParse.GetBool("init")
4755
apiFlag, _ := flagsToParse.GetBool("api")

docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/init.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
package scaffolds
1717

1818
import (
19+
"fmt"
20+
1921
"v1/scaffolds/internal/templates"
2022

2123
"github.com/spf13/pflag"
@@ -55,7 +57,13 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {
5557
// Here is an example of parsing a flag from a Kubebuilder external plugin request
5658
flags := pflag.NewFlagSet("initFlags", pflag.ContinueOnError)
5759
flags.String("domain", "example.domain.com", "sets the domain added in the scaffolded initFile.txt")
58-
flags.Parse(pr.Args)
60+
if err := flags.Parse(pr.Args); err != nil {
61+
pluginResponse.Error = true
62+
pluginResponse.ErrorMsgs = []string{
63+
fmt.Sprintf("failed to parse flags: %s", err.Error()),
64+
}
65+
return pluginResponse
66+
}
5967
domain, _ := flags.GetString("domain")
6068

6169
initFile := templates.NewInitFile(templates.WithDomain(domain))

docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/webhook.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
package scaffolds
1717

1818
import (
19+
"fmt"
20+
1921
"github.com/spf13/pflag"
2022
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
2123
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/external"
@@ -52,7 +54,13 @@ func WebhookCmd(pr *external.PluginRequest) external.PluginResponse {
5254
// Here is an example of parsing a flag from a Kubebuilder external plugin request
5355
flags := pflag.NewFlagSet("apiFlags", pflag.ContinueOnError)
5456
flags.Bool("hooked", false, "add the word `hooked` to the end of the scaffolded webhookFile.txt")
55-
flags.Parse(pr.Args)
57+
if err := flags.Parse(pr.Args); err != nil {
58+
pluginResponse.Error = true
59+
pluginResponse.ErrorMsgs = []string{
60+
fmt.Sprintf("failed to parse flags: %s", err.Error()),
61+
}
62+
return pluginResponse
63+
}
5664
hooked, _ := flags.GetBool("hooked")
5765

5866
msg := "A simple text file created with the `create webhook` subcommand"

0 commit comments

Comments
 (0)