Skip to content

Commit 06e4e8a

Browse files
committed
feat : support kubectl rollout history for statefulset
1 parent 2957526 commit 06e4e8a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlRollout.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2626
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
2727
import io.kubernetes.client.openapi.models.V1ReplicaSet;
28+
import io.kubernetes.client.openapi.models.V1StatefulSet;
2829
import io.kubernetes.client.util.labels.LabelSelector;
2930
import java.util.ArrayList;
3031
import java.util.HashMap;
@@ -77,6 +78,10 @@ public List<History> execute() throws KubectlException {
7778
} else if (apiTypeClass.equals(V1DaemonSet.class)) {
7879
V1DaemonSet daemonSet = api.readNamespacedDaemonSet(name, namespace, null, null, null);
7980
daemonSetViewHistory(daemonSet, api);
81+
} else if (apiTypeClass.equals(V1StatefulSet.class)) {
82+
V1StatefulSet statefulSet =
83+
api.readNamespacedStatefulSet(name, namespace, null, null, null);
84+
statefulSetViewHistory(statefulSet, api);
8085
} else {
8186
throw new KubectlException("Unsupported class for rollout history: " + apiTypeClass);
8287
}
@@ -171,6 +176,26 @@ private V1DaemonSet applyDaemonSetHistory(V1ControllerRevision history)
171176
PatchHelper.dryRunStrategyMergePatch(getGenericApi(), patch, namespace, name);
172177
}
173178

179+
private void statefulSetViewHistory(V1StatefulSet statefulSet, AppsV1Api api)
180+
throws ApiException, KubectlException {
181+
LabelSelector selector = LabelSelector.parse(statefulSet.getSpec().getSelector());
182+
List<V1ControllerRevision> historyList = controlledHistory(api, statefulSet, selector);
183+
parseHistory(
184+
historyList,
185+
history -> {
186+
V1StatefulSet stsOfHistory = applyStatefulSetHistory(history);
187+
return stsOfHistory.getSpec().getTemplate();
188+
});
189+
}
190+
191+
private V1StatefulSet applyStatefulSetHistory(V1ControllerRevision history)
192+
throws KubectlException, ApiException {
193+
String patch = apiClient.getJSON().serialize(history.getData());
194+
return (V1StatefulSet)
195+
io.kubernetes.client.extended.kubectl.util.patch.PatchHelper.dryRunStrategyMergePatch(
196+
getGenericApi(), patch, namespace, name);
197+
}
198+
174199
private void parseHistory(List<V1ControllerRevision> historyList, PodTemplateParser parser)
175200
throws ApiException, KubectlException {
176201
Map<Long, V1ControllerRevision> historyInfo = new HashMap<>();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.extended.kubectl.util.patch;
14+
15+
import io.kubernetes.client.common.KubernetesListObject;
16+
import io.kubernetes.client.common.KubernetesObject;
17+
import io.kubernetes.client.custom.V1Patch;
18+
import io.kubernetes.client.openapi.ApiException;
19+
import io.kubernetes.client.util.generic.GenericKubernetesApi;
20+
import io.kubernetes.client.util.generic.options.PatchOptions;
21+
22+
public class PatchHelper {
23+
public static <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
24+
ApiType dryRunStrategyMergePatch(
25+
GenericKubernetesApi<ApiType, ApiListType> genericApi,
26+
String patch,
27+
String namespace,
28+
String name)
29+
throws ApiException {
30+
PatchOptions options = new PatchOptions();
31+
options.setDryRun("All");
32+
return genericApi
33+
.patch(
34+
namespace,
35+
name,
36+
V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
37+
new V1Patch(patch),
38+
options)
39+
.throwsApiException()
40+
.getObject();
41+
}
42+
}

0 commit comments

Comments
 (0)