diff --git a/commands/installrg.go b/commands/installrg.go index daca6d8a04..141ef0fd86 100644 --- a/commands/installrg.go +++ b/commands/installrg.go @@ -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" @@ -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 } diff --git a/commands/migratecmd.go b/commands/migratecmd.go index bbbe1b4bf3..975746386a 100644 --- a/commands/migratecmd.go +++ b/commands/migratecmd.go @@ -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" @@ -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 } diff --git a/e2e/live/end-to-end-test.sh b/e2e/live/end-to-end-test.sh index 6e024345cc..ea38dca362 100755 --- a/e2e/live/end-to-end-test.sh +++ b/e2e/live/end-to-end-test.sh @@ -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:" @@ -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. @@ -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 diff --git a/pkg/live/inventoryrg.go b/pkg/live/inventoryrg.go index bd56d9cc5f..366e26a991 100644 --- a/pkg/live/inventoryrg.go +++ b/pkg/live/inventoryrg.go @@ -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" @@ -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() @@ -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