Skip to content

Commit

Permalink
Merge pull request #1330 from antoninbas/fix-panic-in-WaitUntilDeploy…
Browse files Browse the repository at this point in the history
…mentAvailable

[k8s] Fix panic in WaitUntilDeploymentAvailable
  • Loading branch information
denis256 committed Aug 11, 2023
2 parents a6467d4 + f689e8c commit 9e475c5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
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"
}

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)
})
}
}

0 comments on commit 9e475c5

Please sign in to comment.