Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate patches for commands using input version #53158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions hack/make-rules/test-cmd-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2985,6 +2985,16 @@ run_rs_tests() {
# Cleanup services
kubectl delete service frontend{,-2} "${kube_flags[@]}"

# Test set commands
# Pre-condition: frontend replica set exists at generation 1
kube::test::get_object_assert 'rs frontend' "{{${generation_field}}}" '1'
kubectl set image rs/frontend "${kube_flags[@]}" *=gcr.io/google-containers/pause:test-cmd
kube::test::get_object_assert 'rs frontend' "{{${generation_field}}}" '2'
kubectl set env rs/frontend "${kube_flags[@]}" foo=bar
kube::test::get_object_assert 'rs frontend' "{{${generation_field}}}" '3'
kubectl set resources rs/frontend "${kube_flags[@]}" --limits=cpu=200m,memory=512Mi
kube::test::get_object_assert 'rs frontend' "{{${generation_field}}}" '4'

### Delete replica set with id
# Pre-condition: frontend replica set exists
kube::test::get_object_assert rs "{{range.items}}{{$id_field}}:{{end}}" 'frontend:'
Expand Down Expand Up @@ -3064,6 +3074,14 @@ run_daemonset_tests() {
kubectl apply -f hack/testdata/rollingupdate-daemonset.yaml "${kube_flags[@]}"
# Template Generation should stay 1
kube::test::get_object_assert 'daemonsets bind' "{{${template_generation_field}}}" '1'
# Test set commands
kubectl set image daemonsets/bind "${kube_flags[@]}" *=gcr.io/google-containers/pause:test-cmd
kube::test::get_object_assert 'daemonsets bind' "{{${template_generation_field}}}" '2'
kubectl set env daemonsets/bind "${kube_flags[@]}" foo=bar
kube::test::get_object_assert 'daemonsets bind' "{{${template_generation_field}}}" '3'
kubectl set resources daemonsets/bind "${kube_flags[@]}" --limits=cpu=200m,memory=512Mi
kube::test::get_object_assert 'daemonsets bind' "{{${template_generation_field}}}" '4'

# Clean up
kubectl delete -f hack/testdata/rollingupdate-daemonset.yaml "${kube_flags[@]}"

Expand Down Expand Up @@ -4355,6 +4373,7 @@ runTests() {
change_cause_annotation='.*kubernetes.io/change-cause.*'
pdb_min_available=".spec.minAvailable"
pdb_max_unavailable=".spec.maxUnavailable"
generation_field=".metadata.generation"
template_generation_field=".spec.templateGeneration"
container_len="(len .spec.template.spec.containers)"
image_field0="(index .spec.template.spec.containers 0).image"
Expand Down
4 changes: 3 additions & 1 deletion pkg/kubectl/cmd/set/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ type patchFn func(*resource.Info) ([]byte, error)
// the changes in the object. Encoder must be able to encode the info into the appropriate destination type.
// This function returns whether the mutation function made any change in the original object.
func CalculatePatch(patch *Patch, encoder runtime.Encoder, mutateFn patchFn) bool {
patch.Before, patch.Err = runtime.Encode(encoder, patch.Info.Object)
versionedEncoder := api.Codecs.EncoderForVersion(encoder, patch.Info.Mapping.GroupVersionKind.GroupVersion())

patch.Before, patch.Err = runtime.Encode(versionedEncoder, patch.Info.Object)

patch.After, patch.Err = mutateFn(patch.Info)
if patch.Err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/kubectl/cmd/set/set_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
})

if err == nil {
return runtime.Encode(o.Encoder, info.Object)
// TODO: switch UpdatePodSpecForObject to work on v1.PodSpec, use info.VersionedObject, and avoid conversion completely
versionedEncoder := api.Codecs.EncoderForVersion(o.Encoder, info.Mapping.GroupVersionKind.GroupVersion())
return runtime.Encode(versionedEncoder, info.Object)
}
return nil, err
})
Expand Down
4 changes: 3 additions & 1 deletion pkg/kubectl/cmd/set/set_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ func (o *ImageOptions) Run() error {
return nil
})
if transformed && err == nil {
return runtime.Encode(o.Encoder, info.Object)
// TODO: switch UpdatePodSpecForObject to work on v1.PodSpec, use info.VersionedObject, and avoid conversion completely
versionedEncoder := api.Codecs.EncoderForVersion(o.Encoder, info.Mapping.GroupVersionKind.GroupVersion())
return runtime.Encode(versionedEncoder, info.Object)
}
return nil, err
})
Expand Down
4 changes: 3 additions & 1 deletion pkg/kubectl/cmd/set/set_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ func (o *ResourcesOptions) Run() error {
return nil
})
if transformed && err == nil {
return runtime.Encode(o.Encoder, info.Object)
// TODO: switch UpdatePodSpecForObject to work on v1.PodSpec, use info.VersionedObject, and avoid conversion completely
versionedEncoder := api.Codecs.EncoderForVersion(o.Encoder, info.Mapping.GroupVersionKind.GroupVersion())
return runtime.Encode(versionedEncoder, info.Object)
}
return nil, err
})
Expand Down
5 changes: 4 additions & 1 deletion pkg/kubectl/cmd/set/set_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/rbac"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
Expand Down Expand Up @@ -218,7 +219,9 @@ func (o *SubjectOptions) Run(f cmdutil.Factory, fn updateSubjects) error {

transformed, err := updateSubjectForObject(info.Object, subjects, fn)
if transformed && err == nil {
return runtime.Encode(o.Encoder, info.Object)
// TODO: switch UpdatePodSpecForObject to work on v1.PodSpec, use info.VersionedObject, and avoid conversion completely
versionedEncoder := api.Codecs.EncoderForVersion(o.Encoder, info.Mapping.GroupVersionKind.GroupVersion())
return runtime.Encode(versionedEncoder, info.Object)
}
return nil, err
})
Expand Down