Skip to content
Open
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/anchore/clio v0.0.0-20250715152405-a0fa658e5084
github.com/anchore/stereoscope v0.1.22
github.com/anchore/syft v1.42.3
github.com/avast/retry-go/v4 v4.7.0
github.com/defenseunicorns/pkg/helpers/v2 v2.0.4
github.com/defenseunicorns/pkg/oci v1.3.0
github.com/derailed/k9s v0.50.18
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:W
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/avast/retry-go/v4 v4.7.0 h1:yjDs35SlGvKwRNSykujfjdMxMhMQQM0TnIjJaHB+Zio=
github.com/avast/retry-go/v4 v4.7.0/go.mod h1:ZMPDa3sY2bKgpLtap9JRUgk2yTAba7cgiFhqxY2Sg6Q=
github.com/aws/aws-sdk-go v1.55.8 h1:JRmEUbU52aJQZ2AjX4q4Wu7t4uZjOu71uyNmaWlUkJQ=
github.com/aws/aws-sdk-go v1.55.8/go.mod h1:ZkViS9AqA6otK+JBBNH2++sx1sgxrPKcSzPPvQkUtXk=
github.com/aws/aws-sdk-go-v2 v1.41.2 h1:LuT2rzqNQsauaGkPK/7813XxcZ3o3yePY0Iy891T2ls=
Expand Down
53 changes: 29 additions & 24 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"helm.sh/helm/v4/pkg/getter"
"helm.sh/helm/v4/pkg/registry"
repov1 "helm.sh/helm/v4/pkg/repo/v1"
"k8s.io/apimachinery/pkg/util/wait"

retry "github.com/avast/retry-go/v4"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/internal/git"
Expand Down Expand Up @@ -239,31 +239,36 @@ func DownloadPublishedChart(ctx context.Context, chart v1alpha1.ZarfChart, chart
},
}

var saved string
err = retry.Do(
func() error {
var downloadErr error
saved, _, downloadErr = chartDownloader.DownloadToCache(chartURL, pull.Version)
return downloadErr
},
retry.Attempts(uint(config.ZarfDefaultRetries)),
retry.Delay(config.ZarfDefaultRetryDelay),
retry.MaxDelay(config.ZarfDefaultRetryMaxDelay),
retry.DelayType(retry.BackOffDelay),
retry.LastErrorOnly(true),
retry.Context(ctx),
retry.OnRetry(func(n uint, err error) {
if config.ZarfDefaultRetries > 1 && n+1 < uint(config.ZarfDefaultRetries) {
l.Warn("retrying chart download",
"attempt", n+1,
"maxAttempts", config.ZarfDefaultRetries,
"chart", chart.Name,
"error", err,
)
}
}),
var (
saved string
lastErr error
attempts int
)
err = wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
Duration: config.ZarfDefaultRetryDelay,
Factor: 2.0,
Steps: config.ZarfDefaultRetries,
Cap: config.ZarfDefaultRetryMaxDelay,
}, func(ctx context.Context) (bool, error) {
var downloadErr error
saved, _, downloadErr = chartDownloader.DownloadToCache(chartURL, pull.Version)
if downloadErr == nil {
return true, nil
}
lastErr = downloadErr
attempts++
l.Warn("retrying chart download",
"attempt", attempts,
"maxAttempts", config.ZarfDefaultRetries,
"chart", chart.Name,
"error", downloadErr,
)
return false, nil
})
if err != nil {
if lastErr != nil {
return fmt.Errorf("unable to download the helm chart: %w", lastErr)
}
return fmt.Errorf("unable to download the helm chart: %w", err)
}

Expand Down
33 changes: 20 additions & 13 deletions src/pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"slices"
"time"

