blob: 95cbd8013fe1324ded97a015e6b3a97e32fef178 [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"
Zack Williamse940c7a2019-08-21 14:25:39 -070020 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070021 "github.com/opencord/voltctl/pkg/format"
22 "github.com/opencord/voltctl/pkg/model"
Zack Williamse940c7a2019-08-21 14:25:39 -070023 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/client-go/kubernetes"
25 "k8s.io/client-go/tools/clientcmd"
David Bainbridge12f036f2019-10-15 22:09:04 +000026 "log"
Zack Williamse940c7a2019-08-21 14:25:39 -070027)
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) {
David Bainbridge12f036f2019-10-15 22:09:04 +000046 if _, err := parser.AddCommand("component", "component instance commands", "Commands to query and manipulate VOLTHA component instances", &componentOpts); err != nil {
47 log.Fatalf("Unexpected error while attempting to register component commands : %s", err)
48 }
Zack Williamse940c7a2019-08-21 14:25:39 -070049}
50
51func (options *ComponentList) Execute(args []string) error {
52
53 ProcessGlobalOptions()
54
55 // If they requested the source to the kubectl command that
56 // can give the same information, then print it and return
57 if options.Kubectl {
58 fmt.Println(COMPONENT_LIST_KUBECTL_CMD)
59 return nil
60 }
61
62 // use the current context in kubeconfig
63 config, err := clientcmd.BuildConfigFromFlags("", GlobalOptions.K8sConfig)
64 if err != nil {
65 panic(err.Error())
66 }
67
68 // create the clientset
69 clientset, err := kubernetes.NewForConfig(config)
70 if err != nil {
71 panic(err.Error())
72 }
73
74 pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{
75 LabelSelector: "app.kubernetes.io/part-of=voltha",
76 })
77 if err != nil {
78 panic(err.Error())
79 }
80
81 outputFormat := CharReplacer.Replace(options.Format)
82 if outputFormat == "" {
83 outputFormat = DEFAULT_COMPONENT_FORMAT
84 }
85 if options.Quiet {
86 outputFormat = "{{.Metadata.Name}}"
87 }
88 data := make([]model.ComponentInstance, len(pods.Items))
89 for i, item := range pods.Items {
90 data[i].PopulateFrom(item)
91 }
92
93 result := CommandResult{
94 Format: format.Format(outputFormat),
95 Filter: options.Filter,
96 OrderBy: options.OrderBy,
97 OutputAs: toOutputType(options.OutputAs),
98 NameLimit: options.NameLimit,
99 Data: data,
100 }
101
102 GenerateOutput(&result)
103 return nil
104}