-
Notifications
You must be signed in to change notification settings - Fork 209
/
Mappers.java
126 lines (109 loc) · 4.34 KB
/
Mappers.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
package io.javaoperatorsdk.operator.processing.event.source.informer;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
public class Mappers {
public static final String DEFAULT_ANNOTATION_FOR_NAME = "io.javaoperatorsdk/primary-name";
public static final String DEFAULT_ANNOTATION_FOR_NAMESPACE =
"io.javaoperatorsdk/primary-namespace";
private Mappers() {}
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromAnnotation(
String nameKey) {
return fromMetadata(nameKey, null, false);
}
@SuppressWarnings("unused")
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromAnnotation(
String nameKey, String namespaceKey) {
return fromMetadata(nameKey, namespaceKey, false);
}
@SuppressWarnings("unused")
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromLabel(String nameKey) {
return fromMetadata(nameKey, null, true);
}
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromDefaultAnnotations() {
return fromMetadata(DEFAULT_ANNOTATION_FOR_NAME, DEFAULT_ANNOTATION_FOR_NAMESPACE, false);
}
@SuppressWarnings("unused")
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromLabel(
String nameKey, String namespaceKey) {
return fromMetadata(nameKey, namespaceKey, true);
}
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReference() {
return fromOwnerReference(false);
}
/**
* @param clusterScope if the owner is a cluster scoped resource
* @return mapper
* @param <T> type of the secondary resource, where the owner reference is
*/
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReference(
boolean clusterScope) {
return resource -> ResourceID.fromFirstOwnerReference(resource, clusterScope).map(Set::of)
.orElseGet(Collections::emptySet);
}
public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReferences(
boolean clusterScope) {
return resource -> resource.getMetadata().getOwnerReferences().stream()
.map(or -> ResourceID.fromOwnerReference(resource, or, clusterScope))
.collect(Collectors.toSet());
}
private static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromMetadata(
String nameKey, String namespaceKey, boolean isLabel) {
return resource -> {
final var metadata = resource.getMetadata();
if (metadata == null) {
return Collections.emptySet();
} else {
final var map = isLabel ? metadata.getLabels() : metadata.getAnnotations();
if (map == null) {
return Collections.emptySet();
}
var name = map.get(nameKey);
if (name == null) {
return Collections.emptySet();
}
var namespace =
namespaceKey == null ? resource.getMetadata().getNamespace() : map.get(namespaceKey);
return Set.of(new ResourceID(name, namespace));
}
};
}
public static ResourceID fromString(String cacheKey) {
if (cacheKey == null) {
return null;
}
final String[] split = cacheKey.split("/");
switch (split.length) {
case 1:
return new ResourceID(split[0]);
case 2:
return new ResourceID(split[1], split[0]);
default:
throw new IllegalArgumentException("Cannot extract a ResourceID from " + cacheKey);
}
}
/**
* Produces a mapper that will associate a secondary resource with all owners of the primary type.
*/
public static <OWNER extends HasMetadata, T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerType(
Class<OWNER> clazz) {
String kind = HasMetadata.getKind(clazz);
return resource -> {
var meta = resource.getMetadata();
if (meta == null) {
return Set.of();
}
var owners = meta.getOwnerReferences();
if (owners == null || owners.isEmpty()) {
return Set.of();
}
return owners.stream()
.filter(it -> kind.equals(it.getKind()))
.map(it -> new ResourceID(it.getName(), resource.getMetadata().getNamespace()))
.collect(Collectors.toSet());
};
}
}