"github.com/avast/retry-go/v4"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/internal/healthchecks"
"github.com/zarf-dev/zarf/src/pkg/logger"
Expand All @@ -22,6 +21,7 @@ import (
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
v1ac "k8s.io/client-go/applyconfigurations/core/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
Expand Down Expand Up @@ -76,25 +76,29 @@ func NewWithWait(ctx context.Context) (*Cluster, error) {
if err != nil {
return nil, err
}
err = retry.Do(func() error {
err = wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
Duration: time.Second,
Factor: 1.0,
}, func(ctx context.Context) (bool, error) {
nodeList, err := c.Clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return err
return false, err
}
if len(nodeList.Items) < 1 {
return fmt.Errorf("cluster does not have any nodes")
return false, fmt.Errorf("cluster does not have any nodes")
}

pods, err := c.Clientset.CoreV1().Pods(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return err
return false, nil
}
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodRunning {
return nil
return true, nil
}
}
return fmt.Errorf("no pods are in succeeded or running state")
}, retry.Context(ctx), retry.Attempts(0), retry.DelayType(retry.FixedDelay), retry.Delay(time.Second))
return false, nil
})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -242,13 +246,16 @@ func (c *Cluster) InitState(ctx context.Context, opts InitStateOptions) (*state.
// The default SA is required for pods to start properly.
saCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
err = retry.Do(func() error {
_, err := c.Clientset.CoreV1().ServiceAccounts(state.ZarfNamespaceName).Get(saCtx, "default", metav1.GetOptions{})
err = wait.ExponentialBackoffWithContext(saCtx, wait.Backoff{
Duration: time.Second,
Factor: 1.0,
}, func(ctx context.Context) (bool, error) {
_, err := c.Clientset.CoreV1().ServiceAccounts(state.ZarfNamespaceName).Get(ctx, "default", metav1.GetOptions{})
if err != nil {
return err
return false, nil
}
return nil
}, retry.Context(saCtx), retry.Attempts(0), retry.DelayType(retry.FixedDelay), retry.Delay(time.Second))
return true, nil
})
if err != nil {
return nil, fmt.Errorf("unable get default Zarf service account: %w", err)
}
Expand Down
27 changes: 16 additions & 11 deletions src/pkg/cluster/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"

"github.com/avast/retry-go/v4"
"github.com/defenseunicorns/pkg/helpers/v2"

"github.com/zarf-dev/zarf/src/api/v1alpha1"
Expand Down Expand Up @@ -172,21 +172,25 @@ type podFilter func(pod corev1.Pod) bool
// TODO: Test, refactor and/or remove.
func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interface, target podLookup, include podFilter) ([]corev1.Pod, error) {
l := logger.From(ctx)
readyPods, err := retry.DoWithData(func() ([]corev1.Pod, error) {
var readyPods []corev1.Pod
err := wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
Duration: time.Second,
Factor: 1.0,
}, func(ctx context.Context) (bool, error) {
listOpts := metav1.ListOptions{
LabelSelector: target.Selector,
}
podList, err := clientset.CoreV1().Pods(target.Namespace).List(ctx, listOpts)
if err != nil {
return nil, err
return false, nil
}
l.Debug("found pods matching the target", "count", len(podList.Items), "target", target)
// Sort the pods from newest to oldest
sort.Slice(podList.Items, func(i, j int) bool {
return podList.Items[i].CreationTimestamp.After(podList.Items[j].CreationTimestamp.Time)
})

readyPods := []corev1.Pod{}
found := []corev1.Pod{}
for _, pod := range podList.Items {
l.Debug("testing pod", "name", pod.Name)

Expand All @@ -204,7 +208,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac
isRunning := initContainer.State.Running != nil
if initContainer.Name == target.Container && isRunning {
// On running match in initContainer break this loop
readyPods = append(readyPods, pod)
found = append(found, pod)
break
}
}
Expand All @@ -213,7 +217,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac
for _, container := range pod.Status.ContainerStatuses {
isRunning := container.State.Running != nil
if container.Name == target.Container && isRunning {
readyPods = append(readyPods, pod)
found = append(found, pod)
break
}
}
Expand All @@ -222,16 +226,17 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac
l.Debug(fmt.Sprintf("checking pod for %s status", corev1.PodRunning), "pod", pod.Name, "status", status)
// Regular status checking without a container
if status == corev1.PodRunning {
readyPods = append(readyPods, pod)
found = append(found, pod)
break
}
}
}
if len(readyPods) == 0 {
return nil, fmt.Errorf("no ready pods found")
if len(found) == 0 {
return false, nil
}
return readyPods, nil
}, retry.Context(ctx), retry.Attempts(0), retry.DelayType(retry.FixedDelay), retry.Delay(time.Second))
readyPods = found
return true, nil
})
if err != nil {
return nil, err
}
Expand Down
39 changes: 22 additions & 17 deletions src/pkg/cluster/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cluster

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -15,7 +14,6 @@ import (
"time"

"github.com/Masterminds/semver/v3"
"github.com/avast/retry-go/v4"
"github.com/google/go-containerregistry/pkg/crane"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -375,10 +373,14 @@ func (c *Cluster) GetInjectorDaemonsetImage(ctx context.Context) (string, error)
l := logger.From(ctx)

var injectorImage string
err := retry.Do(func() error {
err := wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
Duration: 5 * time.Second,
Factor: 1.0,
Steps: 15,
}, func(ctx context.Context) (bool, error) {
nodes, err := c.Clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return err
return false, nil
}

// Track images across all nodes
Expand Down Expand Up @@ -415,12 +417,12 @@ func (c *Cluster) GetInjectorDaemonsetImage(ctx context.Context) (string, error)
}
}
injectorImage = latestPause.name
return nil
return true, nil
}

