Skip to content

Commit

Permalink
feat: add an exit timeout of 10* no of charts sec
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavan Sokke Nagaraj committed Aug 3, 2023
1 parent 444a7ef commit 6bac6f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
71 changes: 38 additions & 33 deletions cli/cmd/cluster_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ func (r *runners) prepareCluster(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "get cluster kubeconfig")
}

if len(release.Charts) == 0 {
return errors.New("no charts found in release")
}

isReleaseReady, err := isReleaseReady(r, log, *release)
if err != nil || !isReleaseReady {
return errors.Wrap(err, "release not ready")
Expand Down Expand Up @@ -372,45 +368,54 @@ func prepareRelease(r *runners, log *logger.Logger) (*types.ReleaseInfo, error)
}

func isReleaseReady(r *runners, log *logger.Logger, release types.ReleaseInfo) (bool, error) {
isReleaseReady := false
for !isReleaseReady {
appRelease, err := r.api.GetRelease(r.appID, r.appType, release.Sequence)
if err != nil {
return false, errors.Wrap(err, "failed to get release")
}
if len(release.Charts) == 0 {
return false, errors.New("no charts found in release")
}

if len(appRelease.Charts) == 0 {
return false, errors.New("no charts found in release")
}
timeout := time.Duration(10*len(release.Charts)) * time.Second
timer := time.NewTimer(timeout)
defer timer.Stop()

for {
select {
case <-timer.C:
return false, errors.Errorf("timed out waiting for release to be ready after %s", timeout)
default:
appRelease, err := r.api.GetRelease(r.appID, r.appType, release.Sequence)
if err != nil {
return false, errors.Wrap(err, "failed to get release")
}

ready, err := areReleaseChartsPushed(appRelease.Charts)
if err != nil {
return false, errors.Wrap(err, "failed to check release charts")
} else if ready {
return true, nil
}

releaseChartsReady, err := areReleaseChartsReady(appRelease.Charts)
if err != nil {
return false, errors.Wrap(err, "failed to get release")
}
if !releaseChartsReady {
time.Sleep(time.Second * 2)
continue
}

isReleaseReady = true
}

return true, nil
}

func areReleaseChartsReady(charts []types.Chart) (bool, error) {
areChartsPushed := false
func areReleaseChartsPushed(charts []types.Chart) (bool, error) {
if len(charts) == 0 {
return false, errors.New("no charts found in release")
}

pushedChartsCount := 0
for _, chart := range charts {
if chart.Status == types.ChartStatusPushing || chart.Status == types.ChartStatusUnknown {
areChartsPushed = false
continue
} else if chart.Status == types.ChartStatusPushed {
areChartsPushed = true
} else if chart.Status == types.ChartStatusError {
switch chart.Status {
case types.ChartStatusPushed:
pushedChartsCount++
case types.ChartStatusUnknown, types.ChartStatusPushing:
// wait for the chart to be pushed
case types.ChartStatusError:
return false, errors.Errorf("chart %q failed to push: %s", chart.Name, chart.Error)
} else {
default:
return false, errors.Errorf("unknown release chart status %q", chart.Status)
}
}
return areChartsPushed, nil

return pushedChartsCount == len(charts), nil
}
6 changes: 3 additions & 3 deletions cli/cmd/cluster_prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func Test_areReleaseChartsReady(t *testing.T) {
want bool
wantErr bool
}{
{"nil charts", nil, false, false},
{"no charts", []types.Chart{}, false, false},
{"nil charts", nil, false, true},
{"no charts", []types.Chart{}, false, true},
{"one chart, no status", []types.Chart{{}}, false, true},
{"one chart, status unkown", []types.Chart{{Status: types.ChartStatusUnknown}}, false, false},
{"one chart, status pushing", []types.Chart{{Status: types.ChartStatusPushing}}, false, false},
Expand All @@ -35,7 +35,7 @@ func Test_areReleaseChartsReady(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := areReleaseChartsReady(tt.charts)
got, err := areReleaseChartsPushed(tt.charts)
if (err != nil) != tt.wantErr {
t.Errorf("areReleaseChartsReady() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 6bac6f8

Please sign in to comment.