From c5ac135a01444a16d0df1b0c2f2e45e38d6c6386 Mon Sep 17 00:00:00 2001 From: Daisy Guo Date: Mon, 26 Oct 2020 16:21:39 +0800 Subject: [PATCH] change the input parameter from ccmd to flagset --- .../service/configuration_edit_flags.go | 2 +- pkg/kn/flags/podspec.go | 39 ++++++------------- pkg/kn/flags/podspec_test.go | 10 +++-- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index 82f4a8578a..d8cbcc0c67 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -197,7 +197,7 @@ func (p *ConfigurationEditFlags) Apply( template := &service.Spec.Template - err := p.PodSpecFlags.ResolvePodSpec(&template.Spec.PodSpec, cmd) + err := p.PodSpecFlags.ResolvePodSpec(&template.Spec.PodSpec, cmd.Flags()) if err != nil { return err } diff --git a/pkg/kn/flags/podspec.go b/pkg/kn/flags/podspec.go index 083e3d0fd0..7fe1bbd89d 100644 --- a/pkg/kn/flags/podspec.go +++ b/pkg/kn/flags/podspec.go @@ -23,7 +23,6 @@ import ( "k8s.io/apimachinery/pkg/api/resource" "knative.dev/client/pkg/util" - "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -171,11 +170,11 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet) []string { return flagNames } -func (p *PodSpecFlags) ResolvePodSpec(podSpec *corev1.PodSpec, cmd *cobra.Command) error { - //var podSpec = &corev1.PodSpec{Containers: []corev1.Container{{}}} +// ResolvePodSpec will create corev1.PodSpec based on the flag inputs +func (p *PodSpecFlags) ResolvePodSpec(podSpec *corev1.PodSpec /*cmd *cobra.Command*/, flags *pflag.FlagSet) error { var err error - if cmd.Flags().Changed("env") { + if flags.Changed("env") { envMap, err := util.MapFromArrayAllowingSingles(p.Env, "=") if err != nil { return fmt.Errorf("Invalid --env: %w", err) @@ -188,7 +187,7 @@ func (p *PodSpecFlags) ResolvePodSpec(podSpec *corev1.PodSpec, cmd *cobra.Comman } } - if cmd.Flags().Changed("env-from") { + if flags.Changed("env-from") { envFromSourceToUpdate := []string{} envFromSourceToRemove := []string{} for _, name := range p.EnvFrom { @@ -207,7 +206,7 @@ func (p *PodSpecFlags) ResolvePodSpec(podSpec *corev1.PodSpec, cmd *cobra.Comman } } - if cmd.Flags().Changed("mount") || cmd.Flags().Changed("volume") { + if flags.Changed("mount") || flags.Changed("volume") { mountsToUpdate, mountsToRemove, err := util.OrderedMapAndRemovalListFromArray(p.Mount, "=") if err != nil { return fmt.Errorf("Invalid --mount: %w", err) @@ -224,27 +223,13 @@ func (p *PodSpecFlags) ResolvePodSpec(podSpec *corev1.PodSpec, cmd *cobra.Comman } } - if cmd.Flags().Changed("image") { + if flags.Changed("image") { err = UpdateImage(podSpec, p.Image.String()) if err != nil { return err } } - if cmd.Flags().Changed("limits-cpu") || cmd.Flags().Changed("limits-memory") { - if cmd.Flags().Changed("limit") { - return fmt.Errorf("only one of (DEPRECATED) --limits-cpu / --limits-memory and --limit can be specified") - } - fmt.Fprintf(cmd.OutOrStdout(), "\nWARNING: flags --limits-cpu / --limits-memory are deprecated and going to be removed in future release, please use --limit instead.\n\n") - } - - if cmd.Flags().Changed("requests-cpu") || cmd.Flags().Changed("requests-memory") { - if cmd.Flags().Changed("request") { - return fmt.Errorf("only one of (DEPRECATED) --requests-cpu / --requests-memory and --request can be specified") - } - fmt.Fprintf(cmd.OutOrStdout(), "\nWARNING: flags --requests-cpu / --requests-memory are deprecated and going to be removed in future release, please use --request instead.\n\n") - } - limitsResources, err := p.computeResources(p.LimitsFlags) if err != nil { return err @@ -268,36 +253,36 @@ func (p *PodSpecFlags) ResolvePodSpec(podSpec *corev1.PodSpec, cmd *cobra.Comman return err } - if cmd.Flags().Changed("cmd") { + if flags.Changed("cmd") { err = UpdateContainerCommand(podSpec, p.Command) if err != nil { return err } } - if cmd.Flags().Changed("arg") { + if flags.Changed("arg") { err = UpdateContainerArg(podSpec, p.Arg) if err != nil { return err } } - if cmd.Flags().Changed("port") { + if flags.Changed("port") { err = UpdateContainerPort(podSpec, p.Port) if err != nil { return err } } - if cmd.Flags().Changed("service-account") { + if flags.Changed("service-account") { UpdateServiceAccountName(podSpec, p.ServiceAccountName) } - if cmd.Flags().Changed("pull-secret") { + if flags.Changed("pull-secret") { UpdateImagePullSecrets(podSpec, p.ImagePullSecrets) } - if cmd.Flags().Changed("user") { + if flags.Changed("user") { err = UpdateUser(podSpec, p.User) if err != nil { return err diff --git a/pkg/kn/flags/podspec_test.go b/pkg/kn/flags/podspec_test.go index f889114bd1..2457188fa6 100644 --- a/pkg/kn/flags/podspec_test.go +++ b/pkg/kn/flags/podspec_test.go @@ -16,6 +16,7 @@ package flags import ( "bytes" + "fmt" "testing" "github.com/spf13/cobra" @@ -113,7 +114,7 @@ func TestPodSpecResolve(t *testing.T) { Use: "test", Run: func(cmd *cobra.Command, args []string) { podSpec := &corev1.PodSpec{Containers: []corev1.Container{{}}} - err := flags.ResolvePodSpec(podSpec, cmd) + err := flags.ResolvePodSpec(podSpec, cmd.Flags()) assert.NilError(t, err, "PodSpec cannot be resolved.") assert.DeepEqual(t, expectedPodSpec, *podSpec) }, @@ -130,15 +131,16 @@ func TestPodSpecResolveReturnError(t *testing.T) { Use: "test", Run: func(cmd *cobra.Command, args []string) { podSpec := &corev1.PodSpec{Containers: []corev1.Container{{}}} - flags.ResolvePodSpec(podSpec, cmd) + err := flags.ResolvePodSpec(podSpec, cmd.Flags()) + fmt.Fprintf(cmd.OutOrStdout(), "Return error: %v", err) }, } testCmd.SetOut(&outBuf) - args := []string{"--requests-cpu", "1000m"} + args := []string{"--mount", "123456"} testCmd.SetArgs(args) flags.AddFlags(testCmd.Flags()) testCmd.Execute() out := outBuf.String() - assert.Assert(t, util.ContainsAll(out, "WARNING", "deprecated")) + assert.Assert(t, util.ContainsAll(out, "Invalid", "mount")) }