Skip to content

fix resources parameter to accept just cpu and/or memory #377

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion cmd/eks-node-viewer/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ParseFlags() (Flags, error) {
flagSet.StringVar(&flags.Kubeconfig, "kubeconfig", kubeconfigDefault, "Absolute path to the kubeconfig file")

resourcesDefault := cfg.getValue("resources", "cpu")
flagSet.StringVar(&flags.Resources, "resources", resourcesDefault, "List of comma separated resources to monitor")
flagSet.StringVar(&flags.Resources, "resources", resourcesDefault, "List of comma separated resources to monitor (allowed: cpu, memory)")

disablePricingDefault := cfg.getBoolValue("disable-pricing", false)
flagSet.BoolVar(&flags.DisablePricing, "disable-pricing", disablePricingDefault, "Disable pricing lookups")
Expand All @@ -95,6 +95,12 @@ func ParseFlags() (Flags, error) {
if err := flagSet.Parse(os.Args[1:]); err != nil {
return Flags{}, err
}

// check flag contain cpu and/or memory
if err := validateResources(flags.Resources); err != nil {
return Flags{}, err
}

return flags, nil
}

Expand Down Expand Up @@ -159,3 +165,23 @@ func loadConfigFile() (configFile, error) {
}
return fileContent, nil
}

// validateResources ensures that the provided resources are only "cpu" and/or "memory"
func validateResources(res string) error {
valid := map[string]bool{
"cpu": true,
"memory": true,
}

// Split for multiple resources
for _, r := range strings.Split(res, ",") {
r = strings.TrimSpace(r)
if r == "" {
continue
}
if !valid[r] {
return fmt.Errorf("invalid resource: %q. Allowed resources are: cpu, memory", r)
}
}
return nil
}