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

[BUG] Autogenerated resource names not properly formatted #16

Closed
tabern opened this issue Feb 27, 2020 · 6 comments · Fixed by #18
Closed

[BUG] Autogenerated resource names not properly formatted #16

tabern opened this issue Feb 27, 2020 · 6 comments · Fixed by #18
Labels
bug Something isn't working

Comments

@tabern
Copy link
Contributor

tabern commented Feb 27, 2020

Auto-generated resource names follow the format this.object.uniqueid. This results in output that does not conform with K8s naming validation.

Input TS

new Deployment(this, 'deployment', {
      spec: {
        replicas: 1,
        selector: {
          matchLabels: label
        },
        template: {
          metadata: { labels: label },
          spec: {
            containers: [
              {
                name: this.node.uniqueId,
                image: options.image,
                ports: [ { containerPort } ]
              }
            ]
          }
        }
      }
    });

Output YAML

spec:
  replicas: 1
  selector:
    matchLabels:
      app: myappnginx37F28063
  template:
    metadata:
      labels:
        app: myappnginx37F28063
    spec:
      containers:
        - name: myappnginx37F28063
          image: nginx
          ports:
            - containerPort: 8080
kind: Deployment
apiVersion: apps/v1
metadata:
  name: myapp.nginx.deployment.f97e0de6

Error

Error from server (Invalid): error when creating "myapp.k8s.yaml": Deployment.apps "myapp.nginx.deployment.f97e0de6" is invalid: spec.template.spec.containers[0].name: Invalid value: "myappnginx37F28063": a DNS-1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')
@tabern tabern added the bug Something isn't working label Feb 27, 2020
@tabern tabern changed the title [BUG] Autogenerated object names are not properly formatted [BUG] Autogenerated resource names not properly formatted Feb 28, 2020
@eladb
Copy link
Contributor

eladb commented Feb 28, 2020

No problem. For some reason I was certain I read somewhere that dots are allowed but that’s a very easy fix.

@eladb eladb added the p0 label Feb 28, 2020
@eladb
Copy link
Contributor

eladb commented Feb 28, 2020

@tabern the auto-generated resource names (like the one auto-generated for the Deployment object myapp.nginx.deployment.f97e0de6) are actually ok and comply with the k8s spec.

The problem here is that you used this.node.uniqueId for the container name (which is not a resource), which resulted in a unique ID that is not compatible with DNS -1123.

What was the motivation of using this.node.uniqueId instead of just supplying a concrete name? Was this in some example that you took?

@eladb eladb changed the title [BUG] Autogenerated resource names not properly formatted [BUG] this.node.uniqueid produces an id that does not comply with DNS-1123 (e.g. container name) Feb 28, 2020
@eladb
Copy link
Contributor

eladb commented Feb 28, 2020

Okay, got it. It's in the README.

Sadly we can't change this.node.uniqueid because it would be a huge breaking change for the AWS CDK (this is coming from @aws-cdk/core).

Let me update our README to actually work, and we'll think about this a bit more.

@eladb
Copy link
Contributor

eladb commented Feb 28, 2020

Well... More testing yielded the following error:

Error from server (Invalid): error when creating "webserviceexample.k8s.yaml": Service "web-service-example.hello.service.0556b74d" is invalid: metadata.name: Invalid value: "web-service-example.hello.service.0556b74d": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')

So, for Deployment, it's okay to have . in the name and for Service it's not okay?? Oh my...

@eladb eladb changed the title [BUG] this.node.uniqueid produces an id that does not comply with DNS-1123 (e.g. container name) [BUG] Autogenerated resource names not properly formatted Feb 28, 2020
@eladb
Copy link
Contributor

eladb commented Feb 28, 2020

Reverting the original bug name and continuing to investigate.

@eladb
Copy link
Contributor

eladb commented Feb 28, 2020

OK, so apparently, names have different constraints based on the resource type (aaaah!). The common denominator seems to be DNS_LABEL which is limited to 63 characters and can only use lowercase alphanumeric and - and must have an alphanumeric character at the beginning and the end.

eladb pushed a commit that referenced this issue Feb 28, 2020
Different resource types may have different constraints on names (`metadata.name`). The previous version of the name generator was compatible with DNS_SUBDOMAIN but not with DNS_LABEL.

For example, `Deployment` names must comply with DNS_SUBDOMAIN while `Service` names must comply with DNS_LABEL.

Since there is no formal specification for this, the default name generation scheme for kubernetes objects in cdk8s was changed to DNS_LABEL, since it’s the common denominator for all kubernetes resources (supposedly).

Fixes #16
@eladb eladb closed this as completed in #18 Feb 28, 2020
eladb pushed a commit that referenced this issue Feb 28, 2020
…es (#18)

Different resource types may have different constraints on names (`metadata.name`). The previous version of the name generator was compatible with DNS_SUBDOMAIN but not with DNS_LABEL.

For example, `Deployment` names must comply with DNS_SUBDOMAIN while `Service` names must comply with DNS_LABEL.

Since there is no formal specification for this, the default name generation scheme for kubernetes objects in cdk8s was changed to DNS_LABEL, since it’s the common denominator for all kubernetes resources (supposedly).

Fixes #16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants