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

WIP: Add Plugin Support to the Argo CD CLI #20074

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nitishfy
Copy link
Contributor

@nitishfy nitishfy commented Sep 24, 2024

Ref: #19624
Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • The title of the PR conforms to the Toolchain Guide
  • I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • I have signed off all my commits as required by DCO
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My build is green (troubleshooting builds).
  • My new feature complies with the feature status guidelines.
  • I have added a brief description of why this PR is necessary and/or what this PR solves.
  • Optional. My organization is added to USERS.md.
  • Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).

Signed-off-by: nitishfy <justnitish06@gmail.com>
Copy link

bunnyshell bot commented Sep 24, 2024

✅ Preview Environment deployed on Bunnyshell

Component Endpoints
argocd https://argocd-3kj6km.bunnyenv.com/
argocd-ttyd https://argocd-web-cli-3kj6km.bunnyenv.com/

See: Environment Details | Pipeline Logs

Available commands (reply to this comment):

  • 🔴 /bns:stop to stop the environment
  • 🚀 /bns:deploy to redeploy the environment
  • /bns:delete to remove the environment

Copy link

bunnyshell bot commented Sep 24, 2024

✅ Preview Environment created on Bunnyshell but will not be auto-deployed

See: Environment Details

Available commands (reply to this comment):

  • 🚀 /bns:deploy to deploy the environment

Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 7.05882% with 79 lines in your changes missing coverage. Please review.

Project coverage is 55.72%. Comparing base (864917b) to head (b35df98).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
cmd/argocd/commands/root.go 3.75% 77 Missing ⚠️
cmd/main.go 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #20074      +/-   ##
==========================================
- Coverage   55.82%   55.72%   -0.10%     
==========================================
  Files         320      320              
  Lines       44431    44514      +83     
==========================================
+ Hits        24805    24807       +2     
- Misses      17060    17143      +83     
+ Partials     2566     2564       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@leoluz leoluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider adding a new documentation page explaining how to write plugins and how to install and consume them.

Comment on lines +27 to +86
type ArgoCDCLIOptions struct {
PluginHandler PluginHandler
Arguments []string
}

// PluginHandler parses command line arguments
// and performs executable filename lookups to search
// for valid plugin files, and execute found plugins.
type PluginHandler interface {
// LookForPlugin will iterate over a list of given prefixes
// in order to recognize valid plugin filenames.
// The first filepath to match a prefix is returned.
LookForPlugin(filename string) (string, bool)
// ExecutePlugin receives an executable's filepath, a slice
// of arguments, and a slice of environment variables
// to relay to the executable.
ExecutePlugin(executablePath string, cmdArgs, environment []string) error
}

// DefaultPluginHandler implements the PluginHandler interface
type DefaultPluginHandler struct {
ValidPrefixes []string
}

func NewDefaultPluginHandler(validPrefixes []string) *DefaultPluginHandler {
return &DefaultPluginHandler{
ValidPrefixes: validPrefixes,
}
}

// LookForPlugin implements PluginHandler
func (h *DefaultPluginHandler) LookForPlugin(filename string) (string, bool) {
for _, prefix := range h.ValidPrefixes {
path, err := exec.LookPath(fmt.Sprintf("%s-%s", prefix, filename))
if shouldSkipOnLookPathErr(err) || len(path) == 0 {
continue
}
return path, true
}
return "", false
}

// ExecutePlugin implements PluginHandler
func (h *DefaultPluginHandler) ExecutePlugin(executablePath string, cmdArgs, environment []string) error {
// Windows does not support exec syscall.
if runtime.GOOS == "windows" {
cmd := Command(executablePath, cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = environment
err := cmd.Run()
if err == nil {
os.Exit(0)
}
return err
}

return syscall.Exec(executablePath, append([]string{executablePath}, cmdArgs...), environment)
}
Copy link
Collaborator

@leoluz leoluz Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract this code in a dedicated file with proper unit tests.
Suggestion: util/cli/plugin.go

return syscall.Exec(executablePath, append([]string{executablePath}, cmdArgs...), environment)
}

func Command(name string, arg ...string) *exec.Cmd {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docs to this command explaining how it works.


// ExecutePlugin implements PluginHandler
func (h *DefaultPluginHandler) ExecutePlugin(executablePath string, cmdArgs, environment []string) error {
// Windows does not support exec syscall.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows doesn't support syscall but all OSs support cmd.Run().
Why not having the plugin execution handled by a Command in all OSs and simplifying this code?

@leoluz leoluz self-assigned this Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants