Skip to content

Commit

Permalink
subclass flagset
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 committed Jun 17, 2022
1 parent a5f5db1 commit a743baa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion hack/docs/configuration_gen_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {

envVarsBlock := "| Environment Variable | CLI Flag | Description |\n"
envVarsBlock += "|--|--|--|\n"
opts.Flagset.VisitAll(func(f *flag.Flag) {
opts.VisitAll(func(f *flag.Flag) {
envVarsBlock += fmt.Sprintf("| %s | %s | %s |\n", strings.ReplaceAll(strings.ToUpper(f.Name), "-", "_"), "\\-\\-"+f.Name, f.Usage)
})

Expand Down
8 changes: 3 additions & 5 deletions pkg/utils/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

// Options for running this binary
type Options struct {
*flag.FlagSet
ClusterName string
ClusterEndpoint string
MetricsPort int
Expand All @@ -46,16 +47,13 @@ type Options struct {
AWSENILimitedPodDensity bool
AWSDefaultInstanceProfile string
AWSEnablePodENI bool

// Flagset is the backing flagset that is used to fill in the Options fields
Flagset *flag.FlagSet
}

// New creates an Options struct and registers CLI flags and environment variables to fill-in the Options struct fields
func New() Options {
opts := Options{}
f := flag.NewFlagSet("karpenter", flag.ContinueOnError)
opts.Flagset = f
opts.FlagSet = f
f.StringVar(&opts.ClusterName, "cluster-name", env.WithDefaultString("CLUSTER_NAME", ""), "The kubernetes cluster name for resource discovery")
f.StringVar(&opts.ClusterEndpoint, "cluster-endpoint", env.WithDefaultString("CLUSTER_ENDPOINT", ""), "The external kubernetes cluster endpoint for new nodes to connect with")
f.IntVar(&opts.MetricsPort, "metrics-port", env.WithDefaultInt("METRICS_PORT", 8080), "The port the metric endpoint binds to for operating metrics about the controller itself")
Expand All @@ -73,7 +71,7 @@ func New() Options {
// MustParse reads the user passed flags, environment variables, and default values.
// Options are valided and panics if an error is returned
func (o Options) MustParse() Options {
err := o.Flagset.Parse(os.Args[1:])
err := o.Parse(os.Args[1:])

if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
Expand Down

0 comments on commit a743baa

Please sign in to comment.