blob: 0f5bb767d5eee2f8124b0f2cee6f257af91cff57 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "fmt"
20 "github.com/ciena/voltctl/pkg/format"
21 "github.com/ciena/voltctl/pkg/model"
22 flags "github.com/jessevdk/go-flags"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/client-go/kubernetes"
25 "k8s.io/client-go/tools/clientcmd"
26 "os"
27)
28
29const (
30 DEFAULT_COMPONENT_FORMAT = "table{{.Namespace}}\t{{.Id}}\t{{.Name}}\t{{.Component}}\t{{.Version}}\t{{.Ready}}\t{{.Restarts}}\t{{.Status}}\t{{.Age}}"
31 COMPONENT_LIST_KUBECTL_CMD = "kubectl get --all-namespaces pod -l app.kubernetes.io/part-of=voltha -L app.kubernetes.io/name,app.kubernetes.io/component,app.kubernetes.io/version"
32)
33
34type ComponentList struct {
35 ListOutputOptions
36 Kubectl bool `long:"kubectl" short:"k" description:"display the kubectl command to execute"`
37}
38
39type ComponentOpts struct {
40 List ComponentList `command:"list"`
41}
42
43var componentOpts = ComponentOpts{}
44
45func RegisterComponentCommands(parser *flags.Parser) {
46 parser.AddCommand("component", "component instance commands", "Commands to query and manipulate VOLTHA component instances", &componentOpts)
47}
48
49func (options *ComponentList) Execute(args []string) error {
50
51 ProcessGlobalOptions()
52
53 // If they requested the source to the kubectl command that
54 // can give the same information, then print it and return
55 if options.Kubectl {
56 fmt.Println(COMPONENT_LIST_KUBECTL_CMD)
57 return nil
58 }
59
60 // use the current context in kubeconfig
61 config, err := clientcmd.BuildConfigFromFlags("", GlobalOptions.K8sConfig)
62 if err != nil {
63 panic(err.Error())
64 }
65
66 // create the clientset
67 clientset, err := kubernetes.NewForConfig(config)
68 if err != nil {
69 panic(err.Error())
70 }
71
72 pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{
73 LabelSelector: "app.kubernetes.io/part-of=voltha",
74 })
75 if err != nil {
76 panic(err.Error())
77 }
78
79 outputFormat := CharReplacer.Replace(options.Format)
80 if outputFormat == "" {
81 outputFormat = DEFAULT_COMPONENT_FORMAT
82 }
83 if options.Quiet {
84 outputFormat = "{{.Metadata.Name}}"
85 }
86 data := make([]model.ComponentInstance, len(pods.Items))
87 for i, item := range pods.Items {
88 data[i].PopulateFrom(item)
89 }
90
91 result := CommandResult{
92 Format: format.Format(outputFormat),
93 Filter: options.Filter,
94 OrderBy: options.OrderBy,
95 OutputAs: toOutputType(options.OutputAs),
96 NameLimit: options.NameLimit,
97 Data: data,
98 }
99
100 GenerateOutput(&result)
101 return nil
102}
103
104func homeDir() string {
105 if h := os.Getenv("HOME"); h != "" {
106 return h
107 }
108 return os.Getenv("USERPROFILE") // windows
109}