From d8eff5cf72b39713ef25441410c7c7d775a38df1 Mon Sep 17 00:00:00 2001 From: Dmitriy Kalinin Date: Tue, 7 Jan 2020 12:26:27 -0500 Subject: [PATCH] print targeted cluster --- pkg/kapp/cmd/core/deps_factory.go | 56 +++++++++++++++++++++++++++++-- pkg/kapp/cmd/kapp.go | 2 +- test/e2e/order_test.go | 14 ++++---- test/e2e/template_test.go | 4 +-- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/pkg/kapp/cmd/core/deps_factory.go b/pkg/kapp/cmd/core/deps_factory.go index 94617f368..6b7e6d3c2 100644 --- a/pkg/kapp/cmd/core/deps_factory.go +++ b/pkg/kapp/cmd/core/deps_factory.go @@ -2,9 +2,13 @@ package core import ( "fmt" + "sync" + "github.com/cppforlife/go-cli-ui/ui" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" ) type DepsFactory interface { @@ -13,13 +17,15 @@ type DepsFactory interface { } type DepsFactoryImpl struct { - configFactory ConfigFactory + configFactory ConfigFactory + ui ui.UI + printTargetOnce *sync.Once } var _ DepsFactory = &DepsFactoryImpl{} -func NewDepsFactoryImpl(configFactory ConfigFactory) *DepsFactoryImpl { - return &DepsFactoryImpl{configFactory} +func NewDepsFactoryImpl(configFactory ConfigFactory, ui ui.UI) *DepsFactoryImpl { + return &DepsFactoryImpl{configFactory, ui, &sync.Once{}} } func (f *DepsFactoryImpl) DynamicClient() (dynamic.Interface, error) { @@ -37,6 +43,8 @@ func (f *DepsFactoryImpl) DynamicClient() (dynamic.Interface, error) { return nil, fmt.Errorf("Building Dynamic clientset: %s", err) } + f.printTarget(config) + return clientset, nil } @@ -54,5 +62,47 @@ func (f *DepsFactoryImpl) CoreClient() (kubernetes.Interface, error) { return nil, fmt.Errorf("Building Core clientset: %s", err) } + f.printTarget(config) + return clientset, nil } + +func (f *DepsFactoryImpl) printTarget(config *rest.Config) { + f.printTargetOnce.Do(func() { + nodesDesc := f.summarizeNodes(config) + if len(nodesDesc) > 0 { + nodesDesc = fmt.Sprintf(" (nodes: %s)", nodesDesc) + } + f.ui.PrintLinef("Target cluster '%s'%s", config.Host, nodesDesc) + }) +} + +func (f *DepsFactoryImpl) summarizeNodes(config *rest.Config) string { + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + return "" + } + + nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{}) + if err != nil { + return "" + } + + if len(nodes.Items) == 0 { + return "" + } + + oldestNode := nodes.Items[0] + for _, node := range nodes.Items { + if node.CreationTimestamp.Before(&oldestNode.CreationTimestamp) { + oldestNode = node + } + } + + desc := oldestNode.Name + if len(nodes.Items) > 1 { + desc += fmt.Sprintf(", %d+", len(nodes.Items)) + } + + return desc +} diff --git a/pkg/kapp/cmd/kapp.go b/pkg/kapp/cmd/kapp.go index fc89b4e00..6286833dc 100644 --- a/pkg/kapp/cmd/kapp.go +++ b/pkg/kapp/cmd/kapp.go @@ -36,7 +36,7 @@ func NewKappOptions(ui *ui.ConfUI, configFactory cmdcore.ConfigFactory, func NewDefaultKappCmd(ui *ui.ConfUI) *cobra.Command { configFactory := cmdcore.NewConfigFactoryImpl() - depsFactory := cmdcore.NewDepsFactoryImpl(configFactory) + depsFactory := cmdcore.NewDepsFactoryImpl(configFactory, ui) options := NewKappOptions(ui, configFactory, depsFactory) flagsFactory := cmdcore.NewFlagsFactory(configFactory, depsFactory) return NewKappCmd(options, flagsFactory) diff --git a/test/e2e/order_test.go b/test/e2e/order_test.go index 3567b92a9..028ccbb39 100644 --- a/test/e2e/order_test.go +++ b/test/e2e/order_test.go @@ -75,9 +75,8 @@ metadata: out, _ := kapp.RunWithOpts([]string{"deploy", "--tty", "-f", "-", "-a", name}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)}) - out = strings.TrimSpace(replaceSpaces(replaceTs(out))) - expectedOutput := strings.TrimSpace(replaceSpaces(` -Changes + out = strings.TrimSpace(replaceSpaces(replaceTarget(replaceTs(out)))) + expectedOutput := strings.TrimSpace(replaceSpaces(`Changes Namespace Name Kind Conds. Age Op Wait to Rs Ri $ kapp-test app ConfigMap - - create reconcile - - $ @@ -180,9 +179,8 @@ metadata: out, _ := kapp.RunWithOpts([]string{"deploy", "--tty", "-f", "-", "-a", name}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml)}) - out = strings.TrimSpace(replaceSpaces(replaceAgeStr(replaceTs(out)))) - expectedOutput := strings.TrimSpace(replaceSpaces(` -Changes + out = strings.TrimSpace(replaceSpaces(replaceAgeStr(replaceTarget(replaceTs(out))))) + expectedOutput := strings.TrimSpace(replaceSpaces(`Changes Namespace Name Kind Conds. Age Op Wait to Rs Ri $ kapp-test app-config2 ConfigMap - - create reconcile - - $ @@ -216,6 +214,10 @@ func replaceTs(result string) string { return regexp.MustCompile("\\d{1,2}:\\d{1,2}:\\d{1,2}(AM|PM)").ReplaceAllString(result, "") } +func replaceTarget(result string) string { + return regexp.MustCompile("Target cluster .+\n").ReplaceAllString(result, "") +} + func replaceAgeStr(result string) string { return regexp.MustCompile("\\d+(s|m|h)\\s+").ReplaceAllString(result, " ") } diff --git a/test/e2e/template_test.go b/test/e2e/template_test.go index 4e01e5a9e..47687551e 100644 --- a/test/e2e/template_test.go +++ b/test/e2e/template_test.go @@ -267,8 +267,8 @@ func checkChangesOutput(t *testing.T, actualOutput, expectedOutput string) { replaceAnns := regexp.MustCompile("kapp\\.k14s\\.io\\/(app|association): .+") actualOutput = replaceAnns.ReplaceAllString(actualOutput, "-replaced-") - actualOutput = strings.TrimSpace(strings.Split(actualOutput, "Changes")[0]) - expectedOutput = strings.TrimSpace(strings.Split(expectedOutput, "Changes")[0]) + actualOutput = strings.TrimSpace(strings.Split(replaceTarget(actualOutput), "Changes")[0]) + expectedOutput = strings.TrimSpace(expectedOutput) // Useful for debugging: // printLines("actual", actualOutput)