Skip to content

Commit bf64216

Browse files
authored
feat: built in properties for the current time (#17)
1 parent 12ed997 commit bf64216

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ metadata:
173173
app.kubernetes.io/version: git-decafbad
174174
```
175175

176+
### Built in Properties
177+
178+
There are a number of built in, non-redefinable properties that are implicitly included for convenience.
179+
180+
| Name | Description | Usage | Example |
181+
|------------|---------------------------------------|------------------|------------------------|
182+
| `DATETIME` | The current time in RFC3339 format | `${{.DATETIME}}` | `2022-07-22T14:52:46Z` |
183+
| `UNIXTIME` | The current time in Unix/Epoch format | `${{.UNIXTIME}}` | `1658501566` |
184+
176185
### ArgoCD
177186

178187
If your are deploying Kustomize applications using ArgoCD, then please take note of the [ArgoCD build environment](https://argo-cd.readthedocs.io/en/stable/user-guide/build-environment/) as it contains a very limited set of environment variables.

main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ func mainCmd() error {
4040
return errors.New("not invoked as a kustomize plugin")
4141
}
4242

43+
// Construct a set of "built in" property values. This includes timestamps
44+
// which are captured immediately after the plugin is invoked.
45+
now := time.Now().UTC()
46+
builtinProperties := map[string]string{
47+
// DATETIME is the current time expressed in the RFC3339 format.
48+
// An example would be "2022-07-22T14:52:46Z".
49+
"DATETIME": now.Format(time.RFC3339),
50+
// UNIXTIME is the current time expressed in the "Unix time" format.
51+
// An example would be "1658501566".
52+
"UNIXTIME": fmt.Sprint(now.Unix()),
53+
}
54+
4355
// Parse the plugin configuration file.
4456
cfg, err := config.Load(os.Args[1])
4557
if err != nil {
@@ -50,6 +62,25 @@ func mainCmd() error {
5062
// to their respective values.
5163
properties := make(map[string]string)
5264
for _, property := range cfg.Properties {
65+
// Validate that a property with the same name as a built in property
66+
// is not configured.
67+
if _, found := builtinProperties[property.Name]; found {
68+
return config.PathError{
69+
Message: "cannot redefine built in property",
70+
Paths: []string{"properties", property.Name},
71+
}
72+
}
73+
74+
// Validate that a property with the same name as a previously
75+
// configured property is not configured.
76+
if _, found := properties[property.Name]; found {
77+
return config.PathError{
78+
Message: "cannot redefine property",
79+
Paths: []string{"properties", property.Name},
80+
}
81+
}
82+
83+
// Resolve a concrete value for the property.
5384
value, err := resolve(property)
5485
if err != nil {
5586
return config.PathError{
@@ -61,6 +92,11 @@ func mainCmd() error {
6192
properties[property.Name] = value
6293
}
6394

95+
// Add the builtin properties to the final set of property values.
96+
for name, value := range builtinProperties {
97+
properties[name] = value
98+
}
99+
64100
// Log the resolved values for each property.
65101
for key, value := range properties {
66102
log.Printf("property %q resolved to %q", key, value)

0 commit comments

Comments
 (0)