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

[k8s] Fix panic in WaitUntilDeploymentAvailable #1330

Merged
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
17 changes: 10 additions & 7 deletions modules/k8s/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ func WaitUntilDeploymentAvailableE(

// IsDeploymentAvailable returns true if all pods within the deployment are ready and started
func IsDeploymentAvailable(deploy *appsv1.Deployment) bool {
for _, dc := range deploy.Status.Conditions {
if dc.Type == appsv1.DeploymentProgressing &&
dc.Status == v1.ConditionTrue &&
dc.Reason == "NewReplicaSetAvailable" {
return true
dc := getDeploymentCondition(deploy, appsv1.DeploymentProgressing)
return dc != nil && dc.Status == v1.ConditionTrue && dc.Reason == "NewReplicaSetAvailable"
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering if it's possible to implement a test case that would generate such a situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which situation are you referring to? The panic?
Won't be possible to reproduce with a real K8s apiserver. However, I just updated the commit to add unit tests.

}

func getDeploymentCondition(deploy *appsv1.Deployment, cType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for idx := range deploy.Status.Conditions {
dc := &deploy.Status.Conditions[idx]
if dc.Type == cType {
return dc
}
}

return false
return nil
}
9 changes: 9 additions & 0 deletions modules/k8s/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ func TestTestIsDeploymentAvailable(t *testing.T) {
},
expectedResult: false,
},
{
title: "TestIsDeploymentAvailableWithoutProgressingCondition",
deploy: &appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Conditions: []appsv1.DeploymentCondition{},
},
},
expectedResult: false,
},
}

for _, tc := range testCases {
Expand Down
16 changes: 13 additions & 3 deletions modules/k8s/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@ type DeploymentNotAvailable struct {

// Error is a simple function to return a formatted error message as a string
func (err DeploymentNotAvailable) Error() string {
dc := getDeploymentCondition(err.deploy, appsv1.DeploymentProgressing)
if dc == nil {
return fmt.Sprintf(
"Deployment %s is not available, missing '%s' condition",
err.deploy.Name,
appsv1.DeploymentProgressing,
)
}
return fmt.Sprintf(
"Deployment %s is not available, reason: %s, message: %s",
"Deployment %s is not available as '%s' condition indicates that the Deployment is not complete, status: %v, reason: %s, message: %s",
err.deploy.Name,
err.deploy.Status.Conditions[0].Reason,
err.deploy.Status.Conditions[0].Message,
appsv1.DeploymentProgressing,
dc.Status,
dc.Reason,
dc.Message,
)
}

Expand Down
60 changes: 60 additions & 0 deletions modules/k8s/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package k8s

import (
"testing"

"github.com/stretchr/testify/assert"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestErrorDeploymentNotAvailable(t *testing.T) {
testCases := []struct {
title string
deploy *appsv1.Deployment
expectedErr string
}{
{
title: "NoProgressingCondition",
deploy: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Status: appsv1.DeploymentStatus{
Conditions: []appsv1.DeploymentCondition{},
},
},
expectedErr: "Deployment foo is not available, missing 'Progressing' condition",
},
{
title: "DeploymentNotComplete",
deploy: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Status: appsv1.DeploymentStatus{
Conditions: []appsv1.DeploymentCondition{
{
Type: appsv1.DeploymentProgressing,
Status: v1.ConditionTrue,
Reason: "ReplicaSetUpdated",
Message: "bar",
},
},
},
},
expectedErr: "Deployment foo is not available as 'Progressing' condition indicates that the Deployment is not complete, status: True, reason: ReplicaSetUpdated, message: bar",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.title, func(t *testing.T) {
t.Parallel()
err := NewDeploymentNotAvailableError(tc.deploy)
assert.EqualError(t, err, tc.expectedErr)
})
}
}