Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/clusters.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"fmt"
10
11
"github.com/spf13/cobra"
12
"golang.org/x/xerrors"
13
"google.golang.org/grpc"
14
"google.golang.org/grpc/credentials"
15
"google.golang.org/grpc/credentials/insecure"
16
"k8s.io/client-go/kubernetes"
17
18
"github.com/gitpod-io/gitpod/common-go/log"
19
"github.com/gitpod-io/gitpod/gpctl/pkg/util"
20
"github.com/gitpod-io/gitpod/ws-manager-bridge/api"
21
)
22
23
// clustersCmd represents the clusters command
24
var clustersCmd = &cobra.Command{
25
Use: "clusters",
26
Short: "Controls and inspects cluster",
27
Args: cobra.ExactArgs(1),
28
}
29
30
var clustersCmdOpts struct {
31
TLS string
32
Port int
33
Name string
34
}
35
36
func init() {
37
clustersCmd.PersistentFlags().StringVarP(&clustersCmdOpts.TLS, "tls", "t", "", "TLS certificate when connecting to a secured gRPC endpoint")
38
clustersCmd.PersistentFlags().IntVarP(&clustersCmdOpts.Port, "port", "p", 8080, "port of the gRPC endpoint")
39
clustersCmd.PersistentFlags().StringVarP(&clustersCmdOpts.Name, "name", "n", "", "name of the cluster to affect")
40
41
rootCmd.AddCommand(clustersCmd)
42
}
43
44
func getClusterName() string {
45
name := clustersCmdOpts.Name
46
if name == "" {
47
log.Fatal("missing --name")
48
}
49
return name
50
}
51
52
func getClustersClient(ctx context.Context) (*grpc.ClientConn, api.ClusterServiceClient, error) {
53
cfg, namespace, err := getKubeconfig()
54
if err != nil {
55
return nil, nil, err
56
}
57
clientSet, err := kubernetes.NewForConfig(cfg)
58
if err != nil {
59
return nil, nil, err
60
}
61
62
localPort := "30303"
63
remotePort := clustersCmdOpts.Port
64
if remotePort == 0 {
65
remotePort = 8099
66
}
67
port := fmt.Sprintf("%s:%d", localPort, remotePort)
68
podName, err := util.FindAnyPodForComponent(clientSet, namespace, "ws-manager-bridge")
69
if err != nil {
70
return nil, nil, err
71
}
72
readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port)
73
select {
74
case <-readychan:
75
case err := <-errchan:
76
return nil, nil, err
77
case <-ctx.Done():
78
return nil, nil, ctx.Err()
79
}
80
81
secopt := grpc.WithTransportCredentials(insecure.NewCredentials())
82
cert, _ := clustersCmd.Flags().GetString("tls")
83
if cert != "" {
84
creds, err := credentials.NewClientTLSFromFile(cert, "")
85
if err != nil {
86
return nil, nil, xerrors.Errorf("could not load tls cert: %w", err)
87
}
88
89
secopt = grpc.WithTransportCredentials(creds)
90
}
91
92
conn, err := grpc.Dial(fmt.Sprintf("localhost:%s", localPort), secopt, util.WithClientUnaryInterceptor())
93
if err != nil {
94
return nil, nil, err
95
}
96
return conn, api.NewClusterServiceClient(conn), nil
97
}
98
99