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

Allow providers to be defined globally on the app, rather than in each stack #2489

Open
acdoussan opened this issue Jan 14, 2023 · 1 comment
Labels
enhancement New feature or request priority/awaiting-more-evidence Lowest priority. Unlikely to be worked on unless/until it gets a lot more upvotes. size/medium estimated < 1 week ux/configuration

Comments

@acdoussan
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

Currently, the providers have to be defined directly on each stack. My provider configuration does not change between stacks. It would be nice if the cdk implementation walked up the object chain and used the first provider definition it finds, so I can define it in a single place.

Example (this doesn't work):

main.ts

import { HomelabApp } from 'src/app';

const app = new HomelabApp();
app.synth();

src/app.ts

import { App, AppOptions } from "cdktf";
import { ProxmoxProvider } from 'gen/providers/proxmox/provider';
import { K8sStack } from './k8s';

export class HomelabApp extends App {
  public readonly proxmoxProvider: ProxmoxProvider;
  public readonly k8sStack: K8sStack;

  constructor(options?: AppOptions) {
    super(options);

    this.proxmoxProvider = new ProxmoxProvider(this, 'proxmox-provider', {
      pmApiUrl: <redacted>,
      pmUser: <redacted>,
      pmPassword: <redacted>,
    });

    this.k8sStack = new K8sStack(this, "k8s");
  }
}

src/k8s.ts

import { Construct } from "constructs";
import { TerraformStack } from "cdktf";
import { Lxc } from "gen/providers/proxmox/lxc";

export class K8sStack extends TerraformStack {
  public readonly test: Lxc;

  constructor(scope: Construct, id: string) {
    super(scope, id);

    this.test = new Lxc(this, 'lxc-test', {
      <redacted config, not relevant>
    });
  }
}

tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "src/*": ["./src/*"],
      "gen/*": ["./.gen/*"]
    },
    "alwaysStrict": true,
    "charset": "utf8",
    "declaration": true,
    "experimentalDecorators": true,
    "inlineSourceMap": true,
    "inlineSources": true,
    "lib": ["es2018"],
    "module": "CommonJS",
    "noEmitOnError": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "resolveJsonModule": true,
    "strict": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "stripInternal": true,
    "target": "ES2018",
    "incremental": true,
    "skipLibCheck": true
  },
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  },
  "include": ["**/*.ts"],
  "exclude": ["node_modules", "cdktf.out"]
}

Error I get when running npm run synth -- --json

[2023-01-14T15:43:03.640] [ERROR] default - Error: Validation failed with the following errors:
  [k8s] Found resources without a matching provider construct. Please make sure to add provider constructs [e.g. new RandomProvider(...)] to your stack 'k8s' for the following providers: proxmox
    at K8sStack.runAllValidations (/Users/acdoussan/git/homelab/terraform/node_modules/cdktf/lib/terraform-stack.ts:342:13)
    at StackSynthesizer.synthesize (/Users/acdoussan/git/homelab/terraform/node_modules/cdktf/lib/synthesize/synthesizer.ts:30:18)
    at /Users/acdoussan/git/homelab/terraform/node_modules/cdktf/lib/app.ts:129:49
    at Array.forEach (<anonymous>)
    at HomelabApp.synth (/Users/acdoussan/git/homelab/terraform/node_modules/cdktf/lib/app.ts:129:12)
    at Object.<anonymous> (/Users/acdoussan/git/homelab/terraform/main.ts:4:5)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module.m._compile (/Users/acdoussan/git/homelab/terraform/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/acdoussan/git/homelab/terraform/node_modules/ts-node/src/in
ERROR: cdktf encountered an error while synthesizing

References

@acdoussan acdoussan added enhancement New feature or request new Un-triaged issue labels Jan 14, 2023
@DanielMSchmidt DanielMSchmidt added priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. ux/configuration size/medium estimated < 1 week and removed new Un-triaged issue labels Jan 19, 2023
@jsteinich
Copy link
Collaborator

A sort of workaround could be to create a function that creates the provider so that the configuration is one spot and you just need to call it from each stack.

@rhatri rhatri added priority/awaiting-more-evidence Lowest priority. Unlikely to be worked on unless/until it gets a lot more upvotes. and removed priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. labels Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority/awaiting-more-evidence Lowest priority. Unlikely to be worked on unless/until it gets a lot more upvotes. size/medium estimated < 1 week ux/configuration
Projects
None yet
Development

No branches or pull requests

4 participants