Skip to content

Commit fe3bd40

Browse files
authored
Accept newer revisions when waiting for policies to be assigned (#1289)
When tearing down tests, we try to reassign the previous policy. If it was modified and we also expect the revision to be the same, it will never succeed, it will wait till timeout.
1 parent 8a7345c commit fe3bd40

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

internal/kibana/agents.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import (
1616
"github.com/elastic/elastic-package/internal/signal"
1717
)
1818

19-
var waitForPolicyAssignedTimeout = 10 * time.Minute
19+
var (
20+
waitForPolicyAssignedTimeout = 10 * time.Minute
21+
waitForPolicyAssignedRetryPeriod = 2 * time.Second
22+
)
2023

2124
// Agent represents an Elastic Agent enrolled with fleet.
2225
type Agent struct {
@@ -83,12 +86,12 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error {
8386
}
8487

8588
func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error {
86-
timeout := time.Now().Add(waitForPolicyAssignedTimeout)
87-
for {
88-
if time.Now().After(timeout) {
89-
return errors.New("timeout: policy hasn't been assigned in time")
90-
}
89+
timeout := time.NewTimer(waitForPolicyAssignedTimeout)
90+
defer timeout.Stop()
91+
ticker := time.NewTicker(waitForPolicyAssignedRetryPeriod)
92+
defer ticker.Stop()
9193

94+
for {
9295
if signal.SIGINT() {
9396
return errors.New("SIGINT: cancel waiting for policy assigned")
9497
}
@@ -99,13 +102,19 @@ func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error {
99102
}
100103
logger.Debugf("Agent data: %s", agent.String())
101104

102-
if agent.PolicyID == p.ID && agent.PolicyRevision == p.Revision {
105+
if agent.PolicyID == p.ID && agent.PolicyRevision >= p.Revision {
103106
logger.Debugf("Policy revision assigned to the agent (ID: %s)...", a.ID)
104107
break
105108
}
106109

107110
logger.Debugf("Wait until the policy (ID: %s, revision: %d) is assigned to the agent (ID: %s)...", p.ID, p.Revision, a.ID)
108-
time.Sleep(2 * time.Second)
111+
select {
112+
case <-timeout.C:
113+
return errors.New("timeout: policy hasn't been assigned in time")
114+
case <-ticker.C:
115+
continue
116+
}
117+
109118
}
110119
return nil
111120
}

0 commit comments

Comments
 (0)