Skip to content

Commit b6e3ccc

Browse files
Honor Annotations and Labels From Existing YAML (#165)
Co-authored-by: Shrinand Thakkar <[email protected]>
1 parent 9daa0fb commit b6e3ccc

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

hoptimator-k8s/src/main/java/com/linkedin/hoptimator/k8s/K8sYamlApi.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Collection;
55
import java.util.HashMap;
66
import java.util.Map;
7+
import java.util.Optional;
78

89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
@@ -58,7 +59,15 @@ public void create(DynamicKubernetesObject obj) throws SQLException {
5859
public void createWithAnnotationsAndLabels(String yaml, Map<String, String> annotations,
5960
Map<String, String> labels) throws SQLException {
6061
DynamicKubernetesObject obj = objFromYaml(yaml);
61-
createWithAnnotationsAndLabels(obj, annotations, labels);
62+
// Merge annotations and labels from existing yaml to the provided maps.
63+
Map<String, String> mergedAnnotations = new HashMap<>();
64+
Optional.ofNullable(obj.getMetadata().getAnnotations()).ifPresent(mergedAnnotations::putAll);
65+
Optional.ofNullable(annotations).ifPresent(mergedAnnotations::putAll);
66+
67+
Map<String, String> mergedLabels = new HashMap<>();
68+
Optional.ofNullable(obj.getMetadata().getLabels()).ifPresent(mergedLabels::putAll);
69+
Optional.ofNullable(labels).ifPresent(mergedLabels::putAll);
70+
createWithAnnotationsAndLabels(obj, mergedAnnotations, mergedLabels);
6271
}
6372

6473
public void createWithAnnotationsAndLabels(DynamicKubernetesObject obj, Map<String, String> annotations,

0 commit comments

Comments
 (0)