From b1e66f81ab53a9ac3b5714f379b2c730057a0783 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Tue, 8 Sep 2020 20:08:28 +0300 Subject: [PATCH] Add label validation --- cmd/gotk/create.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/gotk/create.go b/cmd/gotk/create.go index 0afef7e5..5083fa2a 100644 --- a/cmd/gotk/create.go +++ b/cmd/gotk/create.go @@ -21,6 +21,8 @@ import ( "strings" "time" + "k8s.io/apimachinery/pkg/util/validation" + "github.com/spf13/cobra" ) @@ -47,10 +49,22 @@ func init() { func parseLabels() (map[string]string, error) { result := make(map[string]string) for _, label := range labels { + // validate key value pair parts := strings.Split(label, "=") if len(parts) != 2 { return nil, fmt.Errorf("invalid label format '%s', must be key=value", label) } + + // validate label name + if errors := validation.IsQualifiedName(parts[0]); len(errors) > 0 { + return nil, fmt.Errorf("invalid label '%s': %v", parts[0], errors) + } + + // validate label value + if errors := validation.IsValidLabelValue(parts[1]); len(errors) > 0 { + return nil, fmt.Errorf("invalid label value '%s': %v", parts[1], errors) + } + result[parts[0]] = parts[1] }