-
Notifications
You must be signed in to change notification settings - Fork 85
/
argo.go
106 lines (94 loc) · 2.57 KB
/
argo.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
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package argo
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
argocdcli "github.com/argoproj/argo-cd/v2/pkg/apiclient"
gemlabels "kubegems.io/kubegems/pkg/apis/gems"
)
func NewArgoCDCli(options *Options) (argocdcli.Client, error) {
parsedurl, err := url.Parse(options.Addr)
if err != nil {
return nil, err
}
token := options.Token
if token == "" {
// create from user and password
tk, err := GetTokenFromUserPassword(options.Addr, options.Username, options.Password)
if err != nil {
return nil, err
}
token = tk
}
cliopt := &argocdcli.ClientOptions{
ServerAddr: parsedurl.Host,
Insecure: true, // Same with tls.SkipTLSVerify
AuthToken: token,
// https://argo-cd.readthedocs.io/en/stable/faq/#why-am-i-getting-rpc-error-code-unavailable-desc-transport-is-closing-when-using-the-cli
GRPCWeb: true,
}
if options.Addr == "" {
cliopt.PortForward = true
cliopt.PortForwardNamespace = gemlabels.NamespaceSystem
}
cli, err := argocdcli.NewClient(cliopt)
if err != nil {
return nil, err
}
return cli, nil
}
func GetTokenFromUserPassword(addr string, username, password string) (string, error) {
bts, err := json.Marshal(struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}{
Username: username,
Password: password,
})
if err != nil {
return "", err
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, err := client.Post(addr+"/api/v1/session", "application/json", bytes.NewReader(bts))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return "", errors.New(string(bt))
}
tokenresp := &struct {
Token string `json:"token"`
}{}
if err := json.NewDecoder(resp.Body).Decode(tokenresp); err != nil {
return "", err
}
return tokenresp.Token, nil
}