Skip to content

Commit e2b865b

Browse files
committed
Variable renames and make lint
1 parent 0ec17de commit e2b865b

File tree

11 files changed

+54
-62
lines changed

11 files changed

+54
-62
lines changed

cmd/spire-agent/cli/run/run.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ type experimentalConfig struct {
105105
Flags fflag.RawConfig `hcl:"feature_flags"`
106106

107107
UnusedKeys []string `hcl:",unusedKeys"`
108-
MaxSvidCacheSize int `hcl:"max_svid_cache_size"`
109-
SVIDCacheExpiryPeriod string `hcl:"svid_cache_expiry_interval"`
108+
SVIDCacheMaxSize int `hcl:"svid_cache_max_size"`
109+
SVIDCacheExpiryPeriod string `hcl:"svid_cache_expiry_period"`
110110
}
111111

112112
type Command struct {
@@ -394,10 +394,10 @@ func NewAgentConfig(c *Config, logOptions []log.Option, allowUnknownConfig bool)
394394
}
395395
}
396396

397-
if c.Agent.Experimental.MaxSvidCacheSize < 0 {
398-
return nil, fmt.Errorf("max_svid_cache_size should not be negative")
397+
if c.Agent.Experimental.SVIDCacheMaxSize < 0 {
398+
return nil, errors.New("svid_cache_max_size should not be negative")
399399
}
400-
ac.MaxSvidCacheSize = c.Agent.Experimental.MaxSvidCacheSize
400+
ac.SVIDCacheMaxSize = c.Agent.Experimental.SVIDCacheMaxSize
401401

402402
if c.Agent.Experimental.SVIDCacheExpiryPeriod != "" {
403403
var err error
@@ -406,7 +406,7 @@ func NewAgentConfig(c *Config, logOptions []log.Option, allowUnknownConfig bool)
406406
return nil, fmt.Errorf("could not parse svid cache expiry interval: %w", err)
407407
}
408408
if ac.SVIDCacheExpiryPeriod < 0 {
409-
return nil, fmt.Errorf("svid_cache_expiry_interval should not be negative")
409+
return nil, errors.New("svid_cache_expiry_period should not be negative")
410410
}
411411
}
412412

