-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CustomResourceHandler.java
159 lines (135 loc) · 6.53 KB
/
CustomResourceHandler.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
/*
* 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.crdv2.generator.v1;
import io.fabric8.crdv2.generator.AbstractCustomResourceHandler;
import io.fabric8.crdv2.generator.CRDUtils;
import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.crdv2.generator.ResolvingContext;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersionBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.fabric8.kubernetes.client.utils.KubernetesVersionPriority;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.kubernetes.model.annotation.LabelSelector;
import io.fabric8.kubernetes.model.annotation.SpecReplicas;
import io.fabric8.kubernetes.model.annotation.StatusReplicas;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CustomResourceHandler extends AbstractCustomResourceHandler {
private Queue<Map.Entry<CustomResourceDefinition, Set<String>>> crds = new ConcurrentLinkedQueue<>();
public static final String VERSION = "v1";
@Override
public void handle(CustomResourceInfo config, ResolvingContext resolvingContext) {
final String name = config.crdName();
final String version = config.version();
JsonSchema resolver = new JsonSchema(resolvingContext, config.definition());
JSONSchemaProps schema = resolver.getSchema();
CustomResourceDefinitionVersionBuilder builder = new CustomResourceDefinitionVersionBuilder()
.withName(version)
.withStorage(config.storage())
.withServed(config.served())
.withDeprecated(config.deprecated() ? true : null)
.withDeprecationWarning(config.deprecationWarning())
.withNewSchema()
.withOpenAPIV3Schema(schema)
.endSchema();
handlePrinterColumns(resolver, new PrinterColumnHandler() {
@Override
public void addPrinterColumn(String path, String column, String format, int priority, String type, String description) {
builder.addNewAdditionalPrinterColumn()
.withType(type)
.withName(column)
.withJsonPath(path)
.withFormat(Utils.isNotNullOrEmpty(format) ? format : null)
.withDescription(Utils.isNotNullOrEmpty(description) ? description : null)
.withPriority(priority)
.endAdditionalPrinterColumn();
}
});
resolver.getSinglePath(SpecReplicas.class).ifPresent(path -> {
builder.editOrNewSubresources().editOrNewScale().withSpecReplicasPath(path).endScale().endSubresources();
});
resolver.getSinglePath(StatusReplicas.class).ifPresent(path -> {
builder.editOrNewSubresources().editOrNewScale().withStatusReplicasPath(path).endScale().endSubresources();
});
resolver.getSinglePath(LabelSelector.class).ifPresent(path -> {
builder.editOrNewSubresources().editOrNewScale().withLabelSelectorPath(path).endScale().endSubresources();
});
if (config.statusClassName().isPresent()) {
builder.editOrNewSubresources().withNewStatus().endStatus().endSubresources();
}
CustomResourceDefinition crd = new CustomResourceDefinitionBuilder()
.withNewMetadata()
.withName(name)
.withAnnotations(CRDUtils.toMap(config.annotations()))
.withLabels(CRDUtils.toMap(config.labels()))
.endMetadata()
.withNewSpec()
.withScope(config.scope().value())
.withGroup(config.group())
.withNewNames()
.withKind(config.kind())
.withShortNames(config.shortNames())
.withPlural(config.plural())
.withSingular(config.singular())
.endNames()
.addToVersions(builder.build())
.endSpec()
.build();
crds.add(new AbstractMap.SimpleEntry<>(crd, resolver.getDependentClasses()));
}
@Override
public Stream<Entry<? extends HasMetadata, Set<String>>> finish() {
return crds.stream().collect(Collectors.groupingBy(crd -> crd.getKey().getMetadata().getName())).values().stream()
.map(this::combine);
}
private Map.Entry<CustomResourceDefinition, Set<String>> combine(
List<Map.Entry<CustomResourceDefinition, Set<String>>> definitions) {
Map.Entry<CustomResourceDefinition, Set<String>> primary = definitions.get(0);
if (definitions.size() == 1) {
return primary;
}
List<CustomResourceDefinitionVersion> versions = definitions.stream()
.flatMap(crd -> crd.getKey().getSpec().getVersions().stream())
.collect(Collectors.toList());
Set<String> allDependentClasses = definitions.stream().flatMap(crd -> crd.getValue().stream()).collect(Collectors.toSet());
List<String> storageVersions = versions.stream()
.filter(v -> Optional.ofNullable(v.getStorage()).orElse(true))
.map(CustomResourceDefinitionVersion::getName)
.collect(Collectors.toList());
if (storageVersions.size() > 1) {
throw new IllegalStateException(String.format(
"'%s' custom resource has versions %s marked as storage. Only one version can be marked as storage per custom resource.",
primary.getKey().getMetadata().getName(), storageVersions));
}
versions = KubernetesVersionPriority.sortByPriority(versions, CustomResourceDefinitionVersion::getName);
//TODO: we could double check that the top-level metadata is consistent across all versions
return new AbstractMap.SimpleEntry<>(
new CustomResourceDefinitionBuilder(primary.getKey()).editSpec().withVersions(versions).endSpec().build(),
allDependentClasses);
}
}