Skip to content

Commit

Permalink
print targeted cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Jan 7, 2020
1 parent 5d262cc commit d8eff5c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
56 changes: 53 additions & 3 deletions pkg/kapp/cmd/core/deps_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
}

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/kapp/cmd/kapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions test/e2e/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 - - $
Expand Down Expand Up @@ -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 - - $
Expand Down Expand Up @@ -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, "<replaced>")
}

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, "<replaced> ")
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d8eff5c

Please sign in to comment.