Skip to content

Commit

Permalink
refactor: move package generation to a local variable
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
  • Loading branch information
phillebaba committed Aug 5, 2024
1 parent ee60eb8 commit 3686191
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
13 changes: 8 additions & 5 deletions src/pkg/cluster/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ func (c *Cluster) GetDeployedZarfPackages(ctx context.Context) ([]types.Deployed

// GetDeployedPackage gets the metadata information about the package name provided (if it exists in the cluster).
// We determine what packages have been deployed to the cluster by looking for specific secrets in the Zarf namespace.
func (c *Cluster) GetDeployedPackage(ctx context.Context, packageName string) (deployedPackage *types.DeployedPackage, err error) {
// Get the secret that describes the deployed package
func (c *Cluster) GetDeployedPackage(ctx context.Context, packageName string) (*types.DeployedPackage, error) {
secret, err := c.Clientset.CoreV1().Secrets(ZarfNamespaceName).Get(ctx, config.ZarfPackagePrefix+packageName, metav1.GetOptions{})
if err != nil {
return deployedPackage, err
return nil, err
}

return deployedPackage, json.Unmarshal(secret.Data["data"], &deployedPackage)
deployedPackage := &types.DeployedPackage{}
err = json.Unmarshal(secret.Data["data"], deployedPackage)
if err != nil {
return nil, err
}
return deployedPackage, nil
}

// StripZarfLabelsAndSecretsFromNamespaces removes metadata and secrets from existing namespaces no longer manged by Zarf.
Expand Down
7 changes: 0 additions & 7 deletions src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type Packager struct {
hpaModified bool
connectStrings types.ConnectStrings
source sources.PackageSource
generation int
}

// Modifier is a function that modifies the packager.
Expand Down Expand Up @@ -155,12 +154,6 @@ func (p *Packager) attemptClusterChecks(ctx context.Context) (err error) {
spinner := message.NewProgressSpinner("Gathering additional cluster information (if available)")
defer spinner.Stop()

// Check if the package has already been deployed and get its generation
if existingDeployedPackage, _ := p.cluster.GetDeployedPackage(ctx, p.cfg.Pkg.Metadata.Name); existingDeployedPackage != nil {
// If this package has been deployed before, increment the package generation within the secret
p.generation = existingDeployedPackage.Generation + 1
}

// Check the clusters architecture matches the package spec
if err := p.validatePackageArchitecture(ctx); err != nil {
if errors.Is(err, lang.ErrUnableToCheckArch) {
Expand Down
40 changes: 23 additions & 17 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,8 @@ func (p *Packager) Deploy(ctx context.Context) error {

// deployComponents loops through a list of ZarfComponents and deploys them.
func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []types.DeployedComponent, err error) {
// Check if this package has been deployed before and grab relevant information about already deployed components
if p.generation == 0 {
p.generation = 1 // If this is the first deployment, set the generation to 1
}

// Process all the components we are deploying
// Connect to cluster if a component requires it.
for _, component := range p.cfg.Pkg.Components {
deployedComponent := types.DeployedComponent{
Name: component.Name,
Status: types.ComponentStatusDeploying,
ObservedGeneration: p.generation,
}

// If this component requires a cluster, connect to one
if component.RequiresCluster() {
timeout := cluster.DefaultTimeout
if p.cfg.Pkg.IsInitConfig() {
Expand All @@ -160,8 +148,26 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t
connectCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
if err := p.connectToCluster(connectCtx); err != nil {
return deployedComponents, fmt.Errorf("unable to connect to the Kubernetes cluster: %w", err)
return nil, fmt.Errorf("unable to connect to the Kubernetes cluster: %w", err)
}
break
}
}

// If this package has been deployed before, increment the package generation within the secret
packageGeneration := 1
if p.isConnectedToCluster() {
if existingDeployedPackage, _ := p.cluster.GetDeployedPackage(ctx, p.cfg.Pkg.Metadata.Name); existingDeployedPackage != nil {
packageGeneration = existingDeployedPackage.Generation + 1
}
}

// Process all the components we are deploying
for _, component := range p.cfg.Pkg.Components {
deployedComponent := types.DeployedComponent{
Name: component.Name,
Status: types.ComponentStatusDeploying,
ObservedGeneration: packageGeneration,
}

// Ensure we don't overwrite any installedCharts data when updating the package secret
Expand All @@ -177,7 +183,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t

// Update the package secret to indicate that we are attempting to deploy this component
if p.isConnectedToCluster() {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, p.generation, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
message.Debugf("Unable to record package deployment for component %s: this will affect features like `zarf package remove`: %s", component.Name, err.Error())
}
}
Expand Down Expand Up @@ -205,7 +211,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t
// Update the package secret to indicate that we failed to deploy this component
deployedComponents[idx].Status = types.ComponentStatusFailed
if p.isConnectedToCluster() {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, p.generation, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
message.Debugf("Unable to record package deployment for component %q: this will affect features like `zarf package remove`: %s", component.Name, err.Error())
}
}
Expand All @@ -217,7 +223,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t
deployedComponents[idx].InstalledCharts = charts
deployedComponents[idx].Status = types.ComponentStatusSucceeded
if p.isConnectedToCluster() {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, p.generation, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
message.Debugf("Unable to record package deployment for component %q: this will affect features like `zarf package remove`: %s", component.Name, err.Error())
}
}
Expand Down

0 comments on commit 3686191

Please sign in to comment.