task-postgresql-service.yaml 2.36 KB
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: task-postgresql-service
spec:
  description: >-
    This Task deploys (or delete) a Kubernetes resource (pod). It uses
    kubectl for that.

  params:
    - name: kubectl-image
      default: bitnami/kubectl:1.21
    - name: bash-image
      default: bash
    - name: kustomize-image
      default: us.gcr.io/k8s-artifacts-prod/kustomize/kustomize:v4.5.2
  results:
    - name: service-name
  volumes:
    - name: task-postgresql-service-kustomization
      configMap:
        name: task-postgresql-service-kustomization
  steps:
    - name: mount-postgresql-service-kustomization
      image: $(params.bash-image)
      script: |
        #!/usr/bin/env bash
        set -ex
        mkdir /workspace/kustomizations
        cd /srv/task-postgresql-service-kustomization-cm/
        for file in *; do
          translated="$(echo "$file" | sed "s,_,/,g")"
          dir="$(dirname "$translated")"
          base="$(basename "$translated")"
          mkdir -p "/workspace/kustomizations/$dir"
          cp "$file" "/workspace/kustomizations/$dir/$base"
        done
      volumeMounts:
        - name: task-postgresql-service-kustomization
          mountPath: /srv/task-postgresql-service-kustomization-cm
    - name: run-kustomize-4-x
      image: $(params.kustomize-image)
      script: |
        #!/bin/sh
        set -ex
        kustomize build /workspace/kustomizations/postgresql-service > /workspace/postgresql-service.yaml
      env:
        - name: PARENT_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: PARENT_POD_UID
          valueFrom:
            fieldRef:
              fieldPath: metadata.uid

    - name: set-generate-name
      image: $(params.bash-image)
      script: |
        #!/usr/bin/env bash
        set -ex
        cat /workspace/postgresql-service.yaml
        sed -i -e 's/^  name:/  generateName:/' /workspace/postgresql-service.yaml
        cat /workspace/postgresql-service.yaml
    - name: create-postgresql-service
      image: $(params.kubectl-image)
      script: |
        #!/usr/bin/env bash

        set -ex
        kubectl create -f /workspace/postgresql-service.yaml -o go-template --template '{{ if eq .kind "Service" }}{{.metadata.name }}{{ end }}' > $(results.service-name.path)
        echo "servie name: $(cat $(results.service-name.path))"

---