Skip to content

Commit 4b34b01

Browse files
committed
Add CLI flags for the token cache and method to delete all events
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent c05eb67 commit 4b34b01

File tree

14 files changed

+52
-12
lines changed

14 files changed

+52
-12
lines changed

auth/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0
1212
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.2
1313
github.com/bradleyfalzon/ghinstallation/v2 v2.14.0
14-
github.com/fluxcd/pkg/cache v0.6.0
14+
github.com/fluxcd/pkg/cache v0.7.0
1515
github.com/fluxcd/pkg/ssh v0.17.0
1616
github.com/onsi/gomega v1.36.2
1717
golang.org/x/net v0.35.0
@@ -38,6 +38,7 @@ require (
3838
github.com/prometheus/client_model v0.6.1 // indirect
3939
github.com/prometheus/common v0.55.0 // indirect
4040
github.com/prometheus/procfs v0.15.1 // indirect
41+
github.com/spf13/pflag v1.0.6 // indirect
4142
golang.org/x/crypto v0.34.0 // indirect
4243
golang.org/x/sys v0.30.0 // indirect
4344
golang.org/x/text v0.22.0 // indirect

auth/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa
7171
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
7272
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
7373
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
74+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
75+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
7476
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
7577
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7678
golang.org/x/crypto v0.34.0 h1:+/C6tk6rf/+t5DhUketUbD1aNGqiSX3j15Z6xuIDlBA=

cache/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/go-logr/logr v1.4.2
77
github.com/onsi/gomega v1.36.2
88
github.com/prometheus/client_golang v1.20.5
9+
github.com/spf13/pflag v1.0.6
910
)
1011

1112
require (

cache/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
3535
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
3636
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
3737
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
38+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
39+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
3840
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
3941
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
4042
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=

cache/metrics.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const (
3434
StatusFailure = "failure"
3535
)
3636

37+
var allEventTypes = []string{
38+
CacheEventTypeMiss,
39+
CacheEventTypeHit,
40+
}
41+
3742
type cacheMetrics struct {
3843
// cacheEventsCounter is a counter for cache events.
3944
cacheEventsCounter *prometheus.CounterVec

cache/token.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package cache
1919
import (
2020
"context"
2121
"time"
22+
23+
"github.com/spf13/pflag"
2224
)
2325

2426
// TokenMaxDuration is the maximum duration that a token can have in the
@@ -54,6 +56,12 @@ type TokenCache struct {
5456
maxDuration time.Duration
5557
}
5658

59+
// TokenFlags contains the CLI flags that can be used to configure the TokenCache.
60+
type TokenFlags struct {
61+
MaxSize int
62+
MaxDuration time.Duration
63+
}
64+
5765
type tokenItem struct {
5866
token Token
5967
mono time.Time
@@ -116,10 +124,15 @@ func (c *TokenCache) GetOrSet(ctx context.Context,
116124
return item.token, ok, nil
117125
}
118126

119-
// DeleteCacheEvent deletes the cache event (cache_miss or cache_hit) metric for
120-
// the associated object being reconciled, given their kind, name and namespace.
121-
func (c *TokenCache) DeleteCacheEvent(event, kind, name, namespace string) {
122-
c.cache.DeleteCacheEvent(event, kind, name, namespace)
127+
// DeleteEventsForObject deletes all cache events (cache_miss and cache_hit) for
128+
// the associated object being deleted, given its kind, name and namespace.
129+
func (c *TokenCache) DeleteEventsForObject(kind, name, namespace string) {
130+
if c == nil {
131+
return
132+
}
133+
for _, eventType := range allEventTypes {
134+
c.cache.DeleteCacheEvent(eventType, kind, name, namespace)
135+
}
123136
}
124137

125138
func (c *TokenCache) newItem(token Token) *tokenItem {
@@ -147,3 +160,10 @@ func (ti *tokenItem) expired() bool {
147160
now := time.Now()
148161
return !ti.mono.After(now) || !ti.unix.After(now)
149162
}
163+
164+
func (f *TokenFlags) BindFlags(fs *pflag.FlagSet, defaultMaxSize int) {
165+
fs.IntVar(&f.MaxSize, "token-cache-max-size", defaultMaxSize,
166+
"The maximum size of the cache in number of tokens.")
167+
fs.DurationVar(&f.MaxDuration, "token-cache-max-duration", TokenMaxDuration,
168+
"The maximum duration a token is cached.")
169+
}

git/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/bradleyfalzon/ghinstallation/v2 v2.14.0 // indirect
2626
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2727
github.com/cloudflare/circl v1.5.0 // indirect
28-
github.com/fluxcd/pkg/cache v0.6.0 // indirect
28+
github.com/fluxcd/pkg/cache v0.7.0 // indirect
2929
github.com/go-logr/logr v1.4.2 // indirect
3030
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
3131
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
@@ -40,6 +40,7 @@ require (
4040
github.com/prometheus/client_model v0.6.1 // indirect
4141
github.com/prometheus/common v0.55.0 // indirect
4242
github.com/prometheus/procfs v0.15.1 // indirect
43+
github.com/spf13/pflag v1.0.6 // indirect
4344
golang.org/x/crypto v0.34.0 // indirect
4445
golang.org/x/net v0.35.0 // indirect
4546
golang.org/x/sys v0.30.0 // indirect

git/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa
7777
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
7878
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
7979
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
80+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
81+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
8082
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
8183
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
8284
golang.org/x/crypto v0.34.0 h1:+/C6tk6rf/+t5DhUketUbD1aNGqiSX3j15Z6xuIDlBA=

git/gogit/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ require (
4141
github.com/cloudflare/circl v1.5.0 // indirect
4242
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
4343
github.com/emirpasic/gods v1.18.1 // indirect
44-
github.com/fluxcd/pkg/cache v0.6.0 // indirect
44+
github.com/fluxcd/pkg/cache v0.7.0 // indirect
4545
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
4646
github.com/go-logr/logr v1.4.2 // indirect
4747
github.com/gofrs/uuid v4.4.0+incompatible // indirect
@@ -64,6 +64,7 @@ require (
6464
github.com/prometheus/procfs v0.15.1 // indirect
6565
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
6666
github.com/skeema/knownhosts v1.3.1 // indirect
67+
github.com/spf13/pflag v1.0.6 // indirect
6768
github.com/xanzy/ssh-agent v0.3.3 // indirect
6869
golang.org/x/net v0.35.0 // indirect
6970
golang.org/x/sys v0.30.0 // indirect

git/gogit/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG
127127
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
128128
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
129129
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
130+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
131+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
130132
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
131133
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
132134
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

0 commit comments

Comments
 (0)