tilt_modules: commit to repository
See https://docs.tilt.dev/extensions.html for rationale.
This commit is contained in:
parent
594592e2f6
commit
6fd34e3a8c
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Extensions": [
|
||||
{
|
||||
"Name": "namespace",
|
||||
"ExtensionRegistry": "https://github.com/tilt-dev/tilt-extensions",
|
||||
"TimeFetched": "2020-12-05T16:06:07.229737938+01:00"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
# Namespace
|
||||
|
||||
Author: [Nick Santos](https://github.com/nicks)
|
||||
|
||||
Helper functions for creating Kubernetes namespaces and manipulating
|
||||
namespaces on Kubernetes objects.
|
||||
|
||||
## Functions
|
||||
|
||||
### `namespace_yaml(name: str): Blob`
|
||||
|
||||
Returns YAML for a Kubernetes namespace.
|
||||
|
||||
### `namespace_create(name: str)`
|
||||
|
||||
Deploys a namespace to the cluster. Equivalent to
|
||||
|
||||
```
|
||||
load('ext://namespace', 'namespace_yaml')
|
||||
k8s_yaml(namespace_yaml('name'))
|
||||
```
|
||||
|
||||
### `namespace_inject(objects: Union[str, Blob], namespace: str): Blob`
|
||||
|
||||
Given YAML for Kubernetes objects, return new YAML with a different namespace.
|
||||
|
||||
## Example Usage
|
||||
|
||||
### For a fixed namespace:
|
||||
|
||||
```
|
||||
load('ext://namespace', 'namespace_create', 'namespace_inject')
|
||||
namespace_create('my-namespace')
|
||||
k8s_yaml(namespace_inject(read_file('deployment.yaml'), 'my-namespace'))
|
||||
```
|
||||
|
||||
### For a user-specific namespace:
|
||||
|
||||
```
|
||||
load('ext://namespace', 'namespace_create', 'namespace_inject')
|
||||
ns = 'user-%s' % os.environ.get('USER', 'anonymous')
|
||||
namespace_create(ns)
|
||||
k8s_yaml(namespace_inject(read_file('deployment.yaml'), ns))
|
||||
```
|
||||
|
||||
## Caveats
|
||||
|
||||
- `namespace_inject` assumes all resources are namespaced-scoped.
|
||||
The behavior is undefined for cluster-scoped resources.
|
||||
|
||||
- This extension doesn't do any validation to confirm that namespace names are valid.
|
||||
The behavior is undefined on invalid namespaces.
|
|
@ -0,0 +1,71 @@
|
|||
# -*- mode: Python -*-
|
||||
|
||||
def namespace_yaml(name):
|
||||
"""Returns YAML for a namespace
|
||||
|
||||
Args:
|
||||
name: The namespace name. Currently not validated.
|
||||
|
||||
Returns:
|
||||
The namespace YAML as a blob
|
||||
"""
|
||||
|
||||
return blob("""apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: %s
|
||||
""" % name)
|
||||
|
||||
def namespace_create(name):
|
||||
"""Creates a namespace in the current Kubernetes cluster.
|
||||
|
||||
Args:
|
||||
name: The namespace name. Currently not validated.
|
||||
"""
|
||||
k8s_yaml(namespace_yaml(name))
|
||||
|
||||
def namespace_inject(x, ns):
|
||||
"""Takes K8s yaml, sets its namespace to `ns`, and returns it as a blob.
|
||||
|
||||
This modifies the yaml in two ways:
|
||||
1. Sets .metadata.namespace to `ns`
|
||||
2. Sets ..template.metadata.namespace to `ns`
|
||||
This ensures the namespace in, e.g., Deployment Pod Template Specs is
|
||||
set, but might have false positives if you have a CRD with some other
|
||||
element named 'template'.
|
||||
|
||||
Args:
|
||||
x: K8s yaml. Either a filename (string) or the yaml itself (Blob)
|
||||
ns: The namespace to set the K8s objects to.
|
||||
|
||||
Returns:
|
||||
Blob containing the K8s objects as yaml, with namespaces set to `ns`.
|
||||
"""
|
||||
return _mutate_yaml(x, lambda o: _set_k8s_yaml_namespace(o, ns))
|
||||
|
||||
|
||||
def _mutate_yaml(x, f):
|
||||
if type(x) == 'string':
|
||||
objects = read_yaml_stream(x)
|
||||
elif type(x) == 'blob':
|
||||
objects = decode_yaml_stream(x)
|
||||
else:
|
||||
fail('only takes string or blob, got: %s' % type(x))
|
||||
|
||||
return encode_yaml_stream([f(o) for o in objects])
|
||||
|
||||
def _set_k8s_yaml_namespace(o, ns):
|
||||
o['metadata']['namespace'] = ns
|
||||
_set_template_namespace(o, ns)
|
||||
return o
|
||||
|
||||
def _set_template_namespace(o, ns):
|
||||
if type(o) == 'dict':
|
||||
for k, v in o.items():
|
||||
if k == 'template' and type(v) == 'dict' and type(v.get('metadata', None)) == 'dict':
|
||||
v['metadata']['namespace'] = ns
|
||||
if type(v) == 'dict' or type(v) == 'list':
|
||||
_set_template_namespace(v, ns)
|
||||
elif type(o) == 'list':
|
||||
for v in o:
|
||||
_set_template_namespace(v, ns)
|
|
@ -0,0 +1,12 @@
|
|||
load('../Tiltfile', 'namespace_create', 'namespace_inject')
|
||||
|
||||
# Disable parallelism until this issue is fixed:
|
||||
# https://github.com/tilt-dev/tilt/issues/3421
|
||||
update_settings(max_parallel_updates=1)
|
||||
|
||||
namespace_create('namespace-test')
|
||||
k8s_yaml(namespace_inject('deployment.yaml', 'namespace-test'))
|
||||
k8s_yaml('job.yaml')
|
||||
k8s_yaml(namespace_inject('job-default-namespace.yaml', 'namespace-test'))
|
||||
k8s_resource('namespace-test-verify', resource_deps=['namespace-test-busybox'])
|
||||
k8s_resource('namespace-test-verify2', resource_deps=['namespace-test-busybox'])
|
|
@ -0,0 +1,35 @@
|
|||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: namespace-test-busybox
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: namespace-test-busybox
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: namespace-test-busybox
|
||||
spec:
|
||||
containers:
|
||||
- name: busybox
|
||||
image: busybox
|
||||
ports:
|
||||
- containerPort: 8000
|
||||
command: ["sh", "-c", "echo 'hello world' > index.html; busybox httpd -f -p 8000"]
|
||||
readinessProbe:
|
||||
tcpSocket:
|
||||
port: 8000
|
||||
periodSeconds: 1
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: namespace-test-busybox
|
||||
spec:
|
||||
selector:
|
||||
app: namespace-test-busybox
|
||||
ports:
|
||||
- protocol: TCP
|
||||
port: 8000
|
||||
targetPort: 8000
|
|
@ -0,0 +1,13 @@
|
|||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: namespace-test-verify2
|
||||
spec:
|
||||
backoffLimit: 1
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: namespace-test-verify
|
||||
image: curlimages/curl
|
||||
command: ["curl", "-fsSL", "http://namespace-test-busybox:8000/"]
|
||||
restartPolicy: Never
|
|
@ -0,0 +1,14 @@
|
|||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: namespace-test-verify
|
||||
namespace: namespace-test
|
||||
spec:
|
||||
backoffLimit: 1
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: namespace-test-verify
|
||||
image: curlimages/curl
|
||||
command: ["curl", "-fsSL", "http://namespace-test-busybox:8000/"]
|
||||
restartPolicy: Never
|
|
@ -0,0 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
cd $(dirname $0)
|
||||
|
||||
set -ex
|
||||
tilt ci
|
||||
tilt down --delete-namespaces
|
Loading…
Reference in New Issue