mirror of
https://github.com/fluxcd/flux2.git
synced 2026-02-23 16:11:48 +00:00
Merge pull request #2443 from SomtochiAma/log-bug
Validate that object name adheres to RFC 1123 for `flux create` commands
This commit is contained in:
commit
568c536c3c
21 changed files with 77 additions and 51 deletions
|
|
@ -19,6 +19,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -51,6 +52,18 @@ func init() {
|
||||||
createCmd.PersistentFlags().BoolVar(&createArgs.export, "export", false, "export in YAML format to stdout")
|
createCmd.PersistentFlags().BoolVar(&createArgs.export, "export", false, "export in YAML format to stdout")
|
||||||
createCmd.PersistentFlags().StringSliceVar(&createArgs.labels, "label", nil,
|
createCmd.PersistentFlags().StringSliceVar(&createArgs.labels, "label", nil,
|
||||||
"set labels on the resource (can specify multiple labels with commas: label1=value1,label2=value2)")
|
"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, the name can only contain alphanumeric characters or '-'", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
rootCmd.AddCommand(createCmd)
|
rootCmd.AddCommand(createCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -150,3 +163,8 @@ func parseLabels() (map[string]string, error) {
|
||||||
|
|
||||||
return result, nil
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,9 +63,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAlertCmdRun(cmd *cobra.Command, args []string) error {
|
func createAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("Alert name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if alertArgs.providerRef == "" {
|
if alertArgs.providerRef == "" {
|
||||||
|
|
|
||||||
|
|
@ -73,9 +73,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAlertProviderCmdRun(cmd *cobra.Command, args []string) error {
|
func createAlertProviderCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("Provider name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if alertProviderArgs.alertType == "" {
|
if alertProviderArgs.alertType == "" {
|
||||||
|
|
|
||||||
|
|
@ -139,9 +139,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
|
func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("HelmRelease name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if helmReleaseArgs.chart == "" {
|
if helmReleaseArgs.chart == "" {
|
||||||
|
|
|
||||||
|
|
@ -84,9 +84,6 @@ func (obj imagePolicyAdapter) getObservedGeneration() int64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createImagePolicyRun(cmd *cobra.Command, args []string) error {
|
func createImagePolicyRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("ImagePolicy name is required")
|
|
||||||
}
|
|
||||||
objectName := args[0]
|
objectName := args[0]
|
||||||
|
|
||||||
if imagePolicyArgs.imageRef == "" {
|
if imagePolicyArgs.imageRef == "" {
|
||||||
|
|
|
||||||
|
|
@ -83,9 +83,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createImageRepositoryRun(cmd *cobra.Command, args []string) error {
|
func createImageRepositoryRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("ImageRepository name is required")
|
|
||||||
}
|
|
||||||
objectName := args[0]
|
objectName := args[0]
|
||||||
|
|
||||||
if imageRepoArgs.image == "" {
|
if imageRepoArgs.image == "" {
|
||||||
|
|
|
||||||
|
|
@ -94,9 +94,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createImageUpdateRun(cmd *cobra.Command, args []string) error {
|
func createImageUpdateRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("ImageUpdateAutomation name is required")
|
|
||||||
}
|
|
||||||
objectName := args[0]
|
objectName := args[0]
|
||||||
|
|
||||||
if imageUpdateArgs.gitRepoName == "" {
|
if imageUpdateArgs.gitRepoName == "" {
|
||||||
|
|
|
||||||
|
|
@ -119,9 +119,6 @@ func NewKustomizationFlags() kustomizationFlags {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createKsCmdRun(cmd *cobra.Command, args []string) error {
|
func createKsCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("Kustomization name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if kustomizationArgs.path == "" {
|
if kustomizationArgs.path == "" {
|
||||||
|
|
|
||||||
|
|
@ -67,9 +67,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createReceiverCmdRun(cmd *cobra.Command, args []string) error {
|
func createReceiverCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("Receiver name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if receiverArgs.receiverType == "" {
|
if receiverArgs.receiverType == "" {
|
||||||
|
|
|
||||||
|
|
@ -112,9 +112,6 @@ func NewSecretGitFlags() secretGitFlags {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSecretGitCmdRun(cmd *cobra.Command, args []string) error {
|
func createSecretGitCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("secret name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
if secretGitArgs.url == "" {
|
if secretGitArgs.url == "" {
|
||||||
return fmt.Errorf("url is required")
|
return fmt.Errorf("url is required")
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ func TestCreateGitSecret(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "no args",
|
name: "no args",
|
||||||
args: "create secret git",
|
args: "create secret git",
|
||||||
assert: assertError("secret name is required"),
|
assert: assertError("name is required"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "basic secret",
|
name: "basic secret",
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
|
@ -68,9 +67,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSecretHelmCmdRun(cmd *cobra.Command, args []string) error {
|
func createSecretHelmCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("secret name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
labels, err := parseLabels()
|
labels, err := parseLabels()
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ func TestCreateHelmSecret(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
args: "create secret helm",
|
args: "create secret helm",
|
||||||
assert: assertError("secret name is required"),
|
assert: assertError("name is required"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
args: "create secret helm helm-secret --username=my-username --password=my-password --namespace=my-namespace --export",
|
args: "create secret helm helm-secret --username=my-username --password=my-password --namespace=my-namespace --export",
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
@ -67,9 +66,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSecretTLSCmdRun(cmd *cobra.Command, args []string) error {
|
func createSecretTLSCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("secret name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
labels, err := parseLabels()
|
labels, err := parseLabels()
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ func TestCreateTlsSecretNoArgs(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
args: "create secret tls",
|
args: "create secret tls",
|
||||||
assert: assertError("secret name is required"),
|
assert: assertError("name is required"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
args: "create secret tls certs --namespace=my-namespace --cert-file=./testdata/create_secret/tls/test-cert.pem --key-file=./testdata/create_secret/tls/test-key.pem --export",
|
args: "create secret tls certs --namespace=my-namespace --cert-file=./testdata/create_secret/tls/test-cert.pem --key-file=./testdata/create_secret/tls/test-key.pem --export",
|
||||||
|
|
|
||||||
|
|
@ -93,9 +93,6 @@ func NewSourceBucketFlags() sourceBucketFlags {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
|
func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("Bucket source name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if sourceBucketArgs.name == "" {
|
if sourceBucketArgs.name == "" {
|
||||||
|
|
|
||||||
|
|
@ -150,9 +150,6 @@ func newSourceGitFlags() sourceGitFlags {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
|
func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("GitRepository source name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if sourceGitArgs.url == "" {
|
if sourceGitArgs.url == "" {
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ func TestCreateSourceGit(t *testing.T) {
|
||||||
{
|
{
|
||||||
"NoArgs",
|
"NoArgs",
|
||||||
"create source git",
|
"create source git",
|
||||||
assertError("GitRepository source name is required"),
|
assertError("name is required"),
|
||||||
nil,
|
nil,
|
||||||
}, {
|
}, {
|
||||||
"Succeeded",
|
"Succeeded",
|
||||||
|
|
|
||||||
|
|
@ -91,9 +91,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
|
func createSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("HelmRepository source name is required")
|
|
||||||
}
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
|
||||||
if sourceHelmArgs.url == "" {
|
if sourceHelmArgs.url == "" {
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,6 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTenantCmdRun(cmd *cobra.Command, args []string) error {
|
func createTenantCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) < 1 {
|
|
||||||
return fmt.Errorf("tenant name is required")
|
|
||||||
}
|
|
||||||
tenant := args[0]
|
tenant := args[0]
|
||||||
if err := validation.IsQualifiedName(tenant); len(err) > 0 {
|
if err := validation.IsQualifiedName(tenant); len(err) > 0 {
|
||||||
return fmt.Errorf("invalid tenant name '%s': %v", tenant, err)
|
return fmt.Errorf("invalid tenant name '%s': %v", tenant, err)
|
||||||
|
|
|
||||||
55
cmd/flux/create_test.go
Normal file
55
cmd/flux/create_test.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_validateObjectName(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "flux-system",
|
||||||
|
valid: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "-flux-system",
|
||||||
|
valid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "-flux-system-",
|
||||||
|
valid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "third.first",
|
||||||
|
valid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "THirdfirst",
|
||||||
|
valid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "THirdfirst",
|
||||||
|
valid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: rand.String(63),
|
||||||
|
valid: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: rand.String(64),
|
||||||
|
valid: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
valid := validateObjectName(tt.name)
|
||||||
|
if valid != tt.valid {
|
||||||
|
t.Errorf("expected name %q to return %t for validateObjectName func but got %t",
|
||||||
|
tt.name, tt.valid, valid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue