Skip to content

Commit 39bb184

Browse files
refactor: improve test reliability with conflict retries and constrain network egress DNS to kube-system namespace
Signed-off-by: Ankit Kr. Chowdhury <rakesh856100@gmail.com>
1 parent 300d680 commit 39bb184

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

config/network-policy/tenant-agent-isolation.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ spec:
5959
- port: 6443
6060
protocol: TCP
6161
# Allow CoreDNS resolution on UDP and TCP port 53.
62-
# Matches cluster DNS pods in any namespace (e.g. kube-system).
62+
# Matches cluster DNS pods in the kube-system namespace.
6363
- to:
64-
- namespaceSelector: {}
64+
- namespaceSelector:
65+
matchLabels:
66+
kubernetes.io/metadata.name: kube-system
6567
podSelector:
6668
matchExpressions:
6769
- key: k8s-app

docs/ARCHITECTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ Agentrax maintains a **two-tier network policy model**:
333333
#### Ingress & Egress Invariants:
334334

335335
- **Ingress**: Only TCP port `8080` from namespaces labeled `monitoring: enabled` (Prometheus scraping tenant agent metrics).
336-
- **Egress**: Only to the Kubernetes API server (`kube-apiserver` on TCP ports `443`/`6443`) and cluster CoreDNS (`UDP/TCP :53` in DNS pods). All cross-tenant and arbitrary external internet egress destinations remain blocked at the CNI layer.
336+
- **Egress**: Only to the Kubernetes API server (`kube-apiserver` on TCP ports `443`/`6443`) and cluster CoreDNS (`UDP/TCP :53` in `kube-system` DNS pods). All cross-tenant and arbitrary external internet egress destinations remain blocked at the CNI layer.
337337
- **Label Selector Binding**: The `tenant-agent-isolation` policy selects pods dynamically via `agentrax.io/agent: "true"`. The `AgentDeploymentReconciler` automatically stamps this label into the `PodTemplateSpec` of every managed `Deployment` via `agentLabels()`.
338338

339339
---

internal/controller/tenantquota_controller_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,18 @@ var _ = Describe("TenantQuota Controller", func() {
136136
g.Expect(fetched.Status.UsedAgents).To(BeNumerically("==", 1))
137137
}, timeout, interval).Should(Succeed())
138138

139-
// Remove finalizer so we can delete immediately.
140-
fetched := &agentraxv1alpha1.AgentDeployment{}
141-
Expect(k8sClient.Get(ctx, namespacedName("ad-del-1", tqNS), fetched)).To(Succeed())
142-
fetched.Finalizers = nil
143-
Expect(k8sClient.Update(ctx, fetched)).To(Succeed())
144-
Expect(k8sClient.Delete(ctx, fetched)).To(Succeed())
139+
// Remove finalizer with conflict retry so deletion is not raced by the controller.
140+
Expect(retry.RetryOnConflict(retry.DefaultRetry, func() error {
141+
latest := &agentraxv1alpha1.AgentDeployment{}
142+
if err := k8sClient.Get(ctx, namespacedName("ad-del-1", tqNS), latest); err != nil {
143+
return err
144+
}
145+
latest.Finalizers = nil
146+
return k8sClient.Update(ctx, latest)
147+
})).To(Succeed())
148+
Expect(k8sClient.Delete(ctx, &agentraxv1alpha1.AgentDeployment{
149+
ObjectMeta: metav1.ObjectMeta{Name: "ad-del-1", Namespace: tqNS},
150+
})).To(Succeed())
145151

146152
Eventually(func(g Gomega) {
147153
tqFetched := &agentraxv1alpha1.TenantQuota{}
@@ -217,11 +223,17 @@ var _ = Describe("TenantQuota Controller", func() {
217223
}, timeout, interval).Should(Succeed())
218224

219225
// Delete one AD → usage falls back to 1 == maxAgents; OverQuota clears.
220-
ad1 := &agentraxv1alpha1.AgentDeployment{}
221-
Expect(k8sClient.Get(ctx, namespacedName("ad-clearoq-1", tqNS), ad1)).To(Succeed())
222-
ad1.Finalizers = nil
223-
Expect(k8sClient.Update(ctx, ad1)).To(Succeed())
224-
Expect(k8sClient.Delete(ctx, ad1)).To(Succeed())
226+
Expect(retry.RetryOnConflict(retry.DefaultRetry, func() error {
227+
latest := &agentraxv1alpha1.AgentDeployment{}
228+
if err := k8sClient.Get(ctx, namespacedName("ad-clearoq-1", tqNS), latest); err != nil {
229+
return err
230+
}
231+
latest.Finalizers = nil
232+
return k8sClient.Update(ctx, latest)
233+
})).To(Succeed())
234+
Expect(k8sClient.Delete(ctx, &agentraxv1alpha1.AgentDeployment{
235+
ObjectMeta: metav1.ObjectMeta{Name: "ad-clearoq-1", Namespace: tqNS},
236+
})).To(Succeed())
225237

226238
Eventually(func(g Gomega) {
227239
f := &agentraxv1alpha1.TenantQuota{}

0 commit comments

Comments
 (0)