From 32e9012a2a60e50f0b8882956c75f703318e30f2 Mon Sep 17 00:00:00 2001 From: Nicolo Dalla Valentina Date: Wed, 19 Feb 2025 11:48:45 +0000 Subject: [PATCH] fix resource parameter to accept just cpu and/or memory --- cmd/eks-node-viewer/flag.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/cmd/eks-node-viewer/flag.go b/cmd/eks-node-viewer/flag.go index 4adf8ea..e1cff45 100644 --- a/cmd/eks-node-viewer/flag.go +++ b/cmd/eks-node-viewer/flag.go @@ -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") @@ -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 } @@ -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 +}