/
CustomResourceDefinitionContext.java
218 lines (188 loc) · 7.5 KB
/
CustomResourceDefinitionContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* 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.client.dsl.base;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionVersion;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionSpec;
import io.fabric8.kubernetes.client.utils.KubernetesVersionPriority;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CustomResourceDefinitionContext {
private String name;
private String group;
private String scope;
private String plural;
private String version;
private String kind;
public String getName() { return name; }
public String getGroup() {
return group;
}
public String getScope() {
return scope;
}
public String getPlural() {
return plural;
}
public String getVersion() {
return version;
}
public String getKind() {
return kind;
}
public static CustomResourceDefinitionBuilder v1beta1CRDFromCustomResourceType(Class<? extends CustomResource> customResource) {
try {
final CustomResource instance = customResource.getDeclaredConstructor().newInstance();
final String version = instance.getVersion();
return new CustomResourceDefinitionBuilder()
.withNewMetadata()
.withName(instance.getCRDName())
.endMetadata()
.withNewSpec()
.withGroup(instance.getGroup())
.withVersion(version) // also set version to the first (and only) versions item
.addNewVersion().withName(version).withServed(true).withStorage(true).endVersion()
.withScope(instance.getScope())
.withNewNames()
.withKind(instance.getKind())
.withPlural(instance.getPlural())
.withSingular(instance.getSingular())
.endNames()
.endSpec();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
public static io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder v1CRDFromCustomResourceType(Class<? extends CustomResource> customResource) {
try {
final CustomResource instance = customResource.getDeclaredConstructor().newInstance();
return new io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder()
.withNewMetadata()
.withName(instance.getCRDName())
.endMetadata()
.withNewSpec()
.withGroup(instance.getGroup())
.addNewVersion().withName(instance.getVersion()).withServed(true).withStorage(true).endVersion()
.withScope(instance.getScope())
.withNewNames()
.withKind(instance.getKind())
.withPlural(instance.getPlural())
.withSingular(instance.getSingular())
.endNames()
.endSpec();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
public static CustomResourceDefinitionContext fromCustomResourceType(Class<? extends HasMetadata> customResource) {
try {
final CustomResource instance = (CustomResource) customResource.getDeclaredConstructor().newInstance();
return new Builder()
.withGroup(instance.getGroup())
.withVersion(instance.getVersion())
.withScope(instance.getScope())
.withName(instance.getCRDName())
.withPlural(instance.getPlural())
.withKind(instance.getKind())
.build();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw KubernetesClientException.launderThrowable(e);
}
}
public static CustomResourceDefinitionContext fromCrd(CustomResourceDefinition crd) {
final CustomResourceDefinitionSpec spec = crd.getSpec();
return new CustomResourceDefinitionContext.Builder()
.withGroup(spec.getGroup())
.withVersion(getVersion(spec))
.withScope(spec.getScope())
.withName(crd.getMetadata().getName())
.withPlural(spec.getNames().getPlural())
.withKind(spec.getNames().getKind())
.build();
}
public static CustomResourceDefinitionContext fromCrd(
io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition crd) {
return new CustomResourceDefinitionContext.Builder()
.withGroup(crd.getSpec().getGroup())
.withVersion(getVersion(crd.getSpec()))
.withScope(crd.getSpec().getScope())
.withName(crd.getMetadata().getName())
.withPlural(crd.getSpec().getNames().getPlural())
.withKind(crd.getSpec().getNames().getKind())
.build();
}
private static String getVersion(List<String> versions, String defaultVersion) {
return Optional.ofNullable(versions)
.map(KubernetesVersionPriority::highestPriority)
.orElse(defaultVersion);
}
private static String getVersion(CustomResourceDefinitionSpec spec) {
return getVersion(
spec.getVersions().stream()
.map(CustomResourceDefinitionVersion::getName)
.collect(Collectors.toList()),
spec.getVersion()
);
}
private static String getVersion(
io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec spec) {
return getVersion(
spec.getVersions().stream()
.map(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion::getName)
.collect(Collectors.toList()),
null
);
}
public static class Builder {
private final CustomResourceDefinitionContext customResourceDefinitionContext;
public Builder() {
this.customResourceDefinitionContext = new CustomResourceDefinitionContext();
}
public Builder withName(String name) {
this.customResourceDefinitionContext.name = name;
return this;
}
public Builder withGroup(String group) {
this.customResourceDefinitionContext.group = group;
return this;
}
public Builder withScope(String scope) {
this.customResourceDefinitionContext.scope = scope;
return this;
}
public Builder withPlural(String plural) {
this.customResourceDefinitionContext.plural = plural;
return this;
}
public Builder withVersion(String version) {
this.customResourceDefinitionContext.version = version;
return this;
}
public Builder withKind(String kind) {
this.customResourceDefinitionContext.kind = kind;
return this;
}
public CustomResourceDefinitionContext build() {
return this.customResourceDefinitionContext;
}
}
}