From 90a65add7f147344bcb7244bddc4eb57b1569e01 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Fri, 5 Jan 2024 23:50:52 +0530 Subject: [PATCH] feat (jkube-kit/enricher) : Add WellKnownLabelsEnricher for adding Kubernetes Recommended Well Known Labels + Add WellKnownLabelEnricher that would add these labels to `.metadata` of all resources (also to `selector.matchLabels` in case of controller resources): - `app.kubernetes.io/component` - `app.kubernetes.io/managed-by` - `app.kubernetes.io/name` - `app.kubernetes.io/part-of` - `app.kubernetes.io/version` Signed-off-by: Rohan Kumar --- .../kubernetes.yml | 18 + .../openshift.yml | 22 ++ .../ftp-port-expose-annotation/kubernetes.yml | 18 + .../ftp-port-expose-annotation/openshift.yml | 22 ++ .../expected/ftp-port-external/kubernetes.yml | 22 ++ .../expected/ftp-port-external/openshift.yml | 22 ++ .../expose/expected/ftp-port/kubernetes.yml | 18 + .../it/expose/expected/ftp-port/openshift.yml | 18 + .../http-port-external/kubernetes.yml | 22 ++ .../expected/http-port-external/openshift.yml | 22 ++ .../expose/expected/http-port/kubernetes.yml | 18 + .../expose/expected/http-port/openshift.yml | 22 ++ .../expected/multiple-services/kubernetes.yml | 39 ++ .../expected/multiple-services/openshift.yml | 47 +++ .../expose/expected/no-ports/kubernetes.yml | 11 + .../it/expose/expected/no-ports/openshift.yml | 11 + .../META-INF/jkube/profiles-default.yml | 1 + .../META-INF/jkube/profiles-default.yml | 1 + .../generic/AbstractLabelEnricher.java | 146 ++++++++ .../generic/ProjectLabelEnricher.java | 131 +------ .../generic/WellKnownLabelsEnricher.java | 80 +++++ .../resources/META-INF/jkube/enricher-default | 3 + .../generic/WellKnownLabelsEnricherTest.java | 340 ++++++++++++++++++ .../expected/kubernetes.yml | 13 +- .../src/it/fragments/expected/kubernetes.yml | 16 + .../META-INF/jkube/profiles-default.yml | 1 + .../META-INF/jkube/profiles-default.yml | 1 + 27 files changed, 955 insertions(+), 130 deletions(-) create mode 100644 jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/AbstractLabelEnricher.java create mode 100644 jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricher.java create mode 100644 jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricherTest.java diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/kubernetes.yml index a6f7f809c7..1bb3bc40ff 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube @@ -23,6 +27,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -34,6 +41,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -44,6 +55,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -54,6 +68,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/openshift.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/openshift.yml index 4129baec72..189d1237d1 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation-enricher/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube @@ -25,6 +29,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -38,6 +45,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -47,6 +58,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -63,6 +77,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -104,6 +122,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/kubernetes.yml index a6f7f809c7..1bb3bc40ff 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube @@ -23,6 +27,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -34,6 +41,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -44,6 +55,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -54,6 +68,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/openshift.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/openshift.yml index 4129baec72..189d1237d1 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port-expose-annotation/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube @@ -25,6 +29,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -38,6 +45,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -47,6 +58,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -63,6 +77,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -104,6 +122,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port-external/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port-external/kubernetes.yml index f27aea8cb2..95077b058d 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port-external/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port-external/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -22,6 +26,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -33,6 +40,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -43,6 +54,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -53,6 +67,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -86,6 +104,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port-external/openshift.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port-external/openshift.yml index a061ac11b3..dc0a6bfc91 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port-external/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port-external/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -24,6 +28,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -37,6 +44,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -46,6 +57,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -62,6 +76,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -103,6 +121,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port/kubernetes.yml index 52fe6c39a8..9232036ddb 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port/kubernetes.yml @@ -14,6 +14,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube.integration.tests.gradle + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle name: expose spec: ports: @@ -25,6 +29,9 @@ items: app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle - apiVersion: apps/v1 kind: Deployment metadata: @@ -33,6 +40,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -43,6 +54,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -53,6 +67,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/ftp-port/openshift.yml b/gradle-plugin/it/src/it/expose/expected/ftp-port/openshift.yml index dd29cedcec..e63d8dee9c 100644 --- a/gradle-plugin/it/src/it/expose/expected/ftp-port/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/ftp-port/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -24,6 +28,9 @@ items: protocol: TCP targetPort: 21 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -37,6 +44,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -46,6 +57,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -62,6 +76,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle + app.kubernetes.io/version: "@ignore@" app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/http-port-external/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/http-port-external/kubernetes.yml index 29a9a57af3..a2c628880f 100644 --- a/gradle-plugin/it/src/it/expose/expected/http-port-external/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/http-port-external/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -22,6 +26,9 @@ items: protocol: TCP targetPort: 80 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -33,6 +40,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -43,6 +54,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -53,6 +67,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -86,6 +104,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/http-port-external/openshift.yml b/gradle-plugin/it/src/it/expose/expected/http-port-external/openshift.yml index bba8e016ca..fafc7378c0 100644 --- a/gradle-plugin/it/src/it/expose/expected/http-port-external/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/http-port-external/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -24,6 +28,9 @@ items: protocol: TCP targetPort: 80 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -37,6 +44,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -46,6 +57,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -62,6 +76,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -103,6 +121,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/http-port/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/http-port/kubernetes.yml index c8d6a6c12a..557c02968f 100644 --- a/gradle-plugin/it/src/it/expose/expected/http-port/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/http-port/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -22,6 +26,9 @@ items: protocol: TCP targetPort: 80 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -33,6 +40,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -43,6 +54,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -53,6 +67,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/http-port/openshift.yml b/gradle-plugin/it/src/it/expose/expected/http-port/openshift.yml index bba8e016ca..fafc7378c0 100644 --- a/gradle-plugin/it/src/it/expose/expected/http-port/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/http-port/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -24,6 +28,9 @@ items: protocol: TCP targetPort: 80 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -37,6 +44,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -46,6 +57,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -62,6 +76,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -103,6 +121,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/multiple-services/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/multiple-services/kubernetes.yml index 0f8980275f..6e6d8d68c2 100644 --- a/gradle-plugin/it/src/it/expose/expected/multiple-services/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/multiple-services/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -21,6 +25,9 @@ items: port: 443 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -32,6 +39,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "false" app: expose provider: jkube @@ -44,6 +55,9 @@ items: port: 80 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -55,6 +69,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -70,6 +88,9 @@ items: - port: 8778 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -81,6 +102,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube @@ -93,6 +118,9 @@ items: port: 22 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -104,6 +132,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -114,6 +146,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -124,6 +159,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/multiple-services/openshift.yml b/gradle-plugin/it/src/it/expose/expected/multiple-services/openshift.yml index 0a74e34f4f..2fee0a99fe 100644 --- a/gradle-plugin/it/src/it/expose/expected/multiple-services/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/multiple-services/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -23,6 +27,9 @@ items: port: 443 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -36,6 +43,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "false" app: expose provider: jkube @@ -48,6 +59,9 @@ items: port: 80 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -61,6 +75,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -76,6 +94,9 @@ items: - port: 8778 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -89,6 +110,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube @@ -101,6 +126,9 @@ items: port: 22 protocol: TCP selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -114,6 +142,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -123,6 +155,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -139,6 +174,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -180,6 +219,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -201,6 +244,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle expose: "true" app: expose provider: jkube diff --git a/gradle-plugin/it/src/it/expose/expected/no-ports/kubernetes.yml b/gradle-plugin/it/src/it/expose/expected/no-ports/kubernetes.yml index 617fe24ebf..a5b3784bdd 100644 --- a/gradle-plugin/it/src/it/expose/expected/no-ports/kubernetes.yml +++ b/gradle-plugin/it/src/it/expose/expected/no-ports/kubernetes.yml @@ -10,6 +10,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -20,6 +24,9 @@ items: revisionHistoryLimit: 2 selector: matchLabels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -30,6 +37,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/it/src/it/expose/expected/no-ports/openshift.yml b/gradle-plugin/it/src/it/expose/expected/no-ports/openshift.yml index d1cce010bc..4e108f2cf8 100644 --- a/gradle-plugin/it/src/it/expose/expected/no-ports/openshift.yml +++ b/gradle-plugin/it/src/it/expose/expected/no-ports/openshift.yml @@ -12,6 +12,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" @@ -21,6 +25,9 @@ items: replicas: 1 revisionHistoryLimit: 2 selector: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube group: org.eclipse.jkube.integration.tests.gradle @@ -37,6 +44,10 @@ items: jkube.eclipse.org/git-commit: "@ignore@" jkube.eclipse.org/git-branch: "@ignore@" labels: + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/name: expose + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/part-of: org.eclipse.jkube.integration.tests.gradle app: expose provider: jkube version: "@ignore@" diff --git a/gradle-plugin/kubernetes/src/main/resources/META-INF/jkube/profiles-default.yml b/gradle-plugin/kubernetes/src/main/resources/META-INF/jkube/profiles-default.yml index b36ca490bc..092aa586f2 100644 --- a/gradle-plugin/kubernetes/src/main/resources/META-INF/jkube/profiles-default.yml +++ b/gradle-plugin/kubernetes/src/main/resources/META-INF/jkube/profiles-default.yml @@ -28,6 +28,7 @@ - jkube-image - jkube-portname - jkube-project-label + - jkube-well-known-labels - jkube-dependency - jkube-pod-annotations - jkube-git diff --git a/gradle-plugin/openshift/src/main/resources/META-INF/jkube/profiles-default.yml b/gradle-plugin/openshift/src/main/resources/META-INF/jkube/profiles-default.yml index cc3abee73d..e0744ef0ba 100644 --- a/gradle-plugin/openshift/src/main/resources/META-INF/jkube/profiles-default.yml +++ b/gradle-plugin/openshift/src/main/resources/META-INF/jkube/profiles-default.yml @@ -30,6 +30,7 @@ - jkube-image - jkube-portname - jkube-project-label + - jkube-well-known-labels - jkube-dependency - jkube-pod-annotations - jkube-git diff --git a/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/AbstractLabelEnricher.java b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/AbstractLabelEnricher.java new file mode 100644 index 0000000000..92b8b82f3d --- /dev/null +++ b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/AbstractLabelEnricher.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.enricher.generic; + +import io.fabric8.kubernetes.api.builder.TypedVisitor; +import io.fabric8.kubernetes.api.model.KubernetesListBuilder; +import io.fabric8.kubernetes.api.model.LabelSelector; +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; +import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder; +import io.fabric8.kubernetes.api.model.ReplicationControllerSpec; +import io.fabric8.kubernetes.api.model.ServiceBuilder; +import io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder; +import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; +import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; +import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder; +import io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec; +import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder; +import io.fabric8.openshift.api.model.DeploymentConfigBuilder; +import io.fabric8.openshift.api.model.DeploymentConfigSpec; +import org.eclipse.jkube.kit.common.util.MapUtil; +import org.eclipse.jkube.kit.config.resource.PlatformMode; +import org.eclipse.jkube.kit.enricher.api.BaseEnricher; +import org.eclipse.jkube.kit.enricher.api.EnricherContext; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public abstract class AbstractLabelEnricher extends BaseEnricher { + protected AbstractLabelEnricher(EnricherContext enricherContext, String name) { + super(enricherContext, name); + } + + abstract Map createLabels(boolean withoutVersion); + + @Override + public void create(PlatformMode platformMode, KubernetesListBuilder builder) { + builder.accept(new TypedVisitor() { + @Override + public void visit(ServiceBuilder serviceBuilder) { + Map selectors = new HashMap<>(); + if(serviceBuilder.buildSpec() != null && serviceBuilder.buildSpec().getSelector() != null) { + selectors.putAll(serviceBuilder.buildSpec().getSelector()); + } + MapUtil.mergeIfAbsent(selectors, createLabels(true)); + serviceBuilder.editOrNewSpec().addToSelector(selectors).endSpec(); + } + }); + + builder.accept(new TypedVisitor() { + @Override + public void visit(DeploymentBuilder builder) { + final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) + .map(DeploymentSpec::getSelector) + .map(LabelSelector::getMatchLabels) + .orElse(new HashMap<>())); + builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); + } + }); + + builder.accept(new TypedVisitor() { + @Override + public void visit(DeploymentConfigBuilder builder) { + final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) + .map(DeploymentConfigSpec::getSelector) + .orElse(new HashMap<>())); + builder.editOrNewSpec().addToSelector(selectors).endSpec(); + } + }); + + builder.accept(new TypedVisitor() { + @Override + public void visit(DaemonSetBuilder builder) { + Map selectors = new HashMap<>(); + if(builder.buildSpec() != null && builder.buildSpec().getSelector() != null && builder.buildSpec().getSelector().getMatchLabels() != null) { + selectors.putAll(builder.buildSpec().getSelector().getMatchLabels()); + } + MapUtil.mergeIfAbsent(selectors, createLabels(false)); + builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); + } + }); + + builder.accept(new TypedVisitor() { + @Override + public void visit(ReplicationControllerBuilder builder) { + final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) + .map(ReplicationControllerSpec::getSelector) + .orElse(new HashMap<>())); + builder.editOrNewSpec().addToSelector(selectors).endSpec(); + } + }); + + builder.accept(new TypedVisitor() { + @Override + public void visit(ReplicaSetBuilder builder) { + final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) + .map(ReplicaSetSpec::getSelector) + .map(LabelSelector::getMatchLabels) + .orElse(new HashMap<>())); + builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); + } + }); + + builder.accept(new TypedVisitor() { + @Override + public void visit(StatefulSetBuilder builder) { + Map selectors = new HashMap<>(); + if(builder.buildSpec() != null && builder.buildSpec().getSelector() != null && builder.buildSpec().getSelector().getMatchLabels() != null) { + selectors.putAll(builder.buildSpec().getSelector().getMatchLabels()); + } + MapUtil.mergeIfAbsent(selectors, createLabels(false)); + builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); + } + }); + + } + + @Override + public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) { + // Add to all objects in the builder + builder.accept(new TypedVisitor() { + @Override + public void visit(ObjectMetaBuilder element) { + Map labels = Optional.ofNullable(element.getLabels()).orElse(new HashMap<>()); + MapUtil.mergeIfAbsent(labels, createLabels(false)); + element.withLabels(labels); + } + }); + } + + private Map mergedSelectors(Map originalSelectors) { + MapUtil.mergeIfAbsent(originalSelectors, createLabels(true)); + return originalSelectors; + } +} diff --git a/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/ProjectLabelEnricher.java b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/ProjectLabelEnricher.java index 8d9adc6772..c4b09bbf30 100644 --- a/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/ProjectLabelEnricher.java +++ b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/ProjectLabelEnricher.java @@ -15,30 +15,11 @@ import java.util.HashMap; import java.util.Map; -import java.util.Optional; import org.eclipse.jkube.kit.common.Configs; -import org.eclipse.jkube.kit.common.util.MapUtil; import org.eclipse.jkube.kit.config.resource.GroupArtifactVersion; -import org.eclipse.jkube.kit.config.resource.PlatformMode; -import org.eclipse.jkube.kit.enricher.api.BaseEnricher; import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; -import io.fabric8.kubernetes.api.builder.TypedVisitor; -import io.fabric8.kubernetes.api.model.KubernetesListBuilder; -import io.fabric8.kubernetes.api.model.LabelSelector; -import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; -import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder; -import io.fabric8.kubernetes.api.model.ReplicationControllerSpec; -import io.fabric8.kubernetes.api.model.ServiceBuilder; -import io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder; -import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; -import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; -import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder; -import io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec; -import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder; -import io.fabric8.openshift.api.model.DeploymentConfigBuilder; -import io.fabric8.openshift.api.model.DeploymentConfigSpec; import lombok.AllArgsConstructor; import lombok.Getter; @@ -59,7 +40,7 @@ * * @author roland */ -public class ProjectLabelEnricher extends BaseEnricher { +public class ProjectLabelEnricher extends AbstractLabelEnricher { public static final String LABEL_PROVIDER = "provider"; @@ -82,115 +63,7 @@ public ProjectLabelEnricher(JKubeEnricherContext buildContext) { } @Override - public void create(PlatformMode platformMode, KubernetesListBuilder builder) { - builder.accept(new TypedVisitor() { - @Override - public void visit(ServiceBuilder serviceBuilder) { - Map selectors = new HashMap<>(); - if(serviceBuilder.buildSpec() != null && serviceBuilder.buildSpec().getSelector() != null) { - selectors.putAll(serviceBuilder.buildSpec().getSelector()); - } - MapUtil.mergeIfAbsent(selectors, createLabels(true)); - serviceBuilder.editOrNewSpec().addToSelector(selectors).endSpec(); - } - }); - - builder.accept(new TypedVisitor() { - @Override - public void visit(DeploymentBuilder builder) { - final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) - .map(DeploymentSpec::getSelector) - .map(LabelSelector::getMatchLabels) - .orElse(new HashMap<>())); - builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); - } - }); - - builder.accept(new TypedVisitor() { - @Override - public void visit(DeploymentConfigBuilder builder) { - final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) - .map(DeploymentConfigSpec::getSelector) - .orElse(new HashMap<>())); - builder.editOrNewSpec().addToSelector(selectors).endSpec(); - } - }); - - builder.accept(new TypedVisitor() { - @Override - public void visit(DaemonSetBuilder builder) { - Map selectors = new HashMap<>(); - if(builder.buildSpec() != null && builder.buildSpec().getSelector() != null && builder.buildSpec().getSelector().getMatchLabels() != null) { - selectors.putAll(builder.buildSpec().getSelector().getMatchLabels()); - } - MapUtil.mergeIfAbsent(selectors, createLabels()); - builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); - } - }); - - builder.accept(new TypedVisitor() { - @Override - public void visit(ReplicationControllerBuilder builder) { - final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) - .map(ReplicationControllerSpec::getSelector) - .orElse(new HashMap<>())); - builder.editOrNewSpec().addToSelector(selectors).endSpec(); - } - }); - - builder.accept(new TypedVisitor() { - @Override - public void visit(ReplicaSetBuilder builder) { - final Map selectors = mergedSelectors(Optional.ofNullable(builder.buildSpec()) - .map(ReplicaSetSpec::getSelector) - .map(LabelSelector::getMatchLabels) - .orElse(new HashMap<>())); - builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); - } - }); - - builder.accept(new TypedVisitor() { - @Override - public void visit(StatefulSetBuilder builder) { - Map selectors = new HashMap<>(); - if(builder.buildSpec() != null && builder.buildSpec().getSelector() != null && builder.buildSpec().getSelector().getMatchLabels() != null) { - selectors.putAll(builder.buildSpec().getSelector().getMatchLabels()); - } - MapUtil.mergeIfAbsent(selectors, createLabels()); - builder.editOrNewSpec().editOrNewSelector().withMatchLabels(selectors).endSelector().endSpec(); - } - }); - - } - - private Map mergedSelectors(Map originalSelectors) { - MapUtil.mergeIfAbsent(originalSelectors, createLabels(true)); - return originalSelectors; - } - - @Override - public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) { - // Add to all objects in the builder - builder.accept(new TypedVisitor() { - @Override - public void visit(ObjectMetaBuilder element) { - if (element.getLabels() != null) { - Map labels = element.getLabels(); - MapUtil.mergeIfAbsent(labels, createLabels()); - } else { - Map newLabels = new HashMap<>(); - MapUtil.mergeIfAbsent(newLabels, createLabels()); - element.withLabels(newLabels); - } - } - }); - } - - private Map createLabels() { - return createLabels(false); - } - - private Map createLabels(boolean withoutVersion) { + public Map createLabels(boolean withoutVersion) { Map ret = new HashMap<>(); boolean enableProjectLabel = Configs.asBoolean(getConfig(Config.USE_PROJECT_LABEL)); diff --git a/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricher.java b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricher.java new file mode 100644 index 0000000000..da7fc9128a --- /dev/null +++ b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricher.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.enricher.generic; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jkube.kit.common.Configs; +import org.eclipse.jkube.kit.config.resource.GroupArtifactVersion; +import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; + +public class WellKnownLabelsEnricher extends AbstractLabelEnricher { + public static final String KUBERNETES_APP_LABEL = "app.kubernetes.io/"; + private static final String WELL_KNOWN_LABELS_PROPERTY = "jkube.kubernetes.well-known-labels"; + + @AllArgsConstructor + private enum Config implements Configs.Config { + ENABLED("enabled", "true"), + APP_NAME("name", null), + APP_VERSION("version", null), + APP_COMPONENT("component", null), + APP_PART_OF("partOf", null), + APP_MANAGED_BY("managedBy", "jkube"); + + @Getter + protected String key; + @Getter + protected String defaultValue; + } + + public WellKnownLabelsEnricher(JKubeEnricherContext buildContext) { + super(buildContext, "jkube-well-known-labels"); + } + + private boolean shouldAddWellKnownLabels() { + return Configs.asBoolean(getConfigWithFallback(Config.ENABLED, WELL_KNOWN_LABELS_PROPERTY, "true")); + } + + @Override + public Map createLabels(boolean withoutVersion) { + Map ret = new HashMap<>(); + if (!shouldAddWellKnownLabels()) { + return ret; + } + + final GroupArtifactVersion groupArtifactVersion = getContext().getGav(); + ret.putAll(addWellKnownLabelFromConfig(Config.APP_NAME, "name", groupArtifactVersion.getArtifactId())); + if (!withoutVersion) { + ret.putAll(addWellKnownLabelFromConfig(Config.APP_VERSION, "version", groupArtifactVersion.getVersion())); + } + ret.putAll(addWellKnownLabelFromConfig(Config.APP_PART_OF, "part-of", groupArtifactVersion.getGroupId())); + ret.putAll(addWellKnownLabelFromConfig(Config.APP_MANAGED_BY, "managed-by", "jkube")); + ret.putAll(addWellKnownLabelFromConfig(Config.APP_COMPONENT, "component", null)); + return ret; + } + + private Map addWellKnownLabelFromConfig(Configs.Config key, String labelKey, String defaultValue) { + Map entryMap = new HashMap<>(); + String value = getConfig(key, defaultValue); + if (StringUtils.isNotBlank(value)) { + entryMap.put(KUBERNETES_APP_LABEL + labelKey, value); + } + return entryMap; + } +} diff --git a/jkube-kit/enricher/generic/src/main/resources/META-INF/jkube/enricher-default b/jkube-kit/enricher/generic/src/main/resources/META-INF/jkube/enricher-default index 91e465a395..6005ce5592 100644 --- a/jkube-kit/enricher/generic/src/main/resources/META-INF/jkube/enricher-default +++ b/jkube-kit/enricher/generic/src/main/resources/META-INF/jkube/enricher-default @@ -109,3 +109,6 @@ org.eclipse.jkube.enricher.generic.PersistentVolumeClaimStorageClassEnricher # Enforces best practice and recommended security rules org.eclipse.jkube.enricher.generic.SecurityHardeningEnricher + +# Well Known Kubernetes App Labels +org.eclipse.jkube.enricher.generic.WellKnownLabelsEnricher diff --git a/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricherTest.java b/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricherTest.java new file mode 100644 index 0000000000..1fe01e2ff4 --- /dev/null +++ b/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/WellKnownLabelsEnricherTest.java @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.enricher.generic; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.KubernetesListBuilder; +import io.fabric8.kubernetes.api.model.ReplicationControllerBuilder; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.api.model.ServiceBuilder; +import io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder; +import io.fabric8.kubernetes.api.model.apps.Deployment; +import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; +import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder; +import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder; +import io.fabric8.openshift.api.model.DeploymentConfigBuilder; +import org.assertj.core.api.InstanceOfAssertFactories; +import org.eclipse.jkube.kit.common.JavaProject; +import org.eclipse.jkube.kit.config.resource.PlatformMode; +import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Properties; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +class WellKnownLabelsEnricherTest { + private WellKnownLabelsEnricher wellKnownLabelsEnricher; + private Properties properties; + private KubernetesListBuilder kubernetesListBuilder; + + @BeforeEach + void setup() { + properties = new Properties(); + JKubeEnricherContext context = JKubeEnricherContext.builder() + .project(JavaProject.builder() + .groupId("org.example") + .artifactId("test-project") + .version("0.0.1") + .properties(properties) + .build()) + .build(); + kubernetesListBuilder = new KubernetesListBuilder().withItems(new DeploymentBuilder().withNewMetadata().endMetadata().build()); + wellKnownLabelsEnricher = new WellKnownLabelsEnricher(context); + } + + static Stream resourcesContainingSelectorLabels() { + return Stream.of( + arguments(new DeploymentConfigBuilder().build()), + arguments(new ServiceBuilder().build()), + arguments(new ReplicationControllerBuilder().build()) + ); + } + + @ParameterizedTest + @MethodSource("resourcesContainingSelectorLabels") + void whenControllerResourcesPresentInBuilder_thenAddWellKnownLabelsToSelector(HasMetadata hasMetadata) { + // Given + kubernetesListBuilder = new KubernetesListBuilder().withItems(hasMetadata); + + // When + wellKnownLabelsEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertWellKnownLabelsAddedToResource("spec.selector", false); + } + + static Stream resourcesContainingSelectorMatchLabels() { + return Stream.of( + arguments(new DeploymentBuilder().build()), + arguments(new DaemonSetBuilder().build()), + arguments(new ReplicaSetBuilder().build()), + arguments(new StatefulSetBuilder().build()) + ); + } + + @ParameterizedTest + @MethodSource("resourcesContainingSelectorMatchLabels") + void whenControllerResourcesPresentInBuilder_thenAddWellKnownLabelsToMatchSelector(HasMetadata hasMetadata) { + // Given + kubernetesListBuilder = new KubernetesListBuilder().withItems(hasMetadata); + + // When + wellKnownLabelsEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertWellKnownLabelsAddedToResource("spec.selector.matchLabels", false); + } + + @Nested + @DisplayName("create") + class Create { + @Test + @DisplayName("when resource already contains well-known-label, then do not overwrite") + void whenWellKnownLabelAlreadyPresent_thenDoNotOverwriteLabel() { + // Given + kubernetesListBuilder = new KubernetesListBuilder().withItems(new DeploymentBuilder() + .withNewSpec() + .withNewSelector() + .addToMatchLabels("app.kubernetes.io/part-of", "already-present-part-of") + .addToMatchLabels("app.kubernetes.io/component", "already-present-component") + .addToMatchLabels("app.kubernetes.io/managed-by", "already-present-managed-by") + .addToMatchLabels("app.kubernetes.io/name", "already-present-name") + .addToMatchLabels("app.kubernetes.io/version", "1.0.0-already-present") + .endSelector() + .endSpec() + .build()); + + // When + wellKnownLabelsEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + Deployment deployment = (Deployment) kubernetesListBuilder.buildFirstItem(); + assertThat(deployment) + .extracting("spec.selector.matchLabels") + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("app.kubernetes.io/part-of", "already-present-part-of") + .containsEntry("app.kubernetes.io/component", "already-present-component") + .containsEntry("app.kubernetes.io/managed-by", "already-present-managed-by") + .containsEntry("app.kubernetes.io/name", "already-present-name") + .containsEntry("app.kubernetes.io/version", "1.0.0-already-present"); + } + + @Test + @DisplayName("labels provided via enricher configuration, then add labels") + void whenLabelsProvidedViaEnricherConfiguration_thenAddLabels() { + // Given + properties.put("jkube.enricher.jkube-well-known-labels.name", "test-app"); + properties.put("jkube.enricher.jkube-well-known-labels.component", "authentication"); + properties.put("jkube.enricher.jkube-well-known-labels.partOf", "auth-infra"); + properties.put("jkube.enricher.jkube-well-known-labels.managedBy", "qe"); + + // When + wellKnownLabelsEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + Deployment deployment = (Deployment) kubernetesListBuilder.buildFirstItem(); + assertThat(deployment) + .extracting("spec.selector.matchLabels") + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("app.kubernetes.io/part-of", "auth-infra") + .containsEntry("app.kubernetes.io/component", "authentication") + .containsEntry("app.kubernetes.io/managed-by", "qe") + .containsEntry("app.kubernetes.io/name", "test-app"); + } + + @Test + @DisplayName("jkube.kubernetes.well-known-labels=false , then do not add labels") + void whenDisabledViaProperty_thenNoLabelsAddedToProject() { + // Given + properties.put("jkube.kubernetes.well-known-labels", "false"); + + // When + wellKnownLabelsEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertNoWellKnownLabelsAddedToResource("spec.selector.matchLabels"); + } + + @Test + @DisplayName("disable via enricher config, enabled=false , then do not add labels") + void whenDisabledViaEnricherConfiguration_thenNoLabelsAddedToProject() { + // Given + properties.put("jkube.enricher.jkube-well-known-labels.enabled", "false"); + + // When + wellKnownLabelsEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertNoWellKnownLabelsAddedToResource("spec.selector.matchLabels"); + } + } + + + @Nested + @DisplayName("enrich") + class Enrich { + @Test + @DisplayName("zero configuration, infer well known labels from project") + void whenNoConfigurationProvided_thenInferLabelsFromProject() { + // When + wellKnownLabelsEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertWellKnownLabelsAddedToResource("metadata.labels", true); + } + + @Test + @DisplayName("labels present, merge well known labels with existing labels") + void whenLabelsPresent_thenMergeWellKnownLabelsWithExistingLabels() { + // Given + kubernetesListBuilder = new KubernetesListBuilder().withItems(new ServiceBuilder() + .withNewMetadata() + .addToLabels("release", "canary") + .addToLabels("environment", "test") + .endMetadata() + .build()); + + // When + wellKnownLabelsEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + Service deployment = (Service) kubernetesListBuilder.buildFirstItem(); + assertThat(deployment) + .extracting("metadata.labels") + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("release", "canary") + .containsEntry("environment", "test") + .containsEntry("app.kubernetes.io/managed-by", "jkube") + .containsEntry("app.kubernetes.io/name", "test-project") + .containsEntry("app.kubernetes.io/version", "0.0.1"); + } + + @Test + @DisplayName("when resource already contains well-known-label, then do not overwrite") + void whenWellKnownLabelAlreadyPresent_thenDoNotOverwriteLabel() { + // Given + kubernetesListBuilder = new KubernetesListBuilder().withItems(new DeploymentBuilder() + .withNewMetadata() + .addToLabels("app.kubernetes.io/part-of", "already-present-part-of") + .addToLabels("app.kubernetes.io/component", "already-present-component") + .addToLabels("app.kubernetes.io/managed-by", "already-present-managed-by") + .addToLabels("app.kubernetes.io/name", "already-present-name") + .addToLabels("app.kubernetes.io/version", "1.0.0-already-present") + .endMetadata() + .build()); + + // When + wellKnownLabelsEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + Deployment deployment = (Deployment) kubernetesListBuilder.buildFirstItem(); + assertThat(deployment) + .extracting("metadata.labels") + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("app.kubernetes.io/part-of", "already-present-part-of") + .containsEntry("app.kubernetes.io/component", "already-present-component") + .containsEntry("app.kubernetes.io/managed-by", "already-present-managed-by") + .containsEntry("app.kubernetes.io/name", "already-present-name") + .containsEntry("app.kubernetes.io/version", "1.0.0-already-present"); + } + + @Test + @DisplayName("labels provided via enricher configuration, then add labels") + void whenLabelsProvidedViaEnricherConfiguration_thenAddLabels() { + // Given + properties.put("jkube.enricher.jkube-well-known-labels.name", "test-app"); + properties.put("jkube.enricher.jkube-well-known-labels.version", "v1.0.0-alpha1"); + properties.put("jkube.enricher.jkube-well-known-labels.component", "authentication"); + properties.put("jkube.enricher.jkube-well-known-labels.partOf", "auth-infra"); + properties.put("jkube.enricher.jkube-well-known-labels.managedBy", "qe"); + + // When + wellKnownLabelsEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + Deployment deployment = (Deployment) kubernetesListBuilder.buildFirstItem(); + assertThat(deployment) + .extracting("metadata.labels") + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("app.kubernetes.io/part-of", "auth-infra") + .containsEntry("app.kubernetes.io/component", "authentication") + .containsEntry("app.kubernetes.io/managed-by", "qe") + .containsEntry("app.kubernetes.io/name", "test-app") + .containsEntry("app.kubernetes.io/version", "v1.0.0-alpha1"); + } + + @Test + @DisplayName("disable via enricher config, enabled=false , then do not add labels") + void whenDisabledViaEnricherConfiguration_thenNoLabelsAddedToProject() { + // Given + properties.put("jkube.enricher.jkube-well-known-labels.enabled", "false"); + + // When + wellKnownLabelsEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertNoWellKnownLabelsAddedToResource("metadata.labels"); + } + + @Test + @DisplayName("jkube.kubernetes.well-known-labels=false , then do not add labels") + void whenDisabledViaProperty_thenNoLabelsAddedToProject() { + // Given + properties.put("jkube.kubernetes.well-known-labels", "false"); + + // When + wellKnownLabelsEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); + + // Then + assertNoWellKnownLabelsAddedToResource("metadata.labels"); + } + } + + private void assertWellKnownLabelsAddedToResource(String field, boolean containsVersion) { + HasMetadata hasMetadata = kubernetesListBuilder.buildFirstItem(); + assertThat(hasMetadata) + .extracting(field) + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("app.kubernetes.io/managed-by", "jkube") + .containsEntry("app.kubernetes.io/name", "test-project") + .containsEntry("app.kubernetes.io/part-of", "org.example"); + if (containsVersion) { + assertThat(hasMetadata) + .extracting(field) + .asInstanceOf(InstanceOfAssertFactories.MAP) + .containsEntry("app.kubernetes.io/version", "0.0.1"); + } + } + + private void assertNoWellKnownLabelsAddedToResource(String field) { + HasMetadata hasMetadata = kubernetesListBuilder.buildFirstItem(); + assertThat(hasMetadata) + .extracting(field) + .asInstanceOf(InstanceOfAssertFactories.MAP) + .doesNotContainKey("app.kubernetes.io/managed-by") + .doesNotContainKey("app.kubernetes.io/name") + .doesNotContainKey("app.kubernetes.io/part-of") + .doesNotContainKey("app.kubernetes.io/component") + .doesNotContainKey("app.kubernetes.io/version"); + } +} diff --git a/kubernetes-maven-plugin/it/src/it/dependency-resources/expected/kubernetes.yml b/kubernetes-maven-plugin/it/src/it/dependency-resources/expected/kubernetes.yml index ae7b688dec..e40e135dfc 100644 --- a/kubernetes-maven-plugin/it/src/it/dependency-resources/expected/kubernetes.yml +++ b/kubernetes-maven-plugin/it/src/it/dependency-resources/expected/kubernetes.yml @@ -28,6 +28,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube + app.kubernetes.io/name: jkube-maven-sample-dependency-resources-dependent + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube name: example spec: replicas: 1337 @@ -37,6 +41,9 @@ items: app: jkube-maven-sample-dependency-resources-dependent provider: jkube group: org.eclipse.jkube + app.kubernetes.io/name: jkube-maven-sample-dependency-resources-dependent + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube template: metadata: annotations: @@ -48,6 +55,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube + app.kubernetes.io/name: jkube-maven-sample-dependency-resources-dependent + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube spec: containers: - env: @@ -78,4 +89,4 @@ items: spec: containers: - image: nginx - name: nginx \ No newline at end of file + name: nginx diff --git a/kubernetes-maven-plugin/it/src/it/fragments/expected/kubernetes.yml b/kubernetes-maven-plugin/it/src/it/fragments/expected/kubernetes.yml index a79a2efd16..b70ef48974 100644 --- a/kubernetes-maven-plugin/it/src/it/fragments/expected/kubernetes.yml +++ b/kubernetes-maven-plugin/it/src/it/fragments/expected/kubernetes.yml @@ -26,6 +26,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube + app.kubernetes.io/name: fragments + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube name: ribbon - apiVersion: v1 kind: ConfigMap @@ -35,6 +39,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube + app.kubernetes.io/name: fragments + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube name: config-map data: application.properties: |- @@ -48,6 +56,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube + app.kubernetes.io/name: fragments + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube name: custom.resource.example.com - apiVersion: custom.resource.example.com/v1 kind: Example @@ -57,6 +69,10 @@ items: provider: jkube version: "@ignore@" group: org.eclipse.jkube + app.kubernetes.io/name: fragments + app.kubernetes.io/version: "@ignore@" + app.kubernetes.io/managed-by: jkube + app.kubernetes.io/part-of: org.eclipse.jkube name: custom-resource namespace: code spec: diff --git a/kubernetes-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml b/kubernetes-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml index 89c03329a7..9dacc0a91b 100644 --- a/kubernetes-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml +++ b/kubernetes-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml @@ -28,6 +28,7 @@ - jkube-image - jkube-portname - jkube-project-label + - jkube-well-known-labels - jkube-dependency - jkube-pod-annotations - jkube-git diff --git a/openshift-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml b/openshift-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml index 327dfe556b..c9123ed1f9 100644 --- a/openshift-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml +++ b/openshift-maven-plugin/plugin/src/main/resources/META-INF/jkube/profiles-default.yml @@ -30,6 +30,7 @@ - jkube-image - jkube-portname - jkube-project-label + - jkube-well-known-labels - jkube-dependency - jkube-pod-annotations - jkube-git