Skip to content

Commit 969864d

Browse files
feat: cross ns kubeconfig (#224)
* feat: add a namespace field to the secret ref * feat: allow use of a kubeconfig in a different ns * chore: cleanup * chore(drive-by): dependency management * chore(drive-by): update rand * chore(drive-by): schemars version * chore(drive-by): cleanup
1 parent c17c1b2 commit 969864d

8 files changed

Lines changed: 396 additions & 368 deletions

File tree

Cargo.lock

Lines changed: 347 additions & 337 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,35 @@ license = "MIT"
55
edition = "2021"
66
keywords = ["kubernetes"]
77
repository = "https://github.com/influxdata/sinker"
8-
authors = ["Marko Mikulicic <mkm@influxdata.com>"]
8+
authors = ["InfluxData"]
99
description = "Copy k8s resources (or parts thereof) across clusters"
1010

1111
[dependencies]
12-
clap = { version = "4.5", features = ["derive", "help", "env", "std"] }
13-
futures = "0.3"
14-
kube = { version = "1.1.0", features = ["runtime", "derive", "unstable-runtime"] }
15-
kube-derive = "2.0.1"
16-
k8s-openapi = { version = "0.25.0", features = ["v1_33", "schemars"] }
17-
kubert = { version = "0.25.0", features = [
12+
clap = { version = "^4.5.53", features = ["derive", "help", "env", "std"] }
13+
futures = "^0.3.31"
14+
kube = { version = "^1.1.0", features = ["runtime", "derive", "unstable-runtime"] }
15+
k8s-openapi = { version = "^0.25.0", features = ["v1_33", "schemars"] }
16+
kubert = { version = "^0.25.0", features = [
1817
"clap",
1918
"runtime",
2019
"server",
2120
"rustls-tls",
2221
"prometheus-client",
2322
] }
24-
tokio = { version = "1.44", features = ["full"] }
25-
anyhow = { version = "1", features = ["backtrace"] }
26-
tracing = "0.1"
27-
schemars = "0.8.21"
28-
serde = { version = "1", features = ["derive"] }
29-
serde_json = "1"
30-
serde_yaml = "0.9.34"
31-
thiserror = "2"
32-
serde_json_path = "0.7.2"
33-
tokio-context = "0.1.*"
34-
tokio-stream = "0.1.*"
23+
tokio = { version = "^1.48.0", features = ["full"] }
24+
anyhow = { version = "^1.0.100", features = ["backtrace"] }
25+
tracing = "^0.1.41"
26+
schemars = "^0.8.22"
27+
serde = { version = "^1.0.228", features = ["derive"] }
28+
serde_json = "^1.0.145"
29+
serde_yaml = "^0.9.34"
30+
thiserror = "^2.0.17"
31+
serde_json_path = "^0.7.2"
32+
tokio-context = "^0.1.3"
33+
tokio-stream = "^0.1.17"
3534

3635
[dev-dependencies]
37-
rstest = "0.26.1"
38-
once_cell = "1.21.*"
39-
chrono = "0.4.*"
40-
rand = "0.8.5"
36+
rstest = "^0.26.1"
37+
once_cell = "^1.21.3"
38+
chrono = "^0.4.42"
39+
rand = "^0.9.2"

manifests/crd.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ spec:
5050
type: string
5151
name:
5252
type: string
53+
namespace:
54+
nullable: true
55+
type: string
5356
required:
5457
- key
5558
- name
@@ -98,6 +101,9 @@ spec:
98101
type: string
99102
name:
100103
type: string
104+
namespace:
105+
nullable: true
106+
type: string
101107
required:
102108
- key
103109
- name

src/filters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod tests {
2929
use chrono::TimeZone;
3030
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ManagedFieldsEntry, ObjectMeta, Time};
3131
use once_cell::sync::Lazy;
32-
use rand::distributions::Alphanumeric;
32+
use rand::distr::Alphanumeric;
3333
use rand::Rng;
3434
use rstest::*;
3535

@@ -39,7 +39,7 @@ mod tests {
3939
($len:expr) => {
4040
Lazy::new(|| {
4141
iter::repeat(())
42-
.map(|()| rand::thread_rng().sample(Alphanumeric))
42+
.map(|()| rand::rng().sample::<u8, _>(Alphanumeric))
4343
.filter(|c| c.is_ascii_alphabetic())
4444
.take($len)
4545
.map(char::from)

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub enum Error {
1616
#[error("error parsing kubeconfig from secret")]
1717
KubeconfigUtf8Error(#[source] std::str::Utf8Error),
1818

19-
#[error("referenced key '{0}' does not exist in secret '{0}'")]
20-
MissingKeyError(String, String),
19+
#[error("referenced key '{0}' does not exist in secret '{0}' in namespace '{0}'")]
20+
MissingKeyError(String, String, String),
2121

2222
#[error("SerializationError: {0}")]
2323
SerializationError(#[from] serde_json::Error),

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ async fn main() -> anyhow::Result<()> {
4646
Some(Commands::Manifests) => {
4747
println!(
4848
"{}---\n{}",
49-
serde_yaml::to_string(&sinker::resources::ResourceSync::crd()).unwrap(),
50-
serde_yaml::to_string(&sinker::resources::SinkerContainer::crd_with_manual_schema()).unwrap()
49+
serde_yaml::to_string(&sinker::resources::ResourceSync::crd())?,
50+
serde_yaml::to_string(
51+
&sinker::resources::SinkerContainer::crd_with_manual_schema()
52+
)?
5153
);
5254
}
5355
None => {

src/resource_extensions.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl ResourceSync {
3131
self.metadata
3232
.finalizers
3333
.as_ref()
34-
.map_or(false, |f| f.contains(&FINALIZER.to_string()))
34+
.is_some_and(|f| f.contains(&FINALIZER.to_string()))
3535
}
3636

3737
pub fn api(&self, client: Client) -> Api<Self> {
@@ -101,7 +101,13 @@ async fn cluster_client(
101101
let client = match cluster_ref {
102102
None => client,
103103
Some(cluster_ref) => {
104-
let secrets: Api<Secret> = Api::namespaced(client, local_ns);
104+
let secret_ns = cluster_ref
105+
.kube_config
106+
.secret_ref
107+
.namespace
108+
.as_deref()
109+
.unwrap_or(local_ns);
110+
let secrets: Api<Secret> = Api::namespaced(client, secret_ns);
105111
let secret_ref = &cluster_ref.kube_config.secret_ref;
106112
let sec = secrets.get(&secret_ref.name).await?;
107113

@@ -111,7 +117,11 @@ async fn cluster_client(
111117
.unwrap()
112118
.get(&secret_ref.key)
113119
.ok_or_else(|| {
114-
Error::MissingKeyError(secret_ref.key.clone(), secret_ref.name.clone())
120+
Error::MissingKeyError(
121+
secret_ref.key.clone(),
122+
secret_ref.name.clone(),
123+
secret_ns.to_string(),
124+
)
115125
})?
116126
.0,
117127
)

src/resources.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub struct KubeConfig {
125125
#[serde(rename_all = "camelCase")]
126126
pub struct SecretRef {
127127
pub name: String,
128+
pub namespace: Option<String>,
128129
pub key: String,
129130
}
130131

0 commit comments

Comments
 (0)