Skip to content

[ws-manager] Refactor workspace probes #14109

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

Merged
merged 1 commit into from
Oct 23, 2022
Merged
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
69 changes: 34 additions & 35 deletions components/ws-manager/pkg/manager/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/tracing"
"golang.org/x/xerrors"
"k8s.io/apimachinery/pkg/util/json"
)

Expand Down Expand Up @@ -77,42 +78,12 @@ func (p *WorkspaceReadyProbe) Run(ctx context.Context) WorkspaceProbeResult {
},
}

type probeResult struct {
Ok bool `json:"ok"`
}

callReadyURL := func(url string) (bool, *probeResult, error) {
resp, err := client.Get(p.readyURL)
if err != nil {
return false, nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return false, nil, nil
}

rawBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, nil, err
}

var result probeResult
err = json.Unmarshal(rawBody, &result)
if err != nil {
return false, nil, err
}

return true, &result, nil
}

for {
if ctx.Err() != nil {
return WorkspaceProbeStopped
}

isOk, result, err := callReadyURL(p.readyURL)
result, err := callWorkspaceProbe(p.readyURL, client)
if err != nil {
urlerr, ok := err.(*url.Error)
if !ok || !urlerr.Timeout() {
Expand All @@ -125,17 +96,45 @@ func (p *WorkspaceReadyProbe) Run(ctx context.Context) WorkspaceProbeResult {
continue
}

if !isOk {
if !result.Ok {
log.WithField("url", p.readyURL).Debug("workspace did not respond to ready probe with OK status")
time.Sleep(p.RetryDelay)
continue
}

if result.Ok {
break
}
break
}

// workspace is actually ready
return WorkspaceProbeReady
}

type probeResult struct {
Ok bool `json:"ok"`
}

func callWorkspaceProbe(url string, client *http.Client) (*probeResult, error) {
resp, err := client.Get(url)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, xerrors.Errorf("workspace probe request returned %v as status code", resp.StatusCode)
}

rawBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var result probeResult
err = json.Unmarshal(rawBody, &result)
if err != nil {
return nil, err
}

return &result, nil
}