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

Kubernetes provider can't create a namespace with a name attribute #170

Closed
redeux opened this issue Jul 15, 2020 · 6 comments
Closed

Kubernetes provider can't create a namespace with a name attribute #170

redeux opened this issue Jul 15, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@redeux
Copy link
Contributor

redeux commented Jul 15, 2020

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

cdktf & Language Versions

{
  "name": "tf-cdk",
  "version": "1.0.0",
  "main": "main.js",
  "types": "main.ts",
  "license": "MPL-2.0",
  "private": true,
  "scripts": {
    "get": "cdktf get",
    "build": "yarn get && tsc",
    "synth": "cdktf synth",
    "compile": "tsc",
    "watch": "tsc -w",
    "test": "echo ok",
    "upgrade": "npm i cdktf@latest cdktf-cli@latest",
    "upgrade:next": "npm i cdktf@next cdktf-cli@next"
  },
  "engines": {
    "node": ">=10.12"
  },
  "dependencies": {
    "cdktf": "0.0.10-pre.2655a66d496aff0bbaeae9a518bea153dae3cee0",
    "constructs": "^3.0.4"
  },
  "devDependencies": {
    "@types/node": "^14.0.23",
    "cdktf-cli": "0.0.10",
    "typescript": "^3.9.6"
  }
}

Affected Resource(s)

import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
import {KubernetesProvider, Namespace} from './.gen/providers/kubernetes';

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // define resources here
    new KubernetesProvider(this, 'kind', {});

    new Namespace(this, 'tf-cdk', {
      metadata: [{
        labels: {},
        name: 'tf-cdk'
      }]
    })
  }
}

const app = new App();
new MyStack(app, 'tf-cdk');
app.synth();

Debug Output

main.ts:15:9 - error TS2322: Type '{ labels: {}; name: string; }' is not assignable to type 'NamespaceMetadata'.
  Object literal may only specify known properties, and 'name' does not exist in type 'NamespaceMetadata'.

15         name: 'tf-cdk'
           ~~~~~~~~~~~~~~


Found 1 error.

Expected Behavior

Generated Terraform code in the output directory: cdktf.out

Actual Behavior

⠋ synthesizing ...
Warning: Render methods should be a pure function of props and state; triggering nested component updates from render is not allowed. If necessary, trigger nested updates in componentDidUpdate.

Check the render method of Unknown.
non-zero exit code 1

Steps to Reproduce

Try to create a namespace with a name attribute using the Kubernetes provider

Important Factoids

Name is a valid input for the kubernetes_namespace resource.

In the CDK schema doc in namespace.ts it shows as optional and computed.

            "name": {
              "type": "string",
              "description": "Name of the namespace, must be unique. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names",
              "optional": true,
              "computed": true
            },

It's absent from the NamespaceMetadata interface

export interface NamespaceMetadata {
  /** An unstructured key value map stored with the namespace that may be used to store arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations */
  readonly annotations?: { [key: string]: string };
  /** Prefix, used by the server, to generate a unique name ONLY IF the `name` field has not been provided. This value will also be combined with a unique suffix. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#idempotency */
  readonly generateName?: string;
  /** Map of string keys and values that can be used to organize and categorize (scope and select) the namespace. May match selectors of replication controllers and services. More info: http://kubernetes.io/docs/user-guide/labels */
  readonly labels?: { [key: string]: string };
}

References

None

@redeux redeux added the bug Something isn't working label Jul 15, 2020
@skorfmann
Copy link
Contributor

Thanks for reporting this bug. I can confirm this is a bug, it's due to this line of code - when deactivating this line, the interface is being rendered correctly:

export interface NamespaceMetadata {
  /** An unstructured key value map stored with the namespace that may be used to store arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations */
  readonly annotations?: { [key: string]: string };
  /** Prefix, used by the server, to generate a unique name ONLY IF the `name` field has not been provided. This value will also be combined with a unique suffix. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#idempotency */
  readonly generateName?: string;
  /** A sequence number representing a specific generation of the desired state. */
  readonly generation: number;
  /** Map of string keys and values that can be used to organize and categorize (scope and select) the namespace. May match selectors of replication controllers and services. More info: http://kubernetes.io/docs/user-guide/labels */
  readonly labels?: { [key: string]: string };
  /** Name of the namespace, must be unique. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names */
  readonly name?: string;
  /** An opaque value that represents the internal version of this namespace that can be used by clients to determine when namespace has changed. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency */
  readonly resourceVersion: string;
  /** A URL representing this namespace. */
  readonly selfLink: string;
  /** The unique in time and space value for this namespace. More info: http://kubernetes.io/docs/user-guide/identifiers#uids */
  readonly uid: string;
}

I'll investigate what the original intent of this line was (can't remember off the top of my head)

@skorfmann
Copy link
Contributor

I'll investigate what the original intent of this line was (can't remember off the top of my head)

That's essentially the quickfix for #29. When disabling this, there will be invalid interfaces generated.

We could think about being more specific for the guard clause or doing the check at a later stage in the code generation phase.

@skorfmann
Copy link
Contributor

When looking at the schema

      "metadata": {
        "nesting_mode": "list",
        "block": {
          "attributes": {
            "annotations": {
              "type": [
                "map",
                "string"
              ],
              "description": "An unstructured key value map stored with the namespace that may be used to store arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations",
              "optional": true
            },
            "generate_name": {
              "type": "string",
              "description": "Prefix, used by the server, to generate a unique name ONLY IF the `name` field has not been provided. This value will also be combined with a unique suffix. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#idempotency",
              "optional": true
            },
            "generation": {
              "type": "number",
              "description": "A sequence number representing a specific generation of the desired state.",
              "computed": true
            },
            "labels": {
              "type": [
                "map",
                "string"
              ],
              "description": "Map of string keys and values that can be used to organize and categorize (scope and select) the namespace. May match selectors of replication controllers and services. More info: http://kubernetes.io/docs/user-guide/labels",
              "optional": true
            },
            "name": {
              "type": "string",
              "description": "Name of the namespace, must be unique. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names",
              "optional": true,
              "computed": true
            },
            "resource_version": {
              "type": "string",
              "description": "An opaque value that represents the internal version of this namespace that can be used by clients to determine when namespace has changed. Read more: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency",
              "computed": true
            },
            "self_link": {
              "type": "string",
              "description": "A URL representing this namespace.",
              "computed": true
            },
            "uid": {
              "type": "string",
              "description": "The unique in time and space value for this namespace. More info: http://kubernetes.io/docs/user-guide/identifiers#uids",
              "computed": true
            }
          }
        },
        "min_items": 1,
        "max_items": 1
      },

And comparing it to the docs it looks like we could just guard it by optional being true.

Besides from that, what we certainly don't support at the moment are attribute references for nested types e.g. generation

@skorfmann
Copy link
Contributor

@redeux FYI: the fix for name was published in cdktf-cli@next and will be published in an actual release later today.

@redeux
Copy link
Contributor Author

redeux commented Jul 16, 2020

I confirmed that it works as expected now. Thanks!

@redeux redeux closed this as completed Jul 16, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2022

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants