Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to trigger sync with CRD #4867

Closed
trim777 opened this issue Nov 19, 2020 · 2 comments
Closed

Add possibility to trigger sync with CRD #4867

trim777 opened this issue Nov 19, 2020 · 2 comments
Assignees
Labels
answered enhancement New feature or request

Comments

@trim777
Copy link

trim777 commented Nov 19, 2020

Summary

It would be great to have an option to trigger sync by patching existing CRD (kind: Application).

Motivation

I have application which should be synced only by after external request (preferably, from Argo WorkFlow).
ArgoWF can patch existing manifests, this is example of already working patch:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: k8s-patch-
spec:
  entrypoint: main
  templates:
    - name: main
      steps:
        - - name: set-new-build
            template: argocd-app-patch
    - name: argocd-app-patch
      resource:
        action: patch
        mergeStrategy: merge
        manifest: |
          kind: Application
          apiVersion: argoproj.io/v1alpha1
          metadata:
            name: my-app
          spec:
            source:
              targetRevision: "1.9.20"

So if this feature would be implemented, I'd just add into k8s-manifest above few lines to trigger sync. Now I have workaround with Argo Workflow, where I trigger sync with argocd-cli, this example (source) is similar to my workaround:

apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: argocd-sync-and-wait
  annotations:
    workflows.argoproj.io/description: >-
      This task syncs (deploys) an Argo CD application and waits for it to be healthy.

      To do so, it requires the address of the Argo CD server and some form of
      authentication either a username/password or an authentication token.
    workflows.argoproj.io/maintainer: '@dcherman'
    workflows.argoproj.io/tags: argocd
    workflows.argoproj.io/version: '>= 2.9.0'
spec:
  entrypoint: argocd-sync-and-wait
  templates:
  - name: argocd-sync-and-wait
    inputs:
      parameters:
      - name: argocd-version
        value: v1.6.0
      - name: application-name
      - name: revision
        value: HEAD
      - name: flags
        value: --
      - name: argocd-server-address
      - name: argocd-credentials-secret
    script:
      image: argoproj/argocd:{{inputs.parameters.argocd-version}}
      command: [bash]
      env:
        - name: ARGOCD_AUTH_TOKEN
          valueFrom:
            secretKeyRef:
              name: "{{inputs.parameters.argocd-credentials-secret}}"
              key: token
              optional: true
        - name: ARGOCD_USERNAME
          valueFrom:
            secretKeyRef:
              name: "{{inputs.parameters.argocd-credentials-secret}}"
              key: username
              optional: true
        - name: ARGOCD_PASSWORD
          valueFrom:
            secretKeyRef:
              name: "{{inputs.parameters.argocd-credentials-secret}}"
              key: password
              optional: true
        - name: ARGOCD_SERVER
          value: "{{inputs.parameters.argocd-server-address}}"
      source: |
        #!/bin/bash

        set -euo pipefail

        if [[ -z $ARGOCD_AUTH_TOKEN ]] && [[ -z "$ARGOCD_USERNAME" || -z "$ARGOCD_PASSWORD" ]]; then
          echo "Either the ARGOCD_AUTH_TOKEN must be specified, or the ARGOCD_USERNAME/ARGOCD_PASSWORD must be specified."
          exit 1
        fi 

        if [ -z $ARGOCD_AUTH_TOKEN ]; then
          yes | argocd login "$ARGOCD_SERVER" --username=$ARGOCD_USERNAME --password=$ARGOCD_PASSWORD {{inputs.parameters.flags}}
        fi

        echo "Running as ArgoCD User:"
        argocd account get-user-info {{inputs.parameters.flags}}
        
        argocd app sync {{inputs.parameters.application-name}} --revision {{inputs.parameters.revision}} {{inputs.parameters.flags}}
        argocd app wait {{inputs.parameters.application-name}} --health {{inputs.parameters.flags}}

But I think, that patching CRD is better in this case. May be this is already supported, if yes, please, help me to find out how to do it.
Thank you in advance. And thank you very much for such amazing project!

@trim777 trim777 added the enhancement New feature or request label Nov 19, 2020
@alexmt alexmt self-assigned this Nov 19, 2020
@alexmt
Copy link
Collaborator

alexmt commented Nov 21, 2020

Hello @trim777 , You can manually trigger synchronization using operation field. E.g. :

kubectl patch app sample-app  -p '{"operation": {"sync": { "revision": "HEAD" } }}' --type merge

Argo CD should start synchronization once the field is set and will remove it when the sync is completed. Using Argo Workflows you might simulate "argocd app wait" using resource step and successCondition/failureCondition docs:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: k8s-patch-
spec:
  entrypoint: argocd-patch
  templates:
  - name: argocd-patch
    resource:
      action: patch
      mergeStrategy: merge                 
      successCondition: status.operationState.phase == "Succeeded"
      failureCondition: status.operationState.phase == "Failed"
      manifest: |
        kind: Application
        apiVersion: argoproj.io/v1alpha1
        metadata:
          name: guestbook
        operation:
          sync:
            revision: "1.9.1900" 

@alexmt
Copy link
Collaborator

alexmt commented Nov 21, 2020

We already have sync CRD proposal here: #1283 . Planning to work on it next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants