Skip to content

Commit 6e621b2

Browse files
authored
Merge pull request #417 from davidxia/fix-quantity-serialization
Fix Quantity serialization
2 parents e4f183b + 12b147c commit 6e621b2

File tree

4 files changed

+85
-14
lines changed

4 files changed

+85
-14
lines changed

util/src/main/java/io/kubernetes/client/util/Yaml.java

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

1515
import com.google.common.reflect.ClassPath;
1616
import io.kubernetes.client.custom.IntOrString;
17+
import io.kubernetes.client.custom.Quantity;
1718
import java.io.File;
1819
import java.io.FileReader;
1920
import java.io.IOException;
@@ -356,6 +357,7 @@ public CustomRepresenter() {
356357
this.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
357358
this.representers.put(IntOrString.class, new RepresentIntOrString());
358359
this.representers.put(byte[].class, new RepresentByteArray());
360+
this.representers.put(Quantity.class, new RepresentQuantity());
359361
}
360362

361363
private class RepresentIntOrString implements Represent {
@@ -378,6 +380,14 @@ public Node representData(Object data) {
378380
}
379381
}
380382

383+
private class RepresentQuantity implements Represent {
384+
@Override
385+
public Node representData(Object data) {
386+
Quantity quantity = (Quantity) data;
387+
return representScalar(Tag.STR, quantity.toSuffixedString());
388+
}
389+
}
390+
381391
/**
382392
* This returns the ordering of properties that by convention should appear at the beginning of
383393
* a Yaml object in Kubernetes.

util/src/test/java/io/kubernetes/client/util/YamlTest.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
*/
1313
package io.kubernetes.client.util;
1414

15-
import static org.junit.Assert.*;
15+
import static java.nio.charset.StandardCharsets.UTF_8;
16+
import static org.hamcrest.CoreMatchers.equalTo;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertThat;
21+
import static org.junit.Assert.assertTrue;
1622

1723
import com.google.common.io.Resources;
1824
import io.kubernetes.client.models.AppsV1beta1Deployment;
@@ -26,14 +32,17 @@
2632
import java.io.StringReader;
2733
import java.io.StringWriter;
2834
import java.lang.reflect.Method;
29-
import java.nio.charset.StandardCharsets;
30-
import java.nio.file.Files;
31-
import java.nio.file.Paths;
35+
import java.net.URL;
3236
import java.util.List;
3337
import org.junit.Test;
3438

3539
public class YamlTest {
36-
private static final String TEST_YAML_FILE_PATH = Resources.getResource("test.yaml").getPath();
40+
41+
private static final URL TEST_YAML_FILE = Resources.getResource("test.yaml");
42+
private static final String TEST_YAML_FILE_PATH = TEST_YAML_FILE.getPath();
43+
44+
private static final URL EXPECTED_YAML_FILE = Resources.getResource("expected.yaml");
45+
3746
private static final String[] kinds =
3847
new String[] {
3948
"Pod",
@@ -134,16 +143,14 @@ public void testLoadAllFile() throws Exception {
134143
assertEquals("Secret", secret.getKind());
135144
assertEquals("secret", secret.getMetadata().getName());
136145
assertEquals("Opaque", secret.getType());
137-
assertEquals(
138-
"hello", new String(secret.getData().get("secret-data"), StandardCharsets.UTF_8));
146+
assertEquals("hello", new String(secret.getData().get("secret-data"), UTF_8));
139147
} else {
140148
throw new Exception("some thing wrong happened");
141149
}
142150
}
143151
String result = Yaml.dumpAll(list.iterator());
144-
String expected =
145-
new String(Files.readAllBytes(Paths.get(TEST_YAML_FILE_PATH)), StandardCharsets.UTF_8);
146-
assertEquals(expected, result);
152+
String expected = Resources.toString(EXPECTED_YAML_FILE, UTF_8);
153+
assertThat(result, equalTo(expected));
147154
}
148155

149156
@Test
@@ -202,7 +209,7 @@ public void testLoadBytes() {
202209
assertEquals(
203210
"Incorrect value loaded for Base64 encoded secret",
204211
"hello",
205-
new String(secret.getData().get("hello"), StandardCharsets.UTF_8));
212+
new String(secret.getData().get("hello"), UTF_8));
206213

207214
} catch (Exception ex) {
208215
assertNull("Unexpected exception: " + ex.toString(), ex);
@@ -223,9 +230,7 @@ public void testDateTime() {
223230
assertEquals(
224231
"Incorrect value loaded for creationTimestamp",
225232
"2018-09-06T15:12:24.000Z",
226-
new String(
227-
pod.getMetadata().getCreationTimestamp().toString().getBytes(),
228-
StandardCharsets.UTF_8));
233+
new String(pod.getMetadata().getCreationTimestamp().toString().getBytes(), UTF_8));
229234

230235
} catch (Exception ex) {
231236
assertNull("Unexpected exception: " + ex.toString(), ex);

util/src/test/resources/expected.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
labels:
5+
app: mock
6+
name: mock
7+
spec:
8+
ports:
9+
- port: 99
10+
protocol: TCP
11+
targetPort: 9949
12+
selector:
13+
app: mock
14+
---
15+
apiVersion: apps/v1beta1
16+
kind: Deployment
17+
metadata:
18+
labels:
19+
app: helloworld
20+
name: helloworld
21+
spec:
22+
replicas: 1
23+
template:
24+
metadata:
25+
labels:
26+
app: helloworld
27+
name: helloworld
28+
spec:
29+
containers:
30+
- args:
31+
- -http=127.0.0.1:8080
32+
image: gcr.io/hightowerlabs/helloworld:0.0.1
33+
imagePullPolicy: Always
34+
name: helloworld
35+
resources:
36+
limits:
37+
memory: 1Gi
38+
cpu: 500m
39+
requests:
40+
memory: 4Gi
41+
cpu: '2'
42+
---
43+
apiVersion: v1
44+
kind: Secret
45+
metadata:
46+
name: secret
47+
type: Opaque
48+
data:
49+
secret-data: aGVsbG8=

util/src/test/resources/test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ spec:
3232
image: gcr.io/hightowerlabs/helloworld:0.0.1
3333
imagePullPolicy: Always
3434
name: helloworld
35+
resources:
36+
limits:
37+
memory: 1Gi
38+
cpu: 0.5
39+
requests:
40+
memory: 4Gi
41+
cpu: 2
3542
---
3643
apiVersion: v1
3744
kind: Secret

0 commit comments

Comments
 (0)