-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathget.go
More file actions
196 lines (169 loc) · 6.1 KB
/
get.go
File metadata and controls
196 lines (169 loc) · 6.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2018- The Pixie 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package cmd
import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/blang/semver"
"github.com/dustin/go-humanize"
"github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"px.dev/pixie/src/pixie_cli/pkg/components"
cliUtils "px.dev/pixie/src/pixie_cli/pkg/utils"
"px.dev/pixie/src/pixie_cli/pkg/vizier"
"px.dev/pixie/src/utils"
"px.dev/pixie/src/utils/script"
"px.dev/pixie/src/utils/shared/k8s"
)
func init() {
GetCmd.PersistentFlags().StringP("output", "o", "", "Output format: one of: json|proto")
GetPEMsCmd.Flags().BoolP("all-clusters", "d", false, "Run script across all clusters")
GetPEMsCmd.Flags().StringP("cluster", "c", "", "Run only on selected cluster")
GetPEMsCmd.Flags().MarkHidden("all-clusters")
GetClusterCmd.Flags().Bool("id", false, "Whether to only fetch the cluster ID from the cluster running in the current kubeconfig")
GetClusterCmd.Flags().Bool("cloud-addr", false, "Whether to only fetch the cloud address from the cluster running in the current kubeconfig")
GetCmd.AddCommand(GetPEMsCmd)
GetCmd.AddCommand(GetViziersCmd)
GetCmd.AddCommand(GetClusterCmd)
}
// GetPEMsCmd is the "get pem" command.
var GetPEMsCmd = &cobra.Command{
Use: "pems",
Aliases: []string{"agents", "pem"},
Short: "Get information about running pems",
Run: func(cmd *cobra.Command, args []string) {
cloudAddr := viper.GetString("cloud_addr")
format, _ := cmd.Flags().GetString("output")
format = strings.ToLower(format)
br := mustCreateBundleReader()
execScript := br.MustGetScript(script.AgentStatusScript)
allClusters, _ := cmd.Flags().GetBool("all-clusters")
selectedCluster, _ := cmd.Flags().GetString("cluster")
clusterID := uuid.FromStringOrNil(selectedCluster)
var err error
if !allClusters && clusterID == uuid.Nil {
clusterID, err = vizier.GetCurrentVizier(cloudAddr)
if err != nil {
cliUtils.WithError(err).Fatal("Could not fetch healthy vizier")
}
}
conns := vizier.MustConnectHealthyDefaultVizier(cloudAddr, allClusters, clusterID)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := vizier.RunScriptAndOutputResults(ctx, conns, execScript, format, false); err != nil {
cliUtils.Fatalf("Script failed: %s", vizier.FormatErrorMessage(err))
}
},
}
// GetViziersCmd is the "get viziers" command.
var GetViziersCmd = &cobra.Command{
Use: "viziers",
Aliases: []string{"clusters"},
Short: "Get information about registered viziers",
Run: func(cmd *cobra.Command, args []string) {
cloudAddr := viper.GetString("cloud_addr")
format, _ := cmd.Flags().GetString("output")
format = strings.ToLower(format)
l, err := vizier.NewLister(cloudAddr)
if err != nil {
// Using log.Fatal rather than CLI log in order to track this unexpected error in Sentry.
log.WithError(err).Fatal("Failed to create Vizier lister")
}
vzs, err := l.GetViziersInfo()
if err != nil {
// Using log.Fatal rather than CLI log in order to track this unexpected error in Sentry.
log.WithError(err).Fatalln("Failed to get vizier information")
}
sort.Slice(vzs, func(i, j int) bool { return vzs[i].ClusterName < vzs[j].ClusterName })
w := components.CreateStreamWriter(format, os.Stdout)
defer w.Finish()
w.SetHeader("viziers", []string{"ClusterName", "ID", "K8s Version", "Operator Version", "Vizier Version", "Last Heartbeat", "Status", "Status Message"})
for _, vz := range vzs {
var lastHeartbeat interface{}
lastHeartbeat = vz.LastHeartbeatNs
if format == "" || format == "table" {
if vz.LastHeartbeatNs >= 0 {
lastHeartbeat = humanize.Time(
time.Unix(0,
time.Since(time.Unix(0, vz.LastHeartbeatNs)).Nanoseconds()))
}
}
_ = w.Write([]interface{}{
vz.ClusterName, utils.UUIDFromProtoOrNil(vz.ID), vz.ClusterVersion,
prettyVersion(vz.OperatorVersion), prettyVersion(vz.VizierVersion), lastHeartbeat, vz.Status, vz.StatusMessage,
})
}
},
}
// prettyVersion returns a pretty form of the version string.
func prettyVersion(version string) string {
sb := strings.Builder{}
sv, err := semver.Parse(version)
if err != nil {
return ""
}
sb.WriteString(fmt.Sprintf("%d.%d.%d", sv.Major, sv.Minor, sv.Patch))
for idx, pre := range sv.Pre {
if idx == 0 {
sb.WriteString("-")
} else {
sb.WriteString(".")
}
sb.WriteString(pre.String())
}
return sb.String()
}
// GetClusterCmd is the "get cluster" command to get information about the current kubeconfig cluster.
var GetClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Get information about the current kubeconfig cluster",
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("id", cmd.Flags().Lookup("id"))
viper.BindPFlag("cloud-addr", cmd.Flags().Lookup("cloud-addr"))
},
Run: func(cmd *cobra.Command, args []string) {
id, _ := cmd.Flags().GetBool("id")
addr, _ := cmd.Flags().GetBool("cloud-addr")
config := k8s.GetConfig()
clusterID := vizier.GetClusterIDFromKubeConfig(config)
if clusterID == uuid.Nil {
cliUtils.Infof("Unable to find Pixie cluster running in current kubeconfig")
}
cloudAddr := vizier.GetCloudAddrFromKubeConfig(config)
if id {
fmt.Fprintf(os.Stdout, "%s\n", clusterID)
return
}
if addr {
fmt.Fprintf(os.Stdout, "%s\n", cloudAddr)
return
}
cliUtils.Infof("Cluster ID: %s\nCloud Address: %s", clusterID, cloudAddr)
},
}
// GetCmd is the "get" command.
var GetCmd = &cobra.Command{
Use: "get",
Short: "Get information about cluster/edge modules",
}