Skip to content

Feat/kubectl rollout history statefulset #1819

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
import io.kubernetes.client.openapi.models.V1ReplicaSet;
import io.kubernetes.client.openapi.models.V1StatefulSet;
import io.kubernetes.client.util.labels.LabelSelector;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -77,6 +78,10 @@ public List<History> execute() throws KubectlException {
} else if (apiTypeClass.equals(V1DaemonSet.class)) {
V1DaemonSet daemonSet = api.readNamespacedDaemonSet(name, namespace, null, null, null);
daemonSetViewHistory(daemonSet, api);
} else if (apiTypeClass.equals(V1StatefulSet.class)) {
V1StatefulSet statefulSet =
api.readNamespacedStatefulSet(name, namespace, null, null, null);
statefulSetViewHistory(statefulSet, api);
} else {
throw new KubectlException("Unsupported class for rollout history: " + apiTypeClass);
}
Expand Down Expand Up @@ -171,6 +176,25 @@ private V1DaemonSet applyDaemonSetHistory(V1ControllerRevision history)
PatchHelper.dryRunStrategyMergePatch(getGenericApi(), patch, namespace, name);
}

private void statefulSetViewHistory(V1StatefulSet statefulSet, AppsV1Api api)
throws ApiException, KubectlException {
LabelSelector selector = LabelSelector.parse(statefulSet.getSpec().getSelector());
List<V1ControllerRevision> historyList = controlledHistory(api, statefulSet, selector);
parseHistory(
historyList,
history -> {
V1StatefulSet stsOfHistory = applyStatefulSetHistory(history);
return stsOfHistory.getSpec().getTemplate();
});
}

private V1StatefulSet applyStatefulSetHistory(V1ControllerRevision history)
throws KubectlException, ApiException {
String patch = apiClient.getJSON().serialize(history.getData());
return (V1StatefulSet)
PatchHelper.dryRunStrategyMergePatch(getGenericApi(), patch, namespace, name);
}

private void parseHistory(List<V1ControllerRevision> historyList, PodTemplateParser parser)
throws ApiException, KubectlException {
Map<Long, V1ControllerRevision> historyInfo = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentList;
import io.kubernetes.client.openapi.models.V1PodTemplateSpec;
import io.kubernetes.client.openapi.models.V1StatefulSet;
import io.kubernetes.client.openapi.models.V1StatefulSetList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.ModelMapper;
import java.io.IOException;
Expand Down Expand Up @@ -65,6 +67,18 @@ public class KubectlRolloutTest {
.getResource("daemonset-controllerrevision-list.json")
.getPath();

private static final String STATEFUL_SET =
KubectlRolloutTest.class.getClassLoader().getResource("statefulset.json").getPath();

private static final String PATCHED_STATEFUL_SET =
KubectlRolloutTest.class.getClassLoader().getResource("patched-statefulset.json").getPath();

private static final String STATEFUL_SET_CONTROLLER_REVISION_LIST =
KubectlRolloutTest.class
.getClassLoader()
.getResource("statefulset-controllerrevision-list.json")
.getPath();

@Before
public void setup() throws IOException {
ModelMapper.addModelMap(
Expand All @@ -77,6 +91,14 @@ public void setup() throws IOException {
V1DeploymentList.class);
ModelMapper.addModelMap(
"apps", "v1", "DaemonSet", "daemonsets", true, V1DaemonSet.class, V1DaemonSetList.class);
ModelMapper.addModelMap(
"apps",
"v1",
"StatefulSet",
"statefulsets",
true,
V1StatefulSet.class,
V1StatefulSetList.class);
apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
}

Expand Down Expand Up @@ -223,6 +245,87 @@ public void testKubectlRolloutHistoryDaemonSetWithRevisionShouldWork()
Assert.assertNotNull(template);
}

@Test
public void testKubectlRolloutHistoryStatefulSetShouldWork()
throws KubectlException, IOException {
wireMockRule.stubFor(
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))
.willReturn(
aResponse()
.withStatus(200)
.withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET))))));
wireMockRule.stubFor(
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))
.willReturn(
aResponse()
.withStatus(200)
.withBody(
new String(
Files.readAllBytes(
Paths.get(STATEFUL_SET_CONTROLLER_REVISION_LIST))))));
List<History> histories =
Kubectl.rollout(V1StatefulSet.class)
.history()
.apiClient(apiClient)
.name("foo")
.namespace("default")
.skipDiscovery()
.execute();
wireMockRule.verify(
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))));
wireMockRule.verify(
1,
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")))
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
Assert.assertEquals(3, histories.size());
}

