forked from porter-dev/porter-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use retry helm agent inside worker job
- Loading branch information
Showing
2 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//go:build ee | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/helm" | ||
"github.com/porter-dev/porter/pkg/logger" | ||
"helm.sh/helm/v3/pkg/release" | ||
) | ||
|
||
type RetryHelmAgent struct { | ||
form *helm.Form | ||
l *logger.Logger | ||
agent *helm.Agent | ||
retryCount uint | ||
retryInterval time.Duration | ||
} | ||
|
||
func NewRetryHelmAgent( | ||
form *helm.Form, | ||
l *logger.Logger, | ||
retryCount uint, | ||
retryInterval time.Duration, | ||
) (*RetryHelmAgent, error) { | ||
if l == nil { | ||
l = logger.New(true, os.Stdout) | ||
} | ||
|
||
helmAgent, err := helm.GetAgentOutOfClusterConfig(form, l) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &RetryHelmAgent{ | ||
form, l, helmAgent, retryCount, retryInterval, | ||
}, nil | ||
} | ||
|
||
func (a *RetryHelmAgent) ListReleases( | ||
namespace string, | ||
filter *types.ReleaseListFilter, | ||
) ([]*release.Release, error) { | ||
for i := uint(0); i < a.retryCount; i++ { | ||
releases, err := a.agent.ListReleases(namespace, filter) | ||
|
||
if err == nil { | ||
return releases, nil | ||
} else if strings.Contains(err.Error(), "Unauthorized") { | ||
a.agent, err = helm.GetAgentOutOfClusterConfig(a.form, a.l) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error recreating helm agent for retrying ListReleases: %w", err) | ||
} | ||
} else { | ||
return nil, err | ||
} | ||
|
||
time.Sleep(a.retryInterval) | ||
} | ||
|
||
return nil, fmt.Errorf("maxiumum number of retries (%d) reached for ListReleases", a.retryCount) | ||
} | ||
|
||
func (a *RetryHelmAgent) GetReleaseHistory( | ||
name string, | ||
) ([]*release.Release, error) { | ||
for i := uint(0); i < a.retryCount; i++ { | ||
releases, err := a.agent.GetReleaseHistory(name) | ||
|
||
if err == nil { | ||
return releases, nil | ||
} else if strings.Contains(err.Error(), "Unauthorized") { | ||
a.agent, err = helm.GetAgentOutOfClusterConfig(a.form, a.l) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error recreating helm agent for retrying GetReleaseHistory: %w", err) | ||
} | ||
} else { | ||
return nil, err | ||
} | ||
|
||
time.Sleep(a.retryInterval) | ||
} | ||
|
||
return nil, fmt.Errorf("maxiumum number of retries (%d) reached for GetReleaseHistory", a.retryCount) | ||
} | ||
|
||
func (a *RetryHelmAgent) DeleteReleaseRevision( | ||
name string, | ||
version int, | ||
) error { | ||
for i := uint(0); i < a.retryCount; i++ { | ||
err := a.agent.DeleteReleaseRevision(name, version) | ||
|
||
if err == nil { | ||
return nil | ||
} else if strings.Contains(err.Error(), "Unauthorized") { | ||
a.agent, err = helm.GetAgentOutOfClusterConfig(a.form, a.l) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error recreating helm agent for retrying DeleteReleaseRevision: %w", err) | ||
} | ||
} else { | ||
return err | ||
} | ||
|
||
time.Sleep(a.retryInterval) | ||
} | ||
|
||
return fmt.Errorf("maxiumum number of retries (%d) reached for DeleteReleaseRevision", a.retryCount) | ||
} |