Skip to content

kc.loadFromCluster() doesn't setup getCurrentCluster() correctly. #2168

Closed

Description

@koooge

Hi there,
patch-example.js doesn't work with kc.loadFromCluster() instead of kc.loadFromDefault(). The app is running in a pod, and it causes Error: Cluster is undefined #2117 (comment)

I'm not sure if it's a bug or if there is another usage to fix it. At least, loadFromCluster works in v0 for me.

Activity

  1. brendandburns commented on Jan 21, 2025

    @brendandburns
    Contributor

    it looks like currentCluster is undefined in the loadFromCluster function. You could try substituting getClusters()[0] instead of getCurrentCluster() and see if that works.

    But we should probably check and fix to make it so that getCurrentCluster() works.

  2. changed the title [-]patch-example.js doesn't work with kc.loadFromCluster()[/-] [+]kc.loadFromCluster() doesn't setup `getCurrentCluster()` correctly.[/+] on Jan 21, 2025
  3. cjihrig commented on Jan 30, 2025

    @cjihrig
    Contributor

    I think this is a minimal reproduction:

    import * as k8s from '@kubernetes/client-node';
    const kc = new k8s.KubeConfig();
    kc.loadFromCluster();
    const currentContext = kc.getCurrentContext();
    const currentCluster = kc.getCluster(currentContext);
    console.log(currentCluster); // Logs null

    When kc.loadFromCluster() is called, kc.clusters is set to:

    [
      {
          name: 'inCluster',
          caFile: `${pathPrefix}${SERVICEACCOUNT_CA_PATH}`,
          server: `${scheme}://${serverHost}:${port}`,
          skipTLSVerify: false,
      },
    ]

    The call to kc.getCurrentContext() always returns the value 'inClusterContext'.

    So, you're always executing kc.getCluster('inClusterContext'). Internally, this searches the list of clusters for one whose name is 'inClusterContext'. A cluster with the same name as the context does not exist.

  4. cjihrig commented on Jan 30, 2025

    @cjihrig
    Contributor

    But we should probably check and fix to make it so that getCurrentCluster() works.

    It looks like that does work.

  5. cjihrig commented on Jan 31, 2025

    @cjihrig
    Contributor

    @koooge can you confirm if changing the code from kc.getCluster(currentContext); to kc.getCurrentCluster(); gets you past that error. It seems to me like the example should be using that code.

  6. brendandburns commented on Jan 31, 2025

    @brendandburns
    Contributor

    @cjihrig thanks for looking closer at this. You are correct, there is a bug in the code from @koooge

    It should be:

    kc.getCluster(kc.getContextObject(kc.getCurrentContext()).cluster)

    but really kc.getCurrentCluster() is easier.

    I did notice that our code coverage for this code is low, so I added #2191

    I'm going to close this as working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions