Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 20 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltctl/pkg/format" |
| 22 | "github.com/opencord/voltctl/pkg/model" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 23 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | "k8s.io/client-go/kubernetes" |
| 25 | "k8s.io/client-go/tools/clientcmd" |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 26 | "log" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const ( |
| 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 | |
| 34 | type ComponentList struct { |
| 35 | ListOutputOptions |
| 36 | Kubectl bool `long:"kubectl" short:"k" description:"display the kubectl command to execute"` |
| 37 | } |
| 38 | |
| 39 | type ComponentOpts struct { |
| 40 | List ComponentList `command:"list"` |
| 41 | } |
| 42 | |
| 43 | var componentOpts = ComponentOpts{} |
| 44 | |
| 45 | func RegisterComponentCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 46 | 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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func (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 | } |