Skip to content

get_namespaced_custom_object 404 #2363

@bsiara

Description

@bsiara

Hi,

I try to get HTTPRoute definition from k8s and this is my script which return 404:

k8s_contexts, active_context = config.list_kube_config_contexts()
active_context = k8s_context['name']
k8s_api_client = config.new_client_from_config(context=active_context)
k8s_client = client.CustomObjectsApi(api_client=k8s_api_client)

k8s_object = k8s_client.get_namespaced_custom_object(group='gateway.networking.k8s.io', version='v1', namespace='name', plural='httproute', name='route1')

log.info("descriptor:\n%s", str(yaml.safe_dump(k8s_object.to_dict())))

error:

Traceback (most recent call last):
  File "/app/k8s.py", line 520, in <module>
    sys.exit(main(sys.argv[1:]))
             ^^^^^^^^^^^^^^^^^^
  File "/app/k8s.py", line 501, in main
    k8s_object = k8s_client.get_namespaced_custom_object(group='gateway.networking.k8s.io', version='v1', namespace='name', plural='httproute', name='route1')
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/api/custom_objects_api.py", line 1627, in get_namespaced_custom_object
    return self.get_namespaced_custom_object_with_http_info(group, version, namespace, plural, name, **kwargs)  # noqa: E501
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/api/custom_objects_api.py", line 1734, in get_namespaced_custom_object_with_http_info
    return self.api_client.call_api(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/api_client.py", line 348, in call_api
    return self.__call_api(resource_path, method,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/api_client.py", line 180, in __call_api
    response_data = self.request(
                    ^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/api_client.py", line 373, in request
    return self.rest_client.GET(url,
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/rest.py", line 244, in GET
    return self.request("GET", url,
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/venv/lib/python3.11/site-packages/kubernetes/client/rest.py", line 238, in request
    raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'aabdeebb-7bde-434f-b0ef-c1a7bedda7c7', 'Cache-Control': 'no-cache, private', 'Content-Type': 'text/plain; charset=utf-8', 'X-Content-Type-Options': 'nosniff', 'X-Kubernetes-Pf-Flowschema-Uid': '1b0bef5a-d410-4601-94f8-f8d43cafa43e', 'X-Kubernetes-Pf-Prioritylevel-Uid': '98716d07-5e25-4074-be86-14f146715ca2', 'Date': 'Wed, 26 Feb 2025 15:17:42 GMT', 'Content-Length': '19'})
HTTP response body: 404 page not found

python version is 3.11.11
k8s version 1.29.5
python kubernetes version 29.0.0
What I'm doing wrong? When I use kubectl I get this descriptor:

$ kubectl --context name -n name get httproutes.gateway.networking.k8s.io route1 -o yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"gateway.networking.k8s.io/v1","kind":"HTTPRoute","metadata":{"annotations":{},"labels":{"app":"route1"},"name":"route1","namespace":"name"},"spec":{"hostnames":["route1.name.cluster.com"],"parentRefs":[{"name":"nginxga-gateway","namespace":"nginx-gateway"}],"rules":[{"backendRefs":[{"name":"route1","port":8080}]},{"backendRefs":[{"name":"route1","port":8080}],"matches":[{"headers":[{"name":"header1","value":"active"}]}]}]}}
  creationTimestamp: "2025-02-17T12:53:55Z"
  generation: 1
  labels:
    app: route1
  name: route1
  namespace: name
  resourceVersion: "181756847"
  uid: 172f37c1-7f04-4be7-9860-871e45b46215
spec:
  hostnames:
  - route1.name.cluster.com
  parentRefs:
  - group: gateway.networking.k8s.io
    kind: Gateway
    name: nginxga-gateway
    namespace: nginx-gateway
  rules:
  - backendRefs:
    - group: ""
      kind: Service
      name: route1
      port: 8080
      weight: 1
    matches:
    - path:
        type: PathPrefix
        value: /
  - backendRefs:
    - group: ""
      kind: Service
      name: route1
      port: 8080
      weight: 1
    matches:
    - headers:
      - name: header1
        type: Exact
        value: active
      path:
        type: PathPrefix
        value: /
status:
  parents:
  - conditions:
    - lastTransitionTime: "2025-02-17T12:53:56Z"
      message: The route is accepted
      observedGeneration: 1
      reason: Accepted
      status: "True"
      type: Accepted
    - lastTransitionTime: "2025-02-17T12:53:56Z"
      message: All references are resolved
      observedGeneration: 1
      reason: ResolvedRefs
      status: "True"
      type: ResolvedRefs
    controllerName: gateway.nginx.org/nginx-gateway-controller
    parentRef:
      group: gateway.networking.k8s.io
      kind: Gateway
      name: nginxga-gateway
      namespace: nginx-gateway

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions