Python: Dictionary representations of k8s resource model objects are not valid k8s resource manifest representations

Created on 14 Nov 2017  路  6Comments  路  Source: kubernetes-client/python

My use case is that I'd like to build up a representation of various Kubernetes resources via the classes in kubernetes.client.models and then generate the manifest yaml files for these resources via printing to YAML files the to_dictrepresentations of these resource objects. However, because all the attributes are snake cased, the end YAML also has snake cased keys (which are not valid k8s manifest keys and fail validation).

Example:

with open('deployment.yaml, 'w') as outfile:
    yaml.dump(deploymemnt_resource.to_dict(), outfile, default_flow_style=False)

Resulting deployment.yaml file:

api_version: extensions/v1beta1
kind: Deployment
metadata:
  name: testing
spec:
  progress_deadline_seconds: 300
  replicas: 1
  template:
    metadata:
      labels:
        app: testing
    spec:
      containers:
      - name: testing
        readiness_probe:
          http_get:
            path: /_meta/0/info
            port: http

The progress_deadline_seconds, readiness_probe, and http_get keys are all invalid.

lifecyclrotten

Most helpful comment

I'm not sure if this is canonical, but this is my approach to a similar situation.

from kubernetes.client import ApiClient
api = ApiClient()
api.sanitize_for_serialization(deploymemnt_resource)

This will create a dict.

All 6 comments

I'm not sure if this is canonical, but this is my approach to a similar situation.

from kubernetes.client import ApiClient
api = ApiClient()
api.sanitize_for_serialization(deploymemnt_resource)

This will create a dict.

https://github.com/kubernetes-client/python/issues/683#issuecomment-456082546

@allanadair your workaround is working, thanks

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Was this page helpful?
0 / 5 - 0 ratings