Skip to content

Commit 506473c

Browse files
committed
feat: dynamic shortcut utiltities
1 parent 6c28218 commit 506473c

File tree

4 files changed

+124
-8
lines changed

4 files changed

+124
-8
lines changed

util/src/main/java/io/kubernetes/client/util/generic/dynamic/DynamicKubernetesObject.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import com.google.gson.JsonObject;
1616
import io.kubernetes.client.common.KubernetesObject;
17-
import io.kubernetes.client.openapi.Configuration;
1817
import io.kubernetes.client.openapi.models.V1ObjectMeta;
1918
import java.util.Objects;
2019

@@ -36,8 +35,7 @@ public DynamicKubernetesObject(JsonObject obj) {
3635

3736
@Override
3837
public V1ObjectMeta getMetadata() {
39-
return Configuration.getDefaultApiClient()
40-
.getJSON()
38+
return Dynamics.internalJSONCodec
4139
.getGson()
4240
.fromJson(this.raw.get("metadata"), V1ObjectMeta.class);
4341
}
@@ -65,8 +63,7 @@ public void setKind(String kind) {
6563
}
6664

6765
public void setMetadata(V1ObjectMeta objectMeta) {
68-
this.raw.add(
69-
"metadata", Configuration.getDefaultApiClient().getJSON().getGson().toJsonTree(objectMeta));
66+
this.raw.add("metadata", Dynamics.internalJSONCodec.getGson().toJsonTree(objectMeta));
7067
}
7168

7269
@Override

util/src/main/java/io/kubernetes/client/util/generic/dynamic/Dynamics.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414

1515
import com.google.gson.Gson;
1616
import com.google.gson.JsonElement;
17-
import io.kubernetes.client.openapi.Configuration;
17+
import io.kubernetes.client.openapi.JSON;
18+
import java.util.Map;
19+
import org.yaml.snakeyaml.Yaml;
1820

1921
public class Dynamics {
2022

23+
static final JSON internalJSONCodec = new JSON();
24+
static final Yaml internalYamlCodec = new Yaml();
25+
2126
public static DynamicKubernetesObject newFromJson(String jsonContent) {
22-
return newFromJson(Configuration.getDefaultApiClient().getJSON().getGson(), jsonContent);
27+
return newFromJson(internalJSONCodec.getGson(), jsonContent);
2328
}
2429

2530
public static DynamicKubernetesObject newFromJson(Gson gson, String jsonContent) {
@@ -28,11 +33,39 @@ public static DynamicKubernetesObject newFromJson(Gson gson, String jsonContent)
2833
}
2934

3035
public static DynamicKubernetesListObject newListFromJson(String jsonContent) {
31-
return newListFromJson(Configuration.getDefaultApiClient().getJSON().getGson(), jsonContent);
36+
return newListFromJson(internalJSONCodec.getGson(), jsonContent);
3237
}
3338

3439
public static DynamicKubernetesListObject newListFromJson(Gson gson, String jsonContent) {
3540
JsonElement raw = gson.fromJson(jsonContent, JsonElement.class);
3641
return new DynamicKubernetesListObject(raw.getAsJsonObject());
3742
}
43+
44+
public static DynamicKubernetesObject newFromYaml(String yamlContent) {
45+
return newFromYaml(internalYamlCodec, yamlContent);
46+
}
47+
48+
public static DynamicKubernetesObject newFromYaml(Yaml yamlCodec, String yamlContent) {
49+
return newFromJson(
50+
internalJSONCodec.getGson(),
51+
fromYamlToJson(yamlCodec, internalJSONCodec.getGson(), yamlContent));
52+
}
53+
54+
public static String fromYamlToJson(String yamlContent) {
55+
return fromYamlToJson(internalYamlCodec, internalJSONCodec.getGson(), yamlContent);
56+
}
57+
58+
public static String fromJsonToYaml(String jsonContent) {
59+
return fromJsonToYaml(internalYamlCodec, internalJSONCodec.getGson(), jsonContent);
60+
}
61+
62+
public static String fromYamlToJson(Yaml yamlCodec, Gson gson, String yamlContent) {
63+
Map<String, Object> rawYamlObj = yamlCodec.load(yamlContent);
64+
return gson.toJson(rawYamlObj);
65+
}
66+
67+
public static String fromJsonToYaml(Yaml yamlCodec, Gson gson, String jsonContent) {
68+
Map<String, Object> rawJsonObj = gson.fromJson(jsonContent, Map.class);
69+
return yamlCodec.dump(rawJsonObj);
70+
}
3871
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.util.generic.dynamic;
14+
15+
import static org.junit.Assert.*;
16+
17+
import com.google.gson.JsonObject;
18+
import io.kubernetes.client.Resources;
19+
import io.kubernetes.client.openapi.JSON;
20+
import io.kubernetes.client.util.Yaml;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Paths;
24+
import org.junit.Test;
25+
26+
public class DynamicsTest {
27+
28+
private static final String TEST_POD_YAML_FILE = Resources.getResource("test-pod.yaml").getPath();
29+
30+
private static final String TEST_POD_JSON_FILE = Resources.getResource("test-pod.json").getPath();
31+
32+
private JSON json = new JSON();
33+
34+
@Test
35+
public void testYamlToJson() throws IOException {
36+
String podYamlContent = new String(Files.readAllBytes(Paths.get(TEST_POD_YAML_FILE)));
37+
String podJsonContent = new String(Files.readAllBytes(Paths.get(TEST_POD_JSON_FILE)));
38+
String convertedJsonContent = Dynamics.fromYamlToJson(podYamlContent);
39+
40+
assertEquals(
41+
json.getGson().fromJson(podJsonContent, JsonObject.class),
42+
json.getGson().fromJson(convertedJsonContent, JsonObject.class));
43+
}
44+
45+
@Test
46+
public void testJsonToYaml() throws IOException {
47+
String podYamlContent = new String(Files.readAllBytes(Paths.get(TEST_POD_YAML_FILE)));
48+
String podJsonContent = new String(Files.readAllBytes(Paths.get(TEST_POD_JSON_FILE)));
49+
String convertedYamlContent = Dynamics.fromJsonToYaml(podJsonContent);
50+
51+
assertEquals(Yaml.load(podYamlContent), Yaml.load(convertedYamlContent));
52+
}
53+
54+
@Test
55+
public void testLoadingJsonToDynamicObj() throws IOException {
56+
String podJsonContent = new String(Files.readAllBytes(Paths.get(TEST_POD_JSON_FILE)));
57+
DynamicKubernetesObject obj = Dynamics.newFromJson(podJsonContent);
58+
59+
assertEquals(obj.getApiVersion(), "v1");
60+
assertEquals(obj.getKind(), "Pod");
61+
}
62+
63+
@Test
64+
public void testLoadingYamlToDynamicObj() throws IOException {
65+
String podYamlContent = new String(Files.readAllBytes(Paths.get(TEST_POD_YAML_FILE)));
66+
DynamicKubernetesObject obj = Dynamics.newFromYaml(podYamlContent);
67+
68+
assertEquals(obj.getApiVersion(), "v1");
69+
assertEquals(obj.getKind(), "Pod");
70+
}
71+
}

util/src/test/resources/test-pod.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"apiVersion": "v1",
3+
"kind": "Pod",
4+
"metadata": {
5+
"creationTimestamp": "2018-12-23T01:09:18Z",
6+
"generateName": "test-776d6c86cc-",
7+
"labels": {
8+
"app": "test",
9+
"app-version": "19911",
10+
"pod-template-hash": "3328274277"
11+
},
12+
"name": "test-776d6c86cc-4zwj5",
13+
"namespace": "default"
14+
}
15+
}

0 commit comments

Comments
 (0)