/*
 * 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
 */
plugins {
    id 'java'
    id("org.eclipse.jkube.kubernetes") version "1.18.1"
}

version = '1.18.0'
group = "org.eclipse.jkube.quickstart.gradle.camelcdi"

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    implementation 'org.apache.camel:camel-core:3.11.1'
    implementation 'org.apache.camel:camel-cdi:3.11.1'
    implementation "javax.enterprise:cdi-api:2.0.SP1"
    implementation 'org.jboss.weld.se:weld-se:2.4.8.Final'
    implementation 'org.jboss.weld:weld-core:2.4.8.Final'
    implementation 'org.apache.deltaspike.cdictrl:deltaspike-cdictrl-weld:1.9.5'
    implementation 'org.slf4j:slf4j-log4j12:2.0.0-alpha2'
    implementation 'log4j:log4j:1.2.17'
}

task copyDependencies(type: Copy) {
    from configurations.runtimeClasspath
    into file("build/dependencies")
}
build.dependsOn(copyDependencies)

kubernetes {
    images {
        image {
            name = "jkube/${project.name}:${project.version}"
            alias = "camel-service"
            build {
                from = "quay.io/jkube/jkube-java:0.0.16"
                assembly {
                    targetDir = "/deployments"
                    layers = [{
                        fileSets = [{
                            directory = file("${project.rootDir}/build/dependencies")
                       }]
                    }]
                }
                env {
                    JAVA_LIB_DIR = "/deployments/dependencies/*"
                    JAVA_MAIN_CLASS = "org.apache.camel.cdi.Main"
                }
                labels {
                    labelWithValue = "foo"
                    version = "${project.version}"
                    artifactId = "${project.name}"
                }
                ports = ["8787"]
            }
        }
    }
    resources {
        labels {
            all {
                extraLabel = "quickstart-groovy-dsl-configuration"
            }
        }
        controllerName = "${project.name}-controller"
        replicas = "2"
        volumes = [
                {
                    name = "scratch"
                    type = "emptyDir"
                    medium = "Memory"
                    mounts = ["/var/scratch"]
                },
                {
                    name = "downward-api-volume"
                    type = "downwardAPI"
                    items = [{
                        path = "labels"
                        fieldRef {
                            fieldPath = "metadata.labels"
                        }
                    },
                    {
                        path = "annotations"
                        fieldRef {
                            fieldPath = "metadata.annotations"
                        }
                    }]
                },
                {
                    name = "config-vol"
                    type = "configMap"
                    configMapName = "log-config"
                    configMapItems = [{
                        key = "log_level"
                        path = "log_level"
                    }]
                }
        ]
        services = [{
            name = "${project.name}-service-one"
            headless = "false"
            type = "NodePort"
            ports = [{
                protocol = "tcp"
                port = "8787"
                targetPort = "8787"
            }]
        }]
        configMap {
            name = "log-config"
            entries = [{
                name = "log_level"
                value = "INFO"
            }]
        }
    }
}
