Skip to content

Commit f3874ac

Browse files
authored
Merge pull request #1225 from yue9944882/refactor/model-mapper-pt1
Refactor model mapper: adding group-version-resource mapping
2 parents 1e90a3e + a6804cc commit f3874ac

File tree

7 files changed

+197
-108
lines changed

7 files changed

+197
-108
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.e2e.util
14+
15+
import io.kubernetes.client.Discovery
16+
import io.kubernetes.client.apimachinery.GroupVersionKind
17+
import io.kubernetes.client.apimachinery.GroupVersionResource
18+
import io.kubernetes.client.openapi.models.V1Deployment
19+
import io.kubernetes.client.openapi.models.V1Pod
20+
import io.kubernetes.client.openapi.models.V1beta1CustomResourceDefinition
21+
import io.kubernetes.client.util.ClientBuilder
22+
import io.kubernetes.client.util.ModelMapper
23+
import spock.lang.Specification
24+
25+
class ModelMapperTest extends Specification {
26+
27+
def "api-discovery should work"() {
28+
given:
29+
def apiClient = ClientBuilder.defaultClient();
30+
def discovery = new Discovery(apiClient)
31+
ModelMapper.refresh(discovery);
32+
33+
expect:
34+
new GroupVersionKind("", "v1", "Pod") == ModelMapper.getGroupVersionKindByClass(V1Pod.class)
35+
new GroupVersionResource("", "v1", "pods") == ModelMapper.getGroupVersionResourceByClass(V1Pod.class)
36+
37+
new GroupVersionKind("apps", "v1", "Deployment") == ModelMapper.getGroupVersionKindByClass(V1Deployment.class)
38+
new GroupVersionResource("apps", "v1", "deployments") == ModelMapper.getGroupVersionResourceByClass(V1Deployment.class)
39+
40+
new GroupVersionKind("apiextensions.k8s.io", "v1beta1", "CustomResourceDefinition") == ModelMapper.getGroupVersionKindByClass(V1beta1CustomResourceDefinition.class)
41+
new GroupVersionResource("apiextensions.k8s.io", "v1beta1", "customresourcedefinitions") == ModelMapper.getGroupVersionResourceByClass(V1beta1CustomResourceDefinition.class)
42+
43+
}
44+
}

util/src/main/java/io/kubernetes/client/apimachinery/GroupVersion.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public static GroupVersion parse(KubernetesObject obj) {
3838
}
3939

4040
public GroupVersion(String group, String version) {
41+
if (group == null) {
42+
throw new IllegalArgumentException("group must not be null");
43+
}
44+
if (version == null) {
45+
throw new IllegalArgumentException("version must not be null");
46+
}
4147
this.group = group;
4248
this.version = version;
4349
}

util/src/main/java/io/kubernetes/client/apimachinery/GroupVersionKind.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class GroupVersionKind extends GroupVersion {
2020

2121
public GroupVersionKind(String group, String version, String kind) {
2222
super(group, version);
23+
if (kind == null) {
24+
throw new IllegalArgumentException("kind must not be null");
25+
}
2326
this.kind = kind;
2427
}
2528

util/src/main/java/io/kubernetes/client/apimachinery/GroupVersionResource.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@
1212
*/
1313
package io.kubernetes.client.apimachinery;
1414

15-
public class GroupVersionResource {
15+
import com.google.common.base.Objects;
16+
17+
public class GroupVersionResource extends GroupVersion {
1618

17-
private final String group;
18-
private final String version;
1919
private final String resource;
2020

2121
public GroupVersionResource(String group, String version, String resource) {
22-
this.group = group;
23-
this.version = version;
22+
super(group, version);
23+
if (resource == null) {
24+
throw new IllegalArgumentException("resource must not be null");
25+
}
2426
this.resource = resource;
2527
}
2628

27-
public String getGroup() {
28-
return group;
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
32+
if (o == null || getClass() != o.getClass()) return false;
33+
if (!super.equals(o)) return false;
34+
GroupVersionResource that = (GroupVersionResource) o;
35+
return Objects.equal(resource, that.resource);
2936
}
3037

31-
public String getVersion() {
32-
return version;
38+
@Override
39+
public int hashCode() {
40+
return Objects.hashCode(super.hashCode(), resource);
3341
}
3442

3543
public String getResource() {

0 commit comments

Comments
 (0)