forked from funktionio/funktion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
244 lines (222 loc) · 6.17 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
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2016 Red Hat, Inc.
//
// 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 cmd
import (
"bytes"
"fmt"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/pkg/api"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
"github.com/muter3000/funktion/pkg/funktion"
"github.com/muter3000/funktion/pkg/spec"
)
type getCmd struct {
kubeclient *kubernetes.Clientset
cmd *cobra.Command
kubeConfigPath string
kind string
namespace string
name string
deployments map[string]*v1beta1.Deployment
services map[string]*v1.Service
}
func init() {
RootCmd.AddCommand(newGetCmd())
}
func newGetCmd() *cobra.Command {
p := &getCmd{}
cmd := &cobra.Command{
Use: "get KIND [NAME] [flags]",
Short: "gets a list of the resources",
Long: `This command will list all of the resources of a given kind`,
Run: func(cmd *cobra.Command, args []string) {
p.cmd = cmd
if len(args) == 0 {
handleError(fmt.Errorf("No resource kind argument supplied! Possible values ['connector', 'flow', 'function', 'runtime']"))
return
}
p.kind = args[0]
if len(args) > 1 {
p.name = args[1]
}
err := createKubernetesClient(cmd, p.kubeConfigPath, &p.kubeclient, &p.namespace)
if err != nil {
handleError(err)
return
}
handleError(p.run())
},
}
f := cmd.Flags()
//f.StringVarP(&p.format, "output", "o", "", "The format of the output")
f.StringVar(&p.kubeConfigPath, "kubeconfig", "", "the directory to look for the kubernetes configuration")
f.StringVarP(&p.namespace, "namespace", "n", "", "the namespace to query")
return cmd
}
func (p *getCmd) run() error {
kind, listOpts, err := listOptsForKind(p.kind)
if err != nil {
return err
}
kubeclient := p.kubeclient
cms := kubeclient.ConfigMaps(p.namespace)
resources, err := cms.List(*listOpts)
if err != nil {
return err
}
p.deployments = map[string]*v1beta1.Deployment{}
p.services = map[string]*v1.Service{}
ds, err := kubeclient.Deployments(p.namespace).List(api.ListOptions{})
if err != nil {
return err
}
p.deployments = map[string]*v1beta1.Deployment{}
for _, item := range ds.Items {
// TODO lets assume the name of the Deployment is the name of the Flow
// but we may want to use a label instead to link them?
name := item.Name
copy := v1beta1.Deployment{}
copy = item
p.deployments[name] = ©
}
if kind == functionKind {
ss, err := kubeclient.Services(p.namespace).List(api.ListOptions{})
if err != nil {
return err
}
for _, item := range ss.Items {
// TODO lets assume the name of the Service is the name of the Function
// but we may want to use a label instead to link them?
name := item.Name
copy := v1.Service{}
copy = item
p.services[name] = ©
}
}
name := p.name
if len(name) == 0 {
p.printHeader(kind)
for _, resource := range resources.Items {
p.printResource(&resource, kind)
}
} else {
found := false
for _, resource := range resources.Items {
if resource.Name == name {
p.printHeader(kind)
p.printResource(&resource, kind)
found = true
break
}
}
if !found {
return fmt.Errorf("%s \"%s\" not found", kind, name)
}
}
return nil
}
func (p *getCmd) printHeader(kind string) {
switch kind {
case flowKind:
printFlowRow("NAME", "PODS", "STEPS")
case functionKind:
printFunctionRow("NAME", "PODS", "URL")
default:
printRuntimeRow("NAME", "VERSION")
}
}
func (p *getCmd) printResource(cm *v1.ConfigMap, kind string) {
switch kind {
case functionKind:
printFunctionRow(cm.Name, p.podText(cm), p.functionURLText(cm))
case flowKind:
printFlowRow(cm.Name, p.podText(cm), p.flowStepsText(cm))
default:
printRuntimeRow(cm.Name, p.runtimeVersion(cm))
}
}
func printFunctionRow(name string, pod string, flow string) {
fmt.Printf("%-32s %-9s %s\n", name, pod, flow)
}
func printFlowRow(name string, pod string, flow string) {
fmt.Printf("%-32s %-9s %s\n", name, pod, flow)
}
func printRuntimeRow(name string, version string) {
fmt.Printf("%-32s %s\n", name, version)
}
func (p *getCmd) podText(cm *v1.ConfigMap) string {
name := cm.Name
deployment := p.deployments[name]
if deployment == nil {
return ""
}
var status = deployment.Status
return fmt.Sprintf("%d/%d", status.AvailableReplicas, status.Replicas)
}
func (p *getCmd) functionURLText(cm *v1.ConfigMap) string {
name := cm.Name
service := p.services[name]
if service == nil || service.Annotations == nil {
return ""
}
return service.Annotations[exposeURLAnnotation]
}
func (p *getCmd) runtimeVersion(cm *v1.ConfigMap) string {
labels := cm.Labels
if labels != nil {
return labels[funktion.VersionLabel]
}
return ""
}
func (p *getCmd) flowStepsText(cm *v1.ConfigMap) string {
yamlText := cm.Data[funktion.FunktionYmlProperty]
if len(yamlText) == 0 {
return fmt.Sprintf("No `%s` property specified", funktion.FunktionYmlProperty)
}
fc := spec.FunkionConfig{}
err := yaml.Unmarshal([]byte(yamlText), &fc)
if err != nil {
return fmt.Sprintf("Failed to parse `%s` YAML: %v", funktion.FunktionYmlProperty, err)
}
if len(fc.Flows) == 0 {
return "No funktion flows"
}
rule := fc.Flows[0]
return stepsText(rule.Steps)
}
func stepsText(steps []spec.FunktionStep) string {
actionMessage := "No steps!"
if len(steps) > 0 {
var buffer bytes.Buffer
for i, step := range steps {
if i > 0 {
buffer.WriteString(" => ")
}
kind := step.Kind
text := kind
switch kind {
case spec.EndpointKind:
text = fmt.Sprintf("%s", step.URI)
case spec.FunctionKind:
text = fmt.Sprintf("function %s", step.Name)
}
buffer.WriteString(text)
}
actionMessage = buffer.String()
}
return actionMessage
}