Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
addressed comments
Browse files Browse the repository at this point in the history
Signed-off-by: Marjan Alavi <malavi@vmware.com>
  • Loading branch information
maralavi committed Sep 30, 2021
1 parent 2dfafa8 commit ba67b38
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgpackagedatamodel"
)

func displayProgress(initialMsg string, pp *tkgpackagedatamodel.PackageProgress) error {
// DisplayProgress creates an spinner instance; keeps receiving the progress messages in the channel and displays those using the spinner until an error occurs
func DisplayProgress(initialMsg string, pp *tkgpackagedatamodel.PackageProgress) error {
var (
currMsg string
s *spinner.Spinner
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/plugin/package/package_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func packageInstall(cmd *cobra.Command, args []string) error {
go pkgClient.InstallPackage(packageInstallOp, pp, tkgpackagedatamodel.OperationTypeInstall)

initialMsg := fmt.Sprintf("Installing package '%s'", packageInstallOp.PackageName)
if err := displayProgress(initialMsg, pp); err != nil {
if err := DisplayProgress(initialMsg, pp); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/plugin/package/package_installed_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func packageUninstall(cmd *cobra.Command, args []string) error {
go pkgClient.UninstallPackage(packageInstalledOp, pp)

initialMsg := fmt.Sprintf("Uninstalling package '%s' from namespace '%s'", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace)
if err := displayProgress(initialMsg, pp); err != nil {
if err := DisplayProgress(initialMsg, pp); err != nil {
if err.Error() == tkgpackagedatamodel.ErrPackageNotInstalled {
log.Warningf("\npackage '%s' is not installed in namespace '%s'. Cleaned up related resources", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/plugin/package/package_installed_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func packageUpdate(cmd *cobra.Command, args []string) error {
go pkgClient.UpdatePackage(packageInstalledOp, pp)

initialMsg := fmt.Sprintf("Updating package '%s'", packageInstalledOp.PkgInstallName)
if err := displayProgress(initialMsg, pp); err != nil {
if err := DisplayProgress(initialMsg, pp); err != nil {
if err.Error() == tkgpackagedatamodel.ErrPackageNotInstalled {
log.Warningf("\npackage '%s' is not among the list of installed packages in namespace '%s'. Consider using the --install flag to install the package", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/plugin/package/repository_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func repositoryAdd(cmd *cobra.Command, args []string) error {
go pkgClient.AddRepository(repoOp, pp, tkgpackagedatamodel.OperationTypeInstall)

initialMsg := fmt.Sprintf("Adding package repository '%s'", repoOp.RepositoryName)
if err := displayProgress(initialMsg, pp); err != nil {
if err := DisplayProgress(initialMsg, pp); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/plugin/package/repository_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func repositoryDelete(cmd *cobra.Command, args []string) error {
go pkgClient.DeleteRepository(repoOp, pp)

initialMsg := fmt.Sprintf("Deleting package repository '%s'", repoOp.RepositoryName)
if err := displayProgress(initialMsg, pp); err != nil {
if err := DisplayProgress(initialMsg, pp); err != nil {
if err.Error() == tkgpackagedatamodel.ErrRepoNotExists {
log.Warningf("\npackage repository '%s' does not exist in namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/plugin/package/repository_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func repositoryUpdate(cmd *cobra.Command, args []string) error {
go pkgClient.UpdateRepository(repoOp, pp)

initialMsg := fmt.Sprintf("Updating package repository '%s'", repoOp.RepositoryName)
if err := displayProgress(initialMsg, pp); err != nil {
if err := DisplayProgress(initialMsg, pp); err != nil {
if err.Error() == tkgpackagedatamodel.ErrRepoNotExists {
log.Warningf("\npackage repository '%s' does not exist in namespace '%s'. Consider using the --create flag to add the package repository", repoOp.RepositoryName, repoOp.Namespace)
return nil
Expand Down
40 changes: 40 additions & 0 deletions pkg/v1/tkg/tkgpackageclient/package_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
crtclient "sigs.k8s.io/controller-runtime/pkg/client"

kappctrl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kappipkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
versions "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions/v1alpha1"

Expand Down Expand Up @@ -270,3 +273,40 @@ func (p *pkgClient) createServiceAccount(o *tkgpackagedatamodel.PackageOptions)

return true, nil
}

// waitForResourceInstallation waits until the package get installed successfully or a failure happen
func (p *pkgClient) waitForResourceInstallation(name, namespace string, pollInterval, pollTimeout time.Duration, progress chan string, rscType tkgpackagedatamodel.ResourceType) error {
var status kappctrl.GenericStatus
if err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) {
switch rscType {
case tkgpackagedatamodel.ResourceTypePackageRepository:
resource, err := p.kappClient.GetPackageRepository(name, namespace)
if err != nil {
return false, err
}
status = resource.Status.GenericStatus
case tkgpackagedatamodel.ResourceTypePackageInstall:
resource, err := p.kappClient.GetPackageInstall(name, namespace)
if err != nil {
return false, err
}
status = resource.Status.GenericStatus
}
for _, cond := range status.Conditions {
if progress != nil {
progress <- fmt.Sprintf("Resource install status: %s", cond.Type)
}
switch cond.Type {
case kappctrl.ReconcileSucceeded:
return true, nil
case kappctrl.ReconcileFailed:
return false, fmt.Errorf("resource reconciliation failed: %s. %s", status.UsefulErrorMessage, status.FriendlyDescription)
}
}
return false, nil
}); err != nil {
return err
}

return nil
}
52 changes: 52 additions & 0 deletions pkg/v1/tkg/tkgpackageclient/package_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -15,8 +16,10 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
crtclient "sigs.k8s.io/controller-runtime/pkg/client"

kappctrl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kappipkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgpackagedatamodel"
Expand Down Expand Up @@ -207,3 +210,52 @@ func (p *pkgClient) deleteAnnotatedResource(obj runtime.Object, objKey crtclient

return nil
}

func progressCleanup(err error, progress *tkgpackagedatamodel.PackageProgress) {
if err != nil {
progress.Err <- err
}
close(progress.ProgressMsg)
close(progress.Done)
}

// waitForResourceDeletion waits until the CR gets deleted successfully or a failure happens
func (p *pkgClient) waitForResourceDeletion(name, namespace string, pollInterval, pollTimeout time.Duration, progress chan string, rscType tkgpackagedatamodel.ResourceType) error {
var status kappctrl.GenericStatus
if err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) {
switch rscType {
case tkgpackagedatamodel.ResourceTypePackageRepository:
resource, err := p.kappClient.GetPackageRepository(name, namespace)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
status = resource.Status.GenericStatus
case tkgpackagedatamodel.ResourceTypePackageInstall:
resource, err := p.kappClient.GetPackageInstall(name, namespace)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
status = resource.Status.GenericStatus
}
for _, cond := range status.Conditions {
if progress != nil {
progress <- fmt.Sprintf("Resource deletion status: %s", cond.Type)
}
if cond.Type == kappctrl.DeleteFailed {
return false, fmt.Errorf("resource deletion failed: %s. %s", status.UsefulErrorMessage, status.FriendlyDescription)
}
}

return false, nil
}); err != nil {
return err
}

return nil
}
92 changes: 0 additions & 92 deletions pkg/v1/tkg/tkgpackageclient/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/getkin/kin-openapi/openapi3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"

kappctrl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kapppkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgpackagedatamodel"
)

// DataValueProperty holds the details of each property under Carvel package.spec.valuesSchema.openAPIv3.properties.
Expand Down Expand Up @@ -110,89 +104,3 @@ func (parser *PackageValuesSchemaParser) walkOnValueSchemaProperties(docMap map[
}
return nil
}

// waitForResourceInstallation waits until the package get installed successfully or a failure happen
func (p *pkgClient) waitForResourceInstallation(name, namespace string, pollInterval, pollTimeout time.Duration, progress chan string, rscType tkgpackagedatamodel.ResourceType) error {
var status kappctrl.GenericStatus
if err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) {
switch rscType {
case tkgpackagedatamodel.ResourceTypePackageRepository:
resource, err := p.kappClient.GetPackageRepository(name, namespace)
if err != nil {
return false, err
}
status = resource.Status.GenericStatus
case tkgpackagedatamodel.ResourceTypePackageInstall:
resource, err := p.kappClient.GetPackageInstall(name, namespace)
if err != nil {
return false, err
}
status = resource.Status.GenericStatus
}
for _, cond := range status.Conditions {
if progress != nil {
progress <- fmt.Sprintf("Resource install status: %s", cond.Type)
}
switch cond.Type {
case kappctrl.ReconcileSucceeded:
return true, nil
case kappctrl.ReconcileFailed:
return false, fmt.Errorf("resource reconciliation failed: %s. %s", status.UsefulErrorMessage, status.FriendlyDescription)
}
}
return false, nil
}); err != nil {
return err
}

return nil
}

// waitForResourceDeletion waits until the CR gets deleted successfully or a failure happens
func (p *pkgClient) waitForResourceDeletion(name, namespace string, pollInterval, pollTimeout time.Duration, progress chan string, rscType tkgpackagedatamodel.ResourceType) error {
var status kappctrl.GenericStatus
if err := wait.Poll(pollInterval, pollTimeout, func() (done bool, err error) {
switch rscType {
case tkgpackagedatamodel.ResourceTypePackageRepository:
resource, err := p.kappClient.GetPackageRepository(name, namespace)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
status = resource.Status.GenericStatus
case tkgpackagedatamodel.ResourceTypePackageInstall:
resource, err := p.kappClient.GetPackageInstall(name, namespace)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
status = resource.Status.GenericStatus
}
for _, cond := range status.Conditions {
if progress != nil {
progress <- fmt.Sprintf("Resource deletion status: %s", cond.Type)
}
if cond.Type == kappctrl.DeleteFailed {
return false, fmt.Errorf("resource deletion failed: %s. %s", status.UsefulErrorMessage, status.FriendlyDescription)
}
}

return false, nil
}); err != nil {
return err
}

return nil
}

func progressCleanup(err error, progress *tkgpackagedatamodel.PackageProgress) {
if err != nil {
progress.Err <- err
}
close(progress.ProgressMsg)
close(progress.Done)
}

0 comments on commit ba67b38

Please sign in to comment.