forked from kubernetes/kops
/
get.go
233 lines (186 loc) · 5.4 KB
/
get.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Copyright 2016 The Kubernetes 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 main
import (
"fmt"
"io"
"os"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/kopscodecs"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
)
var (
getLong = templates.LongDesc(i18n.T(`
Display one or many resources.` + validResources))
getExample = templates.Examples(i18n.T(`
# Get all clusters in a state store
kops get clusters
# Get a cluster and its instancegroups
kops get k8s-cluster.example.com
# Get a cluster and its instancegroups' YAML desired configuration
kops get k8s-cluster.example.com -o yaml
# Save a cluster and its instancegroups' desired configuration to YAML file
kops get k8s-cluster.example.com -o yaml > cluster-desired-config.yaml
# Get a secret
kops get secrets kube -oplaintext
# Get the admin password for a cluster
kops get secrets admin -oplaintext`))
getShort = i18n.T(`Get one or many resources.`)
)
type GetOptions struct {
output string
clusterName string
}
const (
OutputYaml = "yaml"
OutputTable = "table"
OutputJSON = "json"
)
func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
options := &GetOptions{
output: OutputTable,
}
cmd := &cobra.Command{
Use: "get",
SuggestFor: []string{"list"},
Short: getShort,
Long: getLong,
Example: getExample,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
options.clusterName = args[0]
}
if rootCommand.clusterName != "" {
if len(args) != 0 {
exitWithError(fmt.Errorf("cannot mix --name for cluster with positional arguments"))
}
options.clusterName = rootCommand.clusterName
}
err := RunGet(&rootCommand, os.Stdout, options)
if err != nil {
exitWithError(err)
}
},
}
cmd.PersistentFlags().StringVarP(&options.output, "output", "o", options.output, "output format. One of: table, yaml, json")
// create subcommands
cmd.AddCommand(NewCmdGetCluster(f, out, options))
cmd.AddCommand(NewCmdGetInstanceGroups(f, out, options))
cmd.AddCommand(NewCmdGetSecrets(f, out, options))
return cmd
}
func RunGet(context Factory, out io.Writer, options *GetOptions) error {
client, err := context.Clientset()
if err != nil {
return err
}
cluster, err := client.GetCluster(options.clusterName)
if err != nil {
return err
}
if cluster == nil {
return fmt.Errorf("No cluster found")
}
clusterList := &api.ClusterList{}
clusterList.Items = make([]api.Cluster, 1)
clusterList.Items[0] = *cluster
args := make([]string, 0)
clusters, err := buildClusters(args, clusterList)
if err != nil {
return fmt.Errorf("error on buildClusters(): %v", err)
}
ig, err := client.InstanceGroupsFor(cluster).List(metav1.ListOptions{})
if err != nil {
return err
}
if ig == nil || ig.Items == nil || len(ig.Items) == 0 {
fmt.Fprintf(os.Stderr, "No instance groups found\n")
}
instancegroups, err := buildInstanceGroups(args, ig)
if err != nil {
return err
}
var obj []runtime.Object
if options.output != OutputTable {
obj = append(obj, cluster)
for _, group := range instancegroups {
obj = append(obj, group)
}
}
switch options.output {
case OutputYaml:
if err := fullOutputYAML(out, obj...); err != nil {
return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
}
return nil
case OutputJSON:
if err := fullOutputJSON(out, obj...); err != nil {
return fmt.Errorf("error writing cluster json to stdout: %v", err)
}
return nil
case OutputTable:
fmt.Fprintf(os.Stdout, "Cluster\n")
err = clusterOutputTable(clusters, out)
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "\nInstance Groups\n")
err = igOutputTable(cluster, instancegroups, out)
if err != nil {
return err
}
default:
return fmt.Errorf("Unknown output format: %q", options.output)
}
return nil
}
func writeYAMLSep(out io.Writer) error {
_, err := out.Write([]byte("\n---\n\n"))
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil
}
type marshalFunc func(obj runtime.Object) ([]byte, error)
func marshalToWriter(obj runtime.Object, marshal marshalFunc, w io.Writer) error {
b, err := marshal(obj)
if err != nil {
return err
}
_, err = w.Write(b)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil
}
// obj must be a pointer to a marshalable object
func marshalYaml(obj runtime.Object) ([]byte, error) {
y, err := kopscodecs.ToVersionedYaml(obj)
if err != nil {
return nil, fmt.Errorf("error marshaling yaml: %v", err)
}
return y, nil
}
// obj must be a pointer to a marshalable object
func marshalJSON(obj runtime.Object) ([]byte, error) {
j, err := kopscodecs.ToVersionedJSON(obj)
if err != nil {
return nil, fmt.Errorf("error marshaling json: %v", err)
}
return j, nil
}