# Copyright 2019 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: helm-cd-trigger
spec:
  suspend: true
  schedule: "12 * * * *"  # Houly at *:12
  jobTemplate:
    spec:
      template:
        spec:
          volumes:
          - name: workspace
            emptyDir: {}
          containers:
          - name: trigger
            image: curlimages/curl
            command:
              - /bin/sh
            args:
              - -ce
              - |
                CHART_PARAMS=$(echo $CHART_PARAMS | sed 's/, /,/g')
                cat <<EOF > /workspace/post-body.json
                {
                  "trigger-template": "helm",
                  "params": {
                    "target": {
                      "namespace": "$NAMESPACE",
                      "cluster-resource": "$CLUSTER_RESOURCE"
                    },
                    "helm": {
                        "name": "$CHART_NAME",
                        "version": "$CHART_VERSION",
                        "repo": "$CHART_REPO",
                        "params": "$CHART_PARAMS",
                        "description": "$CHART_DESCRIPTION",
                        "pre-deploy-resources": "$PRE_DEPLOY_RESOURCES"
                    }
                  }
                }
                EOF
                curl -d @/workspace/post-body.json $SINK_URL
            volumeMounts:
            - mountPath: /workspace
              name: workspace
            env:
              - name: SINK_URL
                value: "http://el-tekton-cd.default.svc.cluster.local:8080"
              - name: NAMESPACE
                value: "default"
              - name: PRE_DEPLOY_RESOURCES
                value: ""
              - name: CLUSTER_RESOURCE
                value: "not-a-real-cluster"
              - name: CHART_NAME
                value: "invalid"
              - name: CHART_VERSION
                value: "invalid"
              - name: CHART_PARAMS
                value: "invalid"
              - name: CHART_DESCRIPTION
                value: "invalid"
          restartPolicy: Never
