mirror of
https://github.com/fluxcd/flux2.git
synced 2026-04-24 20:38:51 +00:00
validate that object name adheres to RFC 1123 for flux create commands
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
parent
8c2983c958
commit
4a893b13f8
6 changed files with 77 additions and 4 deletions
|
|
@ -19,6 +19,7 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -51,6 +52,18 @@ func init() {
|
|||
createCmd.PersistentFlags().BoolVar(&createArgs.export, "export", false, "export in YAML format to stdout")
|
||||
createCmd.PersistentFlags().StringSliceVar(&createArgs.labels, "label", nil,
|
||||
"set labels on the resource (can specify multiple labels with commas: label1=value1,label2=value2)")
|
||||
createCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return fmt.Errorf("name is required")
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
if !validateObjectName(name) {
|
||||
return fmt.Errorf("name '%s' is invalid, it should adhere to standard defined in RFC 1123", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
rootCmd.AddCommand(createCmd)
|
||||
}
|
||||
|
||||
|
|
@ -150,3 +163,8 @@ func parseLabels() (map[string]string, error) {
|
|||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func validateObjectName(name string) bool {
|
||||
r := regexp.MustCompile("^[a-z0-9]([a-z0-9\\-]){0,61}[a-z0-9]$")
|
||||
return r.MatchString(name)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue