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

Refactor deployment healthcheck #2750

Closed
Closed
Show file tree
Hide file tree
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
49 changes: 49 additions & 0 deletions pkg/skaffold/deploy/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package deploy

import (
"context"
"io"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/resources"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
)

type Resource interface {
// String returns the string representation of a resource.
String() string
// Type returns the type of the resource
Type() string
// Status returns the resource status
Status() *resources.Status
// Namespace returns the resource namespace
Namespace() string
// Name returns the resource name
Name() string
// CheckStatus performs the resource status check.
CheckStatus(ctx context.Context, runCtx *runcontext.RunContext)
// Deadline returns the status check deadline for the resource
Deadline() time.Duration
// UpdateStatus updates the status of a resource with the given error message
UpdateStatus(msg string, reason string, err error)
// IsStatusCheckComplete returns if a status check is complete for a resource
IsStatusCheckComplete() bool
// ReportSinceLastUpdated writes the last known status to out if it hasn't been reported earlier.
ReportSinceLastUpdated(out io.Writer)
}
88 changes: 88 additions & 0 deletions pkg/skaffold/deploy/resources/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"context"
"strings"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
)

const (
DeploymentType = "deployment"
KubectlKilled = "Killed"
KubectlConnection = "KubectlConnection"
)

type Deployment struct {
*ResourceObj
deadline time.Duration
}

func NewDeployment(name string, ns string, deadline time.Duration) *Deployment {
return &Deployment{
ResourceObj: &ResourceObj{name: name, namespace: ns, rType: DeploymentType},
deadline: deadline,
}
}

func (d *Deployment) CheckStatus(ctx context.Context, runCtx *runcontext.RunContext) {
kubeCtl := kubectl.NewFromRunContext(runCtx)
b, err := kubeCtl.RunOut(ctx, "rollout", "status", "deployment", d.name, "--namespace", d.namespace, "--watch=false")
if err != nil {
reason, details := parseKubectlError(err.Error())
d.UpdateStatus(details, reason, err)
if reason != KubectlConnection {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would want to keep polling for rollout status if there was a connectivity issue.

d.checkComplete()
}
return
}
if s := string(b); strings.Contains(s, "successfully rolled out") {
d.UpdateStatus(s, s, nil)
d.checkComplete()
return
}
d.UpdateStatus(string(b), string(b), nil)
}

func (d *Deployment) Deadline() time.Duration {
return d.deadline
}

func parseKubectlError(errMsg string) (string, string) {
errMsg = strings.TrimSuffix(errMsg, "\n")
if strings.Contains(errMsg, "Unable to connect to the server") {
return KubectlConnection, errMsg
}
if strings.Contains(errMsg, "signal: killed") {
return KubectlKilled, "kubectl killed due to timeout"
}
return errMsg, errMsg
}

func (d *Deployment) WithError(err error) *Deployment {
d.UpdateStatus("", err.Error(), err)
return d
}

func (d *Deployment) WithStatus(status Status) *Deployment {
d.status = status
return d
}
85 changes: 85 additions & 0 deletions pkg/skaffold/deploy/resources/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"context"
"fmt"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestDeploymentCheckStatus(t *testing.T) {
rolloutCmd := "kubectl --context kubecontext rollout status deployment dep --namespace test --watch=false"
tests := []struct {
description string
command util.Command
expectedErr string
expectedReason string
complete bool
}{
{
description: "rollout status success",
command: testutil.NewFakeCmd(t).
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
WithRunOut(rolloutCmd, "deployment dep successfully rolled out"),
expectedReason: "deployment dep successfully rolled out",
complete: true,
}, {
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
description: "resource not complete",
command: testutil.NewFakeCmd(t).
WithRunOut(rolloutCmd, "Waiting for replicas to be available"),
expectedReason: "Waiting for replicas to be available",
}, {
description: "no output",
command: testutil.NewFakeCmd(t).
WithRunOut(rolloutCmd, ""),
}, {
description: "rollout status error",
command: testutil.NewFakeCmd(t).
WithRunOutErr(rolloutCmd, "", fmt.Errorf("error")),
expectedErr: "error",
complete: true,
},
{
description: "rollout kubectl client connection error",
command: testutil.NewFakeCmd(t).
WithRunOutErr(rolloutCmd, "", fmt.Errorf("Unable to connect to the server")),
expectedReason: "KubectlConnection",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, test.command)
r := Deployment{ResourceObj: &ResourceObj{namespace: "test", name: "dep"}}
runCtx := &runcontext.RunContext{
KubeContext: "kubecontext",
}

r.CheckStatus(context.Background(), runCtx)
t.CheckDeepEqual(test.complete, r.IsStatusCheckComplete())
if test.expectedErr != "" {
t.CheckErrorContains(test.expectedErr, r.Status().Error())
} else {
t.CheckDeepEqual(r.status.reason, test.expectedReason)
}
})
}
}
105 changes: 105 additions & 0 deletions pkg/skaffold/deploy/resources/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"fmt"
"io"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
)

const (
TabHeader = " -"
)

type ResourceObj struct {
name string
namespace string
rType string
status Status
}

func (r *ResourceObj) String() string {
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("%s/%s", r.rType, r.name)
}

func (r *ResourceObj) Type() string {
return r.rType
}

func (r *ResourceObj) UpdateStatus(msg string, reason string, err error) {
newStatus := Status{details: msg, reason: reason, err: err}
if !r.status.Equals(&newStatus) {
r.status.err = err
r.status.details = strings.TrimSuffix(msg, "\n")
tejal29 marked this conversation as resolved.
Show resolved Hide resolved
r.status.reason = strings.TrimSuffix(reason, "\n")
r.status.updated = true
}
}

func (r *ResourceObj) Status() *Status {
return &r.status
}

func (r *ResourceObj) Namespace() string {
return r.namespace
}

func (r *ResourceObj) Name() string {
return r.name
}

func (r *ResourceObj) checkComplete() {
r.status.completed = true
}

func (r *ResourceObj) IsStatusCheckComplete() bool {
return r.status.completed
}

func (r *ResourceObj) ReportSinceLastUpdated(out io.Writer) {
if !r.status.updated {
return
}
r.status.updated = false
color.Default.Fprintln(out, fmt.Sprintf("%s %s %s", TabHeader, r.String(), r.status.String()))
}

type Status struct {
err error
reason string
details string
updated bool
completed bool
}

func (rs *Status) Error() error {
return rs.err
}

func (rs *Status) Equals(other *Status) bool {
return strings.TrimSuffix(rs.reason, "\n") == strings.TrimSuffix(other.reason, "\n")
}

func (rs *Status) String() string {
if rs.err != nil {
return fmt.Sprintf("is pending due to %s", strings.TrimSuffix(rs.err.Error(), "\n"))
}
return fmt.Sprintf("is pending due to %s", rs.details)
}
Loading