Skip to content

Commit

Permalink
Improve error message when kubeconfig contains no clusters / contexts…
Browse files Browse the repository at this point in the history
… / users.

When `kubectl config delete-context` and `kubectl config delete-cluster`
delete the last context / cluser / user, they set these fields to `null`
instead of empty lists. Before this change, these would cause deserialization
itself to fail because `null` cannot be deserialized into a `Vec`. It's better
that the test fails with the "couldn't find ..." error message instead.
  • Loading branch information
Arnavion committed Jul 26, 2023
1 parent 4d75f75 commit c012d0b
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions k8s-openapi-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,13 @@ impl<'a, TResponseFuture, TResponse, R> Stream for MultipleValuesStream<'a, TRes

#[derive(serde::Deserialize)]
struct KubeConfig {
#[serde(deserialize_with = "deserialize_null_to_empty_vec")]
clusters: Vec<KubeConfigClusterEntry>,
#[serde(deserialize_with = "deserialize_null_to_empty_vec")]
contexts: Vec<KubeConfigContextEntry>,
#[serde(rename = "current-context")]
current_context: String,
#[serde(deserialize_with = "deserialize_null_to_empty_vec")]
users: Vec<KubeConfigUserEntry>,
}

Expand Down Expand Up @@ -492,6 +495,11 @@ mod methodstring {
}
}

fn deserialize_null_to_empty_vec<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error> where D: serde::Deserializer<'de>, T: serde::Deserialize<'de> {
let value: Option<Vec<T>> = serde::Deserialize::deserialize(deserializer)?;
Ok(value.unwrap_or_default())
}

mod api_versions;

mod custom_resource_definition;
Expand Down

0 comments on commit c012d0b

Please sign in to comment.