-
Notifications
You must be signed in to change notification settings - Fork 119
/
argocd.go
133 lines (111 loc) · 3.4 KB
/
argocd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package argocd
import (
"context"
"fmt"
"os"
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
"github.com/argoproj-labs/argocd-autopilot/pkg/log"
"github.com/argoproj-labs/argocd-autopilot/pkg/util"
"github.com/argoproj/argo-cd/v2/cmd/argocd/commands"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
argocdcs "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type (
// AddClusterCmd when executed calls the 'argocd cluster add' command
AddClusterCmd interface {
Execute(ctx context.Context, clusterName string) error
}
addClusterImpl struct {
cmd *cobra.Command
args []string
}
LoginOptions struct {
Namespace string
Username string
Password string
KubeConfig string
KubeContext string
Insecure bool
}
)
func AddClusterAddFlags(cmd *cobra.Command) (AddClusterCmd, error) {
root := commands.NewCommand()
args := []string{"cluster", "add"}
addcmd, _, err := root.Find(args)
if err != nil {
return nil, err
}
fs, err := util.StealFlags(addcmd, []string{"logformat", "loglevel", "namespace", "kube-context"})
if err != nil {
return nil, err
}
cmd.Flags().AddFlagSet(fs)
return &addClusterImpl{root, args}, nil
}
// GetAppSyncWaitFunc returns a WaitFunc that will return true when the Application
// is in Sync + Healthy state, and at the specific revision (if supplied. If revision is "", no revision check is made)
func GetAppSyncWaitFunc(revision string, waitForCreation bool) kube.WaitFunc {
return func(ctx context.Context, f kube.Factory, ns, name string) (bool, error) {
rc, err := f.ToRESTConfig()
if err != nil {
return false, err
}
c, err := argocdcs.NewForConfig(rc)
if err != nil {
return false, err
}
app, err := c.ArgoprojV1alpha1().Applications(ns).Get(ctx, name, metav1.GetOptions{})
if err != nil {
se, ok := err.(*kerrors.StatusError)
if !waitForCreation || !ok || se.ErrStatus.Reason != metav1.StatusReasonNotFound {
return false, err
}
return false, nil
}
synced := app.Status.Sync.Status == v1alpha1.SyncStatusCodeSynced
healthy := app.Status.Health.Status == "Healthy"
onRevision := true
if revision != "" {
onRevision = revision == app.Status.Sync.Revision
}
log.G(ctx).Debugf("Application found, Sync Status: %s, Health Status: %s, Revision: %s", app.Status.Sync.Status, app.Status.Health.Status, app.Status.Sync.Revision)
return synced && healthy && onRevision, nil
}
}
func (a *addClusterImpl) Execute(ctx context.Context, clusterName string) error {
a.cmd.SetArgs(append(a.args, clusterName))
return a.cmd.ExecuteContext(ctx)
}
func Login(opts *LoginOptions) error {
root := commands.NewCommand()
args := []string{
"login",
"--port-forward",
"--port-forward-namespace",
opts.Namespace,
"--username",
opts.Username,
"--password",
opts.Password,
"--name",
"autopilot",
}
if opts.KubeConfig != "" {
origKubeConfig := os.Getenv("KUBECONFIG")
defer func() { os.Setenv("KUBECONFIG", origKubeConfig) }()
if err := os.Setenv("KUBECONFIG", opts.KubeConfig); err != nil {
return fmt.Errorf("failed to set KUBECONFIG env var: %w", err)
}
}
if opts.Insecure {
args = append(args, "--plaintext")
}
if opts.KubeContext != "" {
args = append(args, "--kube-context", opts.KubeContext)
}
root.SetArgs(args)
return root.Execute()
}