blob: 203b64d0056db791b2eaf4dc8c1469023a0dc7c7 [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"
Zack Williamse940c7a2019-08-21 14:25:39 -070026)
27
28const (
29 DEFAULT_COMPONENT_FORMAT = "table{{.Namespace}}\t{{.Id}}\t{{.Name}}\t{{.Component}}\t{{.Version}}\t{{.Ready}}\t{{.Restarts}}\t{{.Status}}\t{{.Age}}"
30 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"
31)
32
33type ComponentList struct {
34 ListOutputOptions
35 Kubectl bool `long:"kubectl" short:"k" description:"display the kubectl command to execute"`
36}
37
38type ComponentOpts struct {
39 List ComponentList `command:"list"`
40}
41
42var componentOpts = ComponentOpts{}
43
44func RegisterComponentCommands(parser *flags.Parser) {
David Bainbridge12f036f2019-10-15 22:09:04 +000045 if _, err := parser.AddCommand("component", "component instance commands", "Commands to query and manipulate VOLTHA component instances", &componentOpts); err != nil {
David Bainbridgea6722342019-10-24 23:55:53 +000046 Error.Fatalf("Unexpected error while attempting to register component commands : %s", err)
David Bainbridge12f036f2019-10-15 22:09:04 +000047 }
Zack Williamse940c7a2019-08-21 14:25:39 -070048}
49
50func (options *ComponentList) Execute(args []string) error {
51
52 ProcessGlobalOptions()
53
54 // If they requested the source to the kubectl command that
55 // can give the same information, then print it and return
56 if options.Kubectl {
57 fmt.Println(COMPONENT_LIST_KUBECTL_CMD)
58 return nil
59 }
60
61 // use the current context in kubeconfig
62 config, err := clientcmd.BuildConfigFromFlags("", GlobalOptions.K8sConfig)
63 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000064 Error.Fatalf("Unable to resolve Kubernetes configuration options: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070065 }
66
67 // create the clientset
68 clientset, err := kubernetes.NewForConfig(config)
69 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000070 Error.Fatalf("Unable to create client context for Kubernetes API connection: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070071 }
72
73 pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{
74 LabelSelector: "app.kubernetes.io/part-of=voltha",
75 })
76 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000077 Error.Fatalf("Unexpected error while attempting to query PODs from Kubernetes: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070078 }
79
80 outputFormat := CharReplacer.Replace(options.Format)
81 if outputFormat == "" {
David Bainbridgea6722342019-10-24 23:55:53 +000082 outputFormat = GetCommandOptionWithDefault("component-list", "format", DEFAULT_COMPONENT_FORMAT)
Zack Williamse940c7a2019-08-21 14:25:39 -070083 }
84 if options.Quiet {
85 outputFormat = "{{.Metadata.Name}}"
86 }
David Bainbridgea6722342019-10-24 23:55:53 +000087 orderBy := options.OrderBy
88 if orderBy == "" {
89 orderBy = GetCommandOptionWithDefault("component-list", "order", "")
90 }
91
Zack Williamse940c7a2019-08-21 14:25:39 -070092 data := make([]model.ComponentInstance, len(pods.Items))
93 for i, item := range pods.Items {
94 data[i].PopulateFrom(item)
95 }
96
97 result := CommandResult{
98 Format: format.Format(outputFormat),
99 Filter: options.Filter,
David Bainbridgea6722342019-10-24 23:55:53 +0000100 OrderBy: orderBy,
Zack Williamse940c7a2019-08-21 14:25:39 -0700101 OutputAs: toOutputType(options.OutputAs),
102 NameLimit: options.NameLimit,
103 Data: data,
104 }
105
106 GenerateOutput(&result)
107 return nil
108}