diff --git a/gradle-plugin/it/src/it/initcontainers/build.gradle b/gradle-plugin/it/src/it/initcontainers/build.gradle new file mode 100644 index 0000000000..1b34571d20 --- /dev/null +++ b/gradle-plugin/it/src/it/initcontainers/build.gradle @@ -0,0 +1,101 @@ +plugins { + id 'org.eclipse.jkube.kubernetes' version "${jKubeVersion}" + id 'org.eclipse.jkube.openshift' version "${jKubeVersion}" + id 'java' +} + +group = 'org.eclipse.jkube.integration.tests.gradle' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '11' + +repositories { + mavenCentral() +} + +kubernetes { + offline = true + images { + image1 { + name = 'main-container:latest' + build { + from = 'quay.io/jkube/jkube-java-11' + ports = ['8080'] + } + } + image2 { + name = 'init:latest' + build { + from = 'quay.io/jkube/jkube-java-11' + } + } + } + resources { + controller { + initContainers = [ + { + name = 'init1' + imageName = 'busybox:1.28' + cmd { + exec = ["wget", "-O", "/work-dir/index.html", "http://info.cern.ch"] + } + volumes = [{ + name = 'workdir' + path = '/work-dir' + }] + },{ + name = 'init2' + imageName = 'init:latest' + } + ] + volumes = [{ + type = 'emptyDir' + name = 'workdir' + path = '/work-dir' + }] + } + } +} + +openshift { + offline = true + images { + image1 { + name = 'main-container:latest' + build { + from = 'quay.io/jkube/jkube-java-11' + ports = ['8080'] + } + } + image2 { + name = 'init:latest' + build { + from = 'quay.io/jkube/jkube-java-11' + } + } + } + resources { + controller { + initContainers = [ + { + name = 'init1' + imageName = 'busybox:1.28' + cmd { + exec = ["wget", "-O", "/work-dir/index.html", "http://info.cern.ch"] + } + volumes = [{ + name = 'workdir' + path = '/work-dir' + }] + },{ + name = 'init2' + imageName = 'init:latest' + } + ] + volumes = [{ + type = 'emptyDir' + name = 'workdir' + path = '/work-dir' + }] + } + } +} \ No newline at end of file diff --git a/gradle-plugin/it/src/it/initcontainers/expected/kubernetes.yml b/gradle-plugin/it/src/it/initcontainers/expected/kubernetes.yml new file mode 100644 index 0000000000..55cd0037be --- /dev/null +++ b/gradle-plugin/it/src/it/initcontainers/expected/kubernetes.yml @@ -0,0 +1,106 @@ +--- +apiVersion: v1 +kind: List +items: +- apiVersion: v1 + kind: Service + metadata: + annotations: + jkube.io/git-url: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + ports: + - name: http + port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: initcontainers + provider: jkube + group: org.eclipse.jkube.integration.tests.gradle +- apiVersion: apps/v1 + kind: Deployment + metadata: + annotations: + jkube.io/git-url: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + replicas: 1 + revisionHistoryLimit: 2 + selector: + matchLabels: + app: initcontainers + provider: jkube + group: org.eclipse.jkube.integration.tests.gradle + template: + metadata: + annotations: + jkube.io/git-url: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + containers: + - env: + - name: KUBERNETES_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: HOSTNAME + valueFrom: + fieldRef: + fieldPath: metadata.name + image: main-container:latest + imagePullPolicy: IfNotPresent + name: orgeclipsejkubeintegrationtestsgradle-initcontainers + ports: + - containerPort: 8080 + name: http + protocol: TCP + securityContext: + privileged: false + initContainers: + - command: + - wget + - -O + - /work-dir/index.html + - http://info.cern.ch + env: + - name: HOSTNAME + valueFrom: + fieldRef: + fieldPath: metadata.name + image: busybox:1.28 + name: init1 + volumeMounts: + - mountPath: /work-dir + name: workdir + - env: + - name: HOSTNAME + valueFrom: + fieldRef: + fieldPath: metadata.name + image: init:latest + name: init2 + volumes: + - emptyDir: {} + name: workdir diff --git a/gradle-plugin/it/src/it/initcontainers/expected/openshift.yml b/gradle-plugin/it/src/it/initcontainers/expected/openshift.yml new file mode 100644 index 0000000000..bc83e659c4 --- /dev/null +++ b/gradle-plugin/it/src/it/initcontainers/expected/openshift.yml @@ -0,0 +1,132 @@ +--- +apiVersion: v1 +kind: List +items: + - apiVersion: v1 + kind: Service + metadata: + annotations: + app.openshift.io/vcs-ref: "@ignore@" + jkube.io/git-url: "@ignore@" + app.openshift.io/vcs-uri: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + ports: + - name: http + port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: initcontainers + provider: jkube + group: org.eclipse.jkube.integration.tests.gradle + - apiVersion: apps.openshift.io/v1 + kind: DeploymentConfig + metadata: + annotations: + app.openshift.io/vcs-ref: "@ignore@" + jkube.io/git-url: "@ignore@" + app.openshift.io/vcs-uri: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + replicas: 1 + revisionHistoryLimit: 2 + selector: + app: initcontainers + provider: jkube + group: org.eclipse.jkube.integration.tests.gradle + strategy: + rollingParams: + timeoutSeconds: 3600 + type: Rolling + template: + metadata: + annotations: + app.openshift.io/vcs-ref: "@ignore@" + jkube.io/git-url: "@ignore@" + app.openshift.io/vcs-uri: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + containers: + - env: + - name: KUBERNETES_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + image: main-container:latest + imagePullPolicy: IfNotPresent + name: orgeclipsejkubeintegrationtestsgradle-initcontainers + ports: + - containerPort: 8080 + name: http + protocol: TCP + securityContext: + privileged: false + initContainers: + - command: + - wget + - -O + - /work-dir/index.html + - http://info.cern.ch + image: busybox:1.28 + name: init1 + volumeMounts: + - mountPath: /work-dir + name: workdir + - image: init:latest + name: init2 + volumes: + - emptyDir: {} + name: workdir + triggers: + - type: ConfigChange + - imageChangeParams: + automatic: true + containerNames: + - orgeclipsejkubeintegrationtestsgradle-initcontainers + from: + kind: ImageStreamTag + name: main-container:latest + type: ImageChange + - apiVersion: route.openshift.io/v1 + kind: Route + metadata: + annotations: + app.openshift.io/vcs-ref: "@ignore@" + jkube.io/git-url: "@ignore@" + app.openshift.io/vcs-uri: "@ignore@" + jkube.io/git-commit: "@ignore@" + jkube.io/git-branch: "@ignore@" + labels: + app: initcontainers + provider: jkube + version: "@ignore@" + group: org.eclipse.jkube.integration.tests.gradle + name: initcontainers + spec: + port: + targetPort: 8080 + to: + kind: Service + name: initcontainers diff --git a/gradle-plugin/it/src/test/java/org/eclipse/jkube/gradle/plugin/tests/InitContainersIT.java b/gradle-plugin/it/src/test/java/org/eclipse/jkube/gradle/plugin/tests/InitContainersIT.java new file mode 100644 index 0000000000..39537c14e8 --- /dev/null +++ b/gradle-plugin/it/src/test/java/org/eclipse/jkube/gradle/plugin/tests/InitContainersIT.java @@ -0,0 +1,61 @@ +/** + * 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.gradle.plugin.tests; + +import net.minidev.json.parser.ParseException; +import org.eclipse.jkube.kit.common.ResourceVerify; +import org.gradle.testkit.runner.BuildResult; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.IOException; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class InitContainersIT { + @RegisterExtension + private final ITGradleRunnerExtension gradleRunner = new ITGradleRunnerExtension(); + + @Test + void k8sResource_whenRun_generatesK8sManifests() throws IOException, ParseException { + // When + final BuildResult result = gradleRunner.withITProject("initcontainers") + .withArguments("build", "k8sResource", "--stacktrace") + .build(); + // Then + ResourceVerify.verifyResourceDescriptors(gradleRunner.resolveDefaultKubernetesResourceFile(), + gradleRunner.resolveFile("expected", "kubernetes.yml")); + assertThat(result).extracting(BuildResult::getOutput).asString() + .contains("jkube-controller: Adding a default Deployment") + .contains("jkube-service: Adding a default service") + .contains("jkube-service-discovery: Using first mentioned service port '8080' ") + .contains("jkube-revision-history: Adding revision history limit to 2"); + } + + @Test + void ocResource_whenRun_generatesOpenShiftManifests() throws IOException, ParseException { + // When + final BuildResult result = gradleRunner.withITProject("initcontainers") + .withArguments("build", "ocResource", "--stacktrace") + .build(); + // Then + ResourceVerify.verifyResourceDescriptors(gradleRunner.resolveDefaultOpenShiftResourceFile(), + gradleRunner.resolveFile("expected", "openshift.yml")); + assertThat(result).extracting(BuildResult::getOutput).asString() + .contains("jkube-controller: Adding a default Deployment") + .contains("jkube-service: Adding a default service") + .contains("jkube-openshift-deploymentconfig: Converting Deployment to DeploymentConfig") + .contains("jkube-revision-history: Adding revision history limit to 2"); + } +}