From 8d9bfef9f043c61a48109b3a911f4d7ed65d5ea2 Mon Sep 17 00:00:00 2001 From: Maria Shaldybin Date: Tue, 25 Jul 2023 16:24:56 +0000 Subject: [PATCH] Check last updated endpoint before polling policies --- .../policy_client/internal_policy_client.go | 9 + .../cmd/vxlan-policy-agent/main.go | 11 +- .../vxlan-policy-agent/converger/converger.go | 28 +- .../converger/converger_test.go | 299 +++++++++++------- .../converger/fakes/policy_client.go | 103 ++++++ 5 files changed, 331 insertions(+), 119 deletions(-) create mode 100644 src/code.cloudfoundry.org/vxlan-policy-agent/converger/fakes/policy_client.go diff --git a/src/code.cloudfoundry.org/vendor/code.cloudfoundry.org/policy_client/internal_policy_client.go b/src/code.cloudfoundry.org/vendor/code.cloudfoundry.org/policy_client/internal_policy_client.go index 3d283d36d..6fbf9f8d7 100644 --- a/src/code.cloudfoundry.org/vendor/code.cloudfoundry.org/policy_client/internal_policy_client.go +++ b/src/code.cloudfoundry.org/vendor/code.cloudfoundry.org/policy_client/internal_policy_client.go @@ -56,6 +56,15 @@ func (c *InternalClient) GetPolicies() ([]*Policy, error) { return policies.Policies, nil } +func (c *InternalClient) GetPoliciesLastUpdated() (int, error) { + var lastUpdatedTimestamp int + err := c.JsonClient.Do("GET", "/networking/v1/internal/policies_last_updated", nil, &lastUpdatedTimestamp, "") + if err != nil { + return 0, err + } + return lastUpdatedTimestamp, nil +} + func (c *InternalClient) GetPoliciesByID(ids ...string) ([]Policy, error) { var policies struct { Policies []Policy `json:"policies"` diff --git a/src/code.cloudfoundry.org/vxlan-policy-agent/cmd/vxlan-policy-agent/main.go b/src/code.cloudfoundry.org/vxlan-policy-agent/cmd/vxlan-policy-agent/main.go index 64b803ecf..d80256a08 100644 --- a/src/code.cloudfoundry.org/vxlan-policy-agent/cmd/vxlan-policy-agent/main.go +++ b/src/code.cloudfoundry.org/vxlan-policy-agent/cmd/vxlan-policy-agent/main.go @@ -232,12 +232,19 @@ func main() { go emitter.Run() } - singlePollCycle := converger.NewSinglePollCycle([]converger.Planner{dynamicPlanner}, ruleEnforcer, metricsSender, metronClient, logger) + singlePollCycle := converger.NewSinglePollCycle( + []converger.Planner{dynamicPlanner}, + ruleEnforcer, + policyClient, + metricsSender, + metronClient, + logger, + ) policyPoller := &poller.Poller{ Logger: logger, PollInterval: pollInterval, - SingleCycleFunc: singlePollCycle.DoPolicyCycle, + SingleCycleFunc: singlePollCycle.DoPolicyCycleWithLastUpdatedCheck, } asgPoller := &poller.Poller{ diff --git a/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger.go b/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger.go index 0b7ddc114..ab1ff8371 100644 --- a/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger.go +++ b/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger.go @@ -15,6 +15,11 @@ import ( "github.com/hashicorp/go-multierror" ) +//go:generate counterfeiter -o fakes/policy_client.go --fake-name PolicyClient . policyClient +type policyClient interface { + GetPoliciesLastUpdated() (int, error) +} + //go:generate counterfeiter -o fakes/planner.go --fake-name Planner . Planner type Planner interface { GetPolicyRulesAndChain() (enforcer.RulesWithChain, error) @@ -36,6 +41,8 @@ type SinglePollCycle struct { planners []Planner enforcer ruleEnforcer metricsSender metricsSender + policyClient policyClient + lastUpdated int logger lager.Logger policyRuleSets map[enforcer.Chain]enforcer.RulesWithChain asgRuleSets map[enforcer.LiveChain]enforcer.RulesWithChain @@ -45,11 +52,13 @@ type SinglePollCycle struct { asgMutex sync.Locker } -func NewSinglePollCycle(planners []Planner, re ruleEnforcer, ms metricsSender, metronClient loggingclient.IngressClient, logger lager.Logger) *SinglePollCycle { +func NewSinglePollCycle(planners []Planner, re ruleEnforcer, p policyClient, ms metricsSender, metronClient loggingclient.IngressClient, logger lager.Logger) *SinglePollCycle { return &SinglePollCycle{ planners: planners, enforcer: re, + policyClient: p, metricsSender: ms, + lastUpdated: 0, logger: logger, metronClient: metronClient, policyMutex: new(sync.Mutex), @@ -64,6 +73,23 @@ const metricASGEnforceDuration = "asgIptablesEnforceTime" const metricASGCleanupDuration = "asgIptablesCleanupTime" const metricASGPollDuration = "asgTotalPollTime" +func (m *SinglePollCycle) DoPolicyCycleWithLastUpdatedCheck() error { + lastUpdated, err := m.policyClient.GetPoliciesLastUpdated() + if err != nil { + m.logger.Error("error-getting-policies-last-updated", err) + return m.DoPolicyCycle() + } + if m.lastUpdated == 0 || lastUpdated > m.lastUpdated { + m.logger.Debug("running-poll-cycle-for-updated-policies", lager.Data{"last-updated-remotely": lastUpdated, "last-updated-locally": m.lastUpdated}) + m.lastUpdated = lastUpdated + return m.DoPolicyCycle() + } + + m.logger.Debug("skipping-poll-cycle", lager.Data{"last-updated-remotely": lastUpdated, "last-updated-locally": m.lastUpdated}) + + return nil +} + func (m *SinglePollCycle) DoPolicyCycle() error { m.policyMutex.Lock() diff --git a/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger_test.go b/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger_test.go index bdd92e985..6d3609393 100644 --- a/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger_test.go +++ b/src/code.cloudfoundry.org/vxlan-policy-agent/converger/converger_test.go @@ -22,13 +22,14 @@ import ( ) var _ = Describe("Single Poll Cycle", func() { - Describe("DoPolicyCycle", func() { + Describe("Policies", func() { var ( p *converger.SinglePollCycle fakePolicyPlanner *fakes.Planner fakeLocalPlanner *fakes.Planner fakeRemotePlanner *fakes.Planner fakeEnforcer *fakes.RuleEnforcer + fakePolicyClient *fakes.PolicyClient metricsSender *fakes.MetricsSender fakeMetronClient *diegologgingclientfakes.FakeIngressClient localRulesWithChain enforcer.RulesWithChain @@ -42,6 +43,7 @@ var _ = Describe("Single Poll Cycle", func() { fakeLocalPlanner = &fakes.Planner{} fakeRemotePlanner = &fakes.Planner{} fakeEnforcer = &fakes.RuleEnforcer{} + fakePolicyClient = &fakes.PolicyClient{} fakeMetronClient = &diegologgingclientfakes.FakeIngressClient{} metricsSender = &fakes.MetricsSender{} logger = lagertest.NewTestLogger("test") @@ -49,6 +51,7 @@ var _ = Describe("Single Poll Cycle", func() { p = converger.NewSinglePollCycle( []converger.Planner{fakeLocalPlanner, fakeRemotePlanner, fakePolicyPlanner}, fakeEnforcer, + fakePolicyClient, metricsSender, fakeMetronClient, logger, @@ -84,163 +87,223 @@ var _ = Describe("Single Poll Cycle", func() { fakePolicyPlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, nil) }) - It("enforces local, remote and policy rules on configured interval", func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) - Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) - Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) - - rws := fakeEnforcer.EnforceRulesAndChainArgsForCall(0) - Expect(rws).To(Equal(localRulesWithChain)) - rws = fakeEnforcer.EnforceRulesAndChainArgsForCall(1) - Expect(rws).To(Equal(remoteRulesWithChain)) - rws = fakeEnforcer.EnforceRulesAndChainArgsForCall(2) - Expect(rws).To(Equal(policyRulesWithChain)) - }) + Describe("DoPolicyCycleWithLastUpdatedCheck", func() { + Context("when policy server returns an error getting last updated date", func() { + BeforeEach(func() { + fakePolicyClient.GetPoliciesLastUpdatedReturns(0, errors.New("endpoint-does-not-exist")) + }) - It("emits time metrics", func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(metricsSender.SendDurationCallCount()).To(Equal(2)) - name, _ := metricsSender.SendDurationArgsForCall(0) - Expect(name).To(Equal("iptablesEnforceTime")) - name, _ = metricsSender.SendDurationArgsForCall(1) - Expect(name).To(Equal("totalPollTime")) - }) + It("runs policy cycle", func() { + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(0)) + err := p.DoPolicyCycleWithLastUpdatedCheck() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + }) + }) - Context("when a ruleset has not changed since the last poll cycle", func() { - BeforeEach(func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + Context("when never called before", func() { + It("runs policy cycle", func() { + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(0)) + err := p.DoPolicyCycleWithLastUpdatedCheck() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + }) }) - It("does not re-write the ip tables rules", func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Context("when called before", func() { + BeforeEach(func() { + fakePolicyClient.GetPoliciesLastUpdatedReturns(10, nil) + err := p.DoPolicyCycleWithLastUpdatedCheck() + Expect(err).NotTo(HaveOccurred()) + }) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + Context("when policy server last updated is newer", func() { + BeforeEach(func() { + fakePolicyClient.GetPoliciesLastUpdatedReturns(20, nil) + }) + + It("runs policy cycle", func() { + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + err := p.DoPolicyCycleWithLastUpdatedCheck() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + }) + }) + + Context("when policy server last updated is the same", func() { + BeforeEach(func() { + fakePolicyClient.GetPoliciesLastUpdatedReturns(10, nil) + }) + + It("doesn't run policy cycle", func() { + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + err := p.DoPolicyCycleWithLastUpdatedCheck() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + }) + }) }) }) - Context("when a ruleset has changed since the last poll cycle", func() { - BeforeEach(func() { + Describe("DoPolicyCycle", func() { + It("enforces local, remote and policy rules on configured interval", func() { err := p.DoPolicyCycle() Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) + Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(1)) Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) - localRulesWithChain.Rules = []rules.IPTablesRule{[]string{"new-rule"}} - fakeLocalPlanner.GetPolicyRulesAndChainReturns(localRulesWithChain, nil) - }) - - It("re-writes the ip tables rules for that chain", func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(4)) + rws := fakeEnforcer.EnforceRulesAndChainArgsForCall(0) + Expect(rws).To(Equal(localRulesWithChain)) + rws = fakeEnforcer.EnforceRulesAndChainArgsForCall(1) + Expect(rws).To(Equal(remoteRulesWithChain)) + rws = fakeEnforcer.EnforceRulesAndChainArgsForCall(2) + Expect(rws).To(Equal(policyRulesWithChain)) }) - It("logs a message about writing ip tables rules", func() { + It("emits time metrics", func() { err := p.DoPolicyCycle() Expect(err).NotTo(HaveOccurred()) - Expect(logger).To(gbytes.Say("poll-cycle.*updating iptables rules.*new rules.*new-rule.*num new rules.*1.*num old rules.*1.*old rules.*local-rule")) + Expect(metricsSender.SendDurationCallCount()).To(Equal(2)) + name, _ := metricsSender.SendDurationArgsForCall(0) + Expect(name).To(Equal("iptablesEnforceTime")) + name, _ = metricsSender.SendDurationArgsForCall(1) + Expect(name).To(Equal("totalPollTime")) }) - }) - Context("when a ruleset has all rules removed since the last poll cycle", func() { - BeforeEach(func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) - localRulesWithChain.Rules = []rules.IPTablesRule{} - fakeLocalPlanner.GetPolicyRulesAndChainReturns(localRulesWithChain, nil) - }) + Context("when a ruleset has not changed since the last poll cycle", func() { + BeforeEach(func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + }) - It("re-writes the ip tables rules for that chain", func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) - Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + It("does not re-write the ip tables rules", func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(4)) + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + }) }) - }) - Context("when a new empty chain is created", func() { - BeforeEach(func() { - localRulesWithChain.Rules = []rules.IPTablesRule{} - fakeLocalPlanner.GetPolicyRulesAndChainReturns(localRulesWithChain, nil) - }) + Context("when a ruleset has changed since the last poll cycle", func() { + BeforeEach(func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + localRulesWithChain.Rules = []rules.IPTablesRule{[]string{"new-rule"}} + fakeLocalPlanner.GetPolicyRulesAndChainReturns(localRulesWithChain, nil) + }) - It("enforces the rules for that chain", func() { - err := p.DoPolicyCycle() - Expect(err).NotTo(HaveOccurred()) + It("re-writes the ip tables rules for that chain", func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) - Expect(metricsSender.SendDurationCallCount()).To(Equal(2)) - }) - }) + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(4)) + }) - Context("when the local planner errors", func() { - BeforeEach(func() { - fakeLocalPlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, errors.New("eggplant")) + It("logs a message about writing ip tables rules", func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(logger).To(gbytes.Say("poll-cycle.*updating iptables rules.*new rules.*new-rule.*num new rules.*1.*num old rules.*1.*old rules.*local-rule")) + }) }) - It("logs the error and returns", func() { - err := p.DoPolicyCycle() - Expect(err).To(MatchError("get-rules: eggplant")) + Context("when a ruleset has all rules removed since the last poll cycle", func() { + BeforeEach(func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + localRulesWithChain.Rules = []rules.IPTablesRule{} + fakeLocalPlanner.GetPolicyRulesAndChainReturns(localRulesWithChain, nil) + }) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(0)) - Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) - }) - }) + It("re-writes the ip tables rules for that chain", func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + Expect(fakeLocalPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Expect(fakeRemotePlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) + Expect(fakePolicyPlanner.GetPolicyRulesAndChainCallCount()).To(Equal(2)) - Context("when the remote planner errors", func() { - BeforeEach(func() { - fakeRemotePlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, errors.New("eggplant")) + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(4)) + }) }) - It("logs the error and returns", func() { - err := p.DoPolicyCycle() - Expect(err).To(MatchError("get-rules: eggplant")) + Context("when a new empty chain is created", func() { + BeforeEach(func() { + localRulesWithChain.Rules = []rules.IPTablesRule{} + fakeLocalPlanner.GetPolicyRulesAndChainReturns(localRulesWithChain, nil) + }) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(1)) - Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + It("enforces the rules for that chain", func() { + err := p.DoPolicyCycle() + Expect(err).NotTo(HaveOccurred()) + + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(3)) + Expect(metricsSender.SendDurationCallCount()).To(Equal(2)) + }) }) - }) - Context("when the policy planner errors", func() { - BeforeEach(func() { - fakePolicyPlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, errors.New("eggplant")) + Context("when the local planner errors", func() { + BeforeEach(func() { + fakeLocalPlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, errors.New("eggplant")) + }) + + It("logs the error and returns", func() { + err := p.DoPolicyCycle() + Expect(err).To(MatchError("get-rules: eggplant")) + + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(0)) + Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + }) }) - It("logs the error and returns", func() { - err := p.DoPolicyCycle() - Expect(err).To(MatchError("get-rules: eggplant")) + Context("when the remote planner errors", func() { + BeforeEach(func() { + fakeRemotePlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, errors.New("eggplant")) + }) - Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(2)) - Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + It("logs the error and returns", func() { + err := p.DoPolicyCycle() + Expect(err).To(MatchError("get-rules: eggplant")) + + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(1)) + Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + }) }) - }) - Context("when policy enforcer errors", func() { - BeforeEach(func() { - fakeEnforcer.EnforceRulesAndChainReturns("professional tests", errors.New("eggplant")) + Context("when the policy planner errors", func() { + BeforeEach(func() { + fakePolicyPlanner.GetPolicyRulesAndChainReturns(policyRulesWithChain, errors.New("eggplant")) + }) + + It("logs the error and returns", func() { + err := p.DoPolicyCycle() + Expect(err).To(MatchError("get-rules: eggplant")) + + Expect(fakeEnforcer.EnforceRulesAndChainCallCount()).To(Equal(2)) + Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + }) }) - It("logs the error and returns", func() { - err := p.DoPolicyCycle() - Expect(err).To(MatchError("enforce: eggplant")) + Context("when policy enforcer errors", func() { + BeforeEach(func() { + fakeEnforcer.EnforceRulesAndChainReturns("professional tests", errors.New("eggplant")) + }) - Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + It("logs the error and returns", func() { + err := p.DoPolicyCycle() + Expect(err).To(MatchError("enforce: eggplant")) + + Expect(metricsSender.SendDurationCallCount()).To(Equal(0)) + }) }) }) }) @@ -261,6 +324,7 @@ var _ = Describe("Single Poll Cycle", func() { fakeEnforcer = &fakes.RuleEnforcer{} metricsSender = &fakes.MetricsSender{} fakeMetronClient = &diegologgingclientfakes.FakeIngressClient{} + fakePolicyClient := &fakes.PolicyClient{} logger = lagertest.NewTestLogger("test") fakeEnforcer.EnforceRulesAndChainStub = func(chain enforcer.RulesWithChain) (string, error) { @@ -270,6 +334,7 @@ var _ = Describe("Single Poll Cycle", func() { p = converger.NewSinglePollCycle( []converger.Planner{fakeASGPlanner}, fakeEnforcer, + fakePolicyClient, metricsSender, fakeMetronClient, logger, @@ -732,9 +797,11 @@ var _ = Describe("Single Poll Cycle", func() { var otherRulesWithChain []enforcer.RulesWithChain BeforeEach(func() { fakeOtherPlanner = &fakes.Planner{} + fakePolicyClient := &fakes.PolicyClient{} p = converger.NewSinglePollCycle( []converger.Planner{fakeASGPlanner, fakeOtherPlanner}, fakeEnforcer, + fakePolicyClient, metricsSender, fakeMetronClient, logger, diff --git a/src/code.cloudfoundry.org/vxlan-policy-agent/converger/fakes/policy_client.go b/src/code.cloudfoundry.org/vxlan-policy-agent/converger/fakes/policy_client.go new file mode 100644 index 000000000..0638427ea --- /dev/null +++ b/src/code.cloudfoundry.org/vxlan-policy-agent/converger/fakes/policy_client.go @@ -0,0 +1,103 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package fakes + +import ( + "sync" +) + +type PolicyClient struct { + GetPoliciesLastUpdatedStub func() (int, error) + getPoliciesLastUpdatedMutex sync.RWMutex + getPoliciesLastUpdatedArgsForCall []struct { + } + getPoliciesLastUpdatedReturns struct { + result1 int + result2 error + } + getPoliciesLastUpdatedReturnsOnCall map[int]struct { + result1 int + result2 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *PolicyClient) GetPoliciesLastUpdated() (int, error) { + fake.getPoliciesLastUpdatedMutex.Lock() + ret, specificReturn := fake.getPoliciesLastUpdatedReturnsOnCall[len(fake.getPoliciesLastUpdatedArgsForCall)] + fake.getPoliciesLastUpdatedArgsForCall = append(fake.getPoliciesLastUpdatedArgsForCall, struct { + }{}) + stub := fake.GetPoliciesLastUpdatedStub + fakeReturns := fake.getPoliciesLastUpdatedReturns + fake.recordInvocation("GetPoliciesLastUpdated", []interface{}{}) + fake.getPoliciesLastUpdatedMutex.Unlock() + if stub != nil { + return stub() + } + if specificReturn { + return ret.result1, ret.result2 + } + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *PolicyClient) GetPoliciesLastUpdatedCallCount() int { + fake.getPoliciesLastUpdatedMutex.RLock() + defer fake.getPoliciesLastUpdatedMutex.RUnlock() + return len(fake.getPoliciesLastUpdatedArgsForCall) +} + +func (fake *PolicyClient) GetPoliciesLastUpdatedCalls(stub func() (int, error)) { + fake.getPoliciesLastUpdatedMutex.Lock() + defer fake.getPoliciesLastUpdatedMutex.Unlock() + fake.GetPoliciesLastUpdatedStub = stub +} + +func (fake *PolicyClient) GetPoliciesLastUpdatedReturns(result1 int, result2 error) { + fake.getPoliciesLastUpdatedMutex.Lock() + defer fake.getPoliciesLastUpdatedMutex.Unlock() + fake.GetPoliciesLastUpdatedStub = nil + fake.getPoliciesLastUpdatedReturns = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *PolicyClient) GetPoliciesLastUpdatedReturnsOnCall(i int, result1 int, result2 error) { + fake.getPoliciesLastUpdatedMutex.Lock() + defer fake.getPoliciesLastUpdatedMutex.Unlock() + fake.GetPoliciesLastUpdatedStub = nil + if fake.getPoliciesLastUpdatedReturnsOnCall == nil { + fake.getPoliciesLastUpdatedReturnsOnCall = make(map[int]struct { + result1 int + result2 error + }) + } + fake.getPoliciesLastUpdatedReturnsOnCall[i] = struct { + result1 int + result2 error + }{result1, result2} +} + +func (fake *PolicyClient) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.getPoliciesLastUpdatedMutex.RLock() + defer fake.getPoliciesLastUpdatedMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *PolicyClient) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +}