Skip to content

Commit

Permalink
refactor: move package state to 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 36f1df6 commit 342f3d1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/internal/packager/helm/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (h *Helm) UpdateZarfAgentValues(ctx context.Context) error {
Value: agentImage.Tag,
},
})
applicationTemplates, err := template.GetZarfTemplates("zarf-agent", h.state)
applicationTemplates, err := template.GetZarfTemplates("zarf-agent", h.state.RegistryInfo, h.state.GitServer, h.state.AgentTLS, h.state.StorageClass)
if err != nil {
return fmt.Errorf("error setting up the templates: %w", err)
}
Expand Down
88 changes: 41 additions & 47 deletions src/internal/packager/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,62 +40,56 @@ func GetZarfVariableConfig() *variables.VariableConfig {
}

// GetZarfTemplates returns the template keys and values to be used for templating.
func GetZarfTemplates(componentName string, state *types.ZarfState) (templateMap map[string]*variables.TextTemplate, err error) {
func GetZarfTemplates(componentName string, regInfo types.RegistryInfo, gitInfo types.GitServerInfo, agentTLS types.GeneratedPKI, storageClass string) (templateMap map[string]*variables.TextTemplate, err error) {
templateMap = make(map[string]*variables.TextTemplate)

if state != nil {
regInfo := state.RegistryInfo
gitInfo := state.GitServer
builtinMap := map[string]string{
"STORAGE_CLASS": storageClass,

builtinMap := map[string]string{
"STORAGE_CLASS": state.StorageClass,
// Registry info
"REGISTRY": regInfo.Address,
"NODEPORT": fmt.Sprintf("%d", regInfo.NodePort),
"REGISTRY_AUTH_PUSH": regInfo.PushPassword,
"REGISTRY_AUTH_PULL": regInfo.PullPassword,

// Registry info
"REGISTRY": regInfo.Address,
"NODEPORT": fmt.Sprintf("%d", regInfo.NodePort),
"REGISTRY_AUTH_PUSH": regInfo.PushPassword,
"REGISTRY_AUTH_PULL": regInfo.PullPassword,
// Git server info
"GIT_PUSH": gitInfo.PushUsername,
"GIT_AUTH_PUSH": gitInfo.PushPassword,
"GIT_PULL": gitInfo.PullUsername,
"GIT_AUTH_PULL": gitInfo.PullPassword,
}

builtinMap[depMarker] = config.GetDataInjectionMarker()

// Don't template component-specific variables for every component
switch componentName {
case "zarf-agent":
builtinMap["AGENT_CRT"] = base64.StdEncoding.EncodeToString(agentTLS.Cert)
builtinMap["AGENT_KEY"] = base64.StdEncoding.EncodeToString(agentTLS.Key)
builtinMap["AGENT_CA"] = base64.StdEncoding.EncodeToString(agentTLS.CA)

// Git server info
"GIT_PUSH": gitInfo.PushUsername,
"GIT_AUTH_PUSH": gitInfo.PushPassword,
"GIT_PULL": gitInfo.PullUsername,
"GIT_AUTH_PULL": gitInfo.PullPassword,
case "zarf-seed-registry", "zarf-registry":
builtinMap["SEED_REGISTRY"] = fmt.Sprintf("%s:%s", helpers.IPV4Localhost, config.ZarfSeedPort)
htpasswd, err := generateHtpasswd(&regInfo)
if err != nil {
return templateMap, err
}
builtinMap["HTPASSWD"] = htpasswd
builtinMap["REGISTRY_SECRET"] = regInfo.Secret
}

builtinMap[depMarker] = config.GetDataInjectionMarker()

// Don't template component-specific variables for every component
switch componentName {
case "zarf-agent":
agentTLS := state.AgentTLS
builtinMap["AGENT_CRT"] = base64.StdEncoding.EncodeToString(agentTLS.Cert)
builtinMap["AGENT_KEY"] = base64.StdEncoding.EncodeToString(agentTLS.Key)
builtinMap["AGENT_CA"] = base64.StdEncoding.EncodeToString(agentTLS.CA)

case "zarf-seed-registry", "zarf-registry":
builtinMap["SEED_REGISTRY"] = fmt.Sprintf("%s:%s", helpers.IPV4Localhost, config.ZarfSeedPort)
htpasswd, err := generateHtpasswd(&regInfo)
if err != nil {
return templateMap, err
}
builtinMap["HTPASSWD"] = htpasswd
builtinMap["REGISTRY_SECRET"] = regInfo.Secret
// Iterate over any custom variables and add them to the mappings for templating
for key, value := range builtinMap {
// Builtin keys are always uppercase in the format ###ZARF_KEY###
templateMap[strings.ToUpper(fmt.Sprintf("###ZARF_%s###", key))] = &variables.TextTemplate{
Value: value,
}

// Iterate over any custom variables and add them to the mappings for templating
for key, value := range builtinMap {
// Builtin keys are always uppercase in the format ###ZARF_KEY###
templateMap[strings.ToUpper(fmt.Sprintf("###ZARF_%s###", key))] = &variables.TextTemplate{
Value: value,
}

if key == "REGISTRY_SECRET" || key == "HTPASSWD" ||
key == "AGENT_CA" || key == "AGENT_KEY" || key == "AGENT_CRT" || key == "GIT_AUTH_PULL" ||
key == "GIT_AUTH_PUSH" || key == "REGISTRY_AUTH_PULL" || key == "REGISTRY_AUTH_PUSH" {
// Sanitize any builtin templates that are sensitive
templateMap[strings.ToUpper(fmt.Sprintf("###ZARF_%s###", key))].Sensitive = true
}
if key == "REGISTRY_SECRET" || key == "HTPASSWD" ||
key == "AGENT_CA" || key == "AGENT_KEY" || key == "AGENT_CRT" || key == "GIT_AUTH_PULL" ||
key == "GIT_AUTH_PUSH" || key == "REGISTRY_AUTH_PULL" || key == "REGISTRY_AUTH_PUSH" {
// Sanitize any builtin templates that are sensitive
templateMap[strings.ToUpper(fmt.Sprintf("###ZARF_%s###", key))].Sensitive = true
}
}

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 @@ -32,7 +32,6 @@ import (
type Packager struct {
cfg *types.PackagerConfig
variableConfig *variables.VariableConfig
state *types.ZarfState
cluster *cluster.Cluster
layout *layout.PackagePaths
hpaModified bool
Expand Down
54 changes: 27 additions & 27 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,15 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t
}
}

// TODO: init zarf state here

// Deploy the component
var charts []types.InstalledChart
var deployErr error
if p.cfg.Pkg.IsInitConfig() {
charts, deployErr = p.deployInitComponent(ctx, component)
charts, deployErr = p.deployInitComponent(ctx, nil, component)
} else {
charts, deployErr = p.deployComponent(ctx, component, false /* keep img checksum */, false /* always push images */)
charts, deployErr = p.deployComponent(ctx, nil, component, false, false)
}

onDeploy := component.Actions.OnDeploy
Expand Down Expand Up @@ -231,7 +233,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t
return deployedComponents, nil
}

func (p *Packager) deployInitComponent(ctx context.Context, component types.ZarfComponent) (charts []types.InstalledChart, err error) {
func (p *Packager) deployInitComponent(ctx context.Context, state *types.ZarfState, component types.ZarfComponent) (charts []types.InstalledChart, err error) {
hasExternalRegistry := p.cfg.InitOpts.RegistryInfo.Address != ""
isSeedRegistry := component.Name == "zarf-seed-registry"
isRegistry := component.Name == "zarf-registry"
Expand All @@ -244,7 +246,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component types.Zarf
}

// Always init the state before the first component that requires the cluster (on most deployments, the zarf-seed-registry)
if component.RequiresCluster() && p.state == nil {
if component.RequiresCluster() && state == nil {
err = p.cluster.InitZarfState(ctx, p.cfg.InitOpts)
if err != nil {
return nil, fmt.Errorf("unable to initialize Zarf state: %w", err)
Expand All @@ -269,7 +271,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component types.Zarf
}
}

charts, err = p.deployComponent(ctx, component, isAgent /* skip img checksum if isAgent */, isSeedRegistry /* skip image push if isSeedRegistry */)
charts, err = p.deployComponent(ctx, state, component, isAgent, isSeedRegistry)
if err != nil {
return nil, err
}
Expand All @@ -285,7 +287,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component types.Zarf
}

// Deploy a Zarf Component.
func (p *Packager) deployComponent(ctx context.Context, component types.ZarfComponent, noImgChecksum bool, noImgPush bool) (charts []types.InstalledChart, err error) {
func (p *Packager) deployComponent(ctx context.Context, state *types.ZarfState, component types.ZarfComponent, noImgChecksum bool, noImgPush bool) (charts []types.InstalledChart, err error) {
// Toggles for general deploy operations
componentPath := p.layout.Components.Dirs[component.Name]

Expand All @@ -302,15 +304,15 @@ func (p *Packager) deployComponent(ctx context.Context, component types.ZarfComp

if component.RequiresCluster() {
// Setup the state in the config
if p.state == nil {
if state == nil {
err = p.setupState(ctx)
if err != nil {
return charts, err
}
}

// Disable the registry HPA scale down if we are deploying images and it is not already disabled
if hasImages && !p.hpaModified && p.state.RegistryInfo.InternalRegistry {
if hasImages && !p.hpaModified && state.RegistryInfo.InternalRegistry {
if err := p.cluster.DisableRegHPAScaleDown(ctx); err != nil {
message.Debugf("unable to disable the registry HPA scale down: %s", err.Error())
} else {
Expand All @@ -319,7 +321,7 @@ func (p *Packager) deployComponent(ctx context.Context, component types.ZarfComp
}
}

err = p.populateComponentAndStateTemplates(component.Name)
err = p.populateComponentAndStateTemplates(component.Name, state.RegistryInfo, state.GitServer, state.AgentTLS, state.StorageClass)
if err != nil {
return charts, err
}
Expand All @@ -335,13 +337,13 @@ func (p *Packager) deployComponent(ctx context.Context, component types.ZarfComp
}

if hasImages {
if err := p.pushImagesToRegistry(ctx, component.Images, noImgChecksum); err != nil {
if err := p.pushImagesToRegistry(ctx, state.RegistryInfo, component.Images, noImgChecksum); err != nil {
return charts, fmt.Errorf("unable to push images to the registry: %w", err)
}
}

if hasRepos {
if err = p.pushReposToRepository(ctx, componentPath.Repos, component.Repos); err != nil {
if err = p.pushReposToRepository(ctx, state.GitServer, componentPath.Repos, component.Repos); err != nil {
return charts, fmt.Errorf("unable to push the repos to the repository: %w", err)
}
}
Expand All @@ -354,7 +356,7 @@ func (p *Packager) deployComponent(ctx context.Context, component types.ZarfComp
}

if hasCharts || hasManifests {
if charts, err = p.installChartAndManifests(ctx, componentPath, component); err != nil {
if charts, err = p.installChartAndManifests(ctx, state, componentPath, component); err != nil {
return charts, err
}
}
Expand Down Expand Up @@ -497,14 +499,12 @@ func (p *Packager) setupState(ctx context.Context) (err error) {
"the pod or namespace label `zarf.dev/agent: ignore'.")
}

p.state = state

spinner.Success()
return nil
}

func (p *Packager) populateComponentAndStateTemplates(componentName string) error {
applicationTemplates, err := template.GetZarfTemplates(componentName, p.state)
func (p *Packager) populateComponentAndStateTemplates(componentName string, regInfo types.RegistryInfo, gitInfo types.GitServerInfo, agentTLS types.GeneratedPKI, storageClass string) error {
applicationTemplates, err := template.GetZarfTemplates(componentName, regInfo, gitInfo, agentTLS, storageClass)
if err != nil {
return err
}
Expand All @@ -518,7 +518,7 @@ func (p *Packager) populatePackageVariableConfig() error {
}

// Push all of the components images to the configured container registry.
func (p *Packager) pushImagesToRegistry(ctx context.Context, componentImages []string, noImgChecksum bool) error {
func (p *Packager) pushImagesToRegistry(ctx context.Context, regInfo types.RegistryInfo, componentImages []string, noImgChecksum bool) error {
var combinedImageList []transform.Image
for _, src := range componentImages {
ref, err := transform.ParseImageRef(src)
Expand All @@ -533,7 +533,7 @@ func (p *Packager) pushImagesToRegistry(ctx context.Context, componentImages []s
pushCfg := images.PushConfig{
SourceDirectory: p.layout.Images.Base,
ImageList: imageList,
RegInfo: p.state.RegistryInfo,
RegInfo: regInfo,
NoChecksum: noImgChecksum,
Arch: p.cfg.Pkg.Build.Architecture,
Retries: p.cfg.PkgOpts.Retries,
Expand All @@ -543,7 +543,7 @@ func (p *Packager) pushImagesToRegistry(ctx context.Context, componentImages []s
}

// Push all of the components git repos to the configured git server.
func (p *Packager) pushReposToRepository(ctx context.Context, reposPath string, repos []string) error {
func (p *Packager) pushReposToRepository(ctx context.Context, gitInfo types.GitServerInfo, reposPath string, repos []string) error {
for _, repoURL := range repos {
repository, err := git.Open(reposPath, repoURL)
if err != nil {
Expand All @@ -552,7 +552,7 @@ func (p *Packager) pushReposToRepository(ctx context.Context, reposPath string,

// Create an anonymous function to push the repo to the Zarf git server
tryPush := func() error {
namespace, name, port, err := serviceInfoFromServiceURL(p.state.GitServer.Address)
namespace, name, port, err := serviceInfoFromServiceURL(gitInfo.Address)

// If this is a service (svcInfo is not nil), create a port-forward tunnel to that resource
// TODO: Find a better way as ignoring the error is not a good solution to decide to port forward.
Expand All @@ -574,12 +574,12 @@ func (p *Packager) pushReposToRepository(ctx context.Context, reposPath string,
return err
}
defer tunnel.Close()
giteaClient, err := gitea.NewClient(tunnel.HTTPEndpoint(), p.state.GitServer.PushUsername, p.state.GitServer.PushPassword)
giteaClient, err := gitea.NewClient(tunnel.HTTPEndpoint(), gitInfo.PushUsername, gitInfo.PushPassword)
if err != nil {
return err
}
return tunnel.Wrap(func() error {
err = repository.Push(ctx, tunnel.HTTPEndpoint(), p.state.GitServer.PushUsername, p.state.GitServer.PushPassword)
err = repository.Push(ctx, tunnel.HTTPEndpoint(), gitInfo.PushUsername, gitInfo.PushPassword)
if err != nil {
return err
}
Expand All @@ -588,15 +588,15 @@ func (p *Packager) pushReposToRepository(ctx context.Context, reposPath string,
if err != nil {
return err
}
err = giteaClient.AddReadOnlyUserToRepository(ctx, repoName, p.state.GitServer.PullUsername)
err = giteaClient.AddReadOnlyUserToRepository(ctx, repoName, gitInfo.PullUsername)
if err != nil {
return fmt.Errorf("unable to add the read only user to the repo %s: %w", repoName, err)
}
return nil
})
}

err = repository.Push(ctx, p.state.GitServer.Address, p.state.GitServer.PushUsername, p.state.GitServer.PushPassword)
err = repository.Push(ctx, gitInfo.Address, gitInfo.PushUsername, gitInfo.PushPassword)
if err != nil {
return err
}
Expand Down Expand Up @@ -639,7 +639,7 @@ func (p *Packager) generateValuesOverrides(chart types.ZarfChart, componentName
}

// Install all Helm charts and raw k8s manifests into the k8s cluster.
func (p *Packager) installChartAndManifests(ctx context.Context, componentPaths *layout.ComponentPaths, component types.ZarfComponent) (installedCharts []types.InstalledChart, err error) {
func (p *Packager) installChartAndManifests(ctx context.Context, state *types.ZarfState, componentPaths *layout.ComponentPaths, component types.ZarfComponent) (installedCharts []types.InstalledChart, err error) {
for _, chart := range component.Charts {
// Do not wait for the chart to be ready if data injections are present.
if len(component.DataInjections) > 0 {
Expand Down Expand Up @@ -668,7 +668,7 @@ func (p *Packager) installChartAndManifests(ctx context.Context, componentPaths
helm.WithDeployInfo(
p.cfg,
p.variableConfig,
p.state,
state,
p.cluster,
valuesOverrides,
p.cfg.DeployOpts.Timeout,
Expand Down Expand Up @@ -717,7 +717,7 @@ func (p *Packager) installChartAndManifests(ctx context.Context, componentPaths
helm.WithDeployInfo(
p.cfg,
p.variableConfig,
p.state,
state,
p.cluster,
nil,
p.cfg.DeployOpts.Timeout,
Expand Down
Loading

0 comments on commit 342f3d1

Please sign in to comment.