Add configmap for RPC. Print latest block on loop
This commit is contained in:
54
tilt_modules/configmap/README.md
Normal file
54
tilt_modules/configmap/README.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# Configmap
|
||||
|
||||
Author: [Nick Santos](https://github.com/nicks)
|
||||
|
||||
Helper functions for creating Kubernetes configmaps.
|
||||
|
||||
## Functions
|
||||
|
||||
### configmap_yaml
|
||||
|
||||
```
|
||||
configmap_yaml(name: str, namespace: str = "", from_file: Union[str, List[str]] = None, watch: bool = True, from_env_file: str = None): Blob
|
||||
```
|
||||
|
||||
Returns YAML for a config map generated from a file.
|
||||
|
||||
* `from_file` ( str ) – equivalent to `kubectl create configmap --from-file`
|
||||
* `from_env_file` (str) - equivalent to `kubectl create configmap --from-env-file`
|
||||
* `watch` ( bool ) - auto-reload if the files change
|
||||
|
||||
### configmap_create
|
||||
|
||||
```
|
||||
configmap_create(name: str, namespace: str = "", from_file: Union[str, List[str]] = None, watch: bool = True, from_env_file: str = None)
|
||||
```
|
||||
|
||||
Deploys a config map. Equivalent to
|
||||
|
||||
```
|
||||
k8s_yaml(configmap_yaml('name', from_file=[...]))
|
||||
```
|
||||
|
||||
### configmap_from_dict
|
||||
|
||||
```
|
||||
configmap_from_dict(name: str, namespace: str = "", inputs: Dict[str, str]] = None): Blob
|
||||
```
|
||||
|
||||
Returns YAML for a config map generated from a given dictionary. Nested dictionaries are not supported
|
||||
|
||||
* `inputs` ( dict ) – equivalent to `kubectl create configmap --from-literal` for each key and value
|
||||
|
||||
## Example Usage
|
||||
|
||||
### For a Grafana config
|
||||
|
||||
```
|
||||
load('ext://configmap', 'configmap_create')
|
||||
configmap_create('grafana-config', from_file=['grafana.ini=./grafana.ini'])
|
||||
```
|
||||
|
||||
## Caveats
|
||||
|
||||
- This extension doesn't do any validation to confirm that names or namespaces are valid.
|
||||
109
tilt_modules/configmap/Tiltfile
Normal file
109
tilt_modules/configmap/Tiltfile
Normal file
@@ -0,0 +1,109 @@
|
||||
# -*- mode: Python -*-
|
||||
|
||||
def configmap_yaml(name, namespace="", from_file=None, watch=True, from_env_file=None):
|
||||
"""Returns YAML for a generic configmap
|
||||
|
||||
Args:
|
||||
name: The configmap name.
|
||||
namespace: The namespace.
|
||||
from_file: Use the from-file configmap generator. May be a string or a list of strings.
|
||||
Example: ["grafana.ini=path/to/grafana.ini"]
|
||||
watch: Reruns the Tiltfile and re-deploys automatically if the from-files change.
|
||||
Defaults to true.
|
||||
from_env_file: Use from-env-file configmap generator. Must be string.
|
||||
Example: "./local.env"
|
||||
|
||||
Returns:
|
||||
The configmap YAML as a blob
|
||||
"""
|
||||
|
||||
args = [
|
||||
"kubectl",
|
||||
"create",
|
||||
"configmap",
|
||||
name,
|
||||
]
|
||||
|
||||
if namespace:
|
||||
args.extend(["-n", namespace])
|
||||
|
||||
generator = False
|
||||
|
||||
if from_file and from_env_file:
|
||||
fail("Must specify either 'from_file' OR 'from_env_file'")
|
||||
|
||||
if from_file:
|
||||
if type(from_file) == "string":
|
||||
from_file = [from_file]
|
||||
|
||||
if type(from_file) == "list":
|
||||
for f in from_file:
|
||||
args.extend(["--from-file", f])
|
||||
if watch:
|
||||
l = f.split('=')
|
||||
watch_file(l[len(l)-1])
|
||||
generator = True
|
||||
else:
|
||||
fail("Bad from_file argument: %s" % from_file)
|
||||
elif from_env_file:
|
||||
if type(from_env_file) == "list":
|
||||
fail("from_env_file only supports string as an input to prevent confusion with kubectl behavior of only loading the last item in a list")
|
||||
elif type(from_env_file == "string"):
|
||||
args.extend(["--from-env-file", from_env_file])
|
||||
if watch:
|
||||
watch_file(from_env_file)
|
||||
generator = True
|
||||
|
||||
if not generator:
|
||||
fail("No configmap generator specified")
|
||||
|
||||
args.extend(["-o=yaml", "--dry-run=client"])
|
||||
return local(args, quiet=True)
|
||||
|
||||
def configmap_from_dict(name, namespace="", inputs={}):
|
||||
"""Returns YAML for a generic configmap
|
||||
Args:
|
||||
name: The configmap name.
|
||||
namespace: The namespace.
|
||||
inputs: A dict of keys and values to use. Nesting is not supported
|
||||
Returns:
|
||||
The configmap YAML as a blob
|
||||
"""
|
||||
|
||||
args = [
|
||||
"kubectl",
|
||||
"create",
|
||||
"configmap",
|
||||
name,
|
||||
]
|
||||
|
||||
if namespace:
|
||||
args.extend(["-n", namespace])
|
||||
|
||||
if type(inputs) != "dict":
|
||||
fail("Bad argument to configmap_from_dict, inputs was not dict typed")
|
||||
|
||||
for k,v in inputs.items():
|
||||
args.extend(["--from-literal", "%s=%s" % (k,v)])
|
||||
|
||||
args.extend(["-o=yaml", "--dry-run=client"])
|
||||
return local(args, quiet=True)
|
||||
|
||||
def configmap_create(name, namespace="", from_file=None, watch=True, from_env_file=None):
|
||||
"""Creates a configmap in the current Kubernetes cluster.
|
||||
|
||||
Generators:
|
||||
- from_file: Wraps kubectl from-file behavior.
|
||||
- from_env_file: Wraps kubectl from-env-file behavior.
|
||||
|
||||
Args:
|
||||
name: The configmap name.
|
||||
namespace: The namespace.
|
||||
from_file: Use the from-file configmap generator. May be a string or a list of strings.
|
||||
Example: ["grafana.ini=path/to/grafana.ini"]
|
||||
watch: Reruns the Tiltfile and re-deploys automatically if the from-files change.
|
||||
Defaults to true.
|
||||
from_env_file: Use from-env-file configmap generator. Must be string.
|
||||
Example: "./local.env"
|
||||
"""
|
||||
k8s_yaml(configmap_yaml(name, namespace, from_file, watch, from_env_file))
|
||||
7
tilt_modules/configmap/test/Tiltfile
Normal file
7
tilt_modules/configmap/test/Tiltfile
Normal file
@@ -0,0 +1,7 @@
|
||||
load('../Tiltfile', 'configmap_create', 'configmap_from_dict')
|
||||
|
||||
configmap_create('job-config', from_file='my-job.ini=./my-job.ini')
|
||||
configmap_create('env-job-config', from_env_file='my-job.env')
|
||||
configmap_from_dict('from-dict-config', inputs={"hello":"world"})
|
||||
|
||||
k8s_yaml('job.yaml')
|
||||
29
tilt_modules/configmap/test/job.yaml
Normal file
29
tilt_modules/configmap/test/job.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: configmap-verify
|
||||
spec:
|
||||
backoffLimit: 1
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: configmap-env-verify
|
||||
image: alpine
|
||||
command: ["/bin/echo", "$(TEST_VAR)"]
|
||||
env:
|
||||
- name: TEST_VAR
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: env-job-config
|
||||
key: TEST_VAR
|
||||
- name: configmap-verify
|
||||
image: alpine
|
||||
command: ["cat", "/etc/my-job/my-job.ini"]
|
||||
volumeMounts:
|
||||
- name: job-config
|
||||
mountPath: /etc/my-job
|
||||
restartPolicy: Never
|
||||
volumes:
|
||||
- name: job-config
|
||||
configMap:
|
||||
name: job-config
|
||||
1
tilt_modules/configmap/test/my-job.env
Normal file
1
tilt_modules/configmap/test/my-job.env
Normal file
@@ -0,0 +1 @@
|
||||
TEST_VAR="hello-env!"
|
||||
1
tilt_modules/configmap/test/my-job.ini
Normal file
1
tilt_modules/configmap/test/my-job.ini
Normal file
@@ -0,0 +1 @@
|
||||
hello!
|
||||
7
tilt_modules/configmap/test/test.sh
Executable file
7
tilt_modules/configmap/test/test.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
set -ex
|
||||
tilt ci
|
||||
tilt down
|
||||
@@ -19,6 +19,11 @@
|
||||
"Name": "restart_process",
|
||||
"ExtensionRegistry": "https://github.com/tilt-dev/tilt-extensions",
|
||||
"TimeFetched": "2021-09-13T20:14:11.011803-04:00"
|
||||
},
|
||||
{
|
||||
"Name": "configmap",
|
||||
"ExtensionRegistry": "https://github.com/tilt-dev/tilt-extensions",
|
||||
"TimeFetched": "2021-09-13T20:58:06.169124-04:00"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user