Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions commands/live/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,27 @@ func runApply(r *Runner, invInfo inventory.Info, objs []*unstructured.Unstructur
// Print the preview strategy unless the output format is json.
if dryRunStrategy.ClientOrServerDryRun() && r.output != printers.JSONPrinter {
if dryRunStrategy.ServerDryRun() {
fmt.Println("Dry-run strategy: server")
fmt.Fprintln(r.ioStreams.ErrOut, "Dry-run strategy: server")
} else {
fmt.Println("Dry-run strategy: client")
fmt.Fprintln(r.ioStreams.ErrOut, "Dry-run strategy: client")
}
}

// The printer will print updates from the channel. It will block
// until the channel is closed.
// Use ErrOut for event/table printers (progress logs belong on stderr),
// but keep Out (stdout) for JSON output (structured, machine-parseable data).
printerStreams := r.ioStreams
if r.output != printers.JSONPrinter {
printerStreams.Out = r.ioStreams.ErrOut
}
var printer cliutilsprinter.Printer
if r.alpha && r.output == printers.TablePrinter {
printer = &alphaprinterstable.Printer{
IOStreams: r.ioStreams,
IOStreams: printerStreams,
}
} else {
printer = printers.GetPrinter(r.output, r.ioStreams)
printer = printers.GetPrinter(r.output, printerStreams)
}
return printer.Print(ch, dryRunStrategy, r.printStatusEvents)
}
Expand Down
12 changes: 9 additions & 3 deletions commands/live/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,19 @@ func runDestroy(r *Runner, inv inventory.Info, dryRunStrategy common.DryRunStrat
// Print the preview strategy unless the output format is json.
if dryRunStrategy.ClientOrServerDryRun() && r.output != printers.JSONPrinter {
if dryRunStrategy.ServerDryRun() {
fmt.Println("Dry-run strategy: server")
fmt.Fprintln(r.ioStreams.ErrOut, "Dry-run strategy: server")
} else {
fmt.Println("Dry-run strategy: client")
fmt.Fprintln(r.ioStreams.ErrOut, "Dry-run strategy: client")
}
}
// The printer will print updates from the channel. It will block
// until the channel is closed.
printer := printers.GetPrinter(r.output, r.ioStreams)
// Use ErrOut for event/table printers (progress logs belong on stderr),
// but keep Out (stdout) for JSON output (structured, machine-parseable data).
printerStreams := r.ioStreams
if r.output != printers.JSONPrinter {
printerStreams.Out = r.ioStreams.ErrOut
}
printer := printers.GetPrinter(r.output, printerStreams)
return printer.Print(ch, dryRunStrategy, r.printStatusEvents)
}
6 changes: 3 additions & 3 deletions commands/live/installrg/cmdinstallrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ func (r *Runner) runE(_ *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("too many arguments; install-resource-group takes no arguments")
}
fmt.Fprint(r.ioStreams.Out, "installing inventory ResourceGroup CRD...")
fmt.Fprint(r.ioStreams.ErrOut, "installing inventory ResourceGroup CRD...")

err := (&live.ResourceGroupInstaller{
Factory: r.factory,
}).InstallRG(r.ctx)

