forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatchUtils.java
87 lines (76 loc) · 3.06 KB
/
PatchUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.dsl.internal;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.ResourceCompare;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.zjsonpatch.JsonDiff;
import java.util.Optional;
public class PatchUtils {
public enum Format {
YAML,
JSON
}
private PatchUtils() {
}
private static class SingletonHolder {
public static final ObjectMapper patchMapper;
static {
patchMapper = Serialization.jsonMapper().copy();
// if this isn't set the patches are still correct, but not as compact for some reason - array values are added individually
patchMapper.setConfig(patchMapper.getSerializationConfig().without(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS));
}
}
public static String withoutRuntimeState(Object object, Format format, boolean omitStatus) {
ObjectMapper mapper = format == Format.JSON ? Serialization.jsonMapper() : Serialization.yamlMapper();
try {
return mapper.writeValueAsString(withoutRuntimeState(object, omitStatus));
} catch (JsonProcessingException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
/**
* See also {@link ResourceCompare#withoutRuntimeState}
*/
static JsonNode withoutRuntimeState(Object object, boolean omitStatus) {
ObjectNode raw = SingletonHolder.patchMapper.convertValue(object, ObjectNode.class);
Optional.ofNullable(raw.get("metadata")).filter(ObjectNode.class::isInstance).map(ObjectNode.class::cast).ifPresent(m -> {
m.remove("creationTimestamp");
m.remove("deletionTimestamp");
m.remove("generation");
m.remove("resourceVersion");
m.remove("selfLink");
m.remove("uid");
});
if (omitStatus) {
raw.remove("status");
}
return raw;
}
public static String jsonDiff(Object current, Object updated, boolean omitStatus) {
try {
return Serialization.jsonMapper().writeValueAsString(
JsonDiff.asJson(withoutRuntimeState(current, omitStatus), withoutRuntimeState(updated, omitStatus)));
} catch (JsonProcessingException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
}