From b5af2dff308f7e6d214a404d29bedeb5b881238f Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Thu, 23 Dec 2021 21:48:51 +0530 Subject: [PATCH] Fix #3662: NodeMetrics should be marked as Cluster Scoped resource NodeMetrics should not implement Namespaced interface Signed-off-by: Rohan Kumar --- CHANGELOG.md | 1 + .../kubernetes-model-metrics/pom.xml | 5 ++ .../model/metrics/v1beta1/NodeMetrics.java | 3 +- .../main/resources/schema/kube-schema.json | 3 +- .../resources/schema/validation-schema.json | 3 +- .../metrics/v1beta1/NodeMetricsTest.java | 78 +++++++++++++++++++ .../src/test/resources/valid-nodemetric.json | 15 ++++ .../pkg/schemagen/generate.go | 1 + 8 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 kubernetes-model-generator/kubernetes-model-metrics/src/test/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetricsTest.java create mode 100644 kubernetes-model-generator/kubernetes-model-metrics/src/test/resources/valid-nodemetric.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d49324c016..422d498ab1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 5.12-SNAPSHOT #### Bugs +* Fix #3662: NodeMetrics should be marked as Cluster scoped resource #### Improvements diff --git a/kubernetes-model-generator/kubernetes-model-metrics/pom.xml b/kubernetes-model-generator/kubernetes-model-metrics/pom.xml index 1ca532ddfb6..cc63a55bb31 100644 --- a/kubernetes-model-generator/kubernetes-model-metrics/pom.xml +++ b/kubernetes-model-generator/kubernetes-model-metrics/pom.xml @@ -41,6 +41,11 @@ io.fabric8 kubernetes-model-common + + org.assertj + assertj-core + test + diff --git a/kubernetes-model-generator/kubernetes-model-metrics/src/generated/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetrics.java b/kubernetes-model-generator/kubernetes-model-metrics/src/generated/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetrics.java index 5b97f1a5f5e..f071ac0bfaa 100644 --- a/kubernetes-model-generator/kubernetes-model-metrics/src/generated/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetrics.java +++ b/kubernetes-model-generator/kubernetes-model-metrics/src/generated/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetrics.java @@ -16,7 +16,6 @@ import io.fabric8.kubernetes.api.model.IntOrString; import io.fabric8.kubernetes.api.model.LabelSelector; import io.fabric8.kubernetes.api.model.LocalObjectReference; -import io.fabric8.kubernetes.api.model.Namespaced; import io.fabric8.kubernetes.api.model.ObjectReference; import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; import io.fabric8.kubernetes.api.model.PodTemplateSpec; @@ -66,7 +65,7 @@ @TemplateTransformations({ @TemplateTransformation(value = "/manifest.vm", outputPath = "metrics.properties", gather = true) }) -public class NodeMetrics implements HasMetadata, Namespaced +public class NodeMetrics implements HasMetadata { /** diff --git a/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/kube-schema.json b/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/kube-schema.json index a47ca8d17a2..df4356fed3b 100644 --- a/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/kube-schema.json +++ b/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/kube-schema.json @@ -837,8 +837,7 @@ "additionalProperties": true, "javaType": "io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics", "javaInterfaces": [ - "io.fabric8.kubernetes.api.model.HasMetadata", - "io.fabric8.kubernetes.api.model.Namespaced" + "io.fabric8.kubernetes.api.model.HasMetadata" ] }, "kubernetes_metrics_v1beta1_NodeMetricsList": { diff --git a/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/validation-schema.json b/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/validation-schema.json index cd646cf1984..9f9e69b6d77 100644 --- a/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/validation-schema.json +++ b/kubernetes-model-generator/kubernetes-model-metrics/src/main/resources/schema/validation-schema.json @@ -837,8 +837,7 @@ "additionalProperties": true, "javaType": "io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics", "javaInterfaces": [ - "io.fabric8.kubernetes.api.model.HasMetadata", - "io.fabric8.kubernetes.api.model.Namespaced" + "io.fabric8.kubernetes.api.model.HasMetadata" ] }, "kubernetes_metrics_v1beta1_NodeMetricsList": { diff --git a/kubernetes-model-generator/kubernetes-model-metrics/src/test/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetricsTest.java b/kubernetes-model-generator/kubernetes-model-metrics/src/test/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetricsTest.java new file mode 100644 index 00000000000..0fc69b1381d --- /dev/null +++ b/kubernetes-model-generator/kubernetes-model-metrics/src/test/java/io/fabric8/kubernetes/api/model/metrics/v1beta1/NodeMetricsTest.java @@ -0,0 +1,78 @@ +/** + * 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.api.model.metrics.v1beta1; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.fabric8.kubernetes.api.model.Duration; +import io.fabric8.kubernetes.api.model.Namespaced; +import io.fabric8.kubernetes.api.model.Quantity; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.text.ParseException; +import java.util.Collections; +import java.util.Scanner; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class NodeMetricsTest { + private final ObjectMapper mapper = new ObjectMapper(); + + @Test + void shouldNotImplementNamespacedInterface() { + assertThat(Namespaced.class.isAssignableFrom(NodeMetrics.class)).isFalse(); + } + + @Test + void deserializationAndSerializationShouldWorkAsExpected() throws IOException { + // Given + String originalJson = new Scanner(getClass().getResourceAsStream("/valid-nodemetric.json")) + .useDelimiter("\\A") + .next(); + + // When + final NodeMetrics nodeMetrics = mapper.readValue(originalJson, NodeMetrics.class); + final String serializedJson = mapper.writeValueAsString(nodeMetrics); + final NodeMetrics nodeMetricsFromSerializedJson = mapper.readValue(serializedJson, NodeMetrics.class); + + // Then + assertThat(nodeMetrics).isNotNull(); + assertThat(serializedJson).isNotNull(); + assertThat(nodeMetricsFromSerializedJson).isNotNull(); + assertThat(nodeMetrics).isEqualTo(nodeMetricsFromSerializedJson); + } + + @Test + void builderShouldCreateObject() throws ParseException { + // Given + NodeMetricsBuilder nodeMetricsBuilder = new NodeMetricsBuilder() + .withNewMetadata().withName("minikube").endMetadata() + .withTimestamp("2021-12-23T16:05:09Z") + .withWindow(Duration.parse("30s")) + .withUsage(Collections.singletonMap("cpu", new Quantity("134994244n"))); + + // When + NodeMetrics nodeMetrics = nodeMetricsBuilder.build(); + + // Then + assertThat(nodeMetrics) + .isNotNull() + .hasFieldOrPropertyWithValue("metadata.name", "minikube") + .hasFieldOrPropertyWithValue("timestamp", "2021-12-23T16:05:09Z") + .hasFieldOrPropertyWithValue("window", Duration.parse("30s")) + .hasFieldOrPropertyWithValue("usage", Collections.singletonMap("cpu", new Quantity("134994244n"))); + } +} diff --git a/kubernetes-model-generator/kubernetes-model-metrics/src/test/resources/valid-nodemetric.json b/kubernetes-model-generator/kubernetes-model-metrics/src/test/resources/valid-nodemetric.json new file mode 100644 index 00000000000..0c11fbefb6c --- /dev/null +++ b/kubernetes-model-generator/kubernetes-model-metrics/src/test/resources/valid-nodemetric.json @@ -0,0 +1,15 @@ +{ + "kind": "NodeMetrics", + "apiVersion": "metrics.k8s.io/v1beta1", + "metadata": { + "name": "minikube", + "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/minikube", + "creationTimestamp": "2021-12-23T16:05:25Z" + }, + "timestamp": "2021-12-23T16:05:09Z", + "window": "30s", + "usage": { + "cpu": "134994244n", + "memory": "1275716Ki" + } +} diff --git a/kubernetes-model-generator/pkg/schemagen/generate.go b/kubernetes-model-generator/pkg/schemagen/generate.go index c5333d2ab04..415e6063c32 100644 --- a/kubernetes-model-generator/pkg/schemagen/generate.go +++ b/kubernetes-model-generator/pkg/schemagen/generate.go @@ -637,6 +637,7 @@ func (g *schemaGenerator) isClusterScopedResource(t reflect.Type) bool { "k8s.io/api/storage/v1/CSIDriver", "k8s.io/api/storage/v1/CSINode", "k8s.io/api/storage/v1/VolumeAttachment", + "k8s.io/metrics/pkg/apis/metrics/v1beta1/NodeMetrics", "k8s.io/api/node/v1beta1/RuntimeClass", "k8s.io/api/node/v1/RuntimeClass", "k8s.io/api/node/v1alpha1/RuntimeClass",