-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable persistent agent caching #229
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,10 @@ const ( | |
// AnnotationAgentCacheListenerPort configures the port the agent cache should listen on | ||
AnnotationAgentCacheListenerPort = "vault.hashicorp.com/agent-cache-listener-port" | ||
|
||
// AnnotationAgentCacheExitOnErr configures whether the agent will exit on an | ||
// error while restoring the persistent cache | ||
AnnotationAgentCacheExitOnErr = "vault.hashicorp.com/agent-cache-exit-on-err" | ||
|
||
// AnnotationAgentCopyVolumeMounts is the name of the container or init container | ||
// in the Pod whose volume mounts should be copied onto the Vault Agent init and | ||
// sidecar containers. Ignores any Kubernetes service account token mounts. | ||
|
@@ -363,6 +367,10 @@ func Init(pod *corev1.Pod, cfg AgentConfig) error { | |
pod.ObjectMeta.Annotations[AnnotationAgentCacheUseAutoAuthToken] = DefaultAgentCacheUseAutoAuthToken | ||
} | ||
|
||
if _, ok := pod.ObjectMeta.Annotations[AnnotationAgentCacheExitOnErr]; !ok { | ||
pod.ObjectMeta.Annotations[AnnotationAgentCacheExitOnErr] = strconv.FormatBool(DefaultAgentCacheExitOnErr) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -550,6 +558,22 @@ func (a *Agent) agentCacheEnable() (bool, error) { | |
return strconv.ParseBool(raw) | ||
} | ||
|
||
func (a *Agent) agentCachePersist() bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the caller is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, done in 00f9152 |
||
if a.VaultAgentCache.Enable && a.PrePopulate && !a.PrePopulateOnly { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Determining whether we need persist like so may be fine for now, though I do wonder if we'd have to expose this through annotations down the road. |
||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (a *Agent) agentCacheExitOnErr() (bool, error) { | ||
raw, ok := a.Annotations[AnnotationAgentCacheExitOnErr] | ||
if !ok { | ||
return false, nil | ||
} | ||
|
||
return strconv.ParseBool(raw) | ||
} | ||
|
||
func (a *Agent) authConfig() map[string]interface{} { | ||
authConfig := make(map[string]interface{}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"testing" | ||
|
||
"github.com/mattbaird/jsonpatch" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewConfig(t *testing.T) { | ||
|
@@ -306,8 +308,8 @@ func TestConfigVaultAgentCache(t *testing.T) { | |
t.Error("agent Cache should be enabled") | ||
} | ||
|
||
if config.Cache.UseAuthAuthToken != "force" { | ||
t.Errorf("agent Cache use_auto_auth_token should be 'force', got %s instead", config.Cache.UseAuthAuthToken) | ||
if config.Cache.UseAutoAuthToken != "force" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. |
||
t.Errorf("agent Cache use_auto_auth_token should be 'force', got %s instead", config.Cache.UseAutoAuthToken) | ||
} | ||
|
||
if config.Listener[0].Type != "tcp" { | ||
|
@@ -322,3 +324,129 @@ func TestConfigVaultAgentCache(t *testing.T) { | |
t.Error("agent Cache listener TLS should be disabled") | ||
} | ||
} | ||
|
||
func TestConfigVaultAgentCache_persistent(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
annotations map[string]string | ||
expectedInitCache bool | ||
expectedCache *Cache | ||
expectedListeners []*Listener | ||
}{ | ||
{ | ||
name: "cache defaults", | ||
annotations: map[string]string{ | ||
AnnotationAgentCacheEnable: "true", | ||
}, | ||
expectedInitCache: true, | ||
expectedCache: &Cache{ | ||
UseAutoAuthToken: "true", | ||
Persist: &CachePersist{ | ||
Type: "kubernetes", | ||
Path: "/vault/agent-cache", | ||
}, | ||
}, | ||
expectedListeners: []*Listener{ | ||
{ | ||
Type: "tcp", | ||
Address: "127.0.0.1:8200", | ||
TLSDisable: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "exit on err", | ||
annotations: map[string]string{ | ||
AnnotationAgentCacheEnable: "true", | ||
AnnotationAgentCacheExitOnErr: "true", | ||
}, | ||
expectedInitCache: true, | ||
expectedCache: &Cache{ | ||
UseAutoAuthToken: "true", | ||
Persist: &CachePersist{ | ||
Type: "kubernetes", | ||
Path: "/vault/agent-cache", | ||
ExitOnErr: true, | ||
}, | ||
}, | ||
expectedListeners: []*Listener{ | ||
{ | ||
Type: "tcp", | ||
Address: "127.0.0.1:8200", | ||
TLSDisable: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "just memory cache when only sidecar", | ||
annotations: map[string]string{ | ||
AnnotationAgentCacheEnable: "true", | ||
AnnotationAgentPrePopulate: "false", | ||
}, | ||
expectedInitCache: false, | ||
expectedCache: &Cache{ | ||
UseAutoAuthToken: "true", | ||
}, | ||
expectedListeners: []*Listener{ | ||
{ | ||
Type: "tcp", | ||
Address: "127.0.0.1:8200", | ||
TLSDisable: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no cache at all with only init container", | ||
annotations: map[string]string{ | ||
AnnotationAgentCacheEnable: "true", | ||
AnnotationAgentPrePopulateOnly: "true", | ||
}, | ||
expectedInitCache: false, | ||
expectedCache: nil, | ||
expectedListeners: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
pod := testPod(tt.annotations) | ||
var patches []*jsonpatch.JsonPatchOperation | ||
|
||
agentConfig := AgentConfig{ | ||
"foobar-image", "http://foobar:8200", DefaultVaultAuthType, "test", "test", true, "100", "1000", | ||
DefaultAgentRunAsSameUser, DefaultAgentSetSecurityContext, "", | ||
} | ||
err := Init(pod, agentConfig) | ||
require.NoError(t, err, "got error initialising pod: %s", err) | ||
|
||
agent, err := New(pod, patches) | ||
require.NoError(t, err, "got error creating agent: %s", err) | ||
|
||
initCfg, err := agent.newConfig(true) | ||
require.NoError(t, err, "got error creating Vault config: %s", err) | ||
|
||
initConfig := &Config{} | ||
err = json.Unmarshal(initCfg, initConfig) | ||
require.NoError(t, err, "got error unmarshalling Vault init config: %s", err) | ||
|
||
if tt.expectedInitCache { | ||
assert.Equal(t, tt.expectedCache, initConfig.Cache) | ||
assert.Equal(t, tt.expectedListeners, initConfig.Listener) | ||
} else { | ||
assert.Nil(t, initConfig.Cache) | ||
assert.Nil(t, initConfig.Listener) | ||
} | ||
|
||
sidecarCfg, err := agent.newConfig(false) | ||
require.NoError(t, err, "got error creating Vault sidecar config: %s", err) | ||
|
||
sidecarConfig := &Config{} | ||
err = json.Unmarshal(sidecarCfg, sidecarConfig) | ||
require.NoError(t, err, "got error unmarshalling Vault sidecar config: %s", err) | ||
|
||
assert.Equal(t, tt.expectedCache, sidecarConfig.Cache) | ||
assert.Equal(t, tt.expectedListeners, sidecarConfig.Listener) | ||
}) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I reworked this a bit in 00f9152 to set it all in the struct, since
agentCachePersist()
also checks whether the cache is enabled.