kops get should print additional objects, if they are present

If the user has added additional objects, we should print them in the
full object dump.
This commit is contained in:
justinsb 2021-09-06 16:07:05 -04:00
parent 4f842121d8
commit 39c187ad86
3 changed files with 71 additions and 7 deletions

2
cmd/kops/BUILD.bazel generated
View File

@ -28,6 +28,7 @@ go_library(
"export_kubeconfig.go",
"gen_cli_docs.go",
"get.go",
"get_addons.go",
"get_assets.go",
"get_cluster.go",
"get_instancegroups.go",
@ -114,6 +115,7 @@ go_library(
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",

View File

@ -25,6 +25,7 @@ import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
@ -128,25 +129,39 @@ func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options
instancegroups = append(instancegroups, &igList.Items[i])
}
var obj []runtime.Object
var addonObjects []*unstructured.Unstructured
{
addons, err := client.AddonsFor(cluster).List()
if err != nil {
return err
}
for _, addon := range addons {
addonObjects = append(addonObjects, addon.ToUnstructured())
}
}
var allObjects []runtime.Object
if options.Output != OutputTable {
obj = append(obj, cluster)
allObjects = append(allObjects, cluster)
for _, group := range instancegroups {
obj = append(obj, group)
allObjects = append(allObjects, group)
}
for _, additionalObject := range addonObjects {
allObjects = append(allObjects, additionalObject)
}
}
switch options.Output {
case OutputYaml:
if err := fullOutputYAML(out, obj...); err != nil {
return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
if err := fullOutputYAML(out, allObjects...); err != nil {
return fmt.Errorf("error writing yaml to stdout: %v", err)
}
return nil
case OutputJSON:
if err := fullOutputJSON(out, obj...); err != nil {
return fmt.Errorf("error writing cluster json to stdout: %v", err)
if err := fullOutputJSON(out, allObjects...); err != nil {
return fmt.Errorf("error writing json to stdout: %v", err)
}
return nil
@ -161,6 +176,13 @@ func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options
if err != nil {
return err
}
if len(addonObjects) != 0 {
fmt.Fprintf(out, "\nAddon Objects\n")
err = addonsOutputTable(cluster, addonObjects, out)
if err != nil {
return err
}
}
default:
return fmt.Errorf("Unknown output format: %q", options.Output)

40
cmd/kops/get_addons.go Normal file
View File

@ -0,0 +1,40 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/util/pkg/tables"
)
func addonsOutputTable(cluster *api.Cluster, addons []*unstructured.Unstructured, out io.Writer) error {
t := &tables.Table{}
t.AddColumn("NAME", func(o *unstructured.Unstructured) string {
return o.GetName()
})
t.AddColumn("KIND", func(o *unstructured.Unstructured) string {
return o.GroupVersionKind().Kind
})
t.AddColumn("VERSION", func(o *unstructured.Unstructured) string {
s, _, _ := unstructured.NestedString(o.Object, "spec", "version")
return s
})
return t.Render(addons, out, "NAME", "KIND", "VERSION")
}