Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add logger package deploy #3159

Merged
merged 22 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var initCmd = &cobra.Command{
Long: lang.CmdInitLong,
Example: lang.CmdInitExample,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
if err := validateInitFlags(); err != nil {
return fmt.Errorf("invalid command flags were provided: %w", err)
}
Expand All @@ -50,7 +51,7 @@ var initCmd = &cobra.Command{
return err
}

src, err := sources.New(&pkgConfig.PkgOpts)
src, err := sources.New(ctx, &pkgConfig.PkgOpts)
if err != nil {
return err
}
Expand All @@ -65,7 +66,7 @@ var initCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

err = pkgClient.Deploy(cmd.Context())
err = pkgClient.Deploy(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -142,7 +143,7 @@ func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, er

// If the user wants to download the init-package, download it
if confirmDownload {
remote, err := zoci.NewRemote(url, oci.PlatformForArch(config.GetArch()))
remote, err := zoci.NewRemote(ctx, url, oci.PlatformForArch(config.GetArch()))
if err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,14 @@ var downloadInitCmd = &cobra.Command{
Use: "download-init",
Short: lang.CmdToolsDownloadInitShort,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
url := zoci.GetInitPackageURL(config.CLIVersion)
remote, err := zoci.NewRemote(url, oci.PlatformForArch(config.GetArch()))
remote, err := zoci.NewRemote(ctx, url, oci.PlatformForArch(config.GetArch()))
if err != nil {
return fmt.Errorf("unable to download the init package: %w", err)
}
source := &sources.OCISource{Remote: remote}
_, err = source.Collect(cmd.Context(), outputDirectory)
_, err = source.Collect(ctx, outputDirectory)
if err != nil {
return fmt.Errorf("unable to download the init package: %w", err)
}
Expand Down
6 changes: 6 additions & 0 deletions src/internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"

"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/pkg/utils"
Expand Down Expand Up @@ -146,6 +147,7 @@ func (r *Repository) Path() string {

// Push pushes the repository to the remote git server.
func (r *Repository) Push(ctx context.Context, address, username, password string) error {
l := logger.From(ctx)
repo, err := git.PlainOpen(r.path)
if err != nil {
return fmt.Errorf("not a valid git repo or unable to open: %w", err)
Expand Down Expand Up @@ -195,10 +197,13 @@ func (r *Repository) Push(ctx context.Context, address, username, password strin
err = repo.FetchContext(ctx, fetchOptions)
if errors.Is(err, transport.ErrRepositoryNotFound) {
message.Debugf("Repo not yet available offline, skipping fetch...")
l.Debug("repo not yet available offline, skipping fetch")
} else if errors.Is(err, git.ErrForceNeeded) {
message.Debugf("Repo fetch requires force, skipping fetch...")
l.Debug("repo fetch requires force, skipping fetch")
} else if errors.Is(err, git.NoErrAlreadyUpToDate) {
message.Debugf("Repo already up-to-date, skipping fetch...")
l.Debug("repo already up-to-date, skipping fetch")
} else if err != nil {
return fmt.Errorf("unable to fetch the git repo prior to push: %w", err)
}
Expand All @@ -217,6 +222,7 @@ func (r *Repository) Push(ctx context.Context, address, username, password strin
})
if errors.Is(err, git.NoErrAlreadyUpToDate) {
message.Debug("Repo already up-to-date")
l.Debug("repo already up-to-date")
} else if errors.Is(err, plumbing.ErrObjectNotFound) {
return fmt.Errorf("unable to push repo due to likely shallow clone: %s", err.Error())
} else if err != nil {
Expand Down
30 changes: 20 additions & 10 deletions src/internal/packager/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"context"
"errors"
"fmt"
"github.com/zarf-dev/zarf/src/pkg/logger"
"time"

"github.com/zarf-dev/zarf/src/pkg/logger"

"github.com/Masterminds/semver/v3"
"github.com/avast/retry-go/v4"
plutoversionsfile "github.com/fairwindsops/pluto/v5"
Expand All @@ -35,23 +36,26 @@ import (

// InstallOrUpgradeChart performs a helm install of the given chart.
func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings, string, error) {
fromMessage := h.chart.URL
if fromMessage == "" {
fromMessage = "Zarf-generated helm chart"
l := logger.From(ctx)
start := time.Now()
source := h.chart.URL
if source == "" {
source = "Zarf-generated"
}
spinner := message.NewProgressSpinner("Processing helm chart %s:%s from %s",
spinner := message.NewProgressSpinner("Processing helm chart %s:%s source: %s",
h.chart.Name,
h.chart.Version,
fromMessage)
source)
defer spinner.Stop()
l.Info("processing helm chart", "name", h.chart.Name, "version", h.chart.Version, "source", source)

// If no release name is specified, use the chart name.
if h.chart.ReleaseName == "" {
h.chart.ReleaseName = h.chart.Name
}

// Setup K8s connection.
err := h.createActionConfig(h.chart.Namespace, spinner)
err := h.createActionConfig(ctx, h.chart.Namespace, spinner)
if err != nil {
return nil, "", fmt.Errorf("unable to initialize the K8s client: %w", err)
}
Expand All @@ -73,15 +77,18 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,
releases, histErr := histClient.Run(h.chart.ReleaseName)

spinner.Updatef("Checking for existing helm deployment")
l.Debug("checking for existing helm deployment")

if errors.Is(histErr, driver.ErrReleaseNotFound) {
// No prior release, try to install it.
spinner.Updatef("Attempting chart installation")
l.Info("performing Helm install", "chart", h.chart.Name)

release, err = h.installChart(helmCtx, postRender)
} else if histErr == nil && len(releases) > 0 {
// Otherwise, there is a prior release so upgrade it.
spinner.Updatef("Attempting chart upgrade")
l.Info("performing Helm upgrade", "chart", h.chart.Name)

lastRelease := releases[len(releases)-1]

Expand Down Expand Up @@ -118,6 +125,7 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,

// Attempt to rollback on a failed upgrade.
spinner.Updatef("Performing chart rollback")
l.Info("performing Helm rollback", "chart", h.chart.Name)
err = h.rollbackChart(h.chart.ReleaseName, previouslyDeployedVersion)
if err != nil {
return nil, "", fmt.Errorf("%w: unable to rollback: %w", installErr, err)
Expand All @@ -137,11 +145,13 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,
if !h.chart.NoWait {
// Ensure we don't go past the timeout by using a context initialized with the helm timeout
spinner.Updatef("Running health checks")
l.Info("running health checks", "chart", h.chart.Name)
if err := healthchecks.WaitForReadyRuntime(helmCtx, h.cluster.Watcher, runtimeObjs); err != nil {
return nil, "", err
}
}
spinner.Success()
l.Debug("done processing helm chart", "name", h.chart.Name, "duration", time.Since(start))

// return any collected connect strings for zarf connect.
return postRender.connectStrings, h.chart.ReleaseName, nil
Expand All @@ -152,7 +162,7 @@ func (h *Helm) TemplateChart(ctx context.Context) (manifest string, chartValues
spinner := message.NewProgressSpinner("Templating helm chart %s", h.chart.Name)
defer spinner.Stop()

err = h.createActionConfig(h.chart.Namespace, spinner)
err = h.createActionConfig(ctx, h.chart.Namespace, spinner)

// Setup K8s connection.
if err != nil {
Expand Down Expand Up @@ -216,7 +226,7 @@ func (h *Helm) TemplateChart(ctx context.Context) (manifest string, chartValues
// RemoveChart removes a chart from the cluster.
func (h *Helm) RemoveChart(ctx context.Context, namespace string, name string, spinner *message.Spinner) error {
// Establish a new actionConfig for the namespace.
_ = h.createActionConfig(namespace, spinner)
_ = h.createActionConfig(ctx, namespace, spinner)
// Perform the uninstall.
response, err := h.uninstallChart(name)
message.Debug(response)
Expand All @@ -230,7 +240,7 @@ func (h *Helm) UpdateReleaseValues(ctx context.Context, updatedValues map[string
spinner := message.NewProgressSpinner("Updating values for helm release %s", h.chart.ReleaseName)
defer spinner.Stop()

err := h.createActionConfig(h.chart.Namespace, spinner)
err := h.createActionConfig(ctx, h.chart.Namespace, spinner)
if err != nil {
return fmt.Errorf("unable to initialize the K8s client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager/helm/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Destroy(ctx context.Context, purgeAllZarfInstallations bool) {
h := Helm{}

// Initially load the actionConfig without a namespace
err := h.createActionConfig("", spinner)
err := h.createActionConfig(ctx, "", spinner)
if err != nil {
// Don't fatal since this is a removal action
spinner.Errorf(err, "Unable to initialize the K8s client")
Expand Down
9 changes: 9 additions & 0 deletions src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
Expand Down Expand Up @@ -117,6 +118,7 @@ func (r *renderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) {
}

func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
l := logger.From(ctx)
c := r.cluster
namespaceList, err := r.cluster.Clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
Expand All @@ -142,6 +144,7 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
// https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#initial-namespaces
if slices.Contains([]string{"default", "kube-node-lease", "kube-public", "kube-system"}, name) {
message.Warnf("Refusing to adopt the initial namespace: %s", name)
l.Warn("refusing to adopt initial namespace", "name", name)
} else {
// This is an existing namespace to adopt
_, err := c.Clientset.CoreV1().Namespaces().Update(ctx, namespace, metav1.UpdateOptions{})
Expand Down Expand Up @@ -181,6 +184,7 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
}()
if err != nil {
message.WarnErrf(err, "Problem creating registry secret for the %s namespace", name)
l.Warn("problem creating registry secret", "namespace", name, "error", err.Error())
}

// Create or update the zarf git server secret
Expand All @@ -201,13 +205,15 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
}()
if err != nil {
message.WarnErrf(err, "Problem creating git server secret for the %s namespace", name)
l.Warn("problem creating git server secret", "namespace", name, "error", err.Error())
}
}
}
return nil
}

func (r *renderer) editHelmResources(ctx context.Context, resources []releaseutil.Manifest, finalManifestsOutput *bytes.Buffer) error {
l := logger.From(ctx)
dc, err := dynamic.NewForConfig(r.cluster.RestConfig)
if err != nil {
return err
Expand All @@ -231,8 +237,10 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti
// parse the namespace resource so it can be applied out-of-band by zarf instead of helm to avoid helm ns shenanigans
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(rawData.UnstructuredContent(), namespace); err != nil {
message.WarnErrf(err, "could not parse namespace %s", rawData.GetName())
l.Warn("failed to parse namespace", "name", rawData.GetName(), "error", err)
} else {
message.Debugf("Matched helm namespace %s for zarf annotation", namespace.Name)
l.Debug("matched helm namespace for zarf annotation", "name", namespace.Name)
namespace.Labels = cluster.AdoptZarfManagedLabels(namespace.Labels)
// Add it to the stack
r.namespaces[namespace.Name] = namespace
Expand All @@ -253,6 +261,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti
if key, keyExists := labels[cluster.ZarfConnectLabelName]; keyExists {
// If there is a zarf-connect label
message.Debugf("Match helm service %s for zarf connection %s", rawData.GetName(), key)
l.Debug("match helm service for zarf connection", "service", rawData.GetName(), "connection-key", key)

// Add the connectString for processing later in the deployment
r.connectStrings[key] = types.ConnectString{
Expand Down
12 changes: 10 additions & 2 deletions src/internal/packager/helm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
package helm

import (
"context"
"fmt"
"log/slog"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -60,7 +63,7 @@ func (h *Helm) parseChartValues() (chartutil.Values, error) {
return helpers.MergeMapRecursive(chartValues, h.valuesOverrides), nil
}

func (h *Helm) createActionConfig(namespace string, spinner *message.Spinner) error {
func (h *Helm) createActionConfig(ctx context.Context, namespace string, spinner *message.Spinner) error {
// Initialize helm SDK
actionConfig := new(action.Configuration)
// Set the settings for the helm SDK
Expand All @@ -70,7 +73,12 @@ func (h *Helm) createActionConfig(namespace string, spinner *message.Spinner) er
h.settings.SetNamespace(namespace)

// Setup K8s connection
err := actionConfig.Init(h.settings.RESTClientGetter(), namespace, "", spinner.Updatef)
helmLogger := spinner.Updatef
if logger.Enabled(ctx) {
l := logger.From(ctx)
helmLogger = slog.NewLogLogger(l.Handler(), slog.LevelDebug).Printf
}
err := actionConfig.Init(h.settings.RESTClientGetter(), namespace, "", helmLogger)
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved

// Set the actionConfig is the received Helm pointer
h.actionConfig = actionConfig
Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager/helm/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (h *Helm) UpdateZarfAgentValues(ctx context.Context) error {
return err
}

err = h.createActionConfig(cluster.ZarfNamespaceName, spinner)
err = h.createActionConfig(ctx, cluster.ZarfNamespaceName, spinner)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (h *Helm) UpdateZarfAgentValues(ctx context.Context) error {
Value: agentImage.Tag,
},
})
applicationTemplates, err := template.GetZarfTemplates("zarf-agent", h.state)
applicationTemplates, err := template.GetZarfTemplates(ctx, "zarf-agent", h.state)
if err != nil {
return fmt.Errorf("error setting up the templates: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal/packager/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import (

"github.com/avast/retry-go/v4"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/pkg/utils"
)

// Push pushes images to a registry.
func Push(ctx context.Context, cfg PushConfig) error {
logs.Warn.SetOutput(&message.DebugWriter{})
logs.Progress.SetOutput(&message.DebugWriter{})
l := logger.From(ctx)

toPush := map[transform.Image]v1.Image{}
// Build an image list from the references
Expand Down Expand Up @@ -67,6 +66,7 @@ func Push(ctx context.Context, cfg PushConfig) error {
}()
for refInfo, img := range toPush {
message.Infof("Pushing %s", refInfo.Reference)
l.Info("pushing image", "name", refInfo.Reference)
// If this is not a no checksum image push it for use with the Zarf agent
if !cfg.NoChecksum {
offlineNameCRC, err := transform.ImageTransformHost(registryURL, refInfo.Reference)
Expand Down
9 changes: 6 additions & 3 deletions src/internal/packager/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetZarfVariableConfig(ctx context.Context) *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(ctx context.Context, componentName string, state *types.ZarfState) (templateMap map[string]*variables.TextTemplate, err error) {
templateMap = make(map[string]*variables.TextTemplate)

if state != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func GetZarfTemplates(componentName string, state *types.ZarfState) (templateMap
}
}

debugPrintTemplateMap(templateMap)
debugPrintTemplateMap(ctx, templateMap)

return templateMap, nil
}
Expand All @@ -127,7 +127,9 @@ func generateHtpasswd(regInfo *types.RegistryInfo) (string, error) {
return "", nil
}

func debugPrintTemplateMap(templateMap map[string]*variables.TextTemplate) {
func debugPrintTemplateMap(ctx context.Context, templateMap map[string]*variables.TextTemplate) {
// TODO (@austinabro321) sanitize the template by making a copy and changing the actual keys
// then use json.MarshalIndent to create the json
debugText := "templateMap = { "

for key, template := range templateMap {
Expand All @@ -141,4 +143,5 @@ func debugPrintTemplateMap(templateMap map[string]*variables.TextTemplate) {
debugText += " }"

message.Debug(debugText)
logger.From(ctx).Debug(debugText)
}
Loading