Skip to content
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

support kubeconfigs with more than one document #440

Closed
clux opened this issue Feb 27, 2021 · 3 comments · Fixed by #441
Closed

support kubeconfigs with more than one document #440

clux opened this issue Feb 27, 2021 · 3 comments · Fixed by #441
Labels
config Kube config related

Comments

@clux
Copy link
Member

clux commented Feb 27, 2021

While we now support stacked kubeconfigs (see #132), and have all the merging logic for it, we crash on multi-document kubeconfigs.

Create two k3d clusters, and save their outputs in a single file:

k3d cluster start promstack
k3d cluster start kube
k3d kubeconfig get --all > ~/.kube/k3d
export KUBECONFIG="$HOME/.kube/k3d"

You'll have a file with two ---\n delimited yaml objects, which could be merged.
kubectl handles it, kube crashes:

Error: Error loading kubeconfig: Failed to infer config.. cluster env: (Error loading kubeconfig: Unable to load in cluster config, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined), kubeconfig: (Error loading kubeconfig: Failed to parse Kubeconfig YAML: deserializing from YAML containing more than one document is not supported)

Caused by:
    0: Failed to infer config.. cluster env: (Error loading kubeconfig: Unable to load in cluster config, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined), kubeconfig: (Error loading kubeconfig: Failed to parse Kubeconfig YAML: deserializing from YAML containing more than one document is not supported)
    1: Error loading kubeconfig: Failed to parse Kubeconfig YAML: deserializing from YAML containing more than one document is not supported
    2: Failed to parse Kubeconfig YAML: deserializing from YAML containing more than one document is not supported
    3: deserializing from YAML containing more than one document is not supported

We could probably the yaml objects together and flatten the reads to get the same result in file_config.rs.

AFAIKT: It might be enough to change: Kubeconfig::read_from to:

  • have serde_yaml::from_reader call return Vec<Kubeconfig>
  • lift remapping path inside a loop over configs
  • fold the kubeconfigs together using Kubeconfig::merge
  • return an already merged Kubeconfig

Fold might be awkward, since you need to fold over an existing Kubeconfig, but fold_first is almost stable!

Last point; this isn't a bad lack. You can tell k3d to give you delimited path output with multiple config files (k3d config merge - just don't use it with --output) and fallback to stacked kubeconfigs. It would just be nice to not crash on what kubectl handles.

@clux clux added the config Kube config related label Feb 27, 2021
@clux
Copy link
Member Author

clux commented Feb 27, 2021

Merging looks ok with a manual fold_first that has simplifies error handling. In the new lifted loop:

        let configs: Vec<Kubeconfig> = serde_yaml::from_reader(f).map_err(ConfigError::ParseYaml)?;

        // Remap all files we read to absolute paths.
        let mut cfg = None;
        for mut config in configs {
            // OLD REMAP LOGIC HERE INDENTED
            if let Some(c) = cfg {
                cfg = Some(Kubeconfig::merge(c, config)?);
            } else {
                cfg = Some(config);
            }
        }
        Ok(cfg.expect("Need at least one yaml document in KUBECONFIG file"))

But looks slightly more complicated due to https://www.reddit.com/r/rust/comments/ccpeh4/is_there_a_way_to_deserialize_a_multiple/

might need to split on --- ...

@clux
Copy link
Member Author

clux commented Feb 27, 2021

Opportune pr: dtolnay/serde-yaml#189

clux added a commit that referenced this issue Feb 27, 2021
@clux clux linked a pull request Feb 27, 2021 that will close this issue
clux added a commit that referenced this issue Feb 28, 2021
@clux clux closed this as completed in #441 Feb 28, 2021
clux added a commit that referenced this issue Feb 28, 2021
support multi-doc kubeconfigs - fixes #440
@clux
Copy link
Member Author

clux commented Feb 28, 2021

Released in 0.51.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config Kube config related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant