Skip to content

Calling addCluster/User/Context fails if nothing was previously loaded #365

@JohnCMcDonough

Description

@JohnCMcDonough

Failing Test Code

const kc = new k8s.KubeConfig();
kc.addCluster({
  name: 'test',
  server: `https://localhost:9889`,
  skipTLSVerify: true,
})
///TypeError: Cannot read property 'forEach' of undefined
///        at KubeConfig.addCluster (/Users/test/project/node_modules/@kubernetes/client-node/dist/config.js:176:23)
kc.addUser({
  token: 'test',
  username: 'test',
  name: 'test',
})
kc.addContext({
  cluster: 'test',
  name: 'test',
  user: 'test',
})
kc.setCurrentContext('test')

Code Referenced By StackTrace

// ...
    addCluster(cluster) {
        this.clusters.forEach((c, ix) => {
            if (c.name === cluster.name) {
                throw new Error(`Duplicate cluster: ${c.name}`);
            }
        });
        this.clusters.push(cluster);
    }
    addUser(user) {
        this.users.forEach((c, ix) => {
            if (c.name === user.name) {
                throw new Error(`Duplicate user: ${c.name}`);
            }
        });
        this.users.push(user);
    }
    addContext(ctx) {
        this.contexts.forEach((c, ix) => {
            if (c.name === ctx.name) {
                throw new Error(`Duplicate context: ${c.name}`);
            }
        });
        this.contexts.push(ctx);
    }
// ...

It looks like it's as simple as this.clusters, this.users, this.contexts not being defined when the KubeConfig is created. The fix would be to just define them when the object is created.

I would create a PR, but I've been fighting the CLA for a while now, and never got it figured out.

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