Skip to content

Commit

Permalink
Fixes for new codegen.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Sep 26, 2024
1 parent f93790e commit 3844f99
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ private void validatePods(List<V1Pod> pods) throws KubectlException {
for (V1Pod pod : pods) {
if (pod.getMetadata().getOwnerReferences() == null) continue;

if (!force && pod.getMetadata().getOwnerReferences().size() == 0) {
throw new KubectlException("Pods unmanaged by a controller are present on the node");
}
// Throw exception if there are daemon set pods and ignore daemon set is false
if (!ignoreDaemonSets) {
for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void kubectlDelete() throws KubectlException, ApiException {
kubectlDelete.namespace("foo").name("bar");
DeleteOptions deleteOptions = new DeleteOptions();
deleteOptions.setPropagationPolicy("Foreground");
deleteOptions.setDryRun(null);
kubectlDelete.deleteOptions(deleteOptions);
kubectlDelete.execute();
apiServer.verify(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2024 The Kubernetes Authors.
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.kubernetes.client.gson;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import io.kubernetes.client.openapi.models.V1ObjectMeta;

public class V1MetadataExclusionStrategy implements com.google.gson.ExclusionStrategy {
public boolean shouldSkipField(FieldAttributes f) {
// Don't serialize the 'managedFields' field.
return (f.getDeclaringClass().equals(V1ObjectMeta.class) && f.getName().equalsIgnoreCase("managedFields"));
}

public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
40 changes: 33 additions & 7 deletions kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.google.gson.JsonElement;
import io.gsonfire.GsonFireBuilder;
import io.gsonfire.TypeSelector;

import io.kubernetes.client.gson.V1MetadataExclusionStrategy;
import io.kubernetes.client.gson.V1StatusPreProcessor;
import io.kubernetes.client.openapi.models.V1Status;
import okio.ByteString;

import java.io.IOException;
Expand All @@ -35,6 +37,9 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
Expand All @@ -50,9 +55,19 @@
public class JSON {
private static Gson gson;
private static boolean isLenientOnJson = false;
private static final DateTimeFormatter RFC3339MICRO_FORMATTER =
new DateTimeFormatterBuilder()
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 6, 6, true)
.optionalEnd()
.appendLiteral("Z")
.toFormatter();

private static DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(RFC3339MICRO_FORMATTER);
private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();

Expand All @@ -63,10 +78,12 @@ public class JSON {

@SuppressWarnings("unchecked")
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
;
GsonBuilder builder = fireBuilder.createGsonBuilder();
return builder;
GsonFireBuilder fireBuilder = new GsonFireBuilder();
GsonBuilder builder =
fireBuilder
.registerPreProcessor(V1Status.class, new V1StatusPreProcessor())
.createGsonBuilder();
return builder.setExclusionStrategies(new V1MetadataExclusionStrategy());
}

private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
Expand Down Expand Up @@ -784,11 +801,15 @@ public static class ByteArrayAdapter extends TypeAdapter<byte[]> {

@Override
public void write(JsonWriter out, byte[] value) throws IOException {
boolean oldHtmlSafe = out.isHtmlSafe();
out.setHtmlSafe(false);

if (value == null) {
out.nullValue();
} else {
out.value(ByteString.of(value).base64());
}
out.setHtmlSafe(oldHtmlSafe);
}

@Override
Expand Down Expand Up @@ -844,7 +865,12 @@ public OffsetDateTime read(JsonReader in) throws IOException {
if (date.endsWith("+0000")) {
date = date.substring(0, date.length()-5) + "Z";
}
return OffsetDateTime.parse(date, formatter);
try {
return OffsetDateTime.parse(date, formatter);
} catch (DateTimeParseException e) {
// backward-compatibility for ISO8601 timestamp format
return OffsetDateTime.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ public void write(JsonWriter out, V1ListMeta value) throws IOException {
@Override
public V1ListMeta read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
validateJsonElement(jsonElement);
// Disable validation so delete API can tolerate non-status return object (graceful deletion)
// validateJsonObject(jsonObj);
return thisAdapter.fromJsonTree(jsonElement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.kubernetes.client.custom.MapUtils;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -244,7 +245,7 @@ public boolean equals(Object o) {
}
V1Secret v1Secret = (V1Secret) o;
return Objects.equals(this.apiVersion, v1Secret.apiVersion) &&
Objects.equals(this.data, v1Secret.data) &&
MapUtils.equals(this.data, v1Secret.data) &&
Objects.equals(this.immutable, v1Secret.immutable) &&
Objects.equals(this.kind, v1Secret.kind) &&
Objects.equals(this.metadata, v1Secret.metadata) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ public void write(JsonWriter out, V1Status value) throws IOException {
@Override
public V1Status read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
validateJsonElement(jsonElement);
// Disable validation so delete API can tolerate non-status return object (graceful deletion)
// validateJsonObject(jsonObj);
return thisAdapter.fromJsonTree(jsonElement);
}

Expand Down
8 changes: 5 additions & 3 deletions scripts/patches/json.diff
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ index 4406c2199..f56413a25 100644
+ fireBuilder
+ .registerPreProcessor(V1Status.class, new V1StatusPreProcessor())
+ .createGsonBuilder();
return builder;
- return builder;
+ return builder.setExclusionStrategies(new V1MetadataExclusionStrategy());
}


private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
@@ -721,11 +739,14 @@ public class JSON {

@Override
Expand Down Expand Up @@ -82,4 +84,4 @@ index 4406c2199..f56413a25 100644
+ }
}
}
}
}
11 changes: 5 additions & 6 deletions scripts/patches/list-meta.diff
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1ListMeta.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1ListMeta.java
index 60381b312..7fb47e230 100644
index f161284a2..d3d563bbb 100644
--- a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1ListMeta.java
+++ b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1ListMeta.java
@@ -266,7 +266,9 @@ public class V1ListMeta {
@@ -258,7 +258,8 @@ public class V1ListMeta {
@Override
public V1ListMeta read(JsonReader in) throws IOException {
JsonObject jsonObj = elementAdapter.read(in).getAsJsonObject();
- validateJsonObject(jsonObj);
+
JsonElement jsonElement = elementAdapter.read(in);
- validateJsonElement(jsonElement);
+ // Disable validation so delete API can tolerate non-status return object (graceful deletion)
+ // validateJsonObject(jsonObj);
return thisAdapter.fromJsonTree(jsonObj);
return thisAdapter.fromJsonTree(jsonElement);
}

12 changes: 7 additions & 5 deletions scripts/patches/secret.diff
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java
index 8fdadaac6..5fe296242 100644
index 5684f4776..aa77bd40f 100644
--- a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java
+++ b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Secret.java
@@ -19,6 +19,7 @@ import com.google.gson.annotations.JsonAdapter;
@@ -18,6 +18,7 @@ import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
+import io.kubernetes.client.custom.MapUtils;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import java.io.IOException;
import java.util.HashMap;
@@ -259,7 +260,7 @@ public class V1Secret implements io.kubernetes.client.common.KubernetesObject {
import java.util.Arrays;
@@ -243,8 +244,8 @@ public class V1Secret implements io.kubernetes.client.common.KubernetesObject {
return false;
}
V1Secret v1Secret = (V1Secret) o;
return Objects.equals(this.apiVersion, v1Secret.apiVersion) &&
- return Objects.equals(this.apiVersion, v1Secret.apiVersion) &&
- Objects.equals(this.data, v1Secret.data) &&
+ return Objects.equals(this.apiVersion, v1Secret.apiVersion) &&
+ MapUtils.equals(this.data, v1Secret.data) &&
Objects.equals(this.immutable, v1Secret.immutable) &&
Objects.equals(this.kind, v1Secret.kind) &&
Expand Down
10 changes: 5 additions & 5 deletions scripts/patches/status.diff
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
diff --git a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Status.java b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Status.java
index b2b6db803..8a8a9765d 100644
index b529e3455..c2268419a 100644
--- a/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Status.java
+++ b/kubernetes/src/main/java/io/kubernetes/client/openapi/models/V1Status.java
@@ -394,7 +394,8 @@ public class V1Status {
@@ -378,7 +378,8 @@ public class V1Status {
@Override
public V1Status read(JsonReader in) throws IOException {
JsonObject jsonObj = elementAdapter.read(in).getAsJsonObject();
- validateJsonObject(jsonObj);
JsonElement jsonElement = elementAdapter.read(in);
- validateJsonElement(jsonElement);
+ // Disable validation so delete API can tolerate non-status return object (graceful deletion)
+ // validateJsonObject(jsonObj);
return thisAdapter.fromJsonTree(jsonObj);
return thisAdapter.fromJsonTree(jsonElement);
}

4 changes: 4 additions & 0 deletions util/src/main/java/io/kubernetes/client/util/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.constructor.BaseConstructor;
Expand Down Expand Up @@ -366,6 +367,9 @@ protected NodeTuple representJavaBeanProperty(
if (propertyValue == null) {
return null;
}
if (propertyValue instanceof List<?> && ((List<?>)propertyValue).size() == 0) {
return null;
}
return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
}
}
Expand Down

0 comments on commit 3844f99

Please sign in to comment.