Skip to content

Commit e4fc654

Browse files
authored
Merge pull request #200 from milo-os/feat/list-unleased-retained
feat: List retained addresses that no lease will release
2 parents 372131c + 4dcbee1 commit e4fc654

5 files changed

Lines changed: 165 additions & 14 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Compiled binary
22
/ipam
3+
/milo-ipam
34

45
# GoReleaser build output
56
/dist/

‎cmd/milo-ipam/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ milo-ipam claim show <name|address>
2929
milo-ipam claim release <name> [--yes] [--dry-run]
3030
3131
# Allocations (IPAllocation — namespaced, system-created)
32-
milo-ipam allocation list [--class <n>] [--pool <n>] [--purpose <p>] [--unclaimed]
32+
milo-ipam allocation list [--class <n>] [--pool <n>] [--purpose <p>] [--unclaimed] [--no-lease]
3333
milo-ipam allocation show <name|address>
3434
milo-ipam allocation release <name> [--yes] [--dry-run]
3535

‎cmd/milo-ipam/allocation.go‎

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func newAllocationListCommand(a *app) *cobra.Command {
6161
pool string
6262
purpose string
6363
unclaimed bool
64+
noLease bool
6465
scopeArgs []string
6566
)
6667
cmd := &cobra.Command{
@@ -70,16 +71,24 @@ func newAllocationListCommand(a *app) *cobra.Command {
7071
Long: `List the addresses handed out of pools.
7172
7273
--unclaimed is the leak check: allocations with no claim bound. Under reclaim
73-
policy Retain that is an address still held after its claim was deleted, and
74-
nothing frees it until someone does so deliberately.
74+
policy Retain that is an address still held after its claim was deleted.
75+
76+
--no-lease narrows that to retained allocations with no lease deadline. The
77+
retention sweeper never releases these: each is held until it is released or
78+
a claim of the same name takes it back. Releasing one needs an IPAM Admin.
79+
It includes allocations whose lease is still to be worked out (LEASE
80+
"pending"), which may yet get a deadline. -o wide adds a LEASE column.
7581
7682
` + scopeGrammarHelp(),
7783
Args: cobra.NoArgs,
7884
Example: ` datumctl ipam allocation list
7985
datumctl ipam allocation list --pool us-central-1-tenant-v4 -o wide
8086
8187
# Addresses still held with no claim behind them — the Retain leak check
82-
datumctl ipam allocation list --unclaimed`,
88+
datumctl ipam allocation list --unclaimed
89+
90+
# Retained addresses nothing will release on a schedule
91+
datumctl ipam allocation list --no-lease -o wide`,
8392
RunE: func(cmd *cobra.Command, args []string) error {
8493
cs, ns, err := a.client()
8594
if err != nil {
@@ -112,6 +121,9 @@ nothing frees it until someone does so deliberately.
112121
if unclaimed && al.Spec.ClaimRef != nil {
113122
continue
114123
}
124+
if noLease && !retainedWithoutLease(al) {
125+
continue
126+
}
115127
if !scopeContains(al.Spec.Scope, wantScope) {
116128
continue
117129
}
@@ -143,6 +155,7 @@ nothing frees it until someone does so deliberately.
143155
f.StringVar(&pool, "pool", "", "Only show allocations drawn from this pool")
144156
f.StringVar(&purpose, "purpose", "", "Only show allocations of this purpose: Claim|Reservation|PoolCarve")
145157
f.BoolVar(&unclaimed, "unclaimed", false, "Only show allocations with no claim bound (retained or reserved)")
158+
f.BoolVar(&noLease, "no-lease", false, "Only show retained allocations with no lease deadline, including ones still to be evaluated")
146159
f.StringArrayVar(&scopeArgs, "scope", nil, scopeFlagUsage("Only show allocations whose scope includes these references"))
147160
return cmd
148161
}
@@ -203,16 +216,17 @@ func (a *app) renderAllocationTable(allocs []ipamv1alpha1.IPAllocation) error {
203216
wide := a.opts.output == outputWide
204217
headers := []string{"NAME", "ADDRESS", "CLASS", "POOL", "CLAIM", "PHASE", "AGE"}
205218
if wide {
206-
headers = []string{"NAME", "ADDRESS", "CLASS", "POOL", "CLAIM", "PURPOSE", "SCOPE", "OWNER", "RECLAIM", "PHASE", "AGE"}
219+
headers = []string{"NAME", "ADDRESS", "CLASS", "POOL", "CLAIM", "PURPOSE", "SCOPE", "OWNER", "RECLAIM", "LEASE", "PHASE", "AGE"}
207220
}
221+
now := time.Now()
208222
t := newTable(a.io.Out, headers)
209223
for i := range allocs {
210224
al := &allocs[i]
211225
if wide {
212226
t.row(al.Name, orDash(allocationAddress(al)), orDash(al.Spec.ClassName), al.Spec.PoolRef.Name,
213227
allocationClaimName(al), orDash(string(al.Spec.Purpose)), formatScope(al.Spec.Scope),
214228
formatObjectRef(al.Spec.OwnerRef), orDash(string(al.Spec.ReclaimPolicy)),
215-
phaseText(string(al.Status.Phase)), humanDuration(al.CreationTimestamp))
229+
leaseColumn(al, now), phaseText(string(al.Status.Phase)), humanDuration(al.CreationTimestamp))
216230
} else {
217231
t.row(al.Name, orDash(allocationAddress(al)), orDash(al.Spec.ClassName), al.Spec.PoolRef.Name,
218232
allocationClaimName(al), phaseText(string(al.Status.Phase)),
@@ -274,6 +288,45 @@ func (a *app) resolveAllocation(cs clientset.Interface, ns, arg string) ([]*ipam
274288
return []*ipamv1alpha1.IPAllocation{al}, nil
275289
}
276290

291+
// retained reports whether al is a claim's address held after the claim was
292+
// deleted.
293+
func retained(al *ipamv1alpha1.IPAllocation) bool {
294+
return al.Spec.Purpose == ipamv1alpha1.PurposeClaim && al.Spec.ClaimRef == nil
295+
}
296+
297+
// leasePending reports whether the server has yet to work out al's lease.
298+
func leasePending(al *ipamv1alpha1.IPAllocation) bool {
299+
c := meta.FindStatusCondition(al.Status.Conditions, ipamv1alpha1.AllocationConditionRetained)
300+
return c != nil && c.Reason == ipamv1alpha1.RetainedReasonLeasePending
301+
}
302+
303+
// retainedWithoutLease reports whether al is retained with no lease deadline,
304+
// as the server judges it when it decides who may release it: a lease still
305+
// to be worked out counts, and so does an allocation retained before
306+
// deadlines were recorded.
307+
func retainedWithoutLease(al *ipamv1alpha1.IPAllocation) bool {
308+
return retained(al) && al.Status.ExpiresAt == nil
309+
}
310+
311+
// leaseColumn is the LEASE cell of the wide list: when a retained
312+
// allocation's lease runs out, or why it has no deadline, in the words the
313+
// server's own table uses. An allocation that is not retained has no lease.
314+
func leaseColumn(al *ipamv1alpha1.IPAllocation, now time.Time) string {
315+
switch {
316+
case !retained(al):
317+
return "—"
318+
case al.Status.ExpiresAt != nil:
319+
at := al.Status.ExpiresAt.Time
320+
if at.After(now) {
321+
return "in " + shortDuration(at.Sub(now))
322+
}
323+
return "expired " + shortDuration(now.Sub(at)) + " ago"
324+
case leasePending(al):
325+
return "pending"
326+
}
327+
return "never"
328+
}
329+
277330
// leaseText says when a retained allocation's lease runs out. It does not
278331
// promise a release: the CLI cannot see whether the server's retention
279332
// sweeper runs in enforce mode, which releases an expired allocation, or in
@@ -289,8 +342,7 @@ func leaseText(al *ipamv1alpha1.IPAllocation, now time.Time) string {
289342
return "ran out " + at.UTC().Format(time.RFC3339) + " (" + shortDuration(now.Sub(at)) +
290343
" ago); released by the retention sweeper when it runs in enforce mode"
291344
}
292-
if c := meta.FindStatusCondition(al.Status.Conditions, ipamv1alpha1.AllocationConditionRetained); c != nil &&
293-
c.Reason == ipamv1alpha1.RetainedReasonLeasePending {
345+
if leasePending(al) {
294346
return "not yet evaluated"
295347
}
296348
return "none — held until released or reclaimed"

‎cmd/milo-ipam/allocation_test.go‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"strings"
66
"testing"
7+
"time"
78

89
apierrors "k8s.io/apimachinery/pkg/api/errors"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -68,6 +69,74 @@ func TestAllocationListFilters(t *testing.T) {
6869
}
6970
}
7071

72+
// --no-lease lists retained allocations with no deadline, as the server
73+
// judges them for release: including those retained before deadlines were
74+
// recorded and those whose lease is still to be worked out.
75+
func TestAllocationListNoLease(t *testing.T) {
76+
soon := metav1.NewTime(time.Now().Add(time.Hour))
77+
bound := newAllocation("bound", "c", "10.1.0.0/26", "p", "claim-a")
78+
leased := newAllocation("leased", "c", "10.1.0.64/26", "p", "")
79+
leased.Status.ExpiresAt = &soon
80+
pending := newAllocation("pending", "c", "10.1.0.128/26", "p", "")
81+
pending.Status.Conditions = []metav1.Condition{{
82+
Type: ipamv1alpha1.AllocationConditionRetained, Status: metav1.ConditionTrue,
83+
Reason: ipamv1alpha1.RetainedReasonLeasePending,
84+
}}
85+
none := newAllocation("none", "c", "10.1.0.192/26", "p", "")
86+
none.Status.Conditions = []metav1.Condition{{
87+
Type: ipamv1alpha1.AllocationConditionRetained, Status: metav1.ConditionTrue,
88+
Reason: ipamv1alpha1.RetainedReasonNoLease,
89+
}}
90+
untimestamped := newAllocation("untimestamped", "c", "10.1.1.0/26", "p", "")
91+
reserved := newAllocation("reserved", "", "10.1.255.0/24", "p", "")
92+
reserved.Spec.Purpose = ipamv1alpha1.PurposeReservation
93+
94+
ta := newTestApp(newFakeClientset(bound, leased, pending, none, untimestamped, reserved),
95+
&globalOptions{output: outputName, color: "never"})
96+
cmd := newAllocationListCommand(ta.app)
97+
if err := cmd.Flags().Set("no-lease", "true"); err != nil {
98+
t.Fatal(err)
99+
}
100+
if err := cmd.RunE(cmd, nil); err != nil {
101+
t.Fatal(err)
102+
}
103+
if got, want := ta.out.String(), "ipallocation/none\nipallocation/pending\nipallocation/untimestamped\n"; got != want {
104+
t.Errorf("--no-lease listed\n%s\nwant\n%s", got, want)
105+
}
106+
}
107+
108+
func TestLeaseColumn(t *testing.T) {
109+
now := time.Now()
110+
at := func(d time.Duration) *metav1.Time { t := metav1.NewTime(now.Add(d)); return &t }
111+
cases := []struct {
112+
name string
113+
claim string
114+
expiry *metav1.Time
115+
reason string
116+
want string
117+
}{
118+
{name: "bound", claim: "web", want: "—"},
119+
{name: "running", expiry: at(3 * time.Hour), want: "in 3h"},
120+
{name: "expired", expiry: at(-2 * time.Hour), want: "expired 2h ago"},
121+
{name: "pending", reason: ipamv1alpha1.RetainedReasonLeasePending, want: "pending"},
122+
{name: "none", reason: ipamv1alpha1.RetainedReasonNoLease, want: "never"},
123+
{name: "untimestamped", want: "never"},
124+
}
125+
for _, tc := range cases {
126+
t.Run(tc.name, func(t *testing.T) {
127+
al := newAllocation("a", "c", "10.0.0.0/24", "p", tc.claim)
128+
al.Status.ExpiresAt = tc.expiry
129+
if tc.reason != "" {
130+
al.Status.Conditions = []metav1.Condition{{Type: ipamv1alpha1.AllocationConditionRetained,
131+
Status: metav1.ConditionTrue, Reason: tc.reason}}
132+
}
133+
if got := leaseColumn(al, now); got != tc.want {
134+
t.Errorf("leaseColumn = %q, want %q", got, tc.want)
135+
}
136+
})
137+
}
138+
}
139+
71140
func TestAllocationListRejectsAnUnknownPurpose(t *testing.T) {
72141
ta := newTestApp(newFakeClientset(), nil)
73142
cmd := newAllocationListCommand(ta.app)

‎docs/runbooks/ipam-retained-without-lease-rising.md‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,39 @@ and check whether the class still exists.
6262
ORDER BY 4 DESC
6363
LIMIT 20;
6464
```
65-
2. **Who holds them?** The IPAllocations keep `spec.ownerRef`:
65+
2. **Which are claims that overrode their class?** A claim that set
66+
`reclaimPolicy: Retain` on a class whose default is Delete, created before
67+
that override needed a lease, is held for good: the class owner never chose
68+
to keep it. These are the rows to release first. The query lists every
69+
unleased row whose class does not default to Retain, including rows
70+
retained before IPAM timestamped retention and rows whose class is gone:
71+
```sql
72+
SELECT a.allocation_key, a.pool_key, a.class_name, a.retained_at,
73+
CASE WHEN c.key IS NULL THEN '(class not found)'
74+
ELSE coalesce(convert_from(c.data, 'UTF8')::jsonb #>> '{spec,reclaimPolicy}', 'Delete')
75+
END AS class_default
76+
FROM ipam_cidr_allocations a
77+
LEFT JOIN ipam_objects c
78+
ON c.key = 'project/' || coalesce(nullif(a.class_project, ''), split_part(a.pool_key, '/', 2))
79+
|| '/ipam.miloapis.com/ipclasses/' || a.class_name
80+
WHERE a.claim_key IS NULL AND a.purpose = 'Claim'
81+
AND (a.expires_at = 'infinity' OR a.retained_at IS NULL)
82+
AND coalesce(convert_from(c.data, 'UTF8')::jsonb #>> '{spec,reclaimPolicy}', '') <> 'Retain'
83+
ORDER BY a.retained_at NULLS FIRST, a.allocation_key;
84+
```
85+
A row that did not record its class's project is matched to a class in its
86+
pool's project, as the sweeper does. Two errors follow, both on rows written
87+
before migration 007. A pool provisioned under another project's class can
88+
show `(class not found)` for a class that exists. A class in the pool's
89+
project that is a reference (`spec.source`) shows `Delete`, though the class
90+
it refers to may retain by default.
91+
3. **Who holds them?** The IPAllocations keep `spec.ownerRef`. In a project,
92+
this lists every retained allocation with no deadline, including those
93+
whose class retains by default:
6694
```sh
67-
datumctl ipam allocation list --unclaimed
95+
datumctl ipam allocation list --no-lease -o wide
6896
```
69-
3. **Is it one consumer creating and deleting Retain claims under new names?**
97+
4. **Is it one consumer creating and deleting Retain claims under new names?**
7098
That holds a new address per name and never reclaims one. Look for a high
7199
`ipam_retentions_total` rate with a low `ipam_reclaims_total{result="reclaimed"}`.
72100

@@ -76,9 +104,10 @@ and check whether the class still exists.
76104
on the pool, which also caps classes that set none. Lowering a pool's cap
77105
applies to what the pool already retains. A class's lease applies from the
78106
next retention.
79-
- Release what is not needed: `datumctl ipam allocation release <name>`. The
80-
consumer that retained it can do this itself: the IPAM Consumer role grants
81-
it, and a bound allocation is refused.
107+
- Release what is not needed: `datumctl ipam allocation release <name>`. An
108+
allocation with no lease deadline can be released only by an IPAM Admin
109+
(permission `ipallocations.release-unleased`), or by deleting its namespace.
110+
Ask the owner first: an address held with no lease is often still in use.
82111
- Once a lease applies, the sweeper releases the allocation on a pass shortly
83112
after `status.expiresAt`, usually the next; a pass skips a pool a live
84113
claim holds, so a release can wait for a later one. That holds in the default enforce mode; a

0 commit comments

Comments
 (0)