Skip to content

Commit 428be9b

Browse files
test: add test templating secrets (#1478)
1 parent c23456a commit 428be9b

File tree

9 files changed

+88
-112
lines changed

9 files changed

+88
-112
lines changed

agent-control/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test/k8s/integration: test/k8s/integration-part1 test/k8s/integration-part2
4040
.PHONY: test/k8s/integration-part1
4141
test/k8s/integration-part1:
4242
KUBECONFIG='./tests/k8s/.kubeconfig-dev' minikube update-context
43-
tilt ci --file ./tests/k8s/Tiltfile
43+
ENABLE_VAULT=true tilt ci --file ./tests/k8s/Tiltfile
4444
# reducing the number of threads to 1 forces the tests to run sequentially
4545
cargo test k8s::scenarios -- --nocapture --ignored --test-threads=1
4646
cargo test k8s::agent_control_cli -- --nocapture --ignored --test-threads=1

agent-control/src/agent_type/variable/namespace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Namespace {
4141
}
4242

4343
pub fn is_secret_variable(s: &str) -> bool {
44-
[Namespace::Vault]
44+
[Namespace::Vault, Namespace::K8sSecret]
4545
.iter()
4646
.any(|ns| s.starts_with(ns.to_string().as_str()))
4747
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
k8s:
2+
namespace: <ns>
3+
namespace_agents: <agents-ns>
4+
cluster_name: <cluster-name>
5+
agents:
6+
hello-world:
7+
agent_type: "newrelic/com.newrelic.custom_agent:0.0.1"
8+
secrets_providers:
9+
vault:
10+
sources:
11+
sourceA:
12+
url: http://127.0.0.1:8200/v1/
13+
token: root
14+
engine: kv1
15+
sourceB:
16+
url: http://127.0.0.1:8200/v1/
17+
token: root
18+
engine: kv2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
chart_values:
2+
hashicorpVaultV1Key: ${nr-vault:sourceA:kv-v1:my-secret:foo1}
3+
hashicorpVaultV2Key: ${nr-vault:sourceB:secret:my-secret:foo2}
4+
k8sSecretKey: ${nr-kubesec:<ns>:pod-secrets:foo3}

agent-control/tests/k8s/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub mod agent_control_cli;
22
mod client;
33
mod garbage_collector;
44
mod scenarios;
5-
mod secret_providers;
65
pub mod self_update;
76
mod store;
87
mod tools;

agent-control/tests/k8s/scenarios.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ mod fail_remote_config;
66
mod garbage_collector;
77
mod no_opamp;
88
mod opamp;
9+
mod secrets_providers;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::common::retry::retry;
2+
use crate::common::runtime::block_on;
3+
use crate::k8s::tools::agent_control::{
4+
CUSTOM_AGENT_TYPE_PATH, start_agent_control_with_testdata_config,
5+
};
6+
use crate::k8s::tools::k8s_api::{check_helmrelease_spec_values, create_values_secret};
7+
use crate::k8s::tools::k8s_env::K8sEnv;
8+
use std::time::Duration;
9+
use tempfile::tempdir;
10+
11+
#[test]
12+
#[ignore = "needs k8s cluster"]
13+
fn k8s_template_secrets() {
14+
let test_name = "k8s_template_secrets";
15+
16+
// setup the k8s environment
17+
let mut k8s = block_on(K8sEnv::new());
18+
k8s.port_forward("vault-0", 8200, 8200);
19+
let namespace = block_on(k8s.test_namespace());
20+
let tmp_dir = tempdir().expect("failed to create local temp dir");
21+
22+
// start the agent-control
23+
let _sa = start_agent_control_with_testdata_config(
24+
test_name,
25+
CUSTOM_AGENT_TYPE_PATH,
26+
k8s.client.clone(),
27+
&namespace,
28+
&namespace,
29+
None,
30+
None,
31+
// This config is intended to be empty
32+
vec!["local-data-hello-world"],
33+
tmp_dir.path(),
34+
);
35+
36+
// Now, we create all the required secrets.
37+
// Hashicorp Vault secrets -> handled in the Tiltfile.
38+
39+
// K8s secrets -> created here on demand.
40+
let name = "pod-secrets";
41+
let key = "foo3";
42+
let value = "bar3";
43+
create_values_secret(k8s.client.clone(), &namespace, name, key, value.to_string());
44+
45+
// Check the HelmRelease is created with the secrets correctly populated
46+
let expected_spec_values = r#"
47+
hashicorpVaultV1Key: bar1
48+
hashicorpVaultV2Key: bar2
49+
k8sSecretKey: bar3
50+
"#;
51+
52+
retry(60, Duration::from_secs(1), || {
53+
block_on(check_helmrelease_spec_values(
54+
k8s.client.clone(),
55+
namespace.as_str(),
56+
"hello-world",
57+
expected_spec_values,
58+
))
59+
});
60+
}

agent-control/tests/k8s/secret_providers.rs

Lines changed: 0 additions & 107 deletions
This file was deleted.

agent-control/tests/k8s/tools/k8s_api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ pub async fn check_helmrelease_spec_values(
4242
k8s_client: Client,
4343
namespace: &str,
4444
name: &str,
45-
expected_valus_as_yaml: &str,
45+
expected_values_as_yaml: &str,
4646
) -> Result<(), Box<dyn Error>> {
47-
let expected_as_json: serde_json::Value = serde_yaml::from_str(expected_valus_as_yaml).unwrap();
47+
let expected_as_json: serde_json::Value =
48+
serde_yaml::from_str(expected_values_as_yaml).unwrap();
4849
let api = create_k8s_api(k8s_client, namespace).await;
4950

5051
let obj = api.get(name).await?;

0 commit comments

Comments
 (0)