Skip to content

Commit

Permalink
State
Browse files Browse the repository at this point in the history
  • Loading branch information
videlov committed Jan 24, 2025
1 parent f02eb92 commit 01c45ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
12 changes: 9 additions & 3 deletions pkg/lib/sidecars/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func (p *ProxyRestart) RestartProxies(ctx context.Context, expectedImage predica

warnings, hasMorePodsToRestart, err := p.restartCustomerProxies(ctx, predicates)
if err != nil {
p.logger.Error(err, "Failed to restart Customer proxies")
p.logger.Error(err, "failed to restart Customer proxies")
warnings = append(warnings, restart.RestartWarning{
Name: "n/a",
Namespace: "n/a",
Kind: "n/a",
Message: "failed to restart Customer proxies",
})
}

return warnings, hasMorePodsToRestart, nil
Expand All @@ -73,7 +79,7 @@ func (p *ProxyRestart) RestartWithPredicates(ctx context.Context, preds []predic
warnings, err := restart.Restart(ctx, p.k8sClient, podsToRestart, p.logger, failOnError)
if err != nil {
p.logger.Error(err, "Restarting pods failed")
return []restart.RestartWarning{}, false, err
return warnings, false, err
}

// if there are more pods to restart there should be a continue token in the pod list
Expand Down Expand Up @@ -101,7 +107,7 @@ func (p *ProxyRestart) restartCustomerProxies(ctx context.Context, preds []predi
warnings, hasMorePodsToRestart, err := p.RestartWithPredicates(ctx, preds, limits, false)
if err != nil {
p.logger.Error(err, "Failed to restart Customer proxies")
return []restart.RestartWarning{}, false, err
return warnings, false, err
}

if !hasMorePodsToRestart {
Expand Down
30 changes: 29 additions & 1 deletion pkg/lib/sidecars/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ var _ = Describe("RestartProxies", func() {
// when
warnings, hasMorePods, err := proxyRestarter.RestartProxies(ctx, expectedImage, helpers.DefaultSidecarResources, &istioCR)

// then
Expect(err).ToNot(HaveOccurred())
Expect(warnings).To(ContainElement(restart.RestartWarning{
Name: "n/a",
Namespace: "n/a",
Kind: "n/a",
Message: "failed to restart Customer proxies",
}))
Expect(hasMorePods).To(BeFalse())
})

It("should not return error but a warning when it fails on restart customer proxies", func() {
// given
pod := getPod("test-pods", "test-namespace", "podOwner", "ReplicaSet")
rsOwner := getReplicaSet("podOwner", "test-namespace", "rsOwner", "ReplicaSet")
rsOwnerRS := getReplicaSet("rsOwner", "test-namespace", "base", "ReplicaSet")

c := fakeClient(pod, rsOwner, rsOwnerRS)

// when
failClient := &shouldFailClient{c, false, true}

podsLister := pods.NewPods(c, &logger)
expectedImage := predicates.NewSidecarImage("istio", "1.1.0")
istioCR := helpers.GetIstioCR(expectedImage.Tag)
proxyRestarter := sidecars.NewProxyRestarter(failClient, podsLister, &logger)
warnings, hasMorePods, err := proxyRestarter.RestartProxies(ctx, expectedImage, helpers.DefaultSidecarResources, &istioCR)

// then
Expect(err).ToNot(HaveOccurred())
Expect(warnings).To(BeEmpty())
Expand Down Expand Up @@ -279,7 +307,7 @@ var _ = Describe("RestartWithPredicates", func() {
Expect(err.Error()).To(Equal("running pod restart action failed: intentionally failing client on client.Patch"))
})

It("should not return error if restarting pods fails but failOnError is false", func() {
It("should not return error and warnings if restarting pods fails with failOnError is false", func() {
// given
pod := getPod("test-pod", "test-namespace", "podOwner", "ReplicaSet")
rsOwner := getReplicaSet("podOwner", "test-namespace", "rsOwner", "ReplicaSet")
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/sidecars/restart/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Restart(ctx context.Context, c client.Client, podList *v1.PodList, logger *
if err != nil {
logger.Error(err, "pod", action.object.getKey(), "Creating pod restart action failed")
if failOnError {
return []RestartWarning{}, fmt.Errorf("creating pod restart action failed: %w", err)
return warnings, fmt.Errorf("creating pod restart action failed: %w", err)
}
continue
}
Expand All @@ -48,7 +48,7 @@ func Restart(ctx context.Context, c client.Client, podList *v1.PodList, logger *
if err != nil {
logger.Error(err, "pod", action.object.getKey(), "Running pod restart action failed")
if failOnError {
return []RestartWarning{}, fmt.Errorf("running pod restart action failed: %w", err)
return warnings, fmt.Errorf("running pod restart action failed: %w", err)
}
}
warnings = append(warnings, currentWarnings...)
Expand Down

0 comments on commit 01c45ff

Please sign in to comment.