Skip to content

Commit

Permalink
change the input parameter from ccmd to flagset
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Oct 26, 2020
1 parent fb97dcf commit c5ac135
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
39 changes: 12 additions & 27 deletions pkg/kn/flags/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"knative.dev/client/pkg/util"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pkg/kn/flags/podspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package flags

import (
"bytes"
"fmt"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
},
Expand All @@ -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"))
}

0 comments on commit c5ac135

Please sign in to comment.