Skip to content

Commit

Permalink
Correct logic to return true if allOf conditions in wait pass (#1565)
Browse files Browse the repository at this point in the history
Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>
  • Loading branch information
PrasadG193 authored Jul 24, 2022
1 parent f815219 commit 5e26f7a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
9 changes: 6 additions & 3 deletions pkg/function/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/jsonpath"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/log"
Expand Down Expand Up @@ -114,7 +115,7 @@ func waitForCondition(ctx context.Context, dynCli dynamic.Interface, waitCond Wa
result, evalErr = evaluateCondition(ctx, dynCli, cond)
if evalErr != nil {
// TODO: Fail early if the error is due to jsonpath syntax
log.Debug().WithError(evalErr).Print("Failed to evaluate the condition")
log.Debug().WithError(evalErr).Print("Failed to evaluate the condition", field.M{"result": result})
return false, nil
}
if result {
Expand All @@ -125,14 +126,15 @@ func waitForCondition(ctx context.Context, dynCli dynamic.Interface, waitCond Wa
result, evalErr = evaluateCondition(ctx, dynCli, cond)
if evalErr != nil {
// TODO: Fail early if the error is due to jsonpath syntax
log.Debug().WithError(evalErr).Print("Failed to evaluate the condition")
log.Debug().WithError(evalErr).Print("Failed to evaluate the condition", field.M{"result": result})
return false, nil
}
// Retry if any condition fails
if !result {
return false, nil
}
}
return false, nil
return result, nil
})
err = errors.Wrap(err, "Failed to wait for the condition to be met")
if evalErr != nil {
Expand Down Expand Up @@ -160,6 +162,7 @@ func evaluateCondition(ctx context.Context, dynCli dynamic.Interface, cond Condi
if err = t.Execute(buf, nil); err != nil {
return false, errors.WithStack(err)
}
log.Debug().Print(fmt.Sprintf("Condition evaluation result: %s", strings.TrimSpace(buf.String())))
return strings.TrimSpace(buf.String()) == "true", nil
}

Expand Down
57 changes: 53 additions & 4 deletions pkg/function/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import (
var _ = Suite(&WaitSuite{})

type WaitSuite struct {
cli kubernetes.Interface
namespace string
deploy string
cli kubernetes.Interface
namespace string
deploy string
statefulset string
}

func (s *WaitSuite) SetUpSuite(c *C) {
Expand All @@ -49,11 +50,13 @@ func (s *WaitSuite) SetUpSuite(c *C) {
}
cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
c.Assert(err, IsNil)

d, err := s.cli.AppsV1().Deployments(cns.Name).Create(context.TODO(), testutil.NewTestDeployment(int32(1)), metav1.CreateOptions{})
c.Assert(err, IsNil)
sts, err := s.cli.AppsV1().StatefulSets(cns.Name).Create(context.TODO(), testutil.NewTestStatefulSet(int32(1)), metav1.CreateOptions{})
c.Assert(err, IsNil)
s.namespace = cns.Name
s.deploy = d.Name
s.statefulset = sts.Name
}

func (s *WaitSuite) TearDownSuite(c *C) {
Expand Down Expand Up @@ -152,6 +155,48 @@ func waitDeployPhase(namespace, deploy string) crv1alpha1.BlueprintPhase {
}
}

func waitStatefulSetPhase(namespace, sts string) crv1alpha1.BlueprintPhase {
return crv1alpha1.BlueprintPhase{
Name: "waitStsReady",
Func: WaitFuncName,
Args: map[string]interface{}{
WaitTimeoutArg: "1m",
WaitConditionsArg: map[string]interface{}{
"allOf": []interface{}{
map[string]interface{}{
"condition": `{{ if (eq {$.spec.replicas} {$.status.availableReplicas})}}
true
{{ else }}
false
{{ end }}`,
"objectReference": map[string]interface{}{
"apiVersion": "v1",
"group": "apps",
"resource": "statefulsets",
"name": sts,
"namespace": namespace,
},
},
map[string]interface{}{
"condition": `{{ if (eq {$.spec.replicas} {$.status.readyReplicas})}}
true
{{ else }}
false
{{ end }}`,
"objectReference": map[string]interface{}{
"apiVersion": "v1",
"group": "apps",
"resource": "statefulsets",
"name": sts,
"namespace": namespace,
},
},
},
},
},
}
}

func newWaitBlueprint(phases ...crv1alpha1.BlueprintPhase) *crv1alpha1.Blueprint {
return &crv1alpha1.Blueprint{
Actions: map[string]*crv1alpha1.BlueprintAction{
Expand All @@ -173,6 +218,10 @@ func (s *WaitSuite) TestWait(c *C) {
bp: newWaitBlueprint(waitDeployPhase(s.namespace, s.deploy)),
checker: IsNil,
},
{
bp: newWaitBlueprint(waitStatefulSetPhase(s.namespace, s.statefulset)),
checker: IsNil,
},
{
bp: newWaitBlueprint(waitNsPhase(s.namespace)),
checker: IsNil,
Expand Down

0 comments on commit 5e26f7a

Please sign in to comment.