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

Commit

Permalink
make package install idempotent (#983)
Browse files Browse the repository at this point in the history
* make package install idempotent

Signed-off-by: Marjan Alavi <malavi@vmware.com>
  • Loading branch information
maralavi authored Oct 28, 2021
1 parent 213639f commit aa516ff
Show file tree
Hide file tree
Showing 23 changed files with 594 additions and 188 deletions.
4 changes: 4 additions & 0 deletions cmd/cli/plugin/package/package_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func packageInstall(cmd *cobra.Command, args []string) error {

initialMsg := fmt.Sprintf("Installing package '%s'", packageInstallOp.PackageName)
if err := DisplayProgress(initialMsg, pp); err != nil {
if err.Error() == tkgpackagedatamodel.ErrPackageAlreadyExists {
log.Infof("Updated installed package '%s'", packageInstallOp.PkgInstallName)
return nil
}
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/plugin/package/package_installed_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func packageUninstall(cmd *cobra.Command, args []string) error {
initialMsg := fmt.Sprintf("Uninstalling package '%s' from namespace '%s'", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace)
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)
log.Warningf("package '%s' is not installed in namespace '%s'.", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace)
return nil
}
return err
}

log.Infof("\n %s", fmt.Sprintf("Uninstalled package '%s' from namespace '%s'", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace))
log.Infof("%s", fmt.Sprintf("Uninstalled package '%s' from namespace '%s'", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace))
return nil
}
8 changes: 4 additions & 4 deletions cmd/cli/plugin/package/package_installed_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ func packageUpdate(cmd *cobra.Command, args []string) error {
Err: make(chan error),
Done: make(chan struct{}),
}
go pkgClient.UpdatePackage(packageInstalledOp, pp)
go pkgClient.UpdatePackage(packageInstalledOp, pp, tkgpackagedatamodel.OperationTypeUpdate)

initialMsg := fmt.Sprintf("Updating package '%s'", packageInstalledOp.PkgInstallName)
initialMsg := fmt.Sprintf("Updating installed package '%s'", packageInstalledOp.PkgInstallName)
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)
log.Warningf("package '%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
}
return err
}

log.Infof("\n %s", fmt.Sprintf("Updated package install '%s' in namespace '%s'", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace))
log.Infof("%s", fmt.Sprintf("Updated installed package '%s' in namespace '%s'", packageInstalledOp.PkgInstallName, packageInstalledOp.Namespace))
return nil
}
8 changes: 6 additions & 2 deletions cmd/cli/plugin/package/repository_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
repositoryCmd.AddCommand(repositoryAddCmd)
}

