Skip to content

Create Kubernetes model object from YAML #1379

@macrotex

Description

@macrotex

There is currently a function to take a YAML file and create a Kubernetes resource directly from that file, i.e., create_from_yaml. I need a related function that reads from a standard Kubernetes YAML file but instead of creating an actual Kubernetes resource creates, instead, a Python model object.

Here is an example. Say that namespace.yaml contains this:

apiVersion: v1
kind: Namespace
metadata:
  name: foo
  labels:
    name: baz

I would like to be able to something like

import from kubernetes.client.models.v1_namespace import *

# Create the V1Namespace object
namespace = model_from_yaml('namespace.yaml')
namespace.metadata.name = namespace.metadata.name + '_testing'

# Create the namespace
kubernetes_client.create_namespace(namespace)

Note that namespace would is a V1Namespace object that does not get created until the explicit create_namespace method is used.

This allows me to more easily create objects and use Python's inheritance to re-use code. For example, I could create a Python class that creates a Kubernetes web service model object and then someone else could inherit from that class to create a more specialized web service, perhaps with extra containers or volumes.

Metadata

Metadata

Assignees

Labels

kind/featureCategorizes issue or PR as related to a new feature.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions