Skip to content

Commit

Permalink
Fix #3662: NodeMetrics should be marked as Cluster Scoped resource
Browse files Browse the repository at this point in the history
NodeMetrics should not implement Namespaced interface

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and manusa committed Dec 24, 2021
1 parent d67ca14 commit b5af2df
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 5.12-SNAPSHOT

#### Bugs
* Fix #3662: NodeMetrics should be marked as Cluster scoped resource

#### Improvements

Expand Down
5 changes: 5 additions & 0 deletions kubernetes-model-generator/kubernetes-model-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-common</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions kubernetes-model-generator/pkg/schemagen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b5af2df

Please sign in to comment.