func repositoryAdd(cmd *cobra.Command, args []string) error {
func repositoryAdd(_ *cobra.Command, args []string) error { //nolint
repoOp.RepositoryName = args[0]

pkgClient, err := tkgpackageclient.NewTKGPackageClient(repoOp.KubeConfig)
Expand All @@ -51,9 +51,13 @@ func repositoryAdd(cmd *cobra.Command, args []string) error {

initialMsg := fmt.Sprintf("Adding package repository '%s'", repoOp.RepositoryName)
if err := DisplayProgress(initialMsg, pp); err != nil {
if err.Error() == tkgpackagedatamodel.ErrRepoAlreadyExists {
log.Infof("Updated package repository '%s' in namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
return nil
}
return err
}

log.Infof("\n Added package repository '%s'", repoOp.RepositoryName)
log.Infof("Added package repository '%s' in namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
return nil
}
4 changes: 2 additions & 2 deletions cmd/cli/plugin/package/repository_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func repositoryDelete(cmd *cobra.Command, args []string) error {
initialMsg := fmt.Sprintf("Deleting package repository '%s'", repoOp.RepositoryName)
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)
log.Warningf("package repository '%s' does not exist in namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
return nil
}
return err
}

log.Infof("\n Deleted package repository '%s' from namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
log.Infof("Deleted package repository '%s' from namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
return nil
}
8 changes: 4 additions & 4 deletions cmd/cli/plugin/package/repository_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {
repositoryCmd.AddCommand(repositoryUpdateCmd)
}

func repositoryUpdate(cmd *cobra.Command, args []string) error {
func repositoryUpdate(_ *cobra.Command, args []string) error { //nolint
repoOp.RepositoryName = args[0]

pkgClient, err := tkgpackageclient.NewTKGPackageClient(repoOp.KubeConfig)
Expand All @@ -48,17 +48,17 @@ func repositoryUpdate(cmd *cobra.Command, args []string) error {
Err: make(chan error),
Done: make(chan struct{}),
}
go pkgClient.UpdateRepository(repoOp, pp)
go pkgClient.UpdateRepository(repoOp, pp, tkgpackagedatamodel.OperationTypeUpdate)

initialMsg := fmt.Sprintf("Updating package repository '%s'", repoOp.RepositoryName)
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)
log.Warningf("package repository '%s' does not exist in namespace '%s'. Consider using the --create flag to add the package repository", repoOp.RepositoryName, repoOp.Namespace)
return nil
}
return err
}

log.Infof("\n Updated package repository '%s' in namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
log.Infof("Updated package repository '%s' in namespace '%s'", repoOp.RepositoryName, repoOp.Namespace)
return nil
}
2 changes: 1 addition & 1 deletion pkg/v1/providers/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ AZURE_ENABLE_ACCELERATED_NETWORKING: false

#! Must be unique to each cluster.
AZURE_RESOURCE_GROUP: ""
#! If unset the value of AZURE_RESOURCE_GROUP will be used as the resoure group
#! If unset the value of AZURE_RESOURCE_GROUP will be used as the resource group
#! for the VNET
AZURE_VNET_RESOURCE_GROUP: ""

Expand Down
35 changes: 17 additions & 18 deletions pkg/v1/tkg/fakes/kappclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/v1/tkg/kappclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ func GetKubeConfig(kubeCfgPath string) (*rest.Config, error) {
return restConfig, nil
}

func (c *client) addAnnotations(meta *v1.ObjectMeta, isPkgPluginCreatedSvcAccount, isPkgPluginCreatedSecret bool) {
func (c *client) addAnnotations(meta *v1.ObjectMeta, pkgPluginResourceCreationStatus *tkgpackagedatamodel.PkgPluginResourceCreationStatus) {
if meta.Annotations == nil {
meta.Annotations = make(map[string]string)
}
if isPkgPluginCreatedSvcAccount {
if pkgPluginResourceCreationStatus.IsServiceAccountCreated {
meta.Annotations[tkgpackagedatamodel.TanzuPkgPluginAnnotation+"-"+tkgpackagedatamodel.KindClusterRole] = fmt.Sprintf(tkgpackagedatamodel.ClusterRoleName, meta.Name, meta.Namespace)
meta.Annotations[tkgpackagedatamodel.TanzuPkgPluginAnnotation+"-"+tkgpackagedatamodel.KindClusterRoleBinding] = fmt.Sprintf(tkgpackagedatamodel.ClusterRoleBindingName, meta.Name, meta.Namespace)
meta.Annotations[tkgpackagedatamodel.TanzuPkgPluginAnnotation+"-"+tkgpackagedatamodel.KindServiceAccount] = fmt.Sprintf(tkgpackagedatamodel.ServiceAccountName, meta.Name, meta.Namespace)
}
if isPkgPluginCreatedSecret {
if pkgPluginResourceCreationStatus.IsSecretCreated {
meta.Annotations[tkgpackagedatamodel.TanzuPkgPluginAnnotation+"-"+tkgpackagedatamodel.KindSecret] = fmt.Sprintf(tkgpackagedatamodel.SecretName, meta.Name, meta.Namespace)
}
}
Expand All @@ -138,9 +138,9 @@ func (c *client) DeletePackageRepository(repository *kappipkg.PackageRepository)
}

// CreatePackageInstall creates a PackageInstall CR
func (c *client) CreatePackageInstall(packageInstall *kappipkg.PackageInstall, isPkgPluginCreatedSvcAccount, isPkgPluginCreatedSecret bool) error {
func (c *client) CreatePackageInstall(packageInstall *kappipkg.PackageInstall, pkgPluginResourceCreationStatus *tkgpackagedatamodel.PkgPluginResourceCreationStatus) error {
installedPkg := packageInstall.DeepCopy()
c.addAnnotations(&installedPkg.ObjectMeta, isPkgPluginCreatedSvcAccount, isPkgPluginCreatedSecret)
c.addAnnotations(&installedPkg.ObjectMeta, pkgPluginResourceCreationStatus)

if err := c.client.Create(context.Background(), installedPkg); err != nil {
return err
Expand Down Expand Up @@ -280,8 +280,8 @@ func (c *client) ListPackages(packageName, namespace string) (*kapppkg.PackageLi
}

// UpdatePackageInstall updates the PackageInstall CR
func (c *client) UpdatePackageInstall(packageInstall *kappipkg.PackageInstall, isPkgPluginCreatedSecret bool) error {
c.addAnnotations(&packageInstall.ObjectMeta, false, isPkgPluginCreatedSecret)
func (c *client) UpdatePackageInstall(packageInstall *kappipkg.PackageInstall, pkgPluginResourceCreationStatus *tkgpackagedatamodel.PkgPluginResourceCreationStatus) error {
c.addAnnotations(&packageInstall.ObjectMeta, pkgPluginResourceCreationStatus)

if err := c.client.Update(context.Background(), packageInstall); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/v1/tkg/kappclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
kappipkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
kapppkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
secretgen "github.com/vmware-tanzu/carvel-secretgen-controller/pkg/apis/secretgen2/v1alpha1"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgpackagedatamodel"
)

//go:generate counterfeiter -o ../fakes/kappclient.go --fake-name KappClient . Client

// Client is the kapp client interface
type Client interface {
CreatePackageInstall(packageInstall *kappipkg.PackageInstall, isPkgPluginCreatedSvcAccount bool, isPkgPluginCreatedSecret bool) error
CreatePackageInstall(packageInstall *kappipkg.PackageInstall, pkgPluginResourceCreationStatus *tkgpackagedatamodel.PkgPluginResourceCreationStatus) error
CreatePackageRepository(repository *kappipkg.PackageRepository) error
DeletePackageRepository(repository *kappipkg.PackageRepository) error
GetAppCR(appName string, namespace string) (*kappctrl.App, error)
Expand All @@ -34,6 +35,6 @@ type Client interface {
ListPackageRepositories(namespace string) (*kappipkg.PackageRepositoryList, error)
ListRegistrySecrets(namespace string) (*corev1.SecretList, error)
ListSecretExports(namespace string) (*secretgen.SecretExportList, error)
UpdatePackageInstall(packageInstall *kappipkg.PackageInstall, isPkgPluginCreatedSecret bool) error
UpdatePackageInstall(packageInstall *kappipkg.PackageInstall, pkgPluginResourceCreationStatus *tkgpackagedatamodel.PkgPluginResourceCreationStatus) error
UpdatePackageRepository(repository *kappipkg.PackageRepository) error
}
4 changes: 2 additions & 2 deletions pkg/v1/tkg/tkgpackageclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ type TKGPackageClient interface {
ListRepositories(o *tkgpackagedatamodel.RepositoryOptions) (*kappipkg.PackageRepositoryList, error)
UninstallPackage(o *tkgpackagedatamodel.PackageOptions, packageProgress *tkgpackagedatamodel.PackageProgress)
UpdateRegistrySecret(o *tkgpackagedatamodel.RegistrySecretOptions) error
UpdatePackage(o *tkgpackagedatamodel.PackageOptions, packageProgress *tkgpackagedatamodel.PackageProgress)
UpdateRepository(o *tkgpackagedatamodel.RepositoryOptions, progress *tkgpackagedatamodel.PackageProgress)
UpdatePackage(o *tkgpackagedatamodel.PackageOptions, packageProgress *tkgpackagedatamodel.PackageProgress, operationType tkgpackagedatamodel.OperationType)
UpdateRepository(o *tkgpackagedatamodel.RepositoryOptions, progress *tkgpackagedatamodel.PackageProgress, operationType tkgpackagedatamodel.OperationType)
}
Loading

0 comments on commit aa516ff

Please sign in to comment.