carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.68k stars 137 forks source link

Using YTT in Argocd as Sidecar #924

Closed mdavid01 closed 3 months ago

mdavid01 commented 3 months ago

Hi team: Is there a YTT-specific example for configuring YTT using the sidecar method in Argocd? (other than the example shown in Argocd https://argo-cd.readthedocs.io/en/stable/operator-manual/config-management-plugins/)

Does anyone have an example of sidecar working with YTT plugins? Getting errors on both plugins. Configmaps install correctly.
Error Messages:

  1. error="Manifest generation error (cached): plugin sidecar failed. Couldn't find cmp-server plugin supporting repository .gitops/feature-templates"
  2. error="Manifest generation error (cached): plugin sidecar failed. Couldn't find cmp-server plugin supporting repository .apps/harbor"

Example of error #1: =======================
From Application generated from Git as viewed in Argocd =======================

project: harbor-features
source:
  repoURL: 'https://..../deployment.git'
  path: gitops/feature-templates
  targetRevision: harbor-md
  plugin:
    env:
      - name: BRANCH_NAME
        value: harbor-md
      - name: YTT_VALUES
        value: >
          #@data/values
          ---
          targetNamespace: "harbor-md"
          environment: internal
          defaultDomain: redacted
          externalURL:
          "redacted"
          istioGateway: istio-gateway
          docsUrl: ""
          targetRevision: "harbor-xxx"
          imageStorageType: filesystem
          aquaUrl: redacted
          s3:
            region: us-east-1
            bucket: redacted
            regionendpoint: https://s3.us-east-1.amazonaws.com
          database:
            type: internal
            host: redacted
          redis:
            installSentinel: "no"
            type: internal
            endpoint: sentinel-harbor-xxx-redis:26379
            masterset: mymaster
destination:
  server: 'https://kubernetes.default.svc'
  namespace: argocd
syncPolicy:
  automated: {}
  syncOptions:
    - CreateNamespace=true

======================================
Plugin ConfigMap generated from Git as viewed in Argocd ======================================

apiVersion: v1
data:
  plugin.yaml: |
    apiVersion: argoproj.io/v1alpha1
    kind: ConfigManagementPlugin
    metadata:
      name: harbor-applicationset
    spec:
      kustomize.buildOptions: --enable-helm
      init:
          command: ["/bin/sh", "-c"]
          args: ["helm dependency build || true"]
      generate:
        command: ["/bin/bash", "/swf/harborset-applicationset.sh"]
      discover:
        fileName: "gitops/feature-templates/auto-deploy.yaml"
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/name: harbor-applicationset
    app.kubernetes.io/part-of: argocd
    argocd.argoproj.io/instance: argocd-customizations
  name: harbor-applicationset
  namespace: argocd

======================================
Repo-Server (Sidecar) generated from Git as viewed in Argocd
this includes several other plugins with similar command, mounts, and volume construct ======================================

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    argocd.argoproj.io/instance: argocd-customizations
  name: argocd-argo-cd-repo-server
  namespace: argocd
spec:
  replicas: 5
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app.kubernetes.io/instance: argocd-argo-cd
      app.kubernetes.io/name: argocd-repo-server
  template:
    metadata:
      labels:
        app.kubernetes.io/instance: argocd-argo-cd
        app.kubernetes.io/name: argocd-repo-server
    spec:
      automountServiceAccountToken: true
      containers:
        - command:
            - /var/run/argocd/argocd-cmp-server
          image: busybox
          name: harbor-applicationset-plugin
          securityContext:
            runAsNonRoot: true
            runAsUser: 999
          volumeMounts:
            - mountPath: /var/run/argocd
              name: var-files
            - mountPath: /home/argocd/cmp-server/plugins
              name: plugins
            - mountPath: /home/argocd/cmp-server/config/plugin.yaml
              name: harbor-applicationset
              subPath: plugin.yaml
            - mountPath: /tmp
              name: harbor-applicationsetp-tmp
            - mountPath: /swf/harborset-applicationset.sh
              name: argocd-swf
              subPath: harborset-applicationset.sh
......
Volumes:
      volumes:
        - configMap:
            name: helm-kustomize
          name: helm-kustomize
        - configMap:
            name: helmchart-kustomize
          name: helmchart-kustomize
        - configMap:
            name: helm-ytt
          name: helm-ytt
        - configMap:
            name: helmchart-ytt
          name: helmchart-ytt
        - configMap:
            name: ytt
          name: ytt
        - configMap:
            name: harbor-applicationset
          name: harbor-applicationset
        - configMap:
            name: output-environmentvariables
          name: output-environmentvariables

======================================
harbor-applicationset.sh configmap
======================================

apiVersion: v1
data:
  harborset-applicationset.sh: >
    [ -d $ARGOCD_ENV_BRANCH_NAME ] && SUBPATH="--data-values-file
    $ARGOCD_ENV_BRANCH_NAME/data.yml" || unset SUBPATH

    echo "$ARGOCD_ENV_YTT_VALUES" > x-ytt-values.yml

    MYCMD="cat x-ytt-values.yml | ytt --ignore-unknown-comments -f - $SUBPATH -f
    00-helm-values.yml"

    eval $MYCMD > 01-helm-values.txt

    MYCMD="cat x-ytt-values.yml | ytt --ignore-unknown-comments -f - $SUBPATH -f
    01-helm-values.txt -f 01-app.yml"

    eval $MYCMD
kind: ConfigMap
metadata:
  labels:
    argocd.argoproj.io/instance: argocd-customizations
  name: argocd-swf
  namespace: argocd

Maybe YTT sidecar is not available yet?

prembhaskal commented 3 months ago

ytt sidecar is not readily available but you can make one quite easily by creating a docker image wrapping around ytt binary and using that as a container.