if err == nil {
fmt.Fprintln(r.ioStreams.Out, "success")
fmt.Fprintln(r.ioStreams.ErrOut, "success")
} else {
fmt.Fprintln(r.ioStreams.Out, "failed")
fmt.Fprintln(r.ioStreams.ErrOut, "failed")
}
return err
}
56 changes: 28 additions & 28 deletions commands/live/migrate/migratecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func NewRunner(
// default to current working directory
args = append(args, ".")
}
fmt.Fprint(ioStreams.Out, "inventory migration...\n")
fmt.Fprint(ioStreams.ErrOut, "inventory migration...\n")
if err := r.Run(ioStreams.In, args); err != nil {
fmt.Fprint(ioStreams.Out, "failed\n")
fmt.Fprint(ioStreams.Out, "inventory migration...failed\n")
fmt.Fprint(ioStreams.ErrOut, "failed\n")
fmt.Fprint(ioStreams.ErrOut, "inventory migration...failed\n")
return err
}
fmt.Fprint(ioStreams.Out, "inventory migration...success\n")
fmt.Fprint(ioStreams.ErrOut, "inventory migration...success\n")
return nil
},
}
Expand Down Expand Up @@ -165,10 +165,10 @@ func (mr *Runner) Run(reader io.Reader, args []string) error {
// error if one occurred. Ignores "AlreadyExists" error. Uses the definition
// stored in the "rgCrd" variable.
func (mr *Runner) applyCRD() error {
fmt.Fprint(mr.ioStreams.Out, " ensuring ResourceGroup CRD exists in cluster...")
fmt.Fprint(mr.ioStreams.ErrOut, " ensuring ResourceGroup CRD exists in cluster...")
// Simply return early if this is a dry run
if mr.dryRun {
fmt.Fprintln(mr.ioStreams.Out, "success")
fmt.Fprintln(mr.ioStreams.ErrOut, "success")
return nil
}
// Install the ResourceGroup CRD to the cluster.
Expand All @@ -177,17 +177,17 @@ func (mr *Runner) applyCRD() error {
Factory: mr.factory,
}).InstallRG(mr.ctx)
if err == nil {
fmt.Fprintln(mr.ioStreams.Out, "success")
fmt.Fprintln(mr.ioStreams.ErrOut, "success")
} else {
fmt.Fprintln(mr.ioStreams.Out, "failed")
fmt.Fprintln(mr.ioStreams.ErrOut, "failed")
}
return err
}

// retrieveConfigMapInv retrieves the ConfigMap inventory object or
// an error if one occurred.
func (mr *Runner) retrieveConfigMapInv(reader io.Reader, args []string) (inventory.Info, error) {
fmt.Fprint(mr.ioStreams.Out, " retrieve the current ConfigMap inventory...")
fmt.Fprint(mr.ioStreams.ErrOut, " retrieve the current ConfigMap inventory...")
cmReader, err := mr.cmLoader.ManifestReader(reader, args[0])
if err != nil {
return nil, err
Expand All @@ -198,20 +198,20 @@ func (mr *Runner) retrieveConfigMapInv(reader io.Reader, args []string) (invento
}
cmInvObj, _, err := inventory.SplitUnstructureds(objs)
if err != nil {
fmt.Fprintln(mr.ioStreams.Out, "no ConfigMap inventory...completed")
fmt.Fprintln(mr.ioStreams.ErrOut, "no ConfigMap inventory...completed")
return nil, err
}

// cli-utils treats any resource that contains the inventory-id label as an inventory object. We should
// ignore any inventories that are stored as ResourceGroup resources since they do not need migration.
if cmInvObj.GetKind() == rgfilev1alpha1.ResourceGroupGVK().Kind {
// No ConfigMap inventory means the migration has already run before.
fmt.Fprintln(mr.ioStreams.Out, "no ConfigMap inventory...completed")
fmt.Fprintln(mr.ioStreams.ErrOut, "no ConfigMap inventory...completed")
return nil, &inventory.NoInventoryObjError{}
}

cmInv := inventory.WrapInventoryInfoObj(cmInvObj)
fmt.Fprintf(mr.ioStreams.Out, "success (inventory-id: %s)\n", cmInv.ID())
fmt.Fprintf(mr.ioStreams.ErrOut, "success (inventory-id: %s)\n", cmInv.ID())
return cmInv, nil
}

Expand All @@ -220,12 +220,12 @@ func (mr *Runner) retrieveConfigMapInv(reader io.Reader, args []string) (invento
// or an error if one occurred.
func (mr *Runner) retrieveInvObjs(cmInvClient inventory.Client,
invObj inventory.Info) ([]object.ObjMetadata, error) {
fmt.Fprint(mr.ioStreams.Out, " retrieve ConfigMap inventory objs...")
fmt.Fprint(mr.ioStreams.ErrOut, " retrieve ConfigMap inventory objs...")
cmObjs, err := cmInvClient.GetClusterObjs(invObj)
if err != nil {
return nil, err
}
fmt.Fprintf(mr.ioStreams.Out, "success (%d inventory objects)\n", len(cmObjs))
fmt.Fprintf(mr.ioStreams.ErrOut, "success (%d inventory objects)\n", len(cmObjs))
return cmObjs, nil
}

Expand All @@ -237,13 +237,13 @@ func (mr *Runner) migrateObjs(rgInvClient inventory.Client,
if err := validateParams(reader, args); err != nil {
return err
}
fmt.Fprint(mr.ioStreams.Out, " migrate inventory to ResourceGroup...")
fmt.Fprint(mr.ioStreams.ErrOut, " migrate inventory to ResourceGroup...")
if len(cmObjs) == 0 {
fmt.Fprint(mr.ioStreams.Out, "no inventory objects found\n")
fmt.Fprint(mr.ioStreams.ErrOut, "no inventory objects found\n")
return nil
}
if mr.dryRun {
fmt.Fprintln(mr.ioStreams.Out, "success")
fmt.Fprintln(mr.ioStreams.ErrOut, "success")
return nil
}

Expand All @@ -270,19 +270,19 @@ func (mr *Runner) migrateObjs(rgInvClient inventory.Client,
if err != nil {
return err
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprint(mr.ioStreams.ErrOut, "success\n")
return nil
}

// deleteConfigMapInv removes the passed inventory object from the
// cluster. Returns an error if one occurred.
func (mr *Runner) deleteConfigMapInv(cmInvClient inventory.Client,
invObj inventory.Info) error {
fmt.Fprint(mr.ioStreams.Out, " deleting old ConfigMap inventory object...")
fmt.Fprint(mr.ioStreams.ErrOut, " deleting old ConfigMap inventory object...")
if err := cmInvClient.DeleteInventoryObj(invObj, mr.dryRunStrategy()); err != nil {
return err
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprint(mr.ioStreams.ErrOut, "success\n")
return nil
}

Expand All @@ -299,15 +299,15 @@ func (mr *Runner) deleteConfigMapFile() error {
return err
}
if len(cmFilename) > 0 {
fmt.Fprintf(mr.ioStreams.Out, "deleting inventory template file: %s...", cmFilename)
fmt.Fprintf(mr.ioStreams.ErrOut, "deleting inventory template file: %s...", cmFilename)
if !mr.dryRun {
err = os.Remove(cmFilename)
if err != nil {
fmt.Fprint(mr.ioStreams.Out, "failed\n")
fmt.Fprint(mr.ioStreams.ErrOut, "failed\n")
return err
}
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprint(mr.ioStreams.ErrOut, "success\n")
}
}
return nil
Expand Down Expand Up @@ -345,7 +345,7 @@ func cmInvClient(_ context.Context, factory util.Factory) (inventory.Client, err
func (mr *Runner) migrateKptfileToRG(args []string) error {
const op errors.Op = "migratecmd.migrateKptfileToRG"
klog.V(4).Infoln("attempting to migrate from Kptfile inventory")
fmt.Fprint(mr.ioStreams.Out, " reading existing Kptfile...")
fmt.Fprint(mr.ioStreams.ErrOut, " reading existing Kptfile...")
if !mr.dryRun {
dir, _, err := pathutil.ResolveAbsAndRelPaths(args[0])
if err != nil {
Expand Down Expand Up @@ -388,7 +388,7 @@ func (mr *Runner) migrateKptfileToRG(args []string) error {
return err
}
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprint(mr.ioStreams.ErrOut, "success\n")
return nil
}

Expand Down Expand Up @@ -441,7 +441,7 @@ func (mr *Runner) migrateCMToRG(stdinBytes []byte, args []string) error {

// createRGfile writes the inventory information into the resourcegroup object.
func (mr *Runner) createRGfile(ctx context.Context, args []string, prevID string) error {
fmt.Fprint(mr.ioStreams.Out, " creating ResourceGroup object file...")
fmt.Fprint(mr.ioStreams.ErrOut, " creating ResourceGroup object file...")
if !mr.dryRun {
dir, _, err := pathutil.ResolveAbsAndRelPaths(args[0])
if err != nil {
Expand All @@ -463,12 +463,12 @@ func (mr *Runner) createRGfile(ctx context.Context, args []string, prevID string
if err != nil {
var invExistsError *initialization.InvExistsError
if errors.As(err, &invExistsError) {
fmt.Fprint(mr.ioStreams.Out, "values already exist...")
fmt.Fprint(mr.ioStreams.ErrOut, "values already exist...")
} else {
return err
}
}
}
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprint(mr.ioStreams.ErrOut, "success\n")
return nil
}
4 changes: 2 additions & 2 deletions e2e/live/end-to-end-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ kubectl get resourcegroups.kpt.dev 2>&1 | tee $OUTPUT_DIR/status
assertContains "error: the server doesn't have a resource type \"resourcegroups\""
# Next, add the ResourceGroup CRD
echo "kpt live install-resource-group"
${BIN_DIR}/kpt live install-resource-group 2> /dev/null | tee $OUTPUT_DIR/status
${BIN_DIR}/kpt live install-resource-group 2>&1 | tee $OUTPUT_DIR/status
assertContains "installing inventory ResourceGroup CRD...success"
echo "kubectl get resourcegroups.kpt.dev"
kubectl get resourcegroups.kpt.dev 2>&1 | tee $OUTPUT_DIR/status
Expand All @@ -730,7 +730,7 @@ kubectl get resourcegroups.kpt.dev --no-headers 2>&1 | tee $OUTPUT_DIR/status
assertContains "example-inventory"
# Finally, add the ResourceGroup CRD again, and check it says it already exists.
echo "kpt live install-resource-group"
${BIN_DIR}/kpt live install-resource-group 2> /dev/null | tee $OUTPUT_DIR/status
${BIN_DIR}/kpt live install-resource-group 2>&1 | tee $OUTPUT_DIR/status
assertContains "...success"
printResult

Expand Down
2 changes: 1 addition & 1 deletion e2e/testdata/live-apply/apply-depends-on/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kptArgs:
- "apply"
- "--reconcile-timeout=2m"

stdOut: |
stdErr: |
inventory update started
inventory update finished
apply phase started
Expand Down
2 changes: 1 addition & 1 deletion e2e/testdata/live-apply/crd-and-cr/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kptArgs:
- "apply"
- "--reconcile-timeout=1m"

stdOut: |
stdErr: |
inventory update started
inventory update finished
apply phase started
Expand Down
6 changes: 3 additions & 3 deletions e2e/testdata/live-apply/dry-run-with-install-rg/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ kptArgs:
- "apply"
- "--dry-run"
- "--install-resource-group"
stdOut: |
deployment.apps/nginx-deployment created
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
stdErr: |
installing inventory ResourceGroup CRD.
Dry-run strategy: server
deployment.apps/nginx-deployment created
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
exitCode: 0
5 changes: 2 additions & 3 deletions e2e/testdata/live-apply/install-rg-on-apply/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ kptArgs:
- "live"
- "apply"
- "--reconcile-timeout=1m"
stdOut: |
deployment.apps/nginx-deployment created
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
stdErr: |
installing inventory ResourceGroup CRD.
deployment.apps/nginx-deployment created
1 resource(s) applied. 1 created, 0 unchanged, 0 configured, 0 failed
inventory:
- group: apps
kind: Deployment
Expand Down
3 changes: 1 addition & 2 deletions e2e/testdata/live-apply/prune-depends-on/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kptArgs:
- "apply"
- "--reconcile-timeout=1m"

stdOut: |
stdErr: |
inventory update started
inventory update finished
apply phase started
Expand All @@ -46,7 +46,6 @@ stdOut: |
prune result: 2 attempted, 2 successful, 0 skipped, 0 failed
reconcile result: 3 attempted, 3 successful, 0 skipped, 0 failed, 0 timed out


optionalStdOut:
- configmap/cm reconcile pending
- deployment.apps/second-nginx reconcile pending
Expand Down