Skip to content

Commit ffa4605

Browse files
authored
fix: change rolling update logic to a json merge patch
closes: #7072 Signed-off-by: Steve Hawkins <[email protected]> # Conflicts: # CHANGELOG.md
1 parent 5027a5c commit ffa4605

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
#### Bugs
66
* Fix #7148: corrected octal format detection
7-
* Fix #7167: Allow Informer.isWatching to see underlying Watch state
8-
* Fix #7087: Avoid possible NPE in OkHttp websocket handlinger
7+
- Fix #7167: Allow Informer.isWatching to see underlying Watch state
8+
* Fix #7087: Avoid possible NPE in OkHttp websocket handling
9+
* Fix #7072: Changed rolling update handling to json merge patch to avoid 422 errors
910
* Fix #7080: Avoid NPE in CRDGenerator if post-processor is set to null
1011
* Fix #7116: (java-generator) Use timezone format compatible with Kubernetes
1112
* Fix #7163: Ensure that streams are notified of errors

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/RollingUpdater.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.security.NoSuchAlgorithmException;
4646
import java.time.ZoneOffset;
4747
import java.time.format.DateTimeFormatter;
48+
import java.util.AbstractMap;
4849
import java.util.Date;
4950
import java.util.List;
5051
import java.util.Map;
@@ -168,40 +169,30 @@ public T rollUpdate(T oldObj, T newObj) {
168169
}
169170
}
170171

171-
private static <T> T applyPatch(Resource<T> resource, List<Object> list, KubernetesSerialization serialization) {
172-
return resource.patch(PatchContext.of(PatchType.JSON), serialization.asJson(list));
172+
private static <T> T applyPatch(Resource<T> resource, Map<String, Object> map, KubernetesSerialization serialization) {
173+
return resource.patch(PatchContext.of(PatchType.JSON_MERGE), serialization.asJson(map));
173174
}
174175

175176
public static <T extends HasMetadata> T resume(RollableScalableResourceOperation<T, ?, ?> resource) {
176-
return applyPatch(resource, RollingUpdater.requestPayLoadForRolloutResume(), resource.getKubernetesSerialization());
177+
return applyPatch(resource, RollingUpdater.requestPayLoadForRollout(null), resource.getKubernetesSerialization());
177178
}
178179

179180
public static <T extends HasMetadata> T pause(RollableScalableResourceOperation<T, ?, ?> resource) {
180-
return applyPatch(resource, RollingUpdater.requestPayLoadForRolloutPause(), resource.getKubernetesSerialization());
181+
return applyPatch(resource, RollingUpdater.requestPayLoadForRollout(Boolean.TRUE), resource.getKubernetesSerialization());
181182
}
182183

183184
public static <T extends HasMetadata> T restart(RollableScalableResourceOperation<T, ?, ?> resource) {
184185
return applyPatch(resource, RollingUpdater.requestPayLoadForRolloutRestart(), resource.getKubernetesSerialization());
185186
}
186187

187-
public static List<Object> requestPayLoadForRolloutPause() {
188-
return List.of(Map.of(
189-
"op", "add",
190-
"path", "/spec/paused",
191-
"value", true));
188+
public static Map<String, Object> requestPayLoadForRollout(Boolean paused) {
189+
return Map.of("spec", new AbstractMap.SimpleEntry<>("paused", paused));
192190
}
193191

194-
public static List<Object> requestPayLoadForRolloutResume() {
195-
return List.of(Map.of(
196-
"op", "remove",
197-
"path", "/spec/paused"));
198-
}
199-
200-
public static List<Object> requestPayLoadForRolloutRestart() {
201-
return List.of(Map.of(
202-
"op", "add",
203-
"path", "/spec/template/metadata/annotations/kubectl.kubernetes.io~1restartedAt",
204-
"value", new Date().toInstant().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)));
192+
public static Map<String, Object> requestPayLoadForRolloutRestart() {
193+
String now = new Date().toInstant().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
194+
return Map.of("spec",
195+
Map.of("template", Map.of("metadata", Map.of("annotations", Map.of("kubectl.kubernetes.io/restartedAt", now)))));
205196
}
206197

207198
/**

kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ void testRolloutPause() throws InterruptedException {
623623
// Then
624624
RecordedRequest recordedRequest = server.getLastRequest();
625625
assertEquals("PATCH", recordedRequest.getMethod());
626-
assertThat(client.getKubernetesSerialization().unmarshal(recordedRequest.getBody().readUtf8(), List.class))
627-
.isEqualTo(List.of(Map.of("op", "add", "path", "/spec/paused", "value", true)));
626+
assertThat(recordedRequest.getBody().readUtf8())
627+
.isEqualTo("{\"spec\":{\"paused\":true}}");
628628
}
629629

630630
@Test
@@ -654,8 +654,8 @@ void testRolloutResume() throws InterruptedException {
654654
RecordedRequest recordedRequest = server.getLastRequest();
655655
assertNotNull(deployment);
656656
assertEquals("PATCH", recordedRequest.getMethod());
657-
assertThat(client.getKubernetesSerialization().unmarshal(recordedRequest.getBody().readUtf8(), List.class))
658-
.isEqualTo(List.of(Map.of("op", "remove", "path", "/spec/paused")));
657+
assertThat(recordedRequest.getBody().readUtf8())
658+
.isEqualTo("{\"spec\":{\"paused\":null}}");
659659
}
660660

661661
@Test
@@ -685,7 +685,7 @@ void testRolloutRestart() throws InterruptedException {
685685
RecordedRequest recordedRequest = server.getLastRequest();
686686
assertNotNull(deployment);
687687
assertEquals("PATCH", recordedRequest.getMethod());
688-
assertTrue(recordedRequest.getBody().readUtf8().contains("kubectl.kubernetes.io~1restartedAt"));
688+
assertTrue(recordedRequest.getBody().readUtf8().contains("kubectl.kubernetes.io/restartedAt"));
689689
}
690690

691691
@Test

kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/StatefulSetTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ void testRolloutRestart() throws InterruptedException {
399399
RecordedRequest recordedRequest = server.getLastRequest();
400400
assertNotNull(deployment);
401401
assertEquals("PATCH", recordedRequest.getMethod());
402-
assertTrue(recordedRequest.getBody().readUtf8().contains("kubectl.kubernetes.io~1restartedAt"));
402+
assertTrue(recordedRequest.getBody().readUtf8().contains("kubectl.kubernetes.io/restartedAt"));
403403
}
404404

405405
@Test

0 commit comments

Comments
 (0)