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

Support generateName field #444

Closed
jacobtomlinson opened this issue Jul 10, 2024 · 0 comments · Fixed by #447
Closed

Support generateName field #444

jacobtomlinson opened this issue Jul 10, 2024 · 0 comments · Fixed by #447
Labels
enhancement New feature or request

Comments

@jacobtomlinson
Copy link
Member

Which project are you requesting an enhancement for?

kr8s

What do you need?

The Kubernetes spec allows you to set the metadata.generateName field instead of the metadata.name field which will cause it to generate a name at creation time with a random unique suffix.

This works in kr8s today when creating objects directly.

import kr8s

po = kr8s.objects.Pod({
        "apiVersion": "v1",
        "kind": "Pod",
        "metadata": {"generateName": "foo-"},
        "spec": {
            "containers": [{"name": "foo", "image": "nginx:latest"}],
            "restartPolicy": "Always",
        },
})

po.create()
print(po.name)  # foo-tlprk

However if we try and inspect the name before we create it we get an exception.

import kr8s

po = kr8s.objects.Pod({
        ...
        "metadata": {"generateName": "foo-"},
        ...
})

po.name
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/Projects/kr8s-org/kr8s/kr8s/_objects.py:138, in APIObject.name(self)
    137 try:
--> 138     return self.raw["metadata"]["name"]
    139 except KeyError as e:

KeyError: 'name'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 po.name

File ~/Projects/kr8s-org/kr8s/kr8s/_objects.py:140, in APIObject.name(self)
    138     return self.raw["metadata"]["name"]
    139 except KeyError as e:
--> 140     raise ValueError("Resource does not have a name") from e

ValueError: Resource does not have a name

Object reprs also fail because they access the .name attribute.

>>> po
Traceback (most recent call last):
  File "/home/jacob/Projects/kr8s-org/kr8s/kr8s/_objects.py", line 138, in name
    return self.raw["metadata"]["name"]
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'name'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jacob/Projects/kr8s-org/kr8s/kr8s/_objects.py", line 94, in __repr__
    return f"<{self.kind} {self.name}>"
                           ^^^^^^^^^
  File "/home/jacob/Projects/kr8s-org/kr8s/kr8s/_objects.py", line 140, in name
    raise ValueError("Resource does not have a name") from e
ValueError: Resource does not have a name

It would also be nice to support this in the .gen() method (xref #142).

Currently if you want to use the generateName field with .gen() you need to rename the metadata key.

import kr8s

po = kr8s.objects.Pod.gen(name="foo-", image="nginx:latest")
po.raw["metadata"]["generateName"] = po.raw["metadata"].pop("name")

It would be much nicer if we could just specify this in the .gen() classmethod.

import kr8s

po = kr8s.objects.Pod.gen(generate_name="foo-", image="nginx:latest")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant