Files
zeekayandHanzo Dev 6bdb695a14 kms: speak the tenant surface, write the coordinate reads resolve
Three faults, found live in one incident (index-chat-env pending, a push
"succeeding" while every read missed):

1. The client addressed /v1/kms/orgs/{org}/... — the org-addressed shape both
   servers removed (a URL that names a tenant is caller-selectable). Every
   URL is /v1/kms/secrets/... now; the org rides the TOKEN minted at login,
   and the org parameter still selects which credential logs in.

2. The push wrote the raw spec path while reads normalized it: a leading-slash
   secretsPath was WRITTEN at "/x" and READ (never found) at "x", and the
   reconciler honestly reported the write it had made — to an address nothing
   reads. One normalization (NormaliseScopePath) now rides both directions.

3. resyncInterval "300" failed "invalid time unit" once a minute forever. A
   bare integer is seconds; "5m" still works.

Each pinned by test, negatives included.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 01:52:49 -07:00

59 lines
1.4 KiB
Go

package util
import (
"fmt"
"strconv"
"time"
)
// ConvertIntervalToDuration parses a string like "10s", "5m", "1h" into
// a time.Duration. Used for the resyncInterval field on
// KMSPushSecret.Spec.
func ConvertIntervalToDuration(resyncInterval *string) (time.Duration, error) {
if resyncInterval == nil || *resyncInterval == "" {
return 0, nil
}
// A bare integer is SECONDS. Operators write "300" as naturally as "5m",
// and rejecting it made the reconciler log "invalid time unit" once a
// minute forever while the CR looked fine.
if n, err := strconv.Atoi(*resyncInterval); err == nil {
if n < 0 {
return 0, fmt.Errorf("negative interval")
}
return time.Duration(n) * time.Second, nil
}
length := len(*resyncInterval)
if length < 2 {
return 0, fmt.Errorf("invalid format")
}
unit := (*resyncInterval)[length-1:]
numberPart := (*resyncInterval)[:length-1]
number, err := strconv.Atoi(numberPart)
if err != nil {
return 0, err
}
switch unit {
case "s":
if number < 5 {
return 0, fmt.Errorf("resync interval must be at least 5 seconds")
}
return time.Duration(number) * time.Second, nil
case "m":
return time.Duration(number) * time.Minute, nil
case "h":
return time.Duration(number) * time.Hour, nil
case "d":
return time.Duration(number) * 24 * time.Hour, nil
case "w":
return time.Duration(number) * 7 * 24 * time.Hour, nil
default:
return 0, fmt.Errorf("invalid time unit")
}
}