@Test
public void testKubectlRolloutHistoryStatefulSetWithRevisionShouldWork()
throws KubectlException, IOException {
wireMockRule.stubFor(
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))
.willReturn(
aResponse()
.withStatus(200)
.withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET))))));
wireMockRule.stubFor(
get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))
.willReturn(
aResponse()
.withStatus(200)
.withBody(
new String(
Files.readAllBytes(
Paths.get(STATEFUL_SET_CONTROLLER_REVISION_LIST))))));
wireMockRule.stubFor(
patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))
.willReturn(
aResponse()
.withStatus(200)
.withBody(new String(Files.readAllBytes(Paths.get(PATCHED_STATEFUL_SET))))));
V1PodTemplateSpec template =
Kubectl.rollout(V1StatefulSet.class)
.history()
.apiClient(apiClient)
.name("foo")
.namespace("default")
.revision(2)
.skipDiscovery()
.execute();
wireMockRule.verify(
1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))));
wireMockRule.verify(
1,
getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")))
.withQueryParam("labelSelector", new EqualToPattern("app = bar")));
wireMockRule.verify(
1,
patchRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo")))
.withQueryParam("dryRun", new EqualToPattern("All")));
Assert.assertNotNull(template);
}

@Test
public void testKubectlRolloutHistoryWithInvalidRevisionShouldThrow() throws IOException {
wireMockRule.stubFor(
Expand Down
151 changes: 151 additions & 0 deletions extended/src/test/resources/patched-statefulset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"apiVersion": "apps/v1",
"kind": "StatefulSet",
"metadata": {
"annotations": {
"kubernetes.io/change-cause": "kubectl.exe create --filename\u003dstatefulset.yaml --record\u003dtrue"
},
"creationTimestamp": "2021-08-13T09:44:24.000000Z",
"generation": 4,
"managedFields": [{
"apiVersion": "apps/v1",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:metadata": {
"f:annotations": {
".": {},
"f:kubernetes.io/change-cause": {}
}
},
"f:spec": {
"f:podManagementPolicy": {},
"f:replicas": {},
"f:revisionHistoryLimit": {},
"f:selector": {},
"f:serviceName": {},
"f:template": {
"f:metadata": {
"f:labels": {
".": {},
"f:app": {}
}
},
"f:spec": {
"f:containers": {
"k:{\"name\":\"busybox-host\"}": {
".": {},
"f:args": {},
"f:command": {},
"f:image": {},
"f:imagePullPolicy": {},
"f:name": {},
"f:resources": {},
"f:terminationMessagePath": {},
"f:terminationMessagePolicy": {}
}
},
"f:dnsPolicy": {},
"f:restartPolicy": {},
"f:schedulerName": {},
"f:securityContext": {},
"f:terminationGracePeriodSeconds": {}
}
},
"f:updateStrategy": {
"f:rollingUpdate": {
".": {},
"f:partition": {}
},
"f:type": {}
}
}
},
"manager": "kubectl-create",
"operation": "Update",
"time": "2021-08-13T09:44:24.000000Z"
}, {
"apiVersion": "apps/v1",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:spec": {
"f:template": {
"f:metadata": {
"f:annotations": {}
}
}
}
},
"manager": "kubectl-rollout",
"operation": "Update",
"time": "2021-08-13T09:47:22.000000Z"
}, {
"apiVersion": "apps/v1",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:spec": {
"f:template": {
"f:metadata": {
"f:annotations": {
"f:kubectl.kubernetes.io/restartedAt": {}
}
}
}
}
},
"manager": "Kubernetes Java Client",
"operation": "Update",
"time": "2021-08-17T11:13:42.000000Z"
}],
"name": "foo",
"namespace": "default",
"resourceVersion": "103707",
"uid": "9a968c95-4b78-4b72-baf9-fa18bab00155"
},
"spec": {
"podManagementPolicy": "OrderedReady",
"replicas": 2,
"revisionHistoryLimit": 10,
"selector": {
"matchLabels": {
"app": "bar"
}
},
"serviceName": "busybox-service\"",
"template": {
"metadata": {
"annotations": {
"kubectl.kubernetes.io/restartedAt": "2021-08-13T17:47:22+08:00"
},
"labels": {
"app": "bar"
}
},
"spec": {
"containers": [{
"args": ["1000"],
"command": ["sleep"],
"image": "busybox:1.31.1",
"imagePullPolicy": "IfNotPresent",
"name": "busybox-host",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File"
}],
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"terminationGracePeriodSeconds": 30
}
},
"updateStrategy": {
"rollingUpdate": {
"partition": 0
},
"type": "RollingUpdate"
}
},
"status": {
"replicas": 0
}
}
Loading