Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert options to CLI args #472

Open
displague opened this issue Oct 24, 2023 · 8 comments
Open

convert options to CLI args #472

displague opened this issue Oct 24, 2023 · 8 comments
Labels
kind/documentation Categorizes issue or PR as related to documentation. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@displague
Copy link
Member

displague commented Oct 24, 2023

The README.md today reflects only four options that can be defined via CLI. The pattern of METAL_ env vars and JSON config files (cloud-sa.json) is consistent with how Metal CLI is managed today: https://github.com/equinix/metal-cli/blob/main/internal/cli/root.go#L116-L171. By making the options (defined in README.md) CLI args, users will have a full set of methods to define options: args, env, config. This will improve the user experience through better discoverability and documentation of options.

As in Metal CLI, Viper could be used to replace the environment fetching code in CPEM:

// getMetalConfig returns a Config struct from a cloud config JSON file or environment variables
func getMetalConfig(providerConfig io.Reader) (Config, error) {
// get our token and project
var config, rawConfig Config
// providerConfig may be nil if no --cloud-config is provided
if providerConfig != nil {
configBytes, err := io.ReadAll(providerConfig)
if err != nil {
return config, fmt.Errorf("failed to read configuration : %w", err)
}
err = json.Unmarshal(configBytes, &rawConfig)
if err != nil {
return config, fmt.Errorf("failed to process json of configuration file at path %s: %w", providerConfig, err)
}
}
// read env vars; if not set, use rawConfig
config.AuthToken = override(os.Getenv(apiKeyName), rawConfig.AuthToken)
config.ProjectID = override(os.Getenv(projectIDName), rawConfig.ProjectID)
config.LoadBalancerSetting = override(os.Getenv(loadBalancerSettingName), rawConfig.LoadBalancerSetting)
config.Facility = override(os.Getenv(facilityName), rawConfig.Facility)
config.Metro = override(os.Getenv(metroName), rawConfig.Metro)
if config.AuthToken == "" {
return config, fmt.Errorf("environment variable %q is required if not in config file", apiKeyName)
}
if config.ProjectID == "" {
return config, fmt.Errorf("environment variable %q is required if not in config file", projectIDName)
}
// get the local ASN
localASN := os.Getenv(envVarLocalASN)
switch {
case localASN != "":
localASNNo, err := strconv.Atoi(localASN)
if err != nil {
return config, fmt.Errorf("env var %s must be a number, was %s: %w", envVarLocalASN, localASN, err)
}
config.LocalASN = localASNNo
case rawConfig.LocalASN != 0:
config.LocalASN = rawConfig.LocalASN
default:
config.LocalASN = DefaultLocalASN
}
config.BGPPass = override(os.Getenv(envVarBGPPass), rawConfig.BGPPass)
// set the annotations
config.AnnotationLocalASN = override(os.Getenv(envVarAnnotationLocalASN), rawConfig.AnnotationLocalASN, DefaultAnnotationNodeASN)
config.AnnotationPeerASN = override(os.Getenv(envVarAnnotationPeerASN), rawConfig.AnnotationPeerASN, DefaultAnnotationPeerASN)
config.AnnotationPeerIP = override(os.Getenv(envVarAnnotationPeerIP), rawConfig.AnnotationPeerIP, DefaultAnnotationPeerIP)
config.AnnotationSrcIP = override(os.Getenv(envVarAnnotationSrcIP), rawConfig.AnnotationSrcIP, DefaultAnnotationSrcIP)
config.AnnotationBGPPass = override(os.Getenv(envVarAnnotationBGPPass), rawConfig.AnnotationBGPPass, DefaultAnnotationBGPPass)
config.AnnotationNetworkIPv4Private = override(os.Getenv(envVarAnnotationNetworkIPv4Private), rawConfig.AnnotationNetworkIPv4Private, DefaultAnnotationNetworkIPv4Private)
config.AnnotationEIPMetro = override(os.Getenv(envVarAnnotationEIPMetro), rawConfig.AnnotationEIPMetro, DefaultAnnotationEIPMetro)
config.AnnotationEIPFacility = override(os.Getenv(envVarAnnotationEIPFacility), rawConfig.AnnotationEIPFacility, DefaultAnnotationEIPFacility)
config.EIPTag = override(os.Getenv(envVarEIPTag), rawConfig.EIPTag)
apiServer := os.Getenv(envVarAPIServerPort)
switch {
case apiServer != "":
apiServerNo, err := strconv.Atoi(apiServer)
if err != nil {
return config, fmt.Errorf("env var %s must be a number, was %s: %w", envVarAPIServerPort, apiServer, err)
}
config.APIServerPort = int32(apiServerNo)
case rawConfig.APIServerPort != 0:
config.APIServerPort = rawConfig.APIServerPort
default:
// if nothing else set it, we set it to 0, to indicate that it should use whatever the kube-apiserver port is
config.APIServerPort = 0
}
config.BGPNodeSelector = override(os.Getenv(envVarBGPNodeSelector), rawConfig.BGPNodeSelector)
if _, err := labels.Parse(config.BGPNodeSelector); err != nil {
return config, fmt.Errorf("BGP Node Selector must be valid Kubernetes selector: %w", err)
}
config.EIPHealthCheckUseHostIP = rawConfig.EIPHealthCheckUseHostIP
if v := os.Getenv(envVarEIPHealthCheckUseHostIP); v != "" {
useHostIP, err := strconv.ParseBool(v)
if err != nil {
return config, fmt.Errorf("env var %s must be a boolean, was %s: %w", envVarEIPHealthCheckUseHostIP, v, err)
}
config.EIPHealthCheckUseHostIP = useHostIP
}
return config, nil
}

