question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
allanadaircommented, Nov 16, 2017

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.

3reactions
ca-scribnercommented, Feb 8, 2021

This is a fantastic workaround, but it feels very odd that the python API for objects doesn’t know how to output itself as valid kubernetes yaml

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Kubernetes Objects
This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can express them in .yaml format.
Read more >
Kubernetes API Basics - Resources, Kinds, and Objects - iximiuz
Resource types are organized into API groups, and API groups are versioned. Every resource representation follows a certain schema defined by ...
Read more >
Working With Kubernetes Objects | Cheat Sheet & Tutorial
In Kubernetes, object representation is stored in a manifest, as discussed above. Each kind of object has two specific properties:.
Read more >
kubernetes.core.k8s module - Ansible Documentation
Only implemented for Pod resources. If not specified, the default grace period for the object type will be used. preconditions. dictionary.
Read more >
The Chart Template Developer's Guide - Helm V2
Templates generate manifest files, which are YAML-formatted resource descriptions that Kubernetes can understand. We'll look at how templates are structured ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found