cmd/spire-agent/cli/run/run_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func TestNewAgentConfig(t *testing.T) {
700700
},
701701
},
702702
{
703-
msg: "svid_cache_expiry_interval parses a duration",
703+
msg: "svid_cache_expiry_period parses a duration",
704704
input: func(c *Config) {
705705
c.Agent.Experimental.SVIDCacheExpiryPeriod = "1s50ms"
706706
},
@@ -709,7 +709,7 @@ func TestNewAgentConfig(t *testing.T) {
709709
},
710710
},
711711
{
712-
msg: "svid_cache_expiry_interval is negative",
712+
msg: "svid_cache_expiry_period is negative",
713713
expectError: true,
714714
input: func(c *Config) {
715715
c.Agent.Experimental.SVIDCacheExpiryPeriod = "-1s50ms"
@@ -719,7 +719,7 @@ func TestNewAgentConfig(t *testing.T) {
719719
},
720720
},
721721
{
722-
msg: "invalid svid_cache_expiry_interval returns an error",
722+
msg: "invalid svid_cache_expiry_period returns an error",
723723
expectError: true,
724724
input: func(c *Config) {
725725
c.Agent.Experimental.SVIDCacheExpiryPeriod = "moo"
@@ -729,44 +729,44 @@ func TestNewAgentConfig(t *testing.T) {
729729
},
730730
},
731731
{
732-
msg: "svid_cache_expiry_interval is not set",
732+
msg: "svid_cache_expiry_period is not set",
733733
input: func(c *Config) {
734734
},
735735
test: func(t *testing.T, c *agent.Config) {
736736
require.EqualValues(t, 0, c.SVIDCacheExpiryPeriod)
737737
},
738738
},
739739
{
740-
msg: "max_svid_cache_size is set",
740+
msg: "svid_cache_max_size is set",
741741
input: func(c *Config) {
742-
c.Agent.Experimental.MaxSvidCacheSize = 100
742+
c.Agent.Experimental.SVIDCacheMaxSize = 100
743743
},
744744
test: func(t *testing.T, c *agent.Config) {
745-
require.EqualValues(t, 100, c.MaxSvidCacheSize)
745+
require.EqualValues(t, 100, c.SVIDCacheMaxSize)
746746
},
747747
},
748748
{
749-
msg: "max_svid_cache_size is not set",
749+
msg: "svid_cache_max_size is not set",
750750
input: func(c *Config) {
751751
},
752752
test: func(t *testing.T, c *agent.Config) {
753-
require.EqualValues(t, 0, c.MaxSvidCacheSize)
753+
require.EqualValues(t, 0, c.SVIDCacheMaxSize)
754754
},
755755
},
756756
{
757-
msg: "max_svid_cache_size is zero",
757+
msg: "svid_cache_max_size is zero",
758758
input: func(c *Config) {
759-
c.Agent.Experimental.MaxSvidCacheSize = 0
759+
c.Agent.Experimental.SVIDCacheMaxSize = 0
760760
},
761761
test: func(t *testing.T, c *agent.Config) {
762-
require.EqualValues(t, 0, c.MaxSvidCacheSize)
762+
require.EqualValues(t, 0, c.SVIDCacheMaxSize)
763763
},
764764
},
765765
{
766-
msg: "max_svid_cache_size is negative",
766+
msg: "svid_cache_max_size is negative",
767767
expectError: true,
768768
input: func(c *Config) {
769-
c.Agent.Experimental.MaxSvidCacheSize = -10
769+
c.Agent.Experimental.SVIDCacheMaxSize = -10
770770
},
771771
test: func(t *testing.T, c *agent.Config) {
772772
require.Nil(t, c)

pkg/agent/agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (a *Agent) attest(ctx context.Context, cat catalog.Catalog, metrics telemet
199199

200200
func (a *Agent) newManager(ctx context.Context, cat catalog.Catalog, metrics telemetry.Metrics, as *node_attestor.AttestationResult, cache *storecache.Cache) (manager.Manager, error) {
201201
config := &manager.Config{
202-
SVID: as.SVID,
202+
SVID: as.SVID,
203203
SVIDKey: as.Key,
204204
Bundle: as.Bundle,
205205
Catalog: cat,
@@ -210,7 +210,7 @@ func (a *Agent) newManager(ctx context.Context, cat catalog.Catalog, metrics tel
210210
BundleCachePath: a.bundleCachePath(),
211211
SVIDCachePath: a.agentSVIDPath(),
212212
SyncInterval: a.c.SyncInterval,
213-
MaxSvidCacheSize: a.c.MaxSvidCacheSize,
213+
MaxSvidCacheSize: a.c.SVIDCacheMaxSize,
214214
SVIDCacheExpiryPeriod: a.c.SVIDCacheExpiryPeriod,
215215
SVIDStoreCache: cache,
216216
}

pkg/agent/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ type Config struct {
5555
// SyncInterval controls how often the agent sync synchronizer waits
5656
SyncInterval time.Duration
5757

58-
// MaxSvidCacheSize is a soft limit of max number of SVIDs that would be stored in cache
59-
MaxSvidCacheSize int
58+
// SVIDCacheMaxSize is a soft limit of max number of SVIDs that would be stored in cache
59+
SVIDCacheMaxSize int
6060

61-
// SVIDCacheExpiryPeriod is a period after which svids that don't have subscribers will be removed from cache
61+
// SVIDCacheExpiryPeriod is a period after which SVIDs that don't have subscribers will be removed from cache
6262
SVIDCacheExpiryPeriod time.Duration
6363

6464
// Trust domain and associated CA bundle

pkg/agent/manager/cache/cache.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
const (
19-
DefaultMaxSvidCacheSize = 1000
19+
DefaultSVIDCacheMaxSize = 1000
2020
DefaultSVIDCacheExpiryPeriod = 1 * time.Hour
2121
)
2222

@@ -145,8 +145,8 @@ type Cache struct {
145145
// svids are stored by entry IDs
146146
svids map[string]*X509SVID
147147

148-
// maxSVIDCacheSize is a soft limit of max number of SVIDs that would be stored in cache
149-
maxSvidCacheSize int
148+
// svidCacheMaxSize is a soft limit of max number of SVIDs that would be stored in cache
149+
svidCacheMaxSize int
150150

151151
// svidCacheExpiryPeriod is a period after which svids that don't have subscribers will be removed from cache
152152
svidCacheExpiryPeriod time.Duration
@@ -161,9 +161,9 @@ type StaleEntry struct {
161161
}
162162

163163
func New(log logrus.FieldLogger, trustDomain spiffeid.TrustDomain, bundle *Bundle, metrics telemetry.Metrics,
164-
maxSvidCacheSize int, svidCacheExpiryPeriod time.Duration, clk clock.Clock) *Cache {
165-
if maxSvidCacheSize <= 0 {
166-
maxSvidCacheSize = DefaultMaxSvidCacheSize
164+
svidCacheMaxSize int, svidCacheExpiryPeriod time.Duration, clk clock.Clock) *Cache {
165+
if svidCacheMaxSize <= 0 {
166+
svidCacheMaxSize = DefaultSVIDCacheMaxSize
167167
}
168168

169169
if svidCacheExpiryPeriod <= 0 {
@@ -184,7 +184,7 @@ func New(log logrus.FieldLogger, trustDomain spiffeid.TrustDomain, bundle *Bundl
184184
trustDomain: bundle,
185185
},
186186
svids: make(map[string]*X509SVID),
187-
maxSvidCacheSize: maxSvidCacheSize,
187+
svidCacheMaxSize: svidCacheMaxSize,
188188
svidCacheExpiryPeriod: svidCacheExpiryPeriod,
189189
clk: clk,
190190
}
@@ -431,7 +431,7 @@ func (c *Cache) UpdateEntries(update *UpdateEntries, checkSVID func(*common.Regi
431431

432432
// entries with active subscribers which are not cached will be put in staleEntries map
433433
activeSubs, recordsWithLastAccessTime := c.syncSVIDs()
434-
extraSize := len(c.svids) - c.maxSvidCacheSize
434+
extraSize := len(c.svids) - c.svidCacheMaxSize
435435

436436
// delete svids without subscribers and which have not been accessed since svidCacheExpiryTime
437437
if extraSize > 0 {
@@ -441,7 +441,7 @@ func (c *Cache) UpdateEntries(update *UpdateEntries, checkSVID func(*common.Regi
441441
svidCacheExpiryTime := now.Add(-1 * c.svidCacheExpiryPeriod).UnixMilli()
442442
for _, record := range recordsWithLastAccessTime {
443443
if extraSize <= 0 {
444-
// no need to delete SVIDs any further as cache size <= maxSvidCacheSize
444+
// no need to delete SVIDs any further as cache size <= svidCacheMaxSize
445445
break
446446
}
447447
if _, ok := c.svids[record.id]; ok {
@@ -589,9 +589,10 @@ func (c *Cache) updateLastAccessTimestamp(selectors []*common.Selector) {
589589
records, recordsDone := c.getRecordsForSelectors(set)
590590
defer recordsDone()
591591

592+
now := c.clk.Now().UnixMilli()
592593
for record := range records {
593594
// Set lastAccessTimestamp so that svid LRU cache can be cleaned based on this timestamp
594-
record.lastAccessTimestamp = c.clk.Now().UnixMilli()
595+
record.lastAccessTimestamp = now
595596
}
596597
}
597598

@@ -623,9 +624,9 @@ func (c *Cache) syncSVIDs() (map[string]struct{}, []record) {
623624
i++
624625
}
625626

626-
remainderSize := c.maxSvidCacheSize - len(c.svids)
627+
remainderSize := c.svidCacheMaxSize - len(c.svids)
627628
// add records which are not cached for remainder of cache size
628-
for id, _ := range c.records {
629+
for id := range c.records {
629630
if len(c.staleEntries) >= remainderSize {
630631
break
631632
}

pkg/agent/manager/cache/cache_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ func TestMaxSVIDCacheSize(t *testing.T) {
720720
clk := clock.NewMock()
721721
cache := newTestCacheWithConfig(10, 1*time.Minute, clk)
722722

723-
// create entries more than maxSvidCacheSize
723+
// create entries more than svidCacheMaxSize
724724
updateEntries := createUpdateEntries(12, makeBundles(bundleV1))
725725
cache.UpdateEntries(updateEntries, nil)
726726

@@ -732,7 +732,7 @@ func TestMaxSVIDCacheSize(t *testing.T) {
732732
require.Len(t, cache.GetStaleEntries(), 0)
733733
assert.Equal(t, 10, cache.CountSVIDs())
734734

735-
// Validate that active subscriber will still get SVID even if SVID count is at maxSvidCacheSize
735+
// Validate that active subscriber will still get SVID even if SVID count is at svidCacheMaxSize
736736
foo := makeRegistrationEntry("FOO", "foo")
737737
updateEntries.RegistrationEntries[foo.EntryId] = foo
738738

@@ -799,12 +799,12 @@ func TestNotify(t *testing.T) {
799799
func TestNewCache(t *testing.T) {
800800
// negative values
801801
cache := newTestCacheWithConfig(-5, -5, clock.NewMock())
802-
require.Equal(t, DefaultMaxSvidCacheSize, cache.maxSvidCacheSize)
802+
require.Equal(t, DefaultSVIDCacheMaxSize, cache.svidCacheMaxSize)
803803
require.Equal(t, DefaultSVIDCacheExpiryPeriod, cache.svidCacheExpiryPeriod)
804804

805805
// zero values
806806
cache = newTestCacheWithConfig(0, 0, clock.NewMock())
807-
require.Equal(t, DefaultMaxSvidCacheSize, cache.maxSvidCacheSize)
807+
require.Equal(t, DefaultSVIDCacheMaxSize, cache.svidCacheMaxSize)
808808
require.Equal(t, DefaultSVIDCacheExpiryPeriod, cache.svidCacheExpiryPeriod)
809809
}
810810

pkg/agent/manager/manager.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ func (m *manager) SubscribeToCacheChanges(selectors cache.Selectors) cache.Subsc
157157
if m.cache.Notify(selectors) {
158158
return subscriber
159159
}
160-
select {
161-
case <-m.clk.After(backoff.NextBackOff()):
162-
}
160+
// wait until next backoff interval
161+
<-m.clk.After(backoff.NextBackOff())
163162
}
164163
}
165164

pkg/agent/manager/manager_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ func TestSynchronizationClearsExpiredSVIDCache(t *testing.T) {
809809

810810
require.NoError(t, m.synchronize(context.Background()))
811811

812-
// Make sure svid count is MaxSvidCacheSize and remaining SVIDs are deleted from cache
812+
// Make sure svid count is SVIDCacheMaxSize and remaining SVIDs are deleted from cache
813813
require.Equal(t, 1, m.CountSVIDs())
814814
}
815815

@@ -947,12 +947,8 @@ func TestSubscribersWaitForSVID(t *testing.T) {
947947
sub1 := m.SubscribeToCacheChanges(cache.Selectors{{Type: "unix", Value: "uid:1111"}})
948948
defer sub1.Finish()
949949
u := <-sub1.Updates()
950-
if len(u.Identities) != 2 {
951-
t.Fatalf("expected 2 SVIDs, got: %d", len(u.Identities))
952-
}
953-
if !u.Bundle.EqualTo(c.Bundle) {
954-
t.Fatal("bundles were expected to be equal")
955-
}
950+
require.Len(t, u.Identities, 2)
951+
require.Equal(t, c.Bundle, u.Bundle)
956952
}()
957953

958954
wg.Add(1)
@@ -962,12 +958,8 @@ func TestSubscribersWaitForSVID(t *testing.T) {
962958
cache.Selectors{{Type: "spiffe_id", Value: "spiffe://example.org/spire/agent/join_token/abcd"}})
963959
defer sub2.Finish()
964960
u := <-sub2.Updates()
965-
if len(u.Identities) != 1 {
966-
t.Fatalf("expected 1 SVID, got: %d", len(u.Identities))
967-
}
968-
if !u.Bundle.EqualTo(c.Bundle) {
969-
t.Fatal("bundles were expected to be equal")
970-
}
961+
require.Len(t, u.Identities, 1)
962+
require.Equal(t, c.Bundle, u.Bundle)
971963
}()
972964

973965
wg.Wait()

test/integration/setup/debugagent/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func run() error {
5656
func agentEndpoints(ctx context.Context) error {
5757
s, err := retrieveDebugPage(ctx)
5858
if err == nil {
59-
log.Printf("Debug info: %s", string(s))
59+
log.Printf("Debug info: %s", s)
6060
}
6161
return nil
6262
}

test/integration/suites/fetch-svids/07-fetch-svids

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ docker-compose exec -u 1001 -T spire-agent \
2020
check-svid-count "spire-agent" 17
2121

2222
# Call agent debug endpoints and check if svid count is equal to 12
23-
# 17(svid-count) - 5(svids from entries with uuid 1002 will be removed first after svid_cache_expiry_interval) = 12
23+
# 17(svid-count) - 5(svids from entries with uuid 1002 will be removed first after svid_cache_expiry_period) = 12
2424
check-svid-count "spire-agent" 12
2525

2626
# Call agent debug endpoints and check if svid count is equal to 8
27-
# 12 - 8(cache size) = 4 extra svids from entries with uuid 1001 will be removed after svid_cache_expiry_interval
27+
# 12 - 8(cache size) = 4 extra svids from entries with uuid 1001 will be removed after svid_cache_expiry_period
2828
check-svid-count "spire-agent" $CACHESIZE

0 commit comments

Comments
 (0)