Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions util/src/main/java/io/kubernetes/client/util/Watch.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ protected boolean isStatus(String line) throws IOException {

protected Response<T> parseLine(String line) throws IOException {
if (!isStatus(line)) {
return json.deserialize(line, watchType);
return json.getGson().fromJson(line, watchType);
}
Type statusType = new TypeToken<Response<V1Status>>() {}.getType();
Response<V1Status> status = json.deserialize(line, statusType);
Response<V1Status> status = json.getGson().fromJson(line, statusType);
return new Response<T>(status.type, status.object);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2025 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.util;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.JSON;
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject;
import java.io.IOException;
import org.junit.jupiter.api.Test;

/** Tests for Watch with DynamicKubernetesObject */
class WatchDynamicObjectTest {

@Test
void watchAddedEventShouldPreserveRawData() throws IOException {
JSON json = new JSON();
// Register the DynamicKubernetesTypeAdaptorFactory to properly handle DynamicKubernetesObject
json.setGson(json.getGson().newBuilder()
.registerTypeAdapterFactory(new io.kubernetes.client.util.generic.dynamic.DynamicKubernetesTypeAdaptorFactory())
.create());
Watch<DynamicKubernetesObject> watch =
new Watch<>(
json, null, new TypeToken<Watch.Response<DynamicKubernetesObject>>() {}.getType(), null);

String addedEvent = "{\"type\":\"ADDED\","
+ "\"object\":{"
+ "\"apiVersion\":\"v1\","
+ "\"kind\":\"Pod\","
+ "\"metadata\":{\"name\":\"test-pod\",\"namespace\":\"default\"},"
+ "\"spec\":{\"containers\":[{\"name\":\"nginx\",\"image\":\"nginx:latest\"}]}"
+ "}}";

Watch.Response<DynamicKubernetesObject> response = watch.parseLine(addedEvent);
assertThat(response.type).isEqualTo("ADDED");
assertThat(response.object).isNotNull();
assertThat(response.object.getMetadata()).isNotNull();
assertThat(response.object.getMetadata().getName()).isEqualTo("test-pod");

// Check that raw data is preserved
assertThat(response.object.getRaw()).isNotNull();
assertThat(response.object.getRaw().size()).isGreaterThan(2); // Should have more than just metadata
assertThat(response.object.getRaw().has("spec")).isTrue();
assertThat(response.object.getRaw().get("spec")).isNotNull();
}

@Test
void watchModifiedEventShouldPreserveRawData() throws IOException {
JSON json = new JSON();
// Register the DynamicKubernetesTypeAdaptorFactory to properly handle DynamicKubernetesObject
json.setGson(json.getGson().newBuilder()
.registerTypeAdapterFactory(new io.kubernetes.client.util.generic.dynamic.DynamicKubernetesTypeAdaptorFactory())
.create());
Watch<DynamicKubernetesObject> watch =
new Watch<>(
json, null, new TypeToken<Watch.Response<DynamicKubernetesObject>>() {}.getType(), null);

String modifiedEvent = "{\"type\":\"MODIFIED\","
+ "\"object\":{"
+ "\"apiVersion\":\"v1\","
+ "\"kind\":\"Pod\","
+ "\"metadata\":{\"name\":\"test-pod\",\"namespace\":\"default\"},"
+ "\"spec\":{\"containers\":[{\"name\":\"nginx\",\"image\":\"nginx:1.19\"}]}"
+ "}}";

Watch.Response<DynamicKubernetesObject> response = watch.parseLine(modifiedEvent);
assertThat(response.type).isEqualTo("MODIFIED");
assertThat(response.object).isNotNull();
assertThat(response.object.getMetadata()).isNotNull();
assertThat(response.object.getMetadata().getName()).isEqualTo("test-pod");

// Check that raw data is preserved
assertThat(response.object.getRaw()).isNotNull();
assertThat(response.object.getRaw().size()).isGreaterThan(2); // Should have more than just metadata
assertThat(response.object.getRaw().has("spec")).isTrue();
assertThat(response.object.getRaw().get("spec")).isNotNull();
}
}
Loading