Skip to content

emcfarlane/kubestar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kubestar

GoDev

Build kuberenetes config with Starklark (a dialect of python) and protobuffers. Generate config with python functions, strongly typed to protobuffer objects. Build out modules for different applications and import them with a function syntax for easy configuration.

Here's some example syntax to declare an apps/v1 Deployment object:

apps_v1.Deployment(
  metadata={
    "name": "nginx-deployment",
    "labels": {
      "app": "nginx",
    },
  },
  spec=apps_v1.DeploymentSpec(
    replicas=3,
    selector=struct(matchLabels={
      "app": "nginx",
    }),
    template=core_v1.PodTemplateSpec(
      metadata=apis_meta_v1.ObjectMeta(
        labels={
          "app": "nginx",
        }),
      spec=core_v1.PodSpec(containers=[
        nginx_container(),
      ]),
    ),
  ),
)

Please see examples for more.

How does it work?

  1. kubestar loads all starlark files in a given glob pattern.
  2. Each main() function is ran expecting an array of kubernetes protobuf objects.
  3. Finally, the protobuf is than encoded to yaml and written to the named file with the '.star' -> '.yaml'.

Alternative to:

Install

go install github.com/emcfarlane/kubestar@latest

Examples

Have a look at the examples and try with:

kubestar examples/nginx_deployment.star -v 
examples
├── nginx_deployment.star
└── nginx_deployment.yaml (generated)

Features

Protobuf syntax

Protobuf wrapping is provided by github.com/emcfarlane/starlarkproto. See the repo for more details. Similar types will be cast to protobuffer types.

Load common modules

Load common modules with relative path syntax, or absolute based from the cmd init.

load("./my_application.star", "object")

Protobuffer file descriptors

Protobuffer file descriptors can be loaded using proto.file(path). See protos.star for all global namespaced file descriptors.

apps_v1 = proto.file("k8s.io/api/apps/v1/generated.proto")

Drop the k8s.io/ prefix and /generated.proto suffix and finally replace / with _.

Global config files

Important config can be declared in a global file with the flag -global filename.star. This will exec the file and expose as global config to all source files.

Strongly typed

On generation any errors will generate a stacktrace. For example, adding a bad field, okay=True field would yield:

$ kubestar examples/*.star
Traceback (most recent call last):
  examples/nginx_deployment.star:44:19: in main
  examples/nginx_deployment.star:35:36: in deployment
  examples/nginx_deployment.star:6:29: in nginx_container
Error in Container: Container has no .okay attribute

Contrib

K8s protobuffers are currently gogo generated: kubernetes/kubernetes#96564 We will need to clone api and apimachinery repos to a k8s.io/ directory like:

k8s.io
├── api
└── apimachinery

Then generate the protos.pb file:

export GOOGLEAPIS_DIR=~/src/github.com/googleapis/api-common-protos
export K8S_DIR=~/src/github.com
./gen.sh

About

Kubernetes config without all the YAML

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published