mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 18:01:21 +00:00
Move status command to kustomize.
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/config"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/create"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/status"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/version"
|
||||
)
|
||||
|
||||
@@ -43,6 +44,7 @@ See https://sigs.k8s.io/kustomize
|
||||
edit.NewCmdEdit(fSys, v, uf),
|
||||
create.NewCmdCreate(fSys, uf),
|
||||
version.NewCmdVersion(stdOut),
|
||||
status.NewCmdStatus(),
|
||||
)
|
||||
if cc := config.NewCmdConfig(fSys); cc != nil {
|
||||
c.AddCommand(cc)
|
||||
|
||||
31
kustomize/internal/commands/status/all.go
Normal file
31
kustomize/internal/commands/status/all.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
//go:generate $GOBIN/mdtogo docs/commands generateddocs/commands --license=none
|
||||
|
||||
package status
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/status/cmd"
|
||||
"sigs.k8s.io/kustomize/kyaml/commandutil"
|
||||
)
|
||||
|
||||
func NewCmdStatus() *cobra.Command {
|
||||
var c = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "[Alpha] Commands for working with resource status.",
|
||||
Hidden: commandutil.GetAlphaEnabled(),
|
||||
}
|
||||
|
||||
if !commandutil.GetAlphaEnabled() {
|
||||
c.Short = "[Alpha] To enable set KUSTOMIZE_ENABLE_ALPHA_COMMANDS=true"
|
||||
c.Long = "[Alpha] To enable set KUSTOMIZE_ENABLE_ALPHA_COMMANDS=true"
|
||||
return c
|
||||
}
|
||||
|
||||
c.AddCommand(cmd.FetchCommand())
|
||||
c.AddCommand(cmd.WaitCommand())
|
||||
c.AddCommand(cmd.EventsCommand())
|
||||
return c
|
||||
}
|
||||
102
kustomize/internal/commands/status/cmd/events.go
Normal file
102
kustomize/internal/commands/status/cmd/events.go
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/status/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
)
|
||||
|
||||
// GetEventsRunner returns a command EventsRunner.
|
||||
func GetEventsRunner() *EventsRunner {
|
||||
r := &EventsRunner{
|
||||
newResolverFunc: newResolver,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "events DIR...",
|
||||
Short: commands.EventsShort,
|
||||
Long: commands.EventsLong,
|
||||
Example: commands.EventsExamples,
|
||||
RunE: r.runE,
|
||||
}
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
c.Flags().DurationVar(&r.Interval, "interval", 2*time.Second,
|
||||
"check every n seconds.")
|
||||
c.Flags().DurationVar(&r.Timeout, "timeout", 60*time.Second,
|
||||
"give up after n seconds.")
|
||||
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
|
||||
func EventsCommand() *cobra.Command {
|
||||
return GetEventsRunner().Command
|
||||
}
|
||||
|
||||
// EventsRunner captures the parameters for the command
|
||||
// and contains the run function.
|
||||
type EventsRunner struct {
|
||||
IncludeSubpackages bool
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
Command *cobra.Command
|
||||
|
||||
newResolverFunc newResolverFunc
|
||||
}
|
||||
|
||||
func (r *EventsRunner) runE(c *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
resolver, err := r.newResolverFunc(r.Interval)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating resolver")
|
||||
}
|
||||
|
||||
// Set up a CaptureIdentifierFilter and run all inputs through the
|
||||
// filter with the pipeline to capture the inventory of resources
|
||||
// which we are interested in.
|
||||
captureFilter := &CaptureIdentifiersFilter{}
|
||||
filters := []kio.Filter{captureFilter}
|
||||
|
||||
var inputs []kio.Reader
|
||||
for _, a := range args {
|
||||
inputs = append(inputs, kio.LocalPackageReader{
|
||||
PackagePath: a,
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
})
|
||||
}
|
||||
if len(inputs) == 0 {
|
||||
inputs = append(inputs, &kio.ByteReader{Reader: c.InOrStdin()})
|
||||
}
|
||||
|
||||
err = kio.Pipeline{
|
||||
Inputs: inputs,
|
||||
Filters: filters,
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error reading manifests")
|
||||
}
|
||||
|
||||
// Create a new printer that knows how to print updates about
|
||||
// resourdes and their aggregate status in the events format.
|
||||
printer := newEventPrinter(c.OutOrStdout(), c.OutOrStderr())
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, r.Timeout)
|
||||
defer cancel()
|
||||
resChannel := resolver.WaitForStatus(ctx, captureFilter.Identifiers)
|
||||
|
||||
// Print events until the channel is closed. This will happen
|
||||
// either because all resources has reached the Current status
|
||||
// or it has timed out.
|
||||
for msg := range resChannel {
|
||||
printer.printEvent(msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
301
kustomize/internal/commands/status/cmd/events_test.go
Normal file
301
kustomize/internal/commands/status/cmd/events_test.go
Normal file
@@ -0,0 +1,301 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
)
|
||||
|
||||
func TestEventsNoResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
fakeClient := &FakeClient{}
|
||||
|
||||
r := GetEventsRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err := r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||
|
||||
if want, got := 1, len(eventOutput.events); want != got {
|
||||
t.Errorf("expected %d events, but got %d", want, got)
|
||||
}
|
||||
|
||||
event := eventOutput.events[0]
|
||||
if want, got := status.CurrentStatus, event.aggStatus; want != got {
|
||||
t.Errorf("expected agg status %s, but got %s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventsMultipleUpdates(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Deployment": createDeploymentStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetEventsRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient, appsv1.SchemeGroupVersion.WithKind("Deployment"))
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||
|
||||
aggStatuses := eventOutput.allAggStatuses()
|
||||
expectedAggStatuses := []status.Status{
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := eventOutput.allResources()
|
||||
if want, got := 1, len(resources); want != got {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
resource := resources[0]
|
||||
resourceStatuses := eventOutput.statusesForResource(resource)
|
||||
expectedResourceStatuses := []status.Status{
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(resourceStatuses, expectedResourceStatuses) {
|
||||
t.Errorf("expected statuses to be %s, but got %s", joinStatuses(expectedResourceStatuses),
|
||||
joinStatuses(resourceStatuses))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventsMultipleResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Pod": createPodStatusFunc(),
|
||||
"Service": createServiceStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetEventsRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient, corev1.SchemeGroupVersion.WithKind("Pod"),
|
||||
corev1.SchemeGroupVersion.WithKind("Service"))
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
eventOutput := parseEventOutput(t, outBuffer.String())
|
||||
|
||||
aggStatuses := eventOutput.allAggStatuses()
|
||||
expectedAggStatuses := []status.Status{
|
||||
status.UnknownStatus,
|
||||
status.CurrentStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := eventOutput.allResources()
|
||||
if want, got := 2, len(resources); got != want {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
for _, resource := range resources {
|
||||
resourceStatuses := eventOutput.statusesForResource(resource)
|
||||
if want, got := status.CurrentStatus, resourceStatuses[len(resourceStatuses)-1]; want != got {
|
||||
t.Errorf("expected resource %q to have final status %s, but got %s", resource.name, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type EventOutput struct {
|
||||
events []EventOutputLine
|
||||
unknownLines []string
|
||||
}
|
||||
|
||||
func (e *EventOutput) allAggStatuses() []status.Status {
|
||||
var aggStatuses []status.Status
|
||||
for _, event := range e.events {
|
||||
aggStatuses = append(aggStatuses, event.aggStatus)
|
||||
}
|
||||
return aggStatuses
|
||||
}
|
||||
|
||||
func (e *EventOutput) allResources() []ResourceIdentifier {
|
||||
var resources []ResourceIdentifier
|
||||
seenResources := make(map[ResourceIdentifier]bool)
|
||||
for _, event := range e.events {
|
||||
if !event.isResourceUpdateEvent() {
|
||||
continue
|
||||
}
|
||||
r := event.identifier
|
||||
if _, found := seenResources[r]; !found {
|
||||
resources = append(resources, r)
|
||||
seenResources[r] = true
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
func (e *EventOutput) statusesForResource(resource ResourceIdentifier) []status.Status {
|
||||
var statuses []status.Status
|
||||
for _, event := range e.events {
|
||||
if !event.isResourceUpdateEvent() {
|
||||
continue
|
||||
}
|
||||
if event.identifier.Equals(resource) {
|
||||
statuses = append(statuses, event.status)
|
||||
}
|
||||
}
|
||||
return statuses
|
||||
}
|
||||
|
||||
type EventOutputLine struct {
|
||||
eventType string
|
||||
aggStatus status.Status
|
||||
identifier ResourceIdentifier
|
||||
status status.Status
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *EventOutputLine) isResourceUpdateEvent() bool {
|
||||
return e.eventType == string(wait.ResourceUpdate)
|
||||
}
|
||||
|
||||
var (
|
||||
eventRegex = regexp.MustCompile(`^\s*` +
|
||||
`(?P<eventType>\S+)\s+` +
|
||||
`(?P<aggStatus>\S+)\s+` +
|
||||
`((?P<resourceType>\S+)\s+` +
|
||||
`(?P<namespace>\S+)\s+` +
|
||||
`(?P<name>\S+)\s+` +
|
||||
`(?P<status>\S+)\s+` +
|
||||
`(?P<message>.*\S)){0,1}` +
|
||||
`\s*$`)
|
||||
eventHeaderRegex = regexp.MustCompile(`^\s*` +
|
||||
`EVENT TYPE\s+` +
|
||||
`AGG STATUS\s+` +
|
||||
`TYPE\s+` +
|
||||
`NAMESPACE\s+` +
|
||||
`NAME\s+` +
|
||||
`STATUS\s+` +
|
||||
`MESSAGE` +
|
||||
`\s*$`)
|
||||
)
|
||||
|
||||
func parseEventOutput(_ *testing.T, output string) EventOutput {
|
||||
var eventOutput EventOutput
|
||||
lines := strings.Split(output, "\n")
|
||||
for _, line := range lines {
|
||||
if len(line) == 0 {
|
||||
continue // Ignore empty lines
|
||||
}
|
||||
match := eventHeaderRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
continue // Ignore headers
|
||||
}
|
||||
match = eventRegex.FindStringSubmatch(line)
|
||||
if match == nil {
|
||||
eventOutput.unknownLines = append(eventOutput.unknownLines, line)
|
||||
continue
|
||||
}
|
||||
|
||||
eventOutputLine := EventOutputLine{
|
||||
eventType: match[1],
|
||||
aggStatus: status.FromStringOrDie(match[2]),
|
||||
}
|
||||
|
||||
if eventOutputLine.eventType == string(wait.ResourceUpdate) {
|
||||
resourceType := match[4]
|
||||
parts := strings.Split(resourceType, "/")
|
||||
var identifier ResourceIdentifier
|
||||
if len(parts) == 2 {
|
||||
identifier.apiVersion = parts[0]
|
||||
identifier.kind = parts[1]
|
||||
} else {
|
||||
identifier.apiVersion = strings.Join(parts[:2], "/")
|
||||
identifier.kind = parts[2]
|
||||
}
|
||||
identifier.namespace = match[5]
|
||||
identifier.name = match[6]
|
||||
eventOutputLine.identifier = identifier
|
||||
eventOutputLine.status = status.FromStringOrDie(match[7])
|
||||
eventOutputLine.message = match[8]
|
||||
}
|
||||
|
||||
eventOutput.events = append(eventOutput.events, eventOutputLine)
|
||||
}
|
||||
return eventOutput
|
||||
}
|
||||
125
kustomize/internal/commands/status/cmd/fetch.go
Normal file
125
kustomize/internal/commands/status/cmd/fetch.go
Normal file
@@ -0,0 +1,125 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/status/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
)
|
||||
|
||||
// GetFetchRunner returns a command FetchRunner.
|
||||
func GetFetchRunner() *FetchRunner {
|
||||
r := &FetchRunner{
|
||||
newResolverFunc: newResolver,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "fetch DIR...",
|
||||
Short: commands.FetchShort,
|
||||
Long: commands.FetchLong,
|
||||
Example: commands.FetchExamples,
|
||||
RunE: r.runE,
|
||||
}
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
|
||||
func FetchCommand() *cobra.Command {
|
||||
return GetFetchRunner().Command
|
||||
}
|
||||
|
||||
// FetchRunner captures the parameters for the command and contains
|
||||
// the run function.
|
||||
type FetchRunner struct {
|
||||
IncludeSubpackages bool
|
||||
Command *cobra.Command
|
||||
|
||||
newResolverFunc newResolverFunc
|
||||
}
|
||||
|
||||
func (r *FetchRunner) runE(c *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
resolver, err := r.newResolverFunc(time.Minute)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating resolver")
|
||||
}
|
||||
|
||||
// Set up a CaptureIdentifierFilter and run all inputs through the
|
||||
// filter with the pipeline to capture the inventory of resources
|
||||
// which we are interested in.
|
||||
captureFilter := &CaptureIdentifiersFilter{}
|
||||
filters := []kio.Filter{captureFilter}
|
||||
|
||||
var inputs []kio.Reader
|
||||
for _, a := range args {
|
||||
inputs = append(inputs, kio.LocalPackageReader{
|
||||
PackagePath: a,
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
})
|
||||
}
|
||||
if len(inputs) == 0 {
|
||||
inputs = append(inputs, &kio.ByteReader{Reader: c.InOrStdin()})
|
||||
}
|
||||
|
||||
err = kio.Pipeline{
|
||||
Inputs: inputs,
|
||||
Filters: filters,
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error reading manifests")
|
||||
}
|
||||
|
||||
// Pass in the inventory of resources to the FetchAndResolve function
|
||||
// on the resolver. It will return the status (or an error) for each
|
||||
// resource in the inventory.
|
||||
results := resolver.FetchAndResolve(ctx, captureFilter.Identifiers)
|
||||
|
||||
// Create new printer that knows how to print resource statuses
|
||||
// in a table format and ask it to print the results.
|
||||
newTablePrinter(FetchStatusInfo{results}, c.OutOrStdout(), c.OutOrStderr(), false).Print()
|
||||
return nil
|
||||
}
|
||||
|
||||
// FetchStatusInfo wraps the results from the FetchAndResolve function
|
||||
// to the format expected in the TablePrinter.
|
||||
type FetchStatusInfo struct {
|
||||
Results []wait.ResourceResult
|
||||
}
|
||||
|
||||
// CurrentStatus returns the latest information known about the
|
||||
// status of each of the resources. For FetchStatusInfo, the result
|
||||
// is never updated, so it just returns the information provided
|
||||
// by the slice of wait.ResourceResult at creation.
|
||||
func (f FetchStatusInfo) CurrentStatus() StatusData {
|
||||
var resourceData []ResourceStatusData
|
||||
for _, res := range f.Results {
|
||||
rsd := ResourceStatusData{
|
||||
Identifier: res.ResourceIdentifier,
|
||||
}
|
||||
if res.Error != nil {
|
||||
rsd.Status = status.UnknownStatus
|
||||
rsd.Message = res.Error.Error()
|
||||
} else {
|
||||
rsd.Status = res.Result.Status
|
||||
rsd.Message = res.Result.Message
|
||||
}
|
||||
|
||||
resourceData = append(resourceData, rsd)
|
||||
}
|
||||
|
||||
return StatusData{
|
||||
AggregateStatus: status.UnknownStatus,
|
||||
ResourceStatuses: resourceData,
|
||||
}
|
||||
}
|
||||
231
kustomize/internal/commands/status/cmd/fetch_test.go
Normal file
231
kustomize/internal/commands/status/cmd/fetch_test.go
Normal file
@@ -0,0 +1,231 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
)
|
||||
|
||||
func TestEmptyManifest(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
fakeClient := fake.NewFakeClientWithScheme(scheme)
|
||||
|
||||
r := GetFetchRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err := r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
output := outBuffer.String()
|
||||
lines := strings.Split(output, "\n")
|
||||
|
||||
if want, got := 2, len(lines); want != got {
|
||||
t.Errorf("Expected %d lines, but got %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchStatusFromManifestStdIn(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
deployment := createDeployment("bar", "default", 42, appsv1.DeploymentStatus{
|
||||
ObservedGeneration: 1,
|
||||
})
|
||||
|
||||
fakeClient := fake.NewFakeClientWithScheme(scheme, deployment)
|
||||
|
||||
r := GetFetchRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient, appsv1.SchemeGroupVersion.WithKind("Deployment"))
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
expectedResource := ResourceIdentifier{
|
||||
apiVersion: "apps",
|
||||
kind: "Deployment",
|
||||
namespace: "default",
|
||||
name: "bar",
|
||||
}
|
||||
expectedStatus := status.InProgressStatus
|
||||
expectedMessage := "Deployment generation is 2, but latest observed generation is 1"
|
||||
|
||||
verifyOutputContains(t, tableOutput, expectedResource, expectedStatus, expectedMessage)
|
||||
}
|
||||
|
||||
//nolint:funlen
|
||||
func TestFetchStatusFromManifestsFiles(t *testing.T) {
|
||||
d, err := ioutil.TempDir("", "status-fetch-test")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(d, "dep.yaml"), []byte(`
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(d, "svc.yaml"), []byte(`
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
`), 0600)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
replicas := int32(42)
|
||||
deployment := createDeployment("foo", "default", replicas, appsv1.DeploymentStatus{
|
||||
ObservedGeneration: 2,
|
||||
Replicas: replicas,
|
||||
ReadyReplicas: replicas,
|
||||
AvailableReplicas: replicas,
|
||||
UpdatedReplicas: replicas,
|
||||
Conditions: []appsv1.DeploymentCondition{
|
||||
{
|
||||
Type: appsv1.DeploymentAvailable,
|
||||
Status: v1.ConditionTrue,
|
||||
},
|
||||
},
|
||||
})
|
||||
service := createService("foo", "default")
|
||||
|
||||
fakeClient := fake.NewFakeClientWithScheme(scheme, deployment, service)
|
||||
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
r := GetFetchRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient, appsv1.SchemeGroupVersion.WithKind("Deployment"),
|
||||
v1.SchemeGroupVersion.WithKind("Service"))
|
||||
r.Command.SetArgs([]string{d})
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
expectedDeploymentResource := ResourceIdentifier{
|
||||
apiVersion: "apps",
|
||||
kind: "Deployment",
|
||||
namespace: "default",
|
||||
name: "foo",
|
||||
}
|
||||
expectedDeploymentStatus := status.CurrentStatus
|
||||
expectedDeploymentMessage := "Deployment is available. Replicas: 42"
|
||||
verifyOutputContains(t, tableOutput, expectedDeploymentResource, expectedDeploymentStatus, expectedDeploymentMessage)
|
||||
|
||||
expectedServiceResource := ResourceIdentifier{
|
||||
apiVersion: "",
|
||||
kind: "Service",
|
||||
namespace: "default",
|
||||
name: "foo",
|
||||
}
|
||||
expectedServiceStatus := status.CurrentStatus
|
||||
expectedServiceMessage := "Service is ready"
|
||||
|
||||
verifyOutputContains(t, tableOutput, expectedServiceResource, expectedServiceStatus, expectedServiceMessage)
|
||||
}
|
||||
|
||||
func createDeployment(name, namespace string, replicas int32, status appsv1.DeploymentStatus) *appsv1.Deployment {
|
||||
return &appsv1.Deployment{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "apps/v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
Generation: 2,
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Replicas: &replicas,
|
||||
},
|
||||
Status: status,
|
||||
}
|
||||
}
|
||||
|
||||
func createService(name, namespace string) *v1.Service {
|
||||
return &v1.Service{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Service",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func verifyOutputContains(t *testing.T, tableOutput TableOutput, resource ResourceIdentifier, status status.Status, message string) {
|
||||
if len(tableOutput.Frames) == 0 {
|
||||
t.Fatalf("expected match for resource %s, but output had no frames", resource.name)
|
||||
}
|
||||
firstFrame := tableOutput.Frames[0]
|
||||
var foundResource ResourceOutput
|
||||
match := false
|
||||
for _, resourceOutput := range firstFrame.Resources {
|
||||
if resourceOutput.identifier.Equals(resource) {
|
||||
foundResource = resourceOutput
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
t.Errorf("expected match for resource %s, but didn't find it", resource.name)
|
||||
}
|
||||
if want, got := status, foundResource.status; want != got {
|
||||
t.Errorf("expected status %s for resource %s, but got %s", want, resource.name, got)
|
||||
}
|
||||
if want, got := message, foundResource.message; !strings.HasPrefix(want, got) {
|
||||
t.Errorf("expected message %s for resource %s, but got %s", want, resource.name, got)
|
||||
}
|
||||
}
|
||||
264
kustomize/internal/commands/status/cmd/helpers_test.go
Normal file
264
kustomize/internal/commands/status/cmd/helpers_test.go
Normal file
@@ -0,0 +1,264 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
)
|
||||
|
||||
type TableOutput struct {
|
||||
Frames []TableOutputFrame
|
||||
UnknownRows []string
|
||||
}
|
||||
|
||||
func (t *TableOutput) allAggStatuses() []status.Status {
|
||||
var statuses []status.Status
|
||||
for _, frame := range t.Frames {
|
||||
if frame.AggregateStatus != "" {
|
||||
statuses = append(statuses, frame.AggregateStatus)
|
||||
}
|
||||
}
|
||||
return statuses
|
||||
}
|
||||
|
||||
func (t *TableOutput) dedupedAggStatuses() []status.Status {
|
||||
var dedupedStatuses []status.Status
|
||||
statuses := t.allAggStatuses()
|
||||
var previousStatus status.Status
|
||||
for _, s := range statuses {
|
||||
if s != previousStatus {
|
||||
dedupedStatuses = append(dedupedStatuses, s)
|
||||
previousStatus = s
|
||||
}
|
||||
}
|
||||
return dedupedStatuses
|
||||
}
|
||||
|
||||
func (t *TableOutput) resources() []ResourceIdentifier {
|
||||
seenResources := make(map[ResourceIdentifier]bool)
|
||||
var resources []ResourceIdentifier
|
||||
for _, frame := range t.Frames {
|
||||
for _, resource := range frame.Resources {
|
||||
r := resource.identifier
|
||||
_, found := seenResources[r]
|
||||
if !found {
|
||||
seenResources[r] = true
|
||||
resources = append(resources, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
func (t *TableOutput) dedupedStatusesForResource(resource ResourceIdentifier) []status.Status {
|
||||
var dedupedStatuses []status.Status
|
||||
var previousStatus status.Status
|
||||
for _, frame := range t.Frames {
|
||||
for _, r := range frame.Resources {
|
||||
if r.identifier.Equals(resource) {
|
||||
if r.status != previousStatus {
|
||||
previousStatus = r.status
|
||||
dedupedStatuses = append(dedupedStatuses, r.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dedupedStatuses
|
||||
}
|
||||
|
||||
type TableOutputFrame struct {
|
||||
AggregateStatus status.Status
|
||||
Resources []ResourceOutput
|
||||
}
|
||||
|
||||
type ResourceIdentifier struct {
|
||||
apiVersion string
|
||||
kind string
|
||||
name string
|
||||
namespace string
|
||||
}
|
||||
|
||||
func (r ResourceIdentifier) Equals(identifier ResourceIdentifier) bool {
|
||||
return r.apiVersion == identifier.apiVersion &&
|
||||
r.kind == identifier.kind &&
|
||||
r.namespace == identifier.namespace &&
|
||||
r.name == identifier.name
|
||||
}
|
||||
|
||||
type ResourceOutput struct {
|
||||
identifier ResourceIdentifier
|
||||
status status.Status
|
||||
message string
|
||||
}
|
||||
|
||||
var (
|
||||
headerRegex = regexp.MustCompile(`^\s*TYPE\s+NAMESPACE\s+NAME\s+STATUS\s+MESSAGE\s*$`)
|
||||
resourceRegex = regexp.MustCompile(`^(?P<resourceType>\S+)\s+(?P<namespace>\S+)\s+(?P<name>\S+)\s+(?P<status>\S+)\s+(?P<message>.*\S)\s*$`)
|
||||
aggStatusRegex = regexp.MustCompile(`^\s*AggregateStatus: (?P<aggregateStatus>\S+)\s*$`)
|
||||
)
|
||||
|
||||
func parseTableOutput(_ *testing.T, output string) TableOutput {
|
||||
tableOutput := TableOutput{}
|
||||
|
||||
lines := strings.Split(output, "\n")
|
||||
|
||||
hasAggStatus := false
|
||||
var currentFrame TableOutputFrame
|
||||
for i, line := range lines {
|
||||
if len(line) == 0 {
|
||||
continue // We don't care about empty lines.
|
||||
}
|
||||
|
||||
// Check for lines with aggregate status. They are not always present, but if they are,
|
||||
// they always start a new frame of output.
|
||||
match := aggStatusRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
hasAggStatus = true
|
||||
if i != 0 {
|
||||
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||
}
|
||||
currentFrame = TableOutputFrame{
|
||||
AggregateStatus: status.FromStringOrDie(match[1]),
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
match = headerRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
if !hasAggStatus {
|
||||
if i != 0 {
|
||||
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||
}
|
||||
currentFrame = TableOutputFrame{}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
match = resourceRegex.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
var identifier ResourceIdentifier
|
||||
resourceType := match[1]
|
||||
parts := strings.Split(resourceType, "/")
|
||||
if len(parts) == 2 {
|
||||
identifier.apiVersion = parts[0]
|
||||
identifier.kind = parts[1]
|
||||
} else {
|
||||
identifier.apiVersion = strings.Join(parts[:2], "/")
|
||||
identifier.kind = parts[2]
|
||||
}
|
||||
identifier.namespace = match[2]
|
||||
identifier.name = match[3]
|
||||
|
||||
res := ResourceOutput{
|
||||
identifier: identifier,
|
||||
}
|
||||
res.status = status.FromStringOrDie(match[4])
|
||||
res.message = match[5]
|
||||
currentFrame.Resources = append(currentFrame.Resources, res)
|
||||
continue
|
||||
}
|
||||
tableOutput.UnknownRows = append(tableOutput.UnknownRows, line)
|
||||
}
|
||||
tableOutput.Frames = append(tableOutput.Frames, currentFrame)
|
||||
return tableOutput
|
||||
}
|
||||
|
||||
func createDeploymentStatusFunc() func(*unstructured.Unstructured) error {
|
||||
metadataMap := map[string]interface{}{
|
||||
"generation": int64(2),
|
||||
}
|
||||
specMap := map[string]interface{}{
|
||||
"replicas": int64(2),
|
||||
}
|
||||
statusMap := map[string]interface{}{
|
||||
"observedGeneration": int64(2),
|
||||
"replicas": int64(4),
|
||||
"updatedReplicas": int64(4),
|
||||
"readyReplicas": int64(4),
|
||||
}
|
||||
var conditions = make([]interface{}, 0)
|
||||
conditions = append(conditions, map[string]interface{}{
|
||||
"type": "Available",
|
||||
"status": "True",
|
||||
})
|
||||
callbackCount := int64(0)
|
||||
return func(deployment *unstructured.Unstructured) error {
|
||||
_ = unstructured.SetNestedMap(deployment.Object, metadataMap, "metadata")
|
||||
_ = unstructured.SetNestedMap(deployment.Object, specMap, "spec")
|
||||
statusMap["availableReplicas"] = callbackCount
|
||||
_ = unstructured.SetNestedMap(deployment.Object, statusMap, "status")
|
||||
_ = unstructured.SetNestedSlice(deployment.Object, conditions, "status", "conditions")
|
||||
callbackCount++
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func createServiceStatusFunc() func(*unstructured.Unstructured) error {
|
||||
return func(*unstructured.Unstructured) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func createPodStatusFunc() func(*unstructured.Unstructured) error {
|
||||
statusMap := map[string]interface{}{
|
||||
"phase": "Succeeded",
|
||||
}
|
||||
return func(pod *unstructured.Unstructured) error {
|
||||
_ = unstructured.SetNestedMap(pod.Object, statusMap, "status")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type ResourceGetCallback func(resource *unstructured.Unstructured) error
|
||||
|
||||
type FakeClient struct {
|
||||
resourceCallbackMap map[string]ResourceGetCallback
|
||||
}
|
||||
|
||||
func (f *FakeClient) Get(_ context.Context, _ client.ObjectKey, obj runtime.Object) error {
|
||||
kind := obj.GetObjectKind().GroupVersionKind().Kind
|
||||
callbackFunc, found := f.resourceCallbackMap[kind]
|
||||
if !found {
|
||||
return fmt.Errorf("no callback func found for kind %s", kind)
|
||||
}
|
||||
u := obj.(*unstructured.Unstructured)
|
||||
return callbackFunc(u)
|
||||
}
|
||||
|
||||
func (f *FakeClient) List(context.Context, runtime.Object, ...client.ListOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fakeResolver(fakeClient client.Reader, mapperTypes ...schema.GroupVersionKind) newResolverFunc {
|
||||
return func(pollInterval time.Duration) (*wait.Resolver, error) {
|
||||
var groupVersions []schema.GroupVersion
|
||||
for _, gvk := range mapperTypes {
|
||||
groupVersions = append(groupVersions, gvk.GroupVersion())
|
||||
}
|
||||
mapper := meta.NewDefaultRESTMapper(groupVersions)
|
||||
for _, gvk := range mapperTypes {
|
||||
mapper.Add(gvk, meta.RESTScopeNamespace)
|
||||
}
|
||||
|
||||
return wait.NewResolver(fakeClient, mapper, pollInterval), nil
|
||||
}
|
||||
}
|
||||
|
||||
func joinStatuses(statuses []status.Status) string {
|
||||
var stringStatuses []string
|
||||
for _, s := range statuses {
|
||||
stringStatuses = append(stringStatuses, s.String())
|
||||
}
|
||||
return strings.Join(stringStatuses, ",")
|
||||
}
|
||||
354
kustomize/internal/commands/status/cmd/print.go
Normal file
354
kustomize/internal/commands/status/cmd/print.go
Normal file
@@ -0,0 +1,354 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
)
|
||||
|
||||
const (
|
||||
typeColumn = "type"
|
||||
namespaceColumn = "namespace"
|
||||
nameColumn = "name"
|
||||
statusColumn = "status"
|
||||
messageColumn = "message"
|
||||
|
||||
RESET = 0
|
||||
ESC = 27
|
||||
RED color = 31
|
||||
GREEN color = 32
|
||||
YELLOW color = 33
|
||||
DEFAULT color = -1 // This is not a valid ANSI escape code. It is used here to mean that no color should be set.
|
||||
)
|
||||
|
||||
type color int
|
||||
|
||||
func moveUp(w io.Writer, lineCount int) {
|
||||
printOrDie(w, "%c[%dA", ESC, lineCount)
|
||||
}
|
||||
|
||||
func eraseCurrentLine(w io.Writer) {
|
||||
printOrDie(w, "%c[2K\r", ESC)
|
||||
}
|
||||
|
||||
type colorFunc func(s status.Status) color
|
||||
type contentFunc func(resource ResourceStatusData) string
|
||||
|
||||
type tableColumnInfo struct {
|
||||
header string
|
||||
width int
|
||||
colorFunc colorFunc
|
||||
contentFunc contentFunc
|
||||
}
|
||||
|
||||
func defaultColorFunc(_ status.Status) color {
|
||||
return DEFAULT
|
||||
}
|
||||
|
||||
var (
|
||||
tableColumns = map[string]tableColumnInfo{
|
||||
typeColumn: {
|
||||
header: "TYPE",
|
||||
width: 25,
|
||||
colorFunc: defaultColorFunc,
|
||||
contentFunc: func(data ResourceStatusData) string {
|
||||
return fmt.Sprintf("%s/%s", data.Identifier.GroupKind.Group, data.Identifier.GroupKind.Kind)
|
||||
},
|
||||
},
|
||||
namespaceColumn: {
|
||||
header: "NAMESPACE",
|
||||
width: 15,
|
||||
colorFunc: defaultColorFunc,
|
||||
contentFunc: func(data ResourceStatusData) string {
|
||||
return data.Identifier.Namespace
|
||||
},
|
||||
},
|
||||
nameColumn: {
|
||||
header: "NAME",
|
||||
width: 20,
|
||||
colorFunc: defaultColorFunc,
|
||||
contentFunc: func(data ResourceStatusData) string {
|
||||
return data.Identifier.Name
|
||||
},
|
||||
},
|
||||
statusColumn: {
|
||||
header: "STATUS",
|
||||
width: 10,
|
||||
colorFunc: colorForStatus,
|
||||
contentFunc: func(data ResourceStatusData) string {
|
||||
return data.Status.String()
|
||||
},
|
||||
},
|
||||
messageColumn: {
|
||||
header: "MESSAGE",
|
||||
width: 40,
|
||||
colorFunc: defaultColorFunc,
|
||||
contentFunc: func(data ResourceStatusData) string {
|
||||
return data.Message
|
||||
},
|
||||
},
|
||||
}
|
||||
tableColumnOrder = []string{typeColumn, namespaceColumn, nameColumn, statusColumn, messageColumn}
|
||||
)
|
||||
|
||||
type StatusInfo interface {
|
||||
CurrentStatus() StatusData
|
||||
}
|
||||
|
||||
type StatusData struct {
|
||||
AggregateStatus status.Status
|
||||
ResourceStatuses []ResourceStatusData
|
||||
}
|
||||
|
||||
type ResourceStatusData struct {
|
||||
Identifier wait.ResourceIdentifier
|
||||
Status status.Status
|
||||
Message string
|
||||
}
|
||||
|
||||
type TablePrinter struct {
|
||||
statusInfo StatusInfo
|
||||
out io.Writer
|
||||
err io.Writer
|
||||
showAggStatus bool
|
||||
}
|
||||
|
||||
func newTablePrinter(statusInfo StatusInfo, out io.Writer, err io.Writer, showAggStatus bool) *TablePrinter {
|
||||
return &TablePrinter{
|
||||
statusInfo: statusInfo,
|
||||
out: out,
|
||||
err: err,
|
||||
showAggStatus: showAggStatus,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TablePrinter) Print() {
|
||||
s.printTable(s.statusInfo.CurrentStatus(), false)
|
||||
}
|
||||
|
||||
func (s *TablePrinter) PrintUntil(stop <-chan struct{}, interval time.Duration) <-chan struct{} {
|
||||
completed := make(chan struct{})
|
||||
s.printTable(s.statusInfo.CurrentStatus(), false)
|
||||
go func() {
|
||||
defer close(completed)
|
||||
ticker := time.NewTicker(interval)
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
ticker.Stop()
|
||||
s.printTable(s.statusInfo.CurrentStatus(), true)
|
||||
return
|
||||
case <-ticker.C:
|
||||
s.printTable(s.statusInfo.CurrentStatus(), true)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return completed
|
||||
}
|
||||
|
||||
func (s *TablePrinter) printTable(data StatusData, deleteUp bool) {
|
||||
if deleteUp {
|
||||
if s.showAggStatus {
|
||||
moveUp(s.out, 1)
|
||||
}
|
||||
moveUp(s.out, 1)
|
||||
moveUp(s.out, len(data.ResourceStatuses))
|
||||
}
|
||||
eraseCurrentLine(s.out)
|
||||
if s.showAggStatus {
|
||||
printOrDie(s.out, "AggregateStatus: ")
|
||||
printWithColorOrDie(s.out, colorForStatus(data.AggregateStatus), "%s\n", data.AggregateStatus)
|
||||
}
|
||||
s.printTableRow(headers())
|
||||
for _, resource := range data.ResourceStatuses {
|
||||
s.printTableRow(row(resource))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TablePrinter) printTableRow(rowData []RowData) {
|
||||
for i, row := range rowData {
|
||||
|
||||
format := fmt.Sprintf("%%-%ds", row.width)
|
||||
printWithColorOrDie(s.out, row.color, format, trimString(row.content, row.width))
|
||||
if i != len(rowData)-1 {
|
||||
printOrDie(s.out, " ")
|
||||
}
|
||||
}
|
||||
printOrDie(s.out, "\n")
|
||||
}
|
||||
|
||||
type RowData struct {
|
||||
content string
|
||||
color color
|
||||
width int
|
||||
}
|
||||
|
||||
func headers() []RowData {
|
||||
var headers []RowData
|
||||
for _, columnName := range tableColumnOrder {
|
||||
column := tableColumns[columnName]
|
||||
headers = append(headers, RowData{
|
||||
content: column.header,
|
||||
color: DEFAULT,
|
||||
width: column.width,
|
||||
})
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
func row(resource ResourceStatusData) []RowData {
|
||||
var row []RowData
|
||||
for _, columnName := range tableColumnOrder {
|
||||
column := tableColumns[columnName]
|
||||
row = append(row, RowData{
|
||||
content: column.contentFunc(resource),
|
||||
color: column.colorFunc(resource.Status),
|
||||
width: column.width,
|
||||
})
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
type eventContentFunc func(wait.Event) string
|
||||
|
||||
type eventColumnInfo struct {
|
||||
header string
|
||||
width int
|
||||
requireResourceUpdateEvent bool
|
||||
contentFunc eventContentFunc
|
||||
}
|
||||
|
||||
var (
|
||||
eventColumns = []eventColumnInfo{
|
||||
{
|
||||
header: "EVENT TYPE",
|
||||
width: 15,
|
||||
requireResourceUpdateEvent: false,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
return string(event.Type)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "AGG STATUS",
|
||||
width: 10,
|
||||
requireResourceUpdateEvent: false,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
return event.AggregateStatus.String()
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "TYPE",
|
||||
width: 20,
|
||||
requireResourceUpdateEvent: true,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
return fmt.Sprintf("%s/%s", event.EventResource.ResourceIdentifier.GroupKind.Group,
|
||||
event.EventResource.ResourceIdentifier.GroupKind.Kind)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "NAMESPACE",
|
||||
width: 15,
|
||||
requireResourceUpdateEvent: true,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
return event.EventResource.ResourceIdentifier.Namespace
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "NAME",
|
||||
width: 20,
|
||||
requireResourceUpdateEvent: true,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
return event.EventResource.ResourceIdentifier.Name
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "STATUS",
|
||||
width: 10,
|
||||
requireResourceUpdateEvent: true,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
return event.EventResource.Status.String()
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "MESSAGE",
|
||||
width: 50,
|
||||
requireResourceUpdateEvent: true,
|
||||
contentFunc: func(event wait.Event) string {
|
||||
if event.EventResource.Error != nil {
|
||||
return event.EventResource.Error.Error()
|
||||
}
|
||||
return event.EventResource.Message
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
type EventPrinter struct {
|
||||
out io.Writer
|
||||
err io.Writer
|
||||
}
|
||||
|
||||
func newEventPrinter(out io.Writer, err io.Writer) *EventPrinter {
|
||||
for _, column := range eventColumns {
|
||||
format := fmt.Sprintf("%%-%ds ", column.width)
|
||||
printOrDie(out, format, column.header)
|
||||
}
|
||||
printOrDie(out, "\n")
|
||||
return &EventPrinter{
|
||||
out: out,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EventPrinter) printEvent(event wait.Event) {
|
||||
for _, column := range eventColumns {
|
||||
if event.Type != wait.ResourceUpdate && column.requireResourceUpdateEvent {
|
||||
continue
|
||||
}
|
||||
format := fmt.Sprintf("%%-%ds ", column.width)
|
||||
printOrDie(e.out, format, trimString(column.contentFunc(event), column.width))
|
||||
}
|
||||
printOrDie(e.out, "\n")
|
||||
}
|
||||
|
||||
func printOrDie(w io.Writer, format string, a ...interface{}) {
|
||||
_, err := fmt.Fprintf(w, format, a...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func printWithColorOrDie(w io.Writer, color color, format string, a ...interface{}) {
|
||||
if color == DEFAULT {
|
||||
printOrDie(w, format, a...)
|
||||
} else {
|
||||
printOrDie(w, "%c[%dm", ESC, color)
|
||||
printOrDie(w, format, a...)
|
||||
printOrDie(w, "%c[%dm", ESC, RESET)
|
||||
}
|
||||
}
|
||||
|
||||
func colorForStatus(s status.Status) color {
|
||||
switch s {
|
||||
case status.CurrentStatus:
|
||||
return GREEN
|
||||
case status.InProgressStatus:
|
||||
return YELLOW
|
||||
case status.FailedStatus:
|
||||
return RED
|
||||
}
|
||||
return DEFAULT
|
||||
}
|
||||
|
||||
func trimString(str string, maxLength int) string {
|
||||
if len(str) <= maxLength {
|
||||
return str
|
||||
}
|
||||
return str[:maxLength]
|
||||
}
|
||||
83
kustomize/internal/commands/status/cmd/util.go
Normal file
83
kustomize/internal/commands/status/cmd/util.go
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
var (
|
||||
scheme = runtime.NewScheme()
|
||||
)
|
||||
|
||||
func init() {
|
||||
_ = clientgoscheme.AddToScheme(scheme)
|
||||
}
|
||||
|
||||
type newResolverFunc func(pollInterval time.Duration) (*wait.Resolver, error)
|
||||
|
||||
// newResolver returns a new resolver that can resolve status for resources based
|
||||
// on polling the cluster.
|
||||
func newResolver(pollInterval time.Duration) (*wait.Resolver, error) {
|
||||
config := ctrl.GetConfigOrDie()
|
||||
mapper, err := apiutil.NewDiscoveryRESTMapper(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c, err := client.New(config, client.Options{Scheme: scheme, Mapper: mapper})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return wait.NewResolver(c, mapper, pollInterval), nil
|
||||
}
|
||||
|
||||
// CaptureIdentifiersFilter implements the Filter interface in the kio package. It
|
||||
// captures the identifiers for all resources passed through the pipeline.
|
||||
type CaptureIdentifiersFilter struct {
|
||||
Identifiers []wait.ResourceIdentifier
|
||||
}
|
||||
|
||||
var _ kio.Filter = &CaptureIdentifiersFilter{}
|
||||
|
||||
func (f *CaptureIdentifiersFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
for i := range slice {
|
||||
meta, err := slice[i].GetMeta()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO(mortent): Update kyaml library
|
||||
id := meta.GetIdentifier()
|
||||
gv, err := schema.ParseGroupVersion(id.APIVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if IsValidKubernetesResource(id) {
|
||||
f.Identifiers = append(f.Identifiers, wait.ResourceIdentifier{
|
||||
Name: id.Name,
|
||||
Namespace: id.Namespace,
|
||||
GroupKind: schema.GroupKind{
|
||||
Group: gv.Group,
|
||||
Kind: id.Kind,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
return slice, nil
|
||||
}
|
||||
|
||||
func IsValidKubernetesResource(id yaml.ResourceIdentifier) bool {
|
||||
return id.GetKind() != "" && id.GetAPIVersion() != "" && id.GetName() != ""
|
||||
}
|
||||
41
kustomize/internal/commands/status/cmd/util_test.go
Normal file
41
kustomize/internal/commands/status/cmd/util_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
func TestIsValidKubernetesResource(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
data yaml.ResourceIdentifier
|
||||
expected bool
|
||||
}{
|
||||
"invalid resource": {
|
||||
data: yaml.ResourceIdentifier{
|
||||
Name: "",
|
||||
APIVersion: "",
|
||||
Kind: "",
|
||||
Namespace: "",
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
"valid resource": {
|
||||
data: yaml.ResourceIdentifier{
|
||||
Name: "SomeName",
|
||||
APIVersion: "SomeVersion",
|
||||
Kind: "SomeKind",
|
||||
Namespace: "",
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for tn, tc := range testCases {
|
||||
t.Run(tn, func(t *testing.T) {
|
||||
assert.Equal(t, IsValidKubernetesResource(tc.data), tc.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
203
kustomize/internal/commands/status/cmd/wait.go
Normal file
203
kustomize/internal/commands/status/cmd/wait.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
"sigs.k8s.io/kustomize/kstatus/wait"
|
||||
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/status/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
)
|
||||
|
||||
// GetWaitRunner return a command WaitRunner.
|
||||
func GetWaitRunner() *WaitRunner {
|
||||
r := &WaitRunner{
|
||||
newResolverFunc: newResolver,
|
||||
}
|
||||
c := &cobra.Command{
|
||||
Use: "wait DIR...",
|
||||
Short: commands.WaitShort,
|
||||
Long: commands.WaitLong,
|
||||
Example: commands.WaitExamples,
|
||||
RunE: r.runE,
|
||||
}
|
||||
c.Flags().BoolVar(&r.IncludeSubpackages, "include-subpackages", true,
|
||||
"also print resources from subpackages.")
|
||||
c.Flags().DurationVar(&r.Interval, "interval", 2*time.Second,
|
||||
"check every n seconds. Default is every 2 seconds.")
|
||||
c.Flags().DurationVar(&r.Timeout, "timeout", 60*time.Second,
|
||||
"give up after n seconds. Default is 60 seconds.")
|
||||
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
|
||||
func WaitCommand() *cobra.Command {
|
||||
return GetWaitRunner().Command
|
||||
}
|
||||
|
||||
// WaitRunner captures the parameters for the command and contains
|
||||
// the run function.
|
||||
type WaitRunner struct {
|
||||
IncludeSubpackages bool
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
Command *cobra.Command
|
||||
|
||||
newResolverFunc newResolverFunc
|
||||
}
|
||||
|
||||
// runE implements the logic of the command and will call the Wait command in the wait
|
||||
// package, use a ResourceStatusCollector to capture the events from the channel, and the
|
||||
// TablePrinter to display the information.
|
||||
func (r *WaitRunner) runE(c *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
resolver, err := r.newResolverFunc(r.Interval)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "errors creating resolver")
|
||||
}
|
||||
|
||||
captureFilter := &CaptureIdentifiersFilter{}
|
||||
filters := []kio.Filter{captureFilter}
|
||||
|
||||
var inputs []kio.Reader
|
||||
for _, a := range args {
|
||||
inputs = append(inputs, kio.LocalPackageReader{
|
||||
PackagePath: a,
|
||||
IncludeSubpackages: r.IncludeSubpackages,
|
||||
})
|
||||
}
|
||||
if len(inputs) == 0 {
|
||||
inputs = append(inputs, &kio.ByteReader{Reader: c.InOrStdin()})
|
||||
}
|
||||
|
||||
err = kio.Pipeline{
|
||||
Inputs: inputs,
|
||||
Filters: filters,
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error reading manifests")
|
||||
}
|
||||
|
||||
collector := newResourceStatusCollector(captureFilter.Identifiers)
|
||||
|
||||
stop := make(chan struct{})
|
||||
printer := newTablePrinter(CollectorStatusInfo{collector}, c.OutOrStdout(), c.OutOrStderr(), true)
|
||||
printFinished := printer.PrintUntil(stop, 1*time.Second)
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, r.Timeout)
|
||||
defer cancel()
|
||||
resChannel := resolver.WaitForStatus(ctx, captureFilter.Identifiers)
|
||||
|
||||
for msg := range resChannel {
|
||||
switch msg.Type {
|
||||
case wait.ResourceUpdate:
|
||||
collector.updateResourceStatus(msg)
|
||||
case wait.Aborted:
|
||||
collector.updateAggregateStatus(msg.AggregateStatus)
|
||||
case wait.Completed:
|
||||
collector.updateAggregateStatus(msg.AggregateStatus)
|
||||
}
|
||||
}
|
||||
close(stop)
|
||||
<-printFinished // Wait for printer to finish work.
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResourceStatusCollector captures the latest state seen for all resources
|
||||
// based on the events from the Wait channel. This is used by the TablePrinter
|
||||
// to display status for all resources.
|
||||
type ResourceStatusCollector struct {
|
||||
mux sync.RWMutex
|
||||
|
||||
AggregateStatus status.Status
|
||||
ResourceStatuses []*ResourceStatus
|
||||
}
|
||||
|
||||
// updateResourceStatus takes the given event and update the status info
|
||||
// in the ResourceStatusCollector.
|
||||
func (r *ResourceStatusCollector) updateResourceStatus(msg wait.Event) {
|
||||
r.mux.Lock()
|
||||
defer r.mux.Unlock()
|
||||
r.AggregateStatus = msg.AggregateStatus
|
||||
eventResource := msg.EventResource
|
||||
for _, resourceState := range r.ResourceStatuses {
|
||||
if resourceState.Identifier.GroupKind == eventResource.ResourceIdentifier.GroupKind &&
|
||||
resourceState.Identifier.Namespace == eventResource.ResourceIdentifier.Namespace &&
|
||||
resourceState.Identifier.Name == eventResource.ResourceIdentifier.Name {
|
||||
resourceState.Status = eventResource.Status
|
||||
resourceState.Message = eventResource.Message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// updateAggregateStatus sets the aggregate status of the ResourceStatusCollector to the
|
||||
// given value.
|
||||
func (r *ResourceStatusCollector) updateAggregateStatus(aggregateStatus status.Status) {
|
||||
r.mux.Lock()
|
||||
defer r.mux.Unlock()
|
||||
r.AggregateStatus = aggregateStatus
|
||||
}
|
||||
|
||||
// ResourceStatus contains the status information for a single resource.
|
||||
type ResourceStatus struct {
|
||||
Identifier wait.ResourceIdentifier
|
||||
Status status.Status
|
||||
Message string
|
||||
}
|
||||
|
||||
// newResourceStatusCollector creates a new ResourceStatusCollector with the given
|
||||
// resources and sets the status for all of them to Unknown.
|
||||
func newResourceStatusCollector(identifiers []wait.ResourceIdentifier) *ResourceStatusCollector {
|
||||
var statuses []*ResourceStatus
|
||||
|
||||
for _, id := range identifiers {
|
||||
statuses = append(statuses, &ResourceStatus{
|
||||
Identifier: id,
|
||||
Status: status.UnknownStatus,
|
||||
Message: "",
|
||||
})
|
||||
}
|
||||
|
||||
return &ResourceStatusCollector{
|
||||
AggregateStatus: status.UnknownStatus,
|
||||
ResourceStatuses: statuses,
|
||||
}
|
||||
}
|
||||
|
||||
// CollectorStatusInfo is a wrapper around the ResourceStatusCollector
|
||||
// to make it adhere to the interface of the TableWriter.
|
||||
type CollectorStatusInfo struct {
|
||||
Collector *ResourceStatusCollector
|
||||
}
|
||||
|
||||
// CurrentStatus implements the interface for the TableWriter and
|
||||
// returns a copy of the current status of the resources in the
|
||||
// ResourceStatusCollector. This is done to make sure the TableWriter
|
||||
// does not have to deal with synchronization when accessing the data.
|
||||
func (f CollectorStatusInfo) CurrentStatus() StatusData {
|
||||
f.Collector.mux.RLock()
|
||||
defer f.Collector.mux.RUnlock()
|
||||
|
||||
var resourceData []ResourceStatusData
|
||||
for _, res := range f.Collector.ResourceStatuses {
|
||||
resourceData = append(resourceData, ResourceStatusData{
|
||||
Identifier: res.Identifier,
|
||||
Status: res.Status,
|
||||
Message: res.Message,
|
||||
})
|
||||
}
|
||||
|
||||
return StatusData{
|
||||
AggregateStatus: f.Collector.AggregateStatus,
|
||||
ResourceStatuses: resourceData,
|
||||
}
|
||||
}
|
||||
179
kustomize/internal/commands/status/cmd/wait_test.go
Normal file
179
kustomize/internal/commands/status/cmd/wait_test.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"sigs.k8s.io/kustomize/kstatus/status"
|
||||
)
|
||||
|
||||
func TestWaitNoResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
fakeClient := &FakeClient{}
|
||||
|
||||
r := GetWaitRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient)
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err := r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
if want, got := 2, len(tableOutput.Frames); want != got {
|
||||
t.Errorf("expected %d frames, but found %d", want, got)
|
||||
}
|
||||
|
||||
aggStatuses := tableOutput.allAggStatuses()
|
||||
expectedAggStatuses := []status.Status{
|
||||
status.UnknownStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedAggStatuses) {
|
||||
t.Errorf("expected agg statuses to be %s, but got %s", joinStatuses(expectedAggStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := tableOutput.resources()
|
||||
if want, got := 0, len(resources); want != got {
|
||||
t.Errorf("expected %d resources, but found %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitMultipleUpdates(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Deployment": createDeploymentStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetWaitRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient, appsv1.SchemeGroupVersion.WithKind("Deployment"))
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
aggStatuses := tableOutput.dedupedAggStatuses()
|
||||
expectedStatuses := []status.Status{
|
||||
status.UnknownStatus,
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(aggStatuses, expectedStatuses) {
|
||||
t.Errorf("expected deduped agg statuses to be %s, but got %s", joinStatuses(expectedStatuses),
|
||||
joinStatuses(aggStatuses))
|
||||
}
|
||||
|
||||
resources := tableOutput.resources()
|
||||
if want, got := 1, len(resources); got != want {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
resource := resources[0]
|
||||
resourceStatuses := tableOutput.dedupedStatusesForResource(resource)
|
||||
expectedResourceStatuses := []status.Status{
|
||||
status.InProgressStatus,
|
||||
status.CurrentStatus,
|
||||
}
|
||||
if !reflect.DeepEqual(expectedResourceStatuses, resourceStatuses) {
|
||||
t.Errorf("expected resource %q to have statuses %s, but got %s", resource.name,
|
||||
joinStatuses(expectedResourceStatuses), joinStatuses(resourceStatuses))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitMultipleResources(t *testing.T) {
|
||||
inBuffer := &bytes.Buffer{}
|
||||
outBuffer := &bytes.Buffer{}
|
||||
|
||||
_, err := fmt.Fprint(inBuffer, `
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: foo
|
||||
namespace: default
|
||||
- apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: bar
|
||||
namespace: default
|
||||
`)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient := &FakeClient{
|
||||
resourceCallbackMap: map[string]ResourceGetCallback{
|
||||
"Pod": createPodStatusFunc(),
|
||||
"Service": createServiceStatusFunc(),
|
||||
},
|
||||
}
|
||||
|
||||
r := GetWaitRunner()
|
||||
r.newResolverFunc = fakeResolver(fakeClient, corev1.SchemeGroupVersion.WithKind("Pod"),
|
||||
corev1.SchemeGroupVersion.WithKind("Service"))
|
||||
r.Command.SetArgs([]string{})
|
||||
r.Command.SetIn(inBuffer)
|
||||
r.Command.SetOut(outBuffer)
|
||||
|
||||
err = r.Command.Execute()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
cleanOutput := stripansi.Strip(outBuffer.String())
|
||||
tableOutput := parseTableOutput(t, cleanOutput)
|
||||
|
||||
aggStatuses := tableOutput.dedupedAggStatuses()
|
||||
if want, got := status.CurrentStatus, aggStatuses[len(aggStatuses)-1]; want != got {
|
||||
t.Errorf("expected final agg statuses to be %s, but got %s", want, got)
|
||||
}
|
||||
|
||||
resources := tableOutput.resources()
|
||||
if want, got := 2, len(resources); got != want {
|
||||
t.Errorf("expected %d resource, but got %d", want, got)
|
||||
}
|
||||
|
||||
for _, resource := range resources {
|
||||
resourceStatuses := tableOutput.dedupedStatusesForResource(resource)
|
||||
if want, got := status.CurrentStatus, resourceStatuses[len(resourceStatuses)-1]; want != got {
|
||||
t.Errorf("expected resource %q to have final status %s, but got %s", resource.name, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
kustomize/internal/commands/status/docs/commands/events.md
Normal file
22
kustomize/internal/commands/status/docs/commands/events.md
Normal file
@@ -0,0 +1,22 @@
|
||||
## events
|
||||
|
||||
[Alpha] Poll the cluster until all provided resources have become Current and list the status change events.
|
||||
|
||||
### Synopsis
|
||||
|
||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||
Current or the timeout is reached. The output will be status change events.
|
||||
|
||||
The list of resources which should be polled are provided as manifests either on the filesystem or
|
||||
on StdIn.
|
||||
|
||||
DIR:
|
||||
Path to local directory. If not provided, input is expected on StdIn.
|
||||
|
||||
### Examples
|
||||
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status events my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status events --timeout=5m
|
||||
21
kustomize/internal/commands/status/docs/commands/fetch.md
Normal file
21
kustomize/internal/commands/status/docs/commands/fetch.md
Normal file
@@ -0,0 +1,21 @@
|
||||
## fetch
|
||||
|
||||
[Alpha] Fetch the state of the provided resources from the cluster and display status in a table.
|
||||
|
||||
### Synopsis
|
||||
|
||||
[Alpha] Fetches the state of all provided resources from the cluster and displays the status in
|
||||
a table.
|
||||
|
||||
The list of resources are provided as manifests either on the filesystem or on StdIn.
|
||||
|
||||
DIR:
|
||||
Path to local directory.
|
||||
|
||||
### Examples
|
||||
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status fetch my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status fetch
|
||||
22
kustomize/internal/commands/status/docs/commands/wait.md
Normal file
22
kustomize/internal/commands/status/docs/commands/wait.md
Normal file
@@ -0,0 +1,22 @@
|
||||
## wait
|
||||
|
||||
[Alpha] Poll the cluster until all provided resources have become Current and display progress in a table.
|
||||
|
||||
### Synopsis
|
||||
|
||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||
Current or the timeout is reached. The output will be presented as a table.
|
||||
|
||||
The list of resources which should be polled are provided as manifests either on the filesystem or
|
||||
on StdIn.
|
||||
|
||||
DIR:
|
||||
Path to local directory. If not provided, input is expected on StdIn.
|
||||
|
||||
### Examples
|
||||
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status wait my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status wait --timeout=5m
|
||||
@@ -0,0 +1,55 @@
|
||||
// Code generated by "mdtogo"; DO NOT EDIT.
|
||||
package commands
|
||||
|
||||
var EventsShort = `[Alpha] Poll the cluster until all provided resources have become Current and list the status change events.`
|
||||
var EventsLong = `
|
||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||
Current or the timeout is reached. The output will be status change events.
|
||||
|
||||
The list of resources which should be polled are provided as manifests either on the filesystem or
|
||||
on StdIn.
|
||||
|
||||
DIR:
|
||||
Path to local directory. If not provided, input is expected on StdIn.
|
||||
`
|
||||
var EventsExamples = `
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status events my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status events --timeout=5m`
|
||||
|
||||
var FetchShort = `[Alpha] Fetch the state of the provided resources from the cluster and display status in a table.`
|
||||
var FetchLong = `
|
||||
[Alpha] Fetches the state of all provided resources from the cluster and displays the status in
|
||||
a table.
|
||||
|
||||
The list of resources are provided as manifests either on the filesystem or on StdIn.
|
||||
|
||||
DIR:
|
||||
Path to local directory.
|
||||
`
|
||||
var FetchExamples = `
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status fetch my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status fetch`
|
||||
|
||||
var WaitShort = `[Alpha] Poll the cluster until all provided resources have become Current and display progress in a table. `
|
||||
var WaitLong = `
|
||||
[Alpha] Poll the cluster for the state of all the provided resources until either they have all become
|
||||
Current or the timeout is reached. The output will be presented as a table.
|
||||
|
||||
The list of resources which should be polled are provided as manifests either on the filesystem or
|
||||
on StdIn.
|
||||
|
||||
DIR:
|
||||
Path to local directory. If not provided, input is expected on StdIn.
|
||||
`
|
||||
var WaitExamples = `
|
||||
# Read resources from the filesystem and wait up to 1 minute for all of them to become Current
|
||||
resource status wait my-dir/
|
||||
|
||||
# Fetch all resources in the cluster and wait up to 5 minutes for all of them to become Current
|
||||
kubectl get all --all-namespaces -oyaml | resource status wait --timeout=5m`
|
||||
Reference in New Issue
Block a user