Skip to content

Commit

Permalink
feat: built in properties for the current time (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdk committed Jul 25, 2022
1 parent 12ed997 commit bf64216
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ metadata:
app.kubernetes.io/version: git-decafbad
```

### Built in Properties

There are a number of built in, non-redefinable properties that are implicitly included for convenience.

| Name | Description | Usage | Example |
|------------|---------------------------------------|------------------|------------------------|
| `DATETIME` | The current time in RFC3339 format | `${{.DATETIME}}` | `2022-07-22T14:52:46Z` |
| `UNIXTIME` | The current time in Unix/Epoch format | `${{.UNIXTIME}}` | `1658501566` |

### ArgoCD

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.
Expand Down
36 changes: 36 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func mainCmd() error {
return errors.New("not invoked as a kustomize plugin")
}

// Construct a set of "built in" property values. This includes timestamps
// which are captured immediately after the plugin is invoked.
now := time.Now().UTC()
builtinProperties := map[string]string{
// DATETIME is the current time expressed in the RFC3339 format.
// An example would be "2022-07-22T14:52:46Z".
"DATETIME": now.Format(time.RFC3339),
// UNIXTIME is the current time expressed in the "Unix time" format.
// An example would be "1658501566".
"UNIXTIME": fmt.Sprint(now.Unix()),
}

// Parse the plugin configuration file.
cfg, err := config.Load(os.Args[1])
if err != nil {
Expand All @@ -50,6 +62,25 @@ func mainCmd() error {
// to their respective values.
properties := make(map[string]string)
for _, property := range cfg.Properties {
// Validate that a property with the same name as a built in property
// is not configured.
if _, found := builtinProperties[property.Name]; found {
return config.PathError{
Message: "cannot redefine built in property",
Paths: []string{"properties", property.Name},
}
}

// Validate that a property with the same name as a previously
// configured property is not configured.
if _, found := properties[property.Name]; found {
return config.PathError{
Message: "cannot redefine property",
Paths: []string{"properties", property.Name},
}
}

// Resolve a concrete value for the property.
value, err := resolve(property)
if err != nil {
return config.PathError{
Expand All @@ -61,6 +92,11 @@ func mainCmd() error {
properties[property.Name] = value
}

// Add the builtin properties to the final set of property values.
for name, value := range builtinProperties {
properties[name] = value
}

// Log the resolved values for each property.
for key, value := range properties {
log.Printf("property %q resolved to %q", key, value)
Expand Down

0 comments on commit bf64216

Please sign in to comment.