Skip to content

Commit eee2793

Browse files
authored
namespace Add labels and annotation options to namespace_create and namespace_yaml (#252)
If provided, these will appear int the metadata
1 parent b4aace6 commit eee2793

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

namespace/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespaces on Kubernetes objects.
77

88
## Functions
99

10-
### `namespace_yaml(name: str): Blob`
10+
### `namespace_yaml(name: str, annotations: List [str] = [], labels: List [str] = []): Blob`
1111

1212
Returns YAML for a Kubernetes namespace.
1313

14-
### `namespace_create(name: str, allow_duplicates: boolean = False)`
14+
### `namespace_create(name: str, allow_duplicates: boolean = False, annotations: List [str] = [], labels: List [str] = [])`
1515

1616
Deploys a namespace to the cluster. Equivalent to
1717

@@ -43,6 +43,18 @@ namespace_create(ns)
4343
k8s_yaml(namespace_inject(read_file('deployment.yaml'), ns))
4444
```
4545

46+
### Attach annotations and labels
47+
48+
```
49+
load('ext://namespace', 'namespace_create', 'namespace_inject')
50+
namespace_create(
51+
'my-namespace',
52+
annotations=['kubernetes.io/metadata.name: my-namespace'],
53+
labels=['kubernetes.io/metadata.name: my-namespace']
54+
)
55+
k8s_yaml(namespace_inject(read_file('deployment.yaml'), 'my-namespace'))
56+
```
57+
4658
## Caveats
4759

4860
- `namespace_inject` assumes all resources are namespaced-scoped.

namespace/Tiltfile

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
# -*- mode: Python -*-
22

3-
def namespace_yaml(name):
3+
def namespace_yaml(name, annotations=[], labels=[]):
44
"""Returns YAML for a namespace
55
66
Args:
77
name: The namespace name. Currently not validated.
8+
annotations: Annotations applied to the namespace metadata
9+
labels: Labels applied to the namespace metadata
810
911
Returns:
1012
The namespace YAML as a blob
1113
"""
14+
annotations_str = ''
15+
labels_str = ''
16+
if (len(annotations) > 0):
17+
annotations_str = """ annotations:
18+
%s
19+
""" % '\n'.join([" %s" % s for s in annotations])
20+
if (len(labels) > 0):
21+
labels_str = """ labels:
22+
%s
23+
""" % '\n'.join([" %s" % s for s in labels])
1224

1325
return blob("""apiVersion: v1
1426
kind: Namespace
1527
metadata:
1628
name: %s
17-
""" % name)
29+
%s%s""" % (name, annotations_str, labels_str))
1830

19-
def namespace_create(name, allow_duplicates=False):
31+
def namespace_create(name, allow_duplicates=False, annotations=[], labels=[]):
2032
"""Creates a namespace in the current Kubernetes cluster.
2133
2234
Args:
2335
name: The namespace name. Currently not validated.
2436
allow_duplicates: Whether or not k8s should be allowed to have two
2537
instances of the same resource. Useful if you have more than one
2638
Tiltfile trying to create the same namespace.
39+
annotations: Annotations applied to the namespace metadata
40+
labels: Labels applied to the namespace metadata
2741
"""
28-
k8s_yaml(namespace_yaml(name), allow_duplicates=allow_duplicates)
42+
k8s_yaml(
43+
namespace_yaml(name, annotations=annotations, labels=labels),
44+
allow_duplicates=allow_duplicates
45+
)
2946

3047
def namespace_inject(x, ns):
3148
"""Takes K8s yaml, sets its namespace to `ns`, and returns it as a blob.

0 commit comments

Comments
 (0)