Once these arguments are defined via the CLI, they can included in the --help output and included in generated markdown generation (replacing the hand-maintained README.md table):

diff --git a/main.go b/main.go
index bc35f40..dcc4908 100644
--- a/main.go
+++ b/main.go
@@ -9,6 +9,8 @@ import (
        cloudcontrollerconfig "k8s.io/cloud-provider/app/config"
        "k8s.io/cloud-provider/options"
        "k8s.io/component-base/cli"
+       "github.com/spf13/cobra/doc"
+       "github.com/spf13/cobra"
        cliflag "k8s.io/component-base/cli/flag"
        _ "k8s.io/component-base/logs/json/register"
        _ "k8s.io/component-base/metrics/prometheus/clientgo" // for client metric registration
@@ -34,6 +36,25 @@ func main() {
 
        fss := cliflag.NamedFlagSets{}
        command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, controllerInitializers, fss, wait.NeverStop)
+       command.AddCommand(&cobra.Command{
+               Use:   `docs <destination>`,
+               Short: "Generate command markdown documentation.",
+               Long:  "Generates command markdown documentation in the specified directory. Each command gets a markdown file.",
+               Example: `  # Generate documentation in the ./docs directory:
+       metal docs ./docs`,
+
+               DisableFlagsInUseLine: true,
+               Args:                  cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
+               PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
+                       cmd.SilenceUsage = true
+                       return nil
+               },
+               RunE: func(cmd *cobra.Command, args []string) error {
+                       cmd.SilenceUsage = true
+                       dest := args[0]
+                       return doc.GenMarkdownTree(cmd.Parent(), dest)
+               },
+       })
        code := cli.Run(command)
        os.Exit(code)
 }

The generated documentation could then be included in the docs/ directory (docs/cmd/?).

@displague displague added the kind/documentation Categorizes issue or PR as related to documentation. label Oct 24, 2023
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 31, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Mar 1, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 31, 2024
@cprivitere
Copy link
Member

/reopen

@k8s-ci-robot k8s-ci-robot reopened this May 14, 2024
@k8s-ci-robot
Copy link
Contributor

@cprivitere: Reopened this issue.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@cprivitere
Copy link
Member

/remove-lifecycle rotten

@k8s-ci-robot k8s-ci-robot removed the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label May 14, 2024
@cprivitere
Copy link
Member

/triage accepted

@k8s-ci-robot k8s-ci-robot added the triage/accepted Indicates an issue or PR is ready to be actively worked on. label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/documentation Categorizes issue or PR as related to documentation. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

4 participants