Skip to content

Commit

Permalink
should skip the stale status from last generation (#407)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Mar 23, 2024
1 parent 16488f9 commit 9b1524d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions controller/api/v1/consumer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (consumer *Consumer) SetAccepted(reason ConditionReason, msg ...string) {

func (c *Consumer) IsValid() bool {
for _, cond := range c.Status.Conditions {
if cond.ObservedGeneration != c.Generation {
continue
}
if cond.Type == string(ConditionAccepted) && cond.Reason == string(ReasonInvalid) {
return false
}
Expand Down
3 changes: 3 additions & 0 deletions controller/api/v1/httpfilterpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func (p *HTTPFilterPolicy) SetAccepted(reason gwapiv1a2.PolicyConditionReason, m

func (p *HTTPFilterPolicy) IsValid() bool {
for _, cond := range p.Status.Conditions {
if cond.ObservedGeneration != p.Generation {
continue
}
if cond.Type == string(gwapiv1a2.PolicyConditionAccepted) && cond.Reason == string(gwapiv1a2.PolicyReasonInvalid) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,41 @@ var _ = Describe("Consumer controller", func() {
Expect(marshaledCfg["default"]["spacewander"]).To(BeNil())
Expect(marshaledCfg["default"]["unchanged"]).ToNot(BeNil())
Expect(marshaledCfg["default"]["unchanged"]["v"]).To(Equal(v))

// back to valid
base = client.MergeFrom(c.DeepCopy())
delete(c.Spec.Auth, "unknown")
Expect(k8sClient.Patch(ctx, c, base)).Should(Succeed())
Eventually(func() bool {
if err := k8sClient.List(ctx, &consumers); err != nil {
return false
}
for _, item := range consumers.Items {
if item.Name == "spacewander" {
c = &consumers.Items[0]
cs = c.Status.Conditions
return cs[0].Reason == string(mosniov1.ReasonAccepted)
}
}
return false
}, timeout, interval).Should(BeTrue())

// EnvoyFilter should be updated too
Eventually(func() bool {
if err := k8sClient.List(ctx, &envoyfilters); err != nil {
return false
}
return len(envoyfilters.Items) == 1
}, timeout, interval).Should(BeTrue())

value = envoyfilters.Items[0].Spec.ConfigPatches[0].Patch.Value.AsMap()
typedCfg = value["typed_config"].(map[string]interface{})
pluginCfg = typedCfg["plugin_config"].(map[string]interface{})

marshaledCfg = map[string]map[string]map[string]interface{}{}
b, _ = json.Marshal(pluginCfg["value"])
json.Unmarshal(b, &marshaledCfg)
Expect(marshaledCfg["default"]["spacewander"]).ToNot(BeNil())
})

It("with filter", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,36 @@ var _ = Describe("HTTPFilterPolicy controller", func() {
}

var policies mosniov1.HTTPFilterPolicyList
var p *mosniov1.HTTPFilterPolicy
var cs []metav1.Condition
Eventually(func() bool {
if err := k8sClient.List(ctx, &policies); err != nil {
return false
}
p := policies.Items[0]
p = &policies.Items[0]
cs = p.Status.Conditions
return len(cs) == 1
}, timeout, interval).Should(BeTrue())
Expect(cs[0].Type).To(Equal(string(gwapiv1a2.PolicyConditionAccepted)))
Expect(cs[0].Reason).To(Equal(string(gwapiv1a2.PolicyReasonInvalid)))
Expect(policies.Items[0].IsValid()).To(BeFalse())
Expect(p.IsValid()).To(BeFalse())

// to valid
base := client.MergeFrom(p.DeepCopy())
delete(p.Spec.Filters, "unknown")
Expect(k8sClient.Patch(ctx, p, base)).Should(Succeed())
Eventually(func() bool {
if err := k8sClient.List(ctx, &policies); err != nil {
return false
}
p = &policies.Items[0]
cs = p.Status.Conditions
if len(cs) != 1 {
return false
}
return cs[0].Reason == string(gwapiv1a2.PolicyReasonTargetNotFound)
}, timeout, interval).Should(BeTrue())
Expect(p.IsValid()).To(BeTrue())
})

It("deal with valid crd", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
unknown:
config:
hostName: goldfish
demo:
config:
hostName: Mary

0 comments on commit 9b1524d

Please sign in to comment.