Skip to content

Commit

Permalink
Check servers health before setting status to running
Browse files Browse the repository at this point in the history
Check /healthz endpoint on

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed May 25, 2020
1 parent ca8359c commit 8dd56e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
attributes:
protocol: http
type: ide
path: /static/
discoverable: false
secure: true
cookiesAuthEnabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
attributes:
protocol: http
type: ide
path: /static/
discoverable: false
secure: true
cookiesAuthEnabled: true
Expand Down
27 changes: 26 additions & 1 deletion pkg/controller/workspace/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package workspace
import (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"strings"

Expand Down Expand Up @@ -76,7 +78,7 @@ func (r *ReconcileWorkspace) updateWorkspaceStatus(workspace *v1alpha1.Workspace
return reconcileResult, reconcileError
}

func SyncWorkspaceIdeURL(workspace *v1alpha1.Workspace, exposedEndpoints map[string]v1alpha1.ExposedEndpointList, clusterAPI provision.ClusterAPI) (ok bool, err error) {
func syncWorkspaceIdeURL(workspace *v1alpha1.Workspace, exposedEndpoints map[string]v1alpha1.ExposedEndpointList, clusterAPI provision.ClusterAPI) (ok bool, err error) {
ideUrl := getIdeUrl(exposedEndpoints)

if workspace.Status.IdeUrl == ideUrl {
Expand All @@ -87,6 +89,29 @@ func SyncWorkspaceIdeURL(workspace *v1alpha1.Workspace, exposedEndpoints map[str
return false, err
}

func checkServerStatus(workspace *v1alpha1.Workspace) (ok bool, err error) {
ideUrl := workspace.Status.IdeUrl
if ideUrl == "" {
return false, nil
}
healthz, err := url.Parse(ideUrl)
if err != nil {
return false, err
}
healthz.Path = "healthz"

resp, err := http.Get(healthz.String())
if err != nil {
return false, err
}
if resp.StatusCode == 404 {
// Compatibility: assume endpoint is unimplemented.
return true, nil
}
ok = (resp.StatusCode / 100) == 2
return ok, nil
}

func getIdeUrl(exposedEndpoints map[string]v1alpha1.ExposedEndpointList) string {
for _, endpoints := range exposedEndpoints {
for _, endpoint := range endpoints {
Expand Down
10 changes: 9 additions & 1 deletion pkg/controller/workspace/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
origLog "log"
"os"
"strings"
"time"

"github.com/che-incubator/che-workspace-operator/pkg/common"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -249,7 +250,7 @@ func (r *ReconcileWorkspace) Reconcile(request reconcile.Request) (reconcileResu
}
reconcileStatus.Conditions = append(reconcileStatus.Conditions, workspacev1alpha1.WorkspaceRoutingReady)

statusOk, err := SyncWorkspaceIdeURL(workspace, routingStatus.ExposedEndpoints, clusterAPI)
statusOk, err := syncWorkspaceIdeURL(workspace, routingStatus.ExposedEndpoints, clusterAPI)
if err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -313,6 +314,13 @@ func (r *ReconcileWorkspace) Reconcile(request reconcile.Request) (reconcileResu
}
reconcileStatus.Conditions = append(reconcileStatus.Conditions, workspacev1alpha1.WorkspaceDeploymentReady)

serverReady, err := checkServerStatus(workspace)
if err != nil {
return reconcile.Result{}, err
}
if !serverReady {
return reconcile.Result{RequeueAfter: 1 * time.Second}, nil
}
reconcileStatus.Phase = workspacev1alpha1.WorkspaceStatusRunning
return reconcile.Result{}, nil
}
Expand Down

0 comments on commit 8dd56e9

Please sign in to comment.