// Fallback to smallest image if no valid pause images
if len(allImages) == 0 {
return errors.New("no suitable image found on any node")
return false, nil
}

// Find the smallest image by size
Expand All @@ -432,11 +434,11 @@ func (c *Cluster) GetInjectorDaemonsetImage(ctx context.Context) (string, error)
}

if len(smallestImage.Names) == 0 {
return errors.New("selected image has no names")
return false, nil
}
injectorImage = smallestImage.Names[0]
return nil
}, retry.Attempts(15), retry.Delay(5*time.Second), retry.Context(ctx), retry.DelayType(retry.FixedDelay))
return true, nil
})
if err != nil {
return "", err
}
Expand Down Expand Up @@ -604,7 +606,11 @@ func (c *Cluster) createInjectorNodeportService(ctx context.Context, pkgName str
if opts.InjectorNodePort != 0 {
portConfiguration.WithNodePort(int32(opts.InjectorNodePort))
}
err := retry.Do(func() error {
err := wait.ExponentialBackoffWithContext(timeoutCtx, wait.Backoff{
Duration: 500 * time.Millisecond,
Factor: 1.0,
Steps: 10,
}, func(ctx context.Context) (bool, error) {
svcAc := v1ac.Service("zarf-injector", state.ZarfNamespaceName).
WithSpec(v1ac.ServiceSpec().
WithType(corev1.ServiceTypeNodePort).
Expand All @@ -619,20 +625,19 @@ func (c *Cluster) createInjectorNodeportService(ctx context.Context, pkgName str
var err error
svc, err = c.Clientset.CoreV1().Services(*svcAc.Namespace).Apply(ctx, svcAc, metav1.ApplyOptions{Force: true, FieldManager: FieldManagerName})
if err != nil {
return err
return false, nil
}

assignedNodePort := int(svc.Spec.Ports[0].NodePort)
if assignedNodePort == int(opts.RegistryNodePort) {
l.Info("injector service NodePort conflicts with registry NodePort, recreating service", "conflictingPort", assignedNodePort)
deleteErr := c.Clientset.CoreV1().Services(state.ZarfNamespaceName).Delete(ctx, "zarf-injector", metav1.DeleteOptions{})
if deleteErr != nil {
return deleteErr
if deleteErr := c.Clientset.CoreV1().Services(state.ZarfNamespaceName).Delete(ctx, "zarf-injector", metav1.DeleteOptions{}); deleteErr != nil {
return false, deleteErr
}
return fmt.Errorf("nodePort conflict with registry port %d", opts.RegistryNodePort)
return false, nil
}
return nil
}, retry.Attempts(10), retry.Delay(500*time.Millisecond), retry.Context(timeoutCtx))
return true, nil
})
if err != nil {
return nil, fmt.Errorf("failed to create the injector nodeport service: %w", err)
}
Expand Down
14 changes: 5 additions & 9 deletions src/pkg/cluster/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ package cluster

import (
"context"
"fmt"
"time"

"github.com/avast/retry-go/v4"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/state"
Expand All @@ -32,16 +31,13 @@ func (c *Cluster) DeleteZarfNamespace(ctx context.Context) error {
if err != nil {
return err
}
err = retry.Do(func() error {
err = wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (bool, error) {
_, err := c.Clientset.CoreV1().Namespaces().Get(ctx, state.ZarfNamespaceName, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
return nil
return true, nil
}
if err != nil {
return err
}
return fmt.Errorf("namespace still exists")
}, retry.Context(ctx), retry.Attempts(0), retry.DelayType(retry.FixedDelay), retry.Delay(time.Second))
return false, nil
})
if err != nil {
return err
}
Expand Down
Loading
Loading