Skip to content

Commit

Permalink
[k8s] Fix panic in WaitUntilDeploymentAvailable
Browse files Browse the repository at this point in the history
The `func (err DeploymentNotAvailable) Error()` method should not assume
that `Status.Conditions[0]` is always valid for the Deployment.

Fixes #1329

Signed-off-by: Antonin Bas <abas@vmware.com>
  • Loading branch information
antoninbas committed Aug 9, 2023
1 parent a6467d4 commit 2e00fea
Show file tree
Hide file tree
Showing 2 changed files with 23 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
}
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

0 comments on commit 2e00fea

Please sign in to comment.