From fd3b5bea8f3c42a8dea712f9261d9cf220d5463b Mon Sep 17 00:00:00 2001 From: ymqytw Date: Mon, 7 Aug 2017 20:13:42 -0700 Subject: [PATCH 1/3] add retainKeys in patchStrategy --- staging/src/k8s.io/api/core/v1/types.go | 4 ++-- staging/src/k8s.io/api/extensions/v1beta1/types.go | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/api/core/v1/types.go b/staging/src/k8s.io/api/core/v1/types.go index e5e58c0d84f..ba486ddd6b4 100644 --- a/staging/src/k8s.io/api/core/v1/types.go +++ b/staging/src/k8s.io/api/core/v1/types.go @@ -2489,8 +2489,8 @@ type PodSpec struct { // More info: https://kubernetes.io/docs/concepts/storage/volumes // +optional // +patchMergeKey=name - // +patchStrategy=merge - Volumes []Volume `json:"volumes,omitempty" patchStrategy:"merge" patchMergeKey:"name" protobuf:"bytes,1,rep,name=volumes"` + // +patchStrategy=merge,retainKeys + Volumes []Volume `json:"volumes,omitempty" patchStrategy:"merge,retainKeys" patchMergeKey:"name" protobuf:"bytes,1,rep,name=volumes"` // List of initialization containers belonging to the pod. // Init containers are executed in order prior to containers being started. If any // init container fails, the pod is considered to have failed and is handled according diff --git a/staging/src/k8s.io/api/extensions/v1beta1/types.go b/staging/src/k8s.io/api/extensions/v1beta1/types.go index 2967f053b35..971db945bd0 100644 --- a/staging/src/k8s.io/api/extensions/v1beta1/types.go +++ b/staging/src/k8s.io/api/extensions/v1beta1/types.go @@ -193,7 +193,8 @@ type DeploymentSpec struct { // The deployment strategy to use to replace existing pods with new ones. // +optional - Strategy DeploymentStrategy `json:"strategy,omitempty" protobuf:"bytes,4,opt,name=strategy"` + // +patchStrategy=retainKeys + Strategy DeploymentStrategy `json:"strategy,omitempty" patchStrategy:"retainKeys" protobuf:"bytes,4,opt,name=strategy"` // Minimum number of seconds for which a newly created pod should be ready // without any of its container crashing, for it to be considered available. From 838c47ee10f8d5b74c93a7bc24b5a68e452cbc5f Mon Sep 17 00:00:00 2001 From: ymqytw Date: Mon, 7 Aug 2017 20:14:14 -0700 Subject: [PATCH 2/3] add apply test for retainKeys --- hack/make-rules/test-cmd-util.sh | 24 +++++++++++++++++++ .../deployment/deployment-after.yaml | 22 +++++++++++++++++ .../deployment/deployment-before.yaml | 18 ++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 hack/testdata/retainKeys/deployment/deployment-after.yaml create mode 100644 hack/testdata/retainKeys/deployment/deployment-before.yaml diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index 0c2c61ce05b..a7ac65f198f 100755 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -932,6 +932,30 @@ run_kubectl_apply_tests() { kubectl delete pods test-pod "${kube_flags[@]}" + ## kubectl apply should be able to clear defaulted fields. + # Pre-Condition: no deployment exists + kube::test::get_object_assert deployments "{{range.items}}{{$id_field}}:{{end}}" '' + # Command: apply a deployment "test-deployment-retainkeys" (doesn't exist) should create this deployment + kubectl apply -f hack/testdata/retainKeys/deployment/deployment-before.yaml "${kube_flags[@]}" + # Post-Condition: deployment "test-deployment-retainkeys" created + kube::test::get_object_assert deployments "{{range.items}}{{$id_field}}{{end}}" 'test-deployment-retainkeys' + # Post-Condition: deployment "test-deployment-retainkeys" has defaulted fields + [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep RollingUpdate)" ]] + [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep maxSurge)" ]] + [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep maxUnavailable)" ]] + [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep emptyDir)" ]] + # Command: apply a deployment "test-deployment-retainkeys" should clear + # defaulted fields and successfully update the deployment + [[ "$(kubectl apply -f hack/testdata/retainKeys/deployment/deployment-after.yaml "${kube_flags[@]}")" ]] + # Post-Condition: deployment "test-deployment-retainkeys" has updated fields + [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep Recreate)" ]] + ! [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep RollingUpdate)" ]] + [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep hostPath)" ]] + ! [[ "$(kubectl get deployments test-deployment-retainkeys -o yaml "${kube_flags[@]}" | grep emptyDir)" ]] + # Clean up + kubectl delete deployments test-deployment-retainkeys "${kube_flags[@]}" + + ## kubectl apply -f with label selector should only apply matching objects # Pre-Condition: no POD exists kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' diff --git a/hack/testdata/retainKeys/deployment/deployment-after.yaml b/hack/testdata/retainKeys/deployment/deployment-after.yaml new file mode 100644 index 00000000000..15689a59959 --- /dev/null +++ b/hack/testdata/retainKeys/deployment/deployment-after.yaml @@ -0,0 +1,22 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: test-deployment-retainkeys +spec: + strategy: + type: Recreate + replicas: 1 + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 80 + volumes: + - name: test-volume + hostPath: + path: /data diff --git a/hack/testdata/retainKeys/deployment/deployment-before.yaml b/hack/testdata/retainKeys/deployment/deployment-before.yaml new file mode 100644 index 00000000000..e58a0c34a14 --- /dev/null +++ b/hack/testdata/retainKeys/deployment/deployment-before.yaml @@ -0,0 +1,18 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: test-deployment-retainkeys +spec: + replicas: 1 + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 80 + volumes: + - name: test-volume From 9b05e2644f55aa531b376d83e1fecf8120151aff Mon Sep 17 00:00:00 2001 From: ymqytw Date: Tue, 8 Aug 2017 09:29:40 -0700 Subject: [PATCH 3/3] update generated files --- api/openapi-spec/swagger.json | 3 ++- federation/apis/openapi-spec/swagger.json | 3 ++- staging/src/k8s.io/api/core/v1/generated.proto | 2 +- staging/src/k8s.io/api/extensions/v1beta1/generated.proto | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/api/openapi-spec/swagger.json b/api/openapi-spec/swagger.json index 97825c3bf91..1b03332bfad 100644 --- a/api/openapi-spec/swagger.json +++ b/api/openapi-spec/swagger.json @@ -60953,7 +60953,7 @@ "$ref": "#/definitions/io.k8s.api.core.v1.Volume" }, "x-kubernetes-patch-merge-key": "name", - "x-kubernetes-patch-strategy": "merge" + "x-kubernetes-patch-strategy": "merge,retainKeys" } } }, @@ -62772,6 +62772,7 @@ }, "strategy": { "description": "The deployment strategy to use to replace existing pods with new ones.", + "x-kubernetes-patch-strategy": "retainKeys", "$ref": "#/definitions/io.k8s.api.extensions.v1beta1.DeploymentStrategy" }, "template": { diff --git a/federation/apis/openapi-spec/swagger.json b/federation/apis/openapi-spec/swagger.json index ef779166d69..900946eff80 100644 --- a/federation/apis/openapi-spec/swagger.json +++ b/federation/apis/openapi-spec/swagger.json @@ -11237,7 +11237,7 @@ "$ref": "#/definitions/io.k8s.api.core.v1.Volume" }, "x-kubernetes-patch-merge-key": "name", - "x-kubernetes-patch-strategy": "merge" + "x-kubernetes-patch-strategy": "merge,retainKeys" } } }, @@ -12530,6 +12530,7 @@ }, "strategy": { "description": "The deployment strategy to use to replace existing pods with new ones.", + "x-kubernetes-patch-strategy": "retainKeys", "$ref": "#/definitions/io.k8s.api.extensions.v1beta1.DeploymentStrategy" }, "template": { diff --git a/staging/src/k8s.io/api/core/v1/generated.proto b/staging/src/k8s.io/api/core/v1/generated.proto index 75d84162389..4ce82b8e02f 100644 --- a/staging/src/k8s.io/api/core/v1/generated.proto +++ b/staging/src/k8s.io/api/core/v1/generated.proto @@ -2714,7 +2714,7 @@ message PodSpec { // More info: https://kubernetes.io/docs/concepts/storage/volumes // +optional // +patchMergeKey=name - // +patchStrategy=merge + // +patchStrategy=merge,retainKeys repeated Volume volumes = 1; // List of initialization containers belonging to the pod. diff --git a/staging/src/k8s.io/api/extensions/v1beta1/generated.proto b/staging/src/k8s.io/api/extensions/v1beta1/generated.proto index 87a2f91687b..0b319f99712 100644 --- a/staging/src/k8s.io/api/extensions/v1beta1/generated.proto +++ b/staging/src/k8s.io/api/extensions/v1beta1/generated.proto @@ -276,6 +276,7 @@ message DeploymentSpec { // The deployment strategy to use to replace existing pods with new ones. // +optional + // +patchStrategy=retainKeys optional DeploymentStrategy strategy = 4; // Minimum number of seconds for which a newly created pod should be ready