Skip to content

Can't add middleware without passing the entire Configuration Object #2264

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

Open
JanPfenning opened this issue Feb 24, 2025 · 6 comments
Open

Comments

@JanPfenning
Copy link

JanPfenning commented Feb 24, 2025

Describe the bug

when trying to add a promiseMiddleware to alter the result (post-query) i receive the the following error message:

You need to define an absolute base url for the server

Client Version
1.0.0

To Reproduce
run the provided typescript snipped

Expected behavior
When no configuration is passed to readNamespace at all it works fine to read the information.
As i do not change the configuration of the Target Server or the authentication in any way the addition of a middleware via the createConfiguration method should not change these values. The resulting config should be the same config that would be used if nothing is passed as explicit config but with the sole addition of the middleware

Example Code

const func = async () => {
    const kc = new KubeConfig();
    kc.loadFromDefault();
    const coreApi = kc.makeApiClient(CoreV1Api)
    const getNamespaceInformation = (namespace: string) => {
        const promiseMiddleware: PromiseMiddleware = {
            pre: (c) => Promise.resolve(c),
            post: (c) => Promise.resolve(c),
        };
        const middleware = new PromiseMiddlewareWrapper(promiseMiddleware);
        const config: Configuration = createConfiguration({
            middleware: [middleware],
        });
        return coreApi.readNamespace(
            {
                name: namespace,
            },
            config
        ); // throws
    };
    const namespaceDetails = await getNamespaceInformation('dev'); // throws
    console.log(namespaceDetails );
}
func()

Environment (please complete the following information):

  • OS: Windows (WSL)
  • Node.js version: v22.14.0
  • Cloud runtime: minikube
@brendandburns
Copy link
Contributor

Thanks for the report and the reproduction, we'll try to validate and figure out what is going wrong.

@cjihrig
Copy link
Contributor

cjihrig commented Feb 25, 2025

There is a known issue in v1.x around configuration being ignored inside the code generator: #2160 (comment). Is it possible that this is related to that?

@davidgamero
Copy link
Contributor

v1.1.0 is out https://www.npmjs.com/package/@kubernetes/client-node/v/1.1.0

it should include fixes to make progress on this issue

@cjihrig
Copy link
Contributor

cjihrig commented Apr 13, 2025

@JanPfenning does v1.1.2 fix this issue for you?

@cjihrig
Copy link
Contributor

cjihrig commented Apr 24, 2025

I looked into this, and the cause is:

  1. The user code creates a new Configuration object via the call to createConfiguration(). The new object looks like this:
{
  baseServer: ServerConfiguration { url: '', variableConfiguration: {} },
  httpApi: IsomorphicFetchHttpLibrary {},
  middleware: [],
  authMethods: {}
}
  1. This new configuration is passed to coreApi.readNamespace().
  2. In the library code, we eventually end up in readNamespaceWithHttpInfo() in gen/types/ObservableAPI.js.
  3. In that method, the config to use is computed as shown below (_options is the object from step 1):
if (_options) {
    _config = {
        baseServer: _options.baseServer || this.configuration.baseServer,
        httpApi: _options.httpApi || this.configuration.httpApi,
        authMethods: _options.authMethods || this.configuration.authMethods,
        middleware: allMiddleware || this.configuration.middleware
    };
}
  1. The baseServer always ends up as the object from step 1, which has an empty URL, which eventually leads to the URL error seen.

I'm guessing the expected behavior is either:

  1. The user provides a fully populated configuration object. This seems a bit tedious though.
  2. The config merging logic in step 4 above better handles the empty baseServer object. If this is the correct behavior, then it will need to be fixed in the code generator.

@mstruebing
Copy link
Member

I would argue that 2. is the desired behavior honestly.

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

No branches or pull requests

5 participants