Skip to content

Commit

Permalink
moving utilities of handling podspec to podspec_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Sep 22, 2020
1 parent f4e43c8 commit 89a44ab
Show file tree
Hide file tree
Showing 13 changed files with 1,374 additions and 1,139 deletions.
112 changes: 3 additions & 109 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,53 +181,10 @@ func (p *ConfigurationEditFlags) Apply(
cmd *cobra.Command) error {

template := &service.Spec.Template
if cmd.Flags().Changed("env") {
envMap, err := util.MapFromArrayAllowingSingles(p.PodSpecFlags.Env, "=")
if err != nil {
return fmt.Errorf("Invalid --env: %w", err)
}

envToRemove := util.ParseMinusSuffix(envMap)
err = servinglib.UpdateEnvVars(template, envMap, envToRemove)
if err != nil {
return err
}
}

if cmd.Flags().Changed("env-from") {
envFromSourceToUpdate := []string{}
envFromSourceToRemove := []string{}
for _, name := range p.PodSpecFlags.EnvFrom {
if name == "-" {
return fmt.Errorf("\"-\" is not a valid value for \"--env-from\"")
} else if strings.HasSuffix(name, "-") {
envFromSourceToRemove = append(envFromSourceToRemove, name[:len(name)-1])
} else {
envFromSourceToUpdate = append(envFromSourceToUpdate, name)
}
}

err := servinglib.UpdateEnvFrom(template, envFromSourceToUpdate, envFromSourceToRemove)
if err != nil {
return err
}
}

if cmd.Flags().Changed("mount") || cmd.Flags().Changed("volume") {
mountsToUpdate, mountsToRemove, err := util.OrderedMapAndRemovalListFromArray(p.PodSpecFlags.Mount, "=")
if err != nil {
return fmt.Errorf("Invalid --mount: %w", err)
}

volumesToUpdate, volumesToRemove, err := util.OrderedMapAndRemovalListFromArray(p.PodSpecFlags.Volume, "=")
if err != nil {
return fmt.Errorf("Invalid --volume: %w", err)
}

err = servinglib.UpdateVolumeMountsAndVolumes(template, mountsToUpdate, mountsToRemove, volumesToUpdate, volumesToRemove)
if err != nil {
return err
}
err := p.PodSpecFlags.ResolvePodSpec(&template.Spec.PodSpec, cmd)
if err != nil {
return err
}

name, err := servinglib.GenerateRevisionName(p.RevisionName, service)
Expand All @@ -241,10 +198,6 @@ func (p *ConfigurationEditFlags) Apply(

imageSet := false
if cmd.Flags().Changed("image") {
err = servinglib.UpdateImage(template, p.PodSpecFlags.Image.String())
if err != nil {
return err
}
imageSet = true
}
_, userImagePresent := template.Annotations[servinglib.UserImageAnnotationKey]
Expand Down Expand Up @@ -275,50 +228,6 @@ func (p *ConfigurationEditFlags) Apply(
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.PodSpecFlags.LimitsFlags)
if err != nil {
return err
}
requestsResources, err := p.computeResources(p.PodSpecFlags.RequestsFlags)
if err != nil {
return err
}
err = servinglib.UpdateResourcesDeprecated(template, requestsResources, limitsResources)
if err != nil {
return err
}

requestsToRemove, limitsToRemove, err := p.PodSpecFlags.Resources.Validate()
if err != nil {
return err
}

err = servinglib.UpdateResources(template, p.PodSpecFlags.Resources.ResourceRequirements, requestsToRemove, limitsToRemove)
if err != nil {
return err
}

if cmd.Flags().Changed("cmd") {
err = servinglib.UpdateContainerCommand(template, p.PodSpecFlags.Command)
if err != nil {
return err
}
}

if cmd.Flags().Changed("arg") {
err = servinglib.UpdateContainerArg(template, p.PodSpecFlags.Arg)
if err != nil {
return err
}
}

if cmd.Flags().Changed("port") {
err = servinglib.UpdateContainerPort(template, p.PodSpecFlags.Port)
if err != nil {
return err
}
}

if cmd.Flags().Changed("scale-min") {
err = servinglib.UpdateMinScale(template, p.MinScale)
if err != nil {
Expand Down Expand Up @@ -431,21 +340,6 @@ func (p *ConfigurationEditFlags) Apply(

}

if cmd.Flags().Changed("service-account") {
err = servinglib.UpdateServiceAccountName(template, p.PodSpecFlags.ServiceAccountName)
if err != nil {
return err
}
}

if cmd.Flags().Changed("pull-secret") {
servinglib.UpdateImagePullSecrets(template, p.PodSpecFlags.ImagePullSecrets)
}

if cmd.Flags().Changed("user") {
servinglib.UpdateUser(template, p.PodSpecFlags.User)
}

if cmd.Flags().Changed("scale-init") {
containsAnnotation := func(annotationList []string, annotation string) bool {
for _, element := range annotationList {
Expand Down
8 changes: 4 additions & 4 deletions pkg/kn/commands/service/create_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestServiceCreateWithMountConfigMap(t *testing.T) {
template := &service.Spec.Template
template.Spec.Volumes = []corev1.Volume{
{
Name: servinglib.GenerateVolumeName("/mount/path"),
Name: util.GenerateVolumeName("/mount/path"),
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Expand All @@ -292,7 +292,7 @@ func TestServiceCreateWithMountConfigMap(t *testing.T) {

template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{
{
Name: servinglib.GenerateVolumeName("/mount/path"),
Name: util.GenerateVolumeName("/mount/path"),
MountPath: "/mount/path",
ReadOnly: true,
},
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestServiceCreateWithMountSecret(t *testing.T) {
template := &service.Spec.Template
template.Spec.Volumes = []corev1.Volume{
{
Name: servinglib.GenerateVolumeName("/mount/path"),
Name: util.GenerateVolumeName("/mount/path"),
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "secret-name",
Expand All @@ -370,7 +370,7 @@ func TestServiceCreateWithMountSecret(t *testing.T) {

template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{
{
Name: servinglib.GenerateVolumeName("/mount/path"),
Name: util.GenerateVolumeName("/mount/path"),
MountPath: "/mount/path",
ReadOnly: true,
},
Expand Down
11 changes: 5 additions & 6 deletions pkg/kn/commands/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/apimachinery/pkg/watch"

"knative.dev/client/pkg/kn/commands"
servinglib "knative.dev/client/pkg/serving"
"knative.dev/client/pkg/util"
"knative.dev/client/pkg/wait"

Expand Down Expand Up @@ -224,7 +223,7 @@ func TestServiceCreateEnv(t *testing.T) {
if err != nil {
t.Fatal(err)
}
actualEnvVars, err := servinglib.EnvToMap(template.Spec.Containers[0].Env)
actualEnvVars, err := util.EnvToMap(template.Spec.Containers[0].Env)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -725,7 +724,7 @@ func TestServiceCreateEnvForce(t *testing.T) {
if err != nil {
t.Fatal(err)
}
actualEnvVars, err := servinglib.EnvToMap(template.Spec.Containers[0].Env)
actualEnvVars, err := util.EnvToMap(template.Spec.Containers[0].Env)
if err != nil {
t.Fatal(err)
} else if template.Spec.Containers[0].Image != "gcr.io/foo/bar:v2" {
Expand Down Expand Up @@ -992,7 +991,7 @@ func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")

actualEnvVar, err := servinglib.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
actualEnvVar, err := util.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
assert.NilError(t, err)
assert.DeepEqual(t, actualEnvVar, expectedEnvVars)

Expand All @@ -1006,7 +1005,7 @@ func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")

actualEnvVar, err = servinglib.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
actualEnvVar, err = util.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
assert.NilError(t, err)
assert.DeepEqual(t, actualEnvVar, expectedEnvVars)

Expand All @@ -1019,7 +1018,7 @@ func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")

actualEnvVar, err = servinglib.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
actualEnvVar, err = util.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
assert.NilError(t, err)
assert.DeepEqual(t, actualEnvVar, expectedEnvVars)

Expand Down
Loading

0 comments on commit 89a44ab

Please sign in to comment.