Skip to content

Commit

Permalink
refactor: remove connect strings from packager property
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Aug 29, 2024
1 parent c6d06ba commit ebc75a2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
16 changes: 13 additions & 3 deletions src/pkg/cluster/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func (c *Cluster) PackageSecretNeedsWait(deployedPackage *types.DeployedPackage,
}

// RecordPackageDeploymentAndWait records the deployment of a package to the cluster and waits for any webhooks to complete.
func (c *Cluster) RecordPackageDeploymentAndWait(ctx context.Context, pkg v1alpha1.ZarfPackage, components []types.DeployedComponent, connectStrings types.ConnectStrings, generation int, component v1alpha1.ZarfComponent, skipWebhooks bool) (*types.DeployedPackage, error) {
deployedPackage, err := c.RecordPackageDeployment(ctx, pkg, components, connectStrings, generation)
func (c *Cluster) RecordPackageDeploymentAndWait(ctx context.Context, pkg v1alpha1.ZarfPackage, components []types.DeployedComponent, generation int, component v1alpha1.ZarfComponent, skipWebhooks bool) (*types.DeployedPackage, error) {
deployedPackage, err := c.RecordPackageDeployment(ctx, pkg, components, generation)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (c *Cluster) RecordPackageDeploymentAndWait(ctx context.Context, pkg v1alph
}

// RecordPackageDeployment saves metadata about a package that has been deployed to the cluster.
func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.ZarfPackage, components []types.DeployedComponent, connectStrings types.ConnectStrings, generation int) (deployedPackage *types.DeployedPackage, err error) {
func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.ZarfPackage, components []types.DeployedComponent, generation int) (deployedPackage *types.DeployedPackage, err error) {
packageName := pkg.Metadata.Name

// Attempt to load information about webhooks for the package
Expand All @@ -187,6 +187,16 @@ func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.Zarf
componentWebhooks = existingPackageSecret.ComponentWebhooks
}

// TODO: This is done for backwards compartibility and could be removed in the future.
connectStrings := types.ConnectStrings{}
for _, comp := range components {
for _, chart := range comp.InstalledCharts {
for k, v := range chart.ConnectStrings {
connectStrings[k] = v
}
}
}

deployedPackage = &types.DeployedPackage{
Name: packageName,
CLIVersion: config.CLIVersion,
Expand Down
1 change: 0 additions & 1 deletion src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Packager struct {
cluster *cluster.Cluster
layout *layout.PackagePaths
hpaModified bool
connectStrings types.ConnectStrings
source sources.PackageSource
}

Expand Down
41 changes: 19 additions & 22 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (p *Packager) Deploy(ctx context.Context) error {
}

p.hpaModified = false
p.connectStrings = make(types.ConnectStrings)
// Reset registry HPA scale down whether an error occurs or not
defer p.resetRegistryHPA(ctx)

Expand Down Expand Up @@ -182,7 +181,7 @@ func (p *Packager) deployComponents(ctx context.Context) ([]types.DeployedCompon

// 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, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, 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 All @@ -193,7 +192,7 @@ func (p *Packager) deployComponents(ctx context.Context) ([]types.DeployedCompon
if p.cfg.Pkg.IsInitConfig() {
charts, deployErr = p.deployInitComponent(ctx, component)
} else {
charts, deployErr = p.deployComponent(ctx, component, false /* keep img checksum */, false /* always push images */)
charts, deployErr = p.deployComponent(ctx, component, false, false)
}

onDeploy := component.Actions.OnDeploy
Expand All @@ -210,7 +209,7 @@ func (p *Packager) deployComponents(ctx context.Context) ([]types.DeployedCompon
// 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, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, 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 @@ -221,7 +220,7 @@ func (p *Packager) deployComponents(ctx context.Context) ([]types.DeployedCompon
deployedComponents[idx].InstalledCharts = charts
deployedComponents[idx].Status = types.ComponentStatusSucceeded
if p.isConnectedToCluster() {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil {
if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, 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 Expand Up @@ -361,7 +360,8 @@ func (p *Packager) deployComponent(ctx context.Context, component v1alpha1.ZarfC

charts := []types.InstalledChart{}
if hasCharts || hasManifests {
if charts, err = p.installChartAndManifests(ctx, componentPath, component); err != nil {
charts, err = p.installChartAndManifests(ctx, componentPath, component)
if err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -680,16 +680,11 @@ func (p *Packager) installChartAndManifests(ctx context.Context, componentPaths
p.cfg.PkgOpts.Retries),
)

addedConnectStrings, installedChartName, err := helmCfg.InstallOrUpgradeChart(ctx)
connectStrings, installedChartName, err := helmCfg.InstallOrUpgradeChart(ctx)
if err != nil {
return nil, err
}
installedCharts = append(installedCharts, types.InstalledChart{Namespace: chart.Namespace, ChartName: installedChartName})

// Iterate over any connectStrings and add to the main map
for name, description := range addedConnectStrings {
p.connectStrings[name] = description
}
installedCharts = append(installedCharts, types.InstalledChart{Namespace: chart.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings})
}

for _, manifest := range component.Manifests {
Expand Down Expand Up @@ -733,17 +728,11 @@ func (p *Packager) installChartAndManifests(ctx context.Context, componentPaths
}

// Install the chart.
addedConnectStrings, installedChartName, err := helmCfg.InstallOrUpgradeChart(ctx)
connectStrings, installedChartName, err := helmCfg.InstallOrUpgradeChart(ctx)
if err != nil {
return nil, err
}

installedCharts = append(installedCharts, types.InstalledChart{Namespace: manifest.Namespace, ChartName: installedChartName})

// Iterate over any connectStrings and add to the main map
for name, description := range addedConnectStrings {
p.connectStrings[name] = description
}
installedCharts = append(installedCharts, types.InstalledChart{Namespace: manifest.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings})
}

return installedCharts, nil
Expand All @@ -752,7 +741,15 @@ func (p *Packager) installChartAndManifests(ctx context.Context, componentPaths
func (p *Packager) printTablesForDeployment(ctx context.Context, componentsToDeploy []types.DeployedComponent) error {
// If not init config, print the application connection table
if !p.cfg.Pkg.IsInitConfig() {
message.PrintConnectStringTable(p.connectStrings)
connectStrings := types.ConnectStrings{}
for _, comp := range componentsToDeploy {
for _, chart := range comp.InstalledCharts {
for k, v := range chart.ConnectStrings {
connectStrings[k] = v
}
}
}
message.PrintConnectStringTable(connectStrings)
return nil
}
// Don't print if cluster is not configured
Expand Down
3 changes: 0 additions & 3 deletions src/pkg/packager/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/creator"
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/types"
)

// DevDeploy creates + deploys a package in one shot
Expand Down Expand Up @@ -75,8 +74,6 @@ func (p *Packager) DevDeploy(ctx context.Context) error {

message.HeaderInfof("📦 PACKAGE DEPLOY %s", p.cfg.Pkg.Metadata.Name)

p.connectStrings = make(types.ConnectStrings)

if !p.cfg.CreateOpts.NoYOLO {
p.cfg.Pkg.Metadata.YOLO = true
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/types/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ type Webhook struct {

// InstalledChart contains information about a Helm Chart that has been deployed to a cluster.
type InstalledChart struct {
Namespace string `json:"namespace"`
ChartName string `json:"chartName"`
Namespace string `json:"namespace"`
ChartName string `json:"chartName"`
ConnectStrings ConnectStrings `json:"connectStrings,omitempty"`
}

// GitServerInfo contains information Zarf uses to communicate with a git repository to push/pull repositories to.
Expand Down

0 comments on commit ebc75a2

Please sign in to comment.