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 <[email protected]>
  • Loading branch information
phillebaba committed Aug 5, 2024
1 parent 594a283 commit 6eed76a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
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
38 changes: 21 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,24 @@ 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 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 +181,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 +209,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 +221,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 6eed76a

Please sign in to comment.