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" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | const ( |
| 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 | |
| 33 | type ComponentList struct { |
| 34 | ListOutputOptions |
| 35 | Kubectl bool `long:"kubectl" short:"k" description:"display the kubectl command to execute"` |
| 36 | } |
| 37 | |
| 38 | type ComponentOpts struct { |
| 39 | List ComponentList `command:"list"` |
| 40 | } |
| 41 | |
| 42 | var componentOpts = ComponentOpts{} |
| 43 | |
| 44 | func RegisterComponentCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 45 | if _, err := parser.AddCommand("component", "component instance commands", "Commands to query and manipulate VOLTHA component instances", &componentOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 46 | Error.Fatalf("Unexpected error while attempting to register component commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 47 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func (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 Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 64 | Error.Fatalf("Unable to resolve Kubernetes configuration options: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // create the clientset |
| 68 | clientset, err := kubernetes.NewForConfig(config) |
| 69 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 70 | Error.Fatalf("Unable to create client context for Kubernetes API connection: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{ |
| 74 | LabelSelector: "app.kubernetes.io/part-of=voltha", |
| 75 | }) |
| 76 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 77 | Error.Fatalf("Unexpected error while attempting to query PODs from Kubernetes: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | outputFormat := CharReplacer.Replace(options.Format) |
| 81 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 82 | outputFormat = GetCommandOptionWithDefault("component-list", "format", DEFAULT_COMPONENT_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 83 | } |
| 84 | if options.Quiet { |
| 85 | outputFormat = "{{.Metadata.Name}}" |
| 86 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 87 | orderBy := options.OrderBy |
| 88 | if orderBy == "" { |
| 89 | orderBy = GetCommandOptionWithDefault("component-list", "order", "") |
| 90 | } |
| 91 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 92 | 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 Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 100 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 101 | OutputAs: toOutputType(options.OutputAs), |
| 102 | NameLimit: options.NameLimit, |
| 103 | Data: data, |
| 104 | } |
| 105 | |
| 106 | GenerateOutput(&result) |
| 107 | return nil |
| 108 | } |