Skip to content

Commit

Permalink
Unify ResourceGroup CRD installation output
Browse files Browse the repository at this point in the history
  • Loading branch information
seans3 committed Dec 9, 2020
1 parent e9743ca commit 6067c82
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
16 changes: 14 additions & 2 deletions commands/installrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/GoogleContainerTools/kpt/pkg/live"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
Expand Down Expand Up @@ -53,6 +54,17 @@ func (ir *InstallRGRunner) Run(reader io.Reader, args []string) error {
if len(args) > 0 {
return fmt.Errorf("too many arguments; install-resource-group takes no arguments")
}
// Apply the ResourceGroup CRD to the cluster, ignoring if it already exists.
return live.ApplyResourceGroupCRD(ir.factory)
fmt.Fprint(ir.ioStreams.Out, "installing ResourceGroup custom resource definition...")
// Apply the ResourceGroup CRD to the cluster, swallowing an "AlreadyExists" error.
err := live.ApplyResourceGroupCRD(ir.factory)
if apierrors.IsAlreadyExists(err) {
fmt.Fprint(ir.ioStreams.Out, "already installed...")
err = nil
}
if err != nil {
fmt.Fprintln(ir.ioStreams.Out, "failed")
} else {
fmt.Fprintln(ir.ioStreams.Out, "success")
}
return err
}
15 changes: 12 additions & 3 deletions commands/migratecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/GoogleContainerTools/kpt/pkg/live"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
Expand Down Expand Up @@ -149,11 +150,19 @@ func (mr *MigrateRunner) applyCRD() error {
var err error
// Simply return early if this is a dry run
if mr.dryRun {
fmt.Fprint(mr.ioStreams.Out, "success\n")
fmt.Fprintln(mr.ioStreams.Out, "success")
return nil
}
if err = live.ApplyResourceGroupCRD(mr.cmProvider.Factory()); err == nil {
fmt.Fprint(mr.ioStreams.Out, "success\n")
// Apply the ResourceGroup CRD to the cluster, swallowing an "AlreadyExists" error.
err = live.ApplyResourceGroupCRD(mr.cmProvider.Factory())
if apierrors.IsAlreadyExists(err) {
fmt.Fprint(mr.ioStreams.Out, "already installed...")
err = nil
}
if err != nil {
fmt.Fprintln(mr.ioStreams.Out, "failed")
} else {
fmt.Fprintln(mr.ioStreams.Out, "success")
}
return err
}
Expand Down
7 changes: 4 additions & 3 deletions e2e/live/end-to-end-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ cp -f e2e/live/testdata/inventory-template.yaml e2e/live/testdata/migrate-error
echo "Testing kpt live migrate with no objects in cluster"
echo "kpt live migrate e2e/live/testdata/migrate-error"
${BIN_DIR}/kpt live migrate e2e/live/testdata/migrate-error > $OUTPUT_DIR/status 2>&1
assertContains "ensuring ResourceGroup CRD exists in cluster...success"
assertContains "ensuring ResourceGroup CRD exists in cluster...already installed...success"
assertContains "updating Kptfile inventory values...values already exist...success"
assertContains "retrieve the current ConfigMap inventory...success (0 inventory objects)"
assertContains "deleting inventory template file:"
Expand Down Expand Up @@ -549,6 +549,7 @@ kubectl get resourcegroups.kpt.dev > $OUTPUT_DIR/status 2>&1
assertContains "error: the server doesn't have a resource type \"resourcegroups\""
# Next, add the ResourceGroup CRD
${BIN_DIR}/kpt live install-resource-group > $OUTPUT_DIR/status
assertContains "installing ResourceGroup custom resource definition...success"
kubectl get resourcegroups.kpt.dev > $OUTPUT_DIR/status 2>&1
assertContains "No resources found"
# Add a simple ResourceGroup custom resource, and verify it exists in the cluster.
Expand All @@ -557,8 +558,8 @@ assertContains "resourcegroup.kpt.dev/example-inventory created"
kubectl get resourcegroups.kpt.dev --no-headers > $OUTPUT_DIR/status
assertContains "example-inventory"
# Finally, add the ResourceGroup CRD again, and check it says it already exists.
${BIN_DIR}/kpt live install-resource-group -v=4 > $OUTPUT_DIR/status 2>&1
assertContains "ResourceGroup CRD already exists"
${BIN_DIR}/kpt live install-resource-group > $OUTPUT_DIR/status 2>&1
assertContains "...already installed...success"
printResult

# Clean-up the k8s cluster
Expand Down
15 changes: 3 additions & 12 deletions pkg/live/inventoryrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
Expand Down Expand Up @@ -215,8 +214,8 @@ var crdGroupKind = schema.GroupKind{

// ApplyResourceGroupCRD applies the custom resource definition for the
// ResourceGroup. The apiextensions version applied is based on the RESTMapping
// returned by the RESTMapper. Returns an error if one occurs; "Already Exists"
// error is changed to nil error.
// returned by the RESTMapper. Returns an error if one occurs, including an
// "Already Exists" error.
func ApplyResourceGroupCRD(factory cmdutil.Factory) error {
// Create the mapping from the CustomResourceDefinision GroupKind.
mapper, err := factory.ToRESTMapper()
Expand Down Expand Up @@ -255,15 +254,7 @@ func ApplyResourceGroupCRD(factory cmdutil.Factory) error {
helper := resource.NewHelper(client, mapping)
klog.V(4).Infoln("applying ResourceGroup CRD...")
_, err = helper.Create(emptyNamespace, clearResourceVersion, crd)
if err != nil {
if apierrors.IsAlreadyExists(err) {
klog.V(4).Infoln("ResourceGroup CRD already exists")
return nil
}
return err
}
klog.V(4).Infoln("ResourceGroup CRD successfully applied")
return nil
return err
}

// stringToUnstructured transforms a single resource represented by
Expand Down

0 comments on commit 6067c82

Please sign in to comment.