Skip to content

Commit

Permalink
fix (jkube-kit/enricher) : WellKnownLabelEnricher also considers labe…
Browse files Browse the repository at this point in the history
…ls added via resource configuration

While adding labels and selectors to a resource, WellKnownLabelEnricher
and ProjectLabelEnricher would give precedence to label configured via
resource label XML/Groovy DSL configuration.

This precedence would depend on the type of resource label configuration used.
If `all` has been provided in configuration, this means precedence would be given
for all resources. However, if some specific resource label configuration is
used; precedence would only be given for that specific resource

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and manusa committed Feb 5, 2024
1 parent 51a80d3 commit ed4154b
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 56 deletions.
34 changes: 34 additions & 0 deletions gradle-plugin/it/src/it/well-known-labels/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ repositories {
mavenCentral()
}

def allProperties = new Properties();
allProperties.put("team","via-resource-groovy-dsl-labels-all")
allProperties.put("language", "via-resource-groovy-dsl-labels-all")
allProperties.put("provider", "via-resource-groovy-dsl-labels-all")
allProperties.put("app", "via-resource-groovy-dsl-labels-all")
allProperties.put("version", "via-resource-groovy-dsl-labels-all")
allProperties.put("group", "via-resource-groovy-dsl-labels-all")
allProperties.put("app.kubernetes.io/name", "via-resource-groovy-dsl-labels-all")
allProperties.put("app.kubernetes.io/version", "via-resource-groovy-dsl-labels-all")
allProperties.put("app.kubernetes.io/component", "via-resource-groovy-dsl-labels-all")
allProperties.put("app.kubernetes.io/managed-by", "via-resource-groovy-dsl-labels-all")
allProperties.put("app.kubernetes.io/part-of", "via-resource-groovy-dsl-labels-all")

def serviceProperties = new Properties();
serviceProperties.put("team", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("language", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("provider", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("app", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("version", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("group", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("app.kubernetes.io/name", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("app.kubernetes.io/version", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("app.kubernetes.io/component", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("app.kubernetes.io/managed-by", "via-resource-groovy-dsl-labels-service")
serviceProperties.put("app.kubernetes.io/part-of", "via-resource-groovy-dsl-labels-service")

def extensionConfig = {
offline = true
images {
Expand All @@ -36,6 +62,14 @@ def extensionConfig = {
}
}
}
if (project.hasProperty('labelsViaResourceConfig')) {
resources {
labels {
all = allProperties
service = serviceProperties
}
}
}
}

kubernetes(extensionConfig)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
annotations:
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-service
app.kubernetes.io/component: via-resource-groovy-dsl-labels-service
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-service
app.kubernetes.io/name: via-resource-groovy-dsl-labels-service
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-service
app.kubernetes.io/version: via-resource-groovy-dsl-labels-service
group: via-resource-groovy-dsl-labels-service
provider: via-resource-groovy-dsl-labels-service
version: via-resource-groovy-dsl-labels-service
name: well-known-labels
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: via-resource-groovy-dsl-labels-service
app.kubernetes.io/component: via-resource-groovy-dsl-labels-service
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-service
app.kubernetes.io/name: via-resource-groovy-dsl-labels-service
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-service
group: via-resource-groovy-dsl-labels-service
provider: via-resource-groovy-dsl-labels-service
- apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
app.kubernetes.io/version: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
version: via-resource-groovy-dsl-labels-all
name: well-known-labels
spec:
replicas: 1
revisionHistoryLimit: 2
selector:
matchLabels:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
template:
metadata:
annotations:
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
app.kubernetes.io/version: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
version: via-resource-groovy-dsl-labels-all
name: well-known-labels
spec:
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: HOSTNAME
valueFrom:
fieldRef:
fieldPath: metadata.name
image: repository/well-known-labels:latest
imagePullPolicy: IfNotPresent
name: repository-well-known-labels
ports:
- containerPort: 8080
name: http
protocol: TCP
securityContext:
privileged: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
annotations:
app.openshift.io/vcs-ref: "@ignore@"
app.openshift.io/vcs-uri: "@ignore@"
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-service
app.kubernetes.io/component: via-resource-groovy-dsl-labels-service
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-service
app.kubernetes.io/name: via-resource-groovy-dsl-labels-service
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-service
app.kubernetes.io/version: via-resource-groovy-dsl-labels-service
group: via-resource-groovy-dsl-labels-service
provider: via-resource-groovy-dsl-labels-service
version: via-resource-groovy-dsl-labels-service
name: well-known-labels
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: via-resource-groovy-dsl-labels-service
app.kubernetes.io/component: via-resource-groovy-dsl-labels-service
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-service
app.kubernetes.io/name: via-resource-groovy-dsl-labels-service
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-service
group: via-resource-groovy-dsl-labels-service
provider: via-resource-groovy-dsl-labels-service
- apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
annotations:
app.openshift.io/vcs-ref: "@ignore@"
app.openshift.io/vcs-uri: "@ignore@"
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
app.kubernetes.io/version: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
version: via-resource-groovy-dsl-labels-all
name: well-known-labels
spec:
replicas: 1
revisionHistoryLimit: 2
selector:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
strategy:
rollingParams:
timeoutSeconds: 3600
type: Rolling
template:
metadata:
annotations:
app.openshift.io/vcs-ref: "@ignore@"
app.openshift.io/vcs-uri: "@ignore@"
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
app.kubernetes.io/version: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
version: via-resource-groovy-dsl-labels-all
name: well-known-labels
spec:
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: repository/well-known-labels:latest
imagePullPolicy: IfNotPresent
name: repository-well-known-labels
ports:
- containerPort: 8080
name: http
protocol: TCP
securityContext:
privileged: false
triggers:
- type: ConfigChange
- imageChangeParams:
automatic: true
containerNames:
- repository-well-known-labels
from:
kind: ImageStreamTag
name: well-known-labels:latest
type: ImageChange
- apiVersion: route.openshift.io/v1
kind: Route
metadata:
annotations:
app.openshift.io/vcs-ref: "@ignore@"
app.openshift.io/vcs-uri: "@ignore@"
jkube.eclipse.org/git-branch: "@ignore@"
jkube.eclipse.org/git-commit: "@ignore@"
jkube.eclipse.org/git-url: "@ignore@"
labels:
app: via-resource-groovy-dsl-labels-all
app.kubernetes.io/component: via-resource-groovy-dsl-labels-all
app.kubernetes.io/managed-by: via-resource-groovy-dsl-labels-all
app.kubernetes.io/name: via-resource-groovy-dsl-labels-all
app.kubernetes.io/part-of: via-resource-groovy-dsl-labels-all
app.kubernetes.io/version: via-resource-groovy-dsl-labels-all
group: via-resource-groovy-dsl-labels-all
provider: via-resource-groovy-dsl-labels-all
version: via-resource-groovy-dsl-labels-all
name: well-known-labels
spec:
port:
targetPort: 8080
to:
kind: Service
name: well-known-labels
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ static Stream<Arguments> data() {
"-Pjkube.enricher.jkube-well-known-labels.component=custom-component",
"-Pjkube.enricher.jkube-well-known-labels.partOf=custom-part-of",
"-Pjkube.enricher.jkube-well-known-labels.managedBy=custom-managed-by",
})
}),
arguments("labelsViaResourceConfig", new String[] {"-PlabelsViaResourceConfig=true"})
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ public static void mergeIfAbsent(Map<String, String> map, Map<String, String> to
}

/**
* Returns a new map with all the entries of map1 and any from map2 which don't override map1.
* Returns a new map with all the entries of first map with rest map entries which don't override map1.
*
* Can handle either maps being null. Always returns a new mutable map
* Can handle either maps being null. Always returns a new mutable map.
*
* @param map1 first hash map
* @param map2 second hash map
* <b>Note:</b> Be careful about the ordering of maps passed here. First map passed in the var args
* would always be given precedence over other maps in case there are colliding entries with same key values.
*
* @param maps var arg for maps
* @param <K> first type
* @param <V> second type
* @return merged hash map
*/
public static <K,V> Map<K,V> mergeMaps(Map<K, V> map1, Map<K, V> map2) {
public static <K,V> Map<K,V> mergeMaps(Map<K, V> ...maps) {
Map<K, V> answer = new HashMap<>();
if (map2 != null) {
answer.putAll(map2);
}
if (map1 != null) {
answer.putAll(map1);
for (int i = maps.length-1; i >= 0; i--) {
if (maps[i] != null) {
answer.putAll(maps[i]);
}
}
return answer;

Expand Down
Loading

0 comments on commit ed4154b

Please sign in to comment.