Skip to content
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

[supervisor] Improve IDE readiness probe #9706

Merged
merged 1 commit into from
May 3, 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: 42 additions & 27 deletions components/supervisor/pkg/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ func runIDEReadinessProbe(cfg *Config, ideConfig *IDEConfig, ide IDEKind) (deskt
if ide == DesktopIDE {
defaultProbePort = desktopIDEPort
}

switch ideConfig.ReadinessProbe.Type {
case ReadinessProcessProbe:
return
Expand All @@ -944,48 +945,62 @@ func runIDEReadinessProbe(cfg *Config, ideConfig *IDEConfig, ide IDEKind) (deskt
host = defaultIfEmpty(ideConfig.ReadinessProbe.HTTPProbe.Host, "localhost")
port = defaultIfZero(ideConfig.ReadinessProbe.HTTPProbe.Port, defaultProbePort)
url = fmt.Sprintf("%s://%s:%d/%s", schema, host, port, strings.TrimPrefix(ideConfig.ReadinessProbe.HTTPProbe.Path, "/"))
client = http.Client{Timeout: 1 * time.Second}
tick = time.NewTicker(500 * time.Millisecond)
)
defer tick.Stop()

t0 := time.Now()

for {
<-tick.C

resp, err := client.Get(url)
var body []byte
for range time.Tick(250 * time.Millisecond) {
var err error
body, err = ideStatusRequest(url)
if err != nil {
log.WithField("ide", ide.String()).WithError(err).Debug("Error running IDE readiness probe")
continue
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
log.WithField("ide", ide.String()).WithField("status", resp.StatusCode).Infof("IDE readiness took %.3f seconds", time.Since(t0).Seconds())
break
}

if ide == DesktopIDE {
bodyBytes, err := ioutil.ReadAll(resp.Body)
log.WithField("ide", ide.String()).Infof("IDE status probe body: %s", string(bodyBytes))
if err != nil {
log.WithField("ide", ide.String()).WithError(err).Infof("Error reading response body from IDE status probe.")
break
}
err = json.Unmarshal(bodyBytes, &desktopIDEStatus)
if err != nil {
log.WithField("ide", ide.String()).WithError(err).WithField("body", bodyBytes).Debugf("Error parsing JSON body from IDE status probe.")
break
}
log.WithField("ide", ide.String()).Infof("Desktop IDE status: %s", desktopIDEStatus)
}
break
}
log.WithField("ide", ide.String()).Infof("IDE readiness took %.3f seconds", time.Since(t0).Seconds())

log.WithField("ide", ide.String()).WithField("status", resp.StatusCode).Info("IDE readiness probe came back with non-200 status code")
if ide != DesktopIDE {
return
}

err := json.Unmarshal(body, &desktopIDEStatus)
if err != nil {
log.WithField("ide", ide.String()).WithError(err).WithField("body", body).Debugf("Error parsing JSON body from IDE status probe.")
return
}

log.WithField("ide", ide.String()).Infof("Desktop IDE status: %s", desktopIDEStatus)
return
}

return
}

func ideStatusRequest(url string) ([]byte, error) {
client := http.Client{Timeout: 1 * time.Second}

resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, xerrors.Errorf("IDE readiness probe came back with non-200 status code (%v)", resp.StatusCode)
}

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

return body, nil
}

func isBlacklistedEnvvar(name string) bool {
// exclude blacklisted
prefixBlacklist := []string{
Expand Down