Skip to content

Commit ae9bb96

Browse files
feat: support proxy in our cluster (#342)
1 parent b8705bb commit ae9bb96

File tree

11 files changed

+893
-710
lines changed

11 files changed

+893
-710
lines changed

assets/swagger.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7234,12 +7234,20 @@
72347234
"description": "Server requires Bearer authentication. This client will not attempt to use\nrefresh tokens for an OAuth2 flow.\nTODO: demonstrate an OAuth2 compatible client.",
72357235
"type": "string"
72367236
},
7237+
"disableCompression": {
7238+
"description": "DisableCompression bypasses automatic GZip compression requests to the server.",
7239+
"type": "boolean"
7240+
},
72377241
"execProviderConfig": {
72387242
"$ref": "#/definitions/v1alpha1ExecProviderConfig"
72397243
},
72407244
"password": {
72417245
"type": "string"
72427246
},
7247+
"proxyUrl": {
7248+
"type": "string",
7249+
"title": "ProxyURL is the URL to the proxy to be used for all requests send to the server"
7250+
},
72437251
"tlsClientConfig": {
72447252
"$ref": "#/definitions/v1alpha1TLSClientConfig"
72457253
},

cmd/argocd/commands/cluster.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67
"regexp"
78
"strings"
@@ -34,6 +35,10 @@ const (
3435
clusterFieldName = "name"
3536
// cluster field is 'namespaces'
3637
clusterFieldNamespaces = "namespaces"
38+
// cluster field is 'labels'
39+
clusterFieldLabel = "labels"
40+
// cluster field is 'annotations'
41+
clusterFieldAnnotation = "annotations"
3742
// indicates managing all namespaces
3843
allNamespaces = "*"
3944
)
@@ -102,6 +107,11 @@ func NewClusterAddCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clie
102107
contextName := args[0]
103108
conf, err := getRestConfig(pathOpts, contextName)
104109
errors.CheckError(err)
110+
if clusterOpts.ProxyUrl != "" {
111+
u, err := argoappv1.ParseProxyUrl(clusterOpts.ProxyUrl)
112+
errors.CheckError(err)
113+
conf.Proxy = http.ProxyURL(u)
114+
}
105115
clientset, err := kubernetes.NewForConfig(conf)
106116
errors.CheckError(err)
107117
managerBearerToken := ""
@@ -187,6 +197,7 @@ func NewClusterAddCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clie
187197
command.Flags().BoolVarP(&skipConfirmation, "yes", "y", false, "Skip explicit confirmation")
188198
command.Flags().StringArrayVar(&labels, "label", nil, "Set metadata labels (e.g. --label key=value)")
189199
command.Flags().StringArrayVar(&annotations, "annotation", nil, "Set metadata annotations (e.g. --annotation key=value)")
200+
command.Flags().StringVar(&clusterOpts.ProxyUrl, "proxy-url", "", "use proxy to connect cluster")
190201
cmdutil.AddClusterFlags(command, &clusterOpts)
191202
return command
192203
}
@@ -220,6 +231,8 @@ func NewClusterSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command
220231
var (
221232
clusterOptions cmdutil.ClusterOptions
222233
clusterName string
234+
labels []string
235+
annotations []string
223236
)
224237
command := &cobra.Command{
225238
Use: "set NAME",
@@ -238,17 +251,25 @@ func NewClusterSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command
238251
conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie()
239252
defer io.Close(conn)
240253
// checks the fields that needs to be updated
241-
updatedFields := checkFieldsToUpdate(clusterOptions)
254+
updatedFields := checkFieldsToUpdate(clusterOptions, labels, annotations)
242255
namespaces := clusterOptions.Namespaces
243256
// check if all namespaces have to be considered
244257
if len(namespaces) == 1 && strings.EqualFold(namespaces[0], allNamespaces) {
245258
namespaces[0] = ""
246259
}
260+
// parse the labels you're receiving from the label flag
261+
labelsMap, err := label.Parse(labels)
262+
errors.CheckError(err)
263+
// parse the annotations you're receiving from the annotation flag
264+
annotationsMap, err := label.Parse(annotations)
265+
errors.CheckError(err)
247266
if updatedFields != nil {
248267
clusterUpdateRequest := clusterpkg.ClusterUpdateRequest{
249268
Cluster: &argoappv1.Cluster{
250-
Name: clusterOptions.Name,
251-
Namespaces: namespaces,
269+
Name: clusterOptions.Name,
270+
Namespaces: namespaces,
271+
Labels: labelsMap,
272+
Annotations: annotationsMap,
252273
},
253274
UpdatedFields: updatedFields,
254275
Id: &clusterpkg.ClusterID{
@@ -266,18 +287,26 @@ func NewClusterSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command
266287
}
267288
command.Flags().StringVar(&clusterOptions.Name, "name", "", "Overwrite the cluster name")
268289
command.Flags().StringArrayVar(&clusterOptions.Namespaces, "namespace", nil, "List of namespaces which are allowed to manage. Specify '*' to manage all namespaces")
290+
command.Flags().StringArrayVar(&labels, "label", nil, "Set metadata labels (e.g. --label key=value)")
291+
command.Flags().StringArrayVar(&annotations, "annotation", nil, "Set metadata annotations (e.g. --annotation key=value)")
269292
return command
270293
}
271294

272295
// checkFieldsToUpdate returns the fields that needs to be updated
273-
func checkFieldsToUpdate(clusterOptions cmdutil.ClusterOptions) []string {
296+
func checkFieldsToUpdate(clusterOptions cmdutil.ClusterOptions, labels []string, annotations []string) []string {
274297
var updatedFields []string
275298
if clusterOptions.Name != "" {
276299
updatedFields = append(updatedFields, clusterFieldName)
277300
}
278301
if clusterOptions.Namespaces != nil {
279302
updatedFields = append(updatedFields, clusterFieldNamespaces)
280303
}
304+
if labels != nil {
305+
updatedFields = append(updatedFields, clusterFieldLabel)
306+
}
307+
if annotations != nil {
308+
updatedFields = append(updatedFields, clusterFieldAnnotation)
309+
}
281310
return updatedFields
282311
}
283312

@@ -341,6 +370,7 @@ func printClusterDetails(clusters []argoappv1.Cluster) {
341370
fmt.Printf("Cluster information\n\n")
342371
fmt.Printf(" Server URL: %s\n", cluster.Server)
343372
fmt.Printf(" Server Name: %s\n", strWithDefault(cluster.Name, "-"))
373+
// nolint:staticcheck
344374
fmt.Printf(" Server Version: %s\n", cluster.ServerVersion)
345375
fmt.Printf(" Namespaces: %s\n", formatNamespaces(cluster))
346376
fmt.Printf("\nTLS configuration\n\n")
@@ -350,6 +380,8 @@ func printClusterDetails(clusters []argoappv1.Cluster) {
350380
fmt.Printf(" Basic authentication: %v\n", cluster.Config.Username != "")
351381
fmt.Printf(" oAuth authentication: %v\n", cluster.Config.BearerToken != "")
352382
fmt.Printf(" AWS authentication: %v\n", cluster.Config.AWSAuthConfig != nil)
383+
fmt.Printf("\nDisable compression: %v\n", cluster.Config.DisableCompression)
384+
fmt.Printf("\nUse proxy: %v\n", cluster.Config.ProxyUrl != "")
353385
fmt.Println()
354386
}
355387
}
@@ -433,6 +465,7 @@ func printClusterTable(clusters []argoappv1.Cluster) {
433465
if len(c.Namespaces) > 0 {
434466
server = fmt.Sprintf("%s (%d namespaces)", c.Server, len(c.Namespaces))
435467
}
468+
// nolint:staticcheck
436469
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", server, c.Name, c.ServerVersion, c.ConnectionState.Status, c.ConnectionState.Message, c.Project)
437470
}
438471
_ = w.Flush()

cmd/util/cluster.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,18 @@ func NewCluster(name string, namespaces []string, clusterResources bool, conf *r
100100
TLSClientConfig: tlsClientConfig,
101101
AWSAuthConfig: awsAuthConf,
102102
ExecProviderConfig: execProviderConf,
103+
DisableCompression: conf.DisableCompression,
103104
},
104105
Labels: labels,
105106
Annotations: annotations,
106107
}
107-
108+
// it's a tradeoff to get proxy url from rest config
109+
// more detail: https://github.com/kubernetes/kubernetes/pull/81443
110+
if conf.Proxy != nil {
111+
if url, err := conf.Proxy(nil); err == nil {
112+
clst.Config.ProxyUrl = url.String()
113+
}
114+
}
108115
// Bearer token will preferentially be used for auth if present,
109116
// Even in presence of key/cert credentials
110117
// So set bearer token only if the key/cert data is absent
@@ -158,6 +165,8 @@ type ClusterOptions struct {
158165
ExecProviderAPIVersion string
159166
ExecProviderInstallHint string
160167
ClusterEndpoint string
168+
DisableCompression bool
169+
ProxyUrl string
161170
}
162171

163172
// InClusterEndpoint returns true if ArgoCD should reference the in-cluster
@@ -182,4 +191,5 @@ func AddClusterFlags(command *cobra.Command, opts *ClusterOptions) {
182191
command.Flags().StringVar(&opts.ExecProviderAPIVersion, "exec-command-api-version", "", "Preferred input version of the ExecInfo for the --exec-command executable")
183192
command.Flags().StringVar(&opts.ExecProviderInstallHint, "exec-command-install-hint", "", "Text shown to the user when the --exec-command executable doesn't seem to be present")
184193
command.Flags().StringVar(&opts.ClusterEndpoint, "cluster-endpoint", "", "Cluster endpoint to use. Can be one of the following: 'kubeconfig', 'kube-public', or 'internal'.")
194+
command.Flags().BoolVar(&opts.DisableCompression, "disable-compression", false, "Bypasses automatic GZip compression requests to the server")
185195
}

docs/operator-manual/declarative-setup.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ execProviderConfig:
562562
}
563563
apiVersion: string
564564
installHint: string
565+
# Proxy URL for the kubernetes client to use when connecting to the cluster api server
566+
proxyUrl: string
565567
# Transport layer security configuration settings
566568
tlsClientConfig:
567569
# Base64 encoded PEM-encoded bytes (typically read from a client certificate file).

docs/user-guide/commands/argocd_admin_cluster_generate-spec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ argocd admin cluster generate-spec CONTEXT [flags]
1818
--bearer-token string Authentication token that should be used to access K8S API server
1919
--cluster-endpoint string Cluster endpoint to use. Can be one of the following: 'kubeconfig', 'kube-public', or 'internal'.
2020
--cluster-resources Indicates if cluster level resources should be managed. The setting is used only if list of managed namespaces is not empty.
21+
--disable-compression Bypasses automatic GZip compression requests to the server
2122
--exec-command string Command to run to provide client credentials to the cluster. You may need to build a custom ArgoCD image to ensure the command is available at runtime.
2223
--exec-command-api-version string Preferred input version of the ExecInfo for the --exec-command executable
2324
--exec-command-args stringArray Arguments to supply to the --exec-command executable

docs/user-guide/commands/argocd_cluster_add.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ argocd cluster add CONTEXT [flags]
1717
--aws-role-arn string Optional AWS role arn. If set then AWS IAM Authenticator assumes a role to perform cluster operations instead of the default AWS credential provider chain.
1818
--cluster-endpoint string Cluster endpoint to use. Can be one of the following: 'kubeconfig', 'kube-public', or 'internal'.
1919
--cluster-resources Indicates if cluster level resources should be managed. The setting is used only if list of managed namespaces is not empty.
20+
--disable-compression Bypasses automatic GZip compression requests to the server
2021
--exec-command string Command to run to provide client credentials to the cluster. You may need to build a custom ArgoCD image to ensure the command is available at runtime.
2122
--exec-command-api-version string Preferred input version of the ExecInfo for the --exec-command executable
2223
--exec-command-args stringArray Arguments to supply to the --exec-command executable
@@ -29,6 +30,7 @@ argocd cluster add CONTEXT [flags]
2930
--name string Overwrite the cluster name
3031
--namespace stringArray List of namespaces which are allowed to manage
3132
--project string project of the cluster
33+
--proxy-url string use proxy to connect cluster
3234
--service-account string System namespace service account to use for kubernetes resource management. If not set then default "argocd-manager" SA will be created
3335
--shard int Cluster shard number; inferred from hostname if not set (default -1)
3436
--system-namespace string Use different system namespace (default "kube-system")

docs/user-guide/commands/argocd_cluster_set.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ argocd cluster set NAME [flags]
1919
### Options
2020

2121
```
22-
-h, --help help for set
23-
--name string Overwrite the cluster name
24-
--namespace stringArray List of namespaces which are allowed to manage. Specify '*' to manage all namespaces
22+
--annotation stringArray Set metadata annotations (e.g. --annotation key=value)
23+
-h, --help help for set
24+
--label stringArray Set metadata labels (e.g. --label key=value)
25+
--name string Overwrite the cluster name
26+
--namespace stringArray List of namespaces which are allowed to manage. Specify '*' to manage all namespaces
2527
```
2628

2729
### Options inherited from parent commands

0 commit comments

Comments
 (0)