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

Duplicate resource names #1

Open
abriko opened this issue Nov 17, 2022 · 1 comment
Open

Duplicate resource names #1

abriko opened this issue Nov 17, 2022 · 1 comment

Comments

@abriko
Copy link

abriko commented Nov 17, 2022

Hi

Look like the id() method in python is not return unique value. It will cause 409 Conflict error when create items.

Code for reproduce

import yaml
from kubernetes import client, config
from kubecrd import KubeResourceBase
from apischema import schema
from dataclasses import dataclass, field


@dataclass
class Member(KubeResourceBase):
    __group__ = "example.io"
    __version__ = "v1"

    id: int = field(
        metadata=schema(
            description="Member ID"
        )
    )
    name: str = field(
        metadata=schema(
            description="Member name"
        )
    )
    squad: str = field(
        metadata=schema(
            description="Member belong to squad"
        )
    )


def create_items(members: list, k8s_client):
    for m in members:
        name = m["name"]
        squad = m["squad"]
        member = Member(
            id=m["id"],
            name=name,
            squad=squad)
        member.save(k8s_client)
        print(f"🚀 Added {name} in {squad}")


member_yaml = """
members:
- id: 1
  name: Cola
  squad: A
- id: 2
  name: Ash
  squad: A
- id: 3
  name: Casey
  squad: A
- id: 4
  name: Oreo
  squad: B
- id: 5
  name: Mrs Muddy
  squad: B
- id: 6
  name: Mrs Peculiarpop
  squad: B
"""

config.load_incluster_config()
k8s_client = client.ApiClient()
Member.install(k8s_client)

members = yaml.safe_load(member_yaml)
create_items(members["members"], k8s_client)

error message

python3 test2.py 
🚀 Added Cola in Cola
🚀 Added Ash in Ash
🚀 Added Casey in Casey
Traceback (most recent call last):
  File "/data/roster-reminder/test2.py", line 69, in <module>
    create_items(members["members"], k8s_client)
  File "/data/roster-reminder/test2.py", line 38, in create_items
    member.save(k8s_client)
  File "/usr/local/lib/python3.9/dist-packages/kubecrd/schemabase.py", line 219, in save
    resp = api_instance.create_namespaced_custom_object(
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/api/custom_objects_api.py", line 225, in create_namespaced_custom_object
    return self.create_namespaced_custom_object_with_http_info(group, version, namespace, plural, body, **kwargs)  # noqa: E501
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/api/custom_objects_api.py", line 344, in create_namespaced_custom_object_with_http_info
    return self.api_client.call_api(
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/api_client.py", line 348, in call_api
    return self.__call_api(resource_path, method,
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/api_client.py", line 180, in __call_api
    response_data = self.request(
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/api_client.py", line 391, in request
    return self.rest_client.POST(url,
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/rest.py", line 275, in POST
    return self.request("POST", url,
  File "/usr/local/lib/python3.9/dist-packages/kubernetes/client/rest.py", line 234, in request
    raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (409)
Reason: Conflict
HTTP response headers: HTTPHeaderDict({'Audit-Id': '3267083b-7bb1-469c-b79d-ed742c074343', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Kubernetes-Pf-Flowschema-Uid': '7b16e3db-c9ab-40ae-819a-1a4b90061a5f', 'X-Kubernetes-Pf-Prioritylevel-Uid': '4af6bed6-a525-4ea7-9599-5e3f481a867b', 'Date': 'Thu, 17 Nov 2022 01:20:26 GMT', 'Content-Length': '258'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"members.example.io \"member140400840708880\" already exists","reason":"AlreadyExists","details":{"name":"member140400840708880","group":"example.io","kind":"members"},"code":409}

kubectl get return:

apiVersion: v1
items:
- apiVersion: example.io/v1
  kind: Member
  metadata:
    creationTimestamp: "2022-11-17T01:20:26Z"
    generation: 1
    name: member140400840708880
    namespace: default
    resourceVersion: "152738485"
    uid: 6abe05e2-f3ac-4177-b4b9-acd959f50c3b
  spec:
    id: 2
    name: Ash
    squad: Ash
- apiVersion: example.io/v1
  kind: Member
  metadata:
    creationTimestamp: "2022-11-17T01:20:26Z"
    generation: 1
    name: member140400840710368
    namespace: default
    resourceVersion: "152738484"
    uid: 4944ac38-e210-4cd1-bbda-04eea2fd1cb0
  spec:
    id: 1
    name: Cola
    squad: Cola
- apiVersion: example.io/v1
  kind: Member
  metadata:
    creationTimestamp: "2022-11-17T01:20:26Z"
    generation: 1
    name: member140400863499312
    namespace: default
    resourceVersion: "152738486"
    uid: eaa875b9-a6c2-477c-af23-af3b5254f705
  spec:
    id: 3
    name: Casey
    squad: Casey
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
@abriko
Copy link
Author

abriko commented Nov 17, 2022

Hi @maxking

I create PR fix this issue, any concerns about use the, class name field or UUID as resources name?

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

1 participant