Skip to content

Commit

Permalink
feat: IGNORE_NOT_FOUND env support (#1)
Browse files Browse the repository at this point in the history
* feat: `IGNORE_NOT_FOUND` env support

* docs: update README.md
  • Loading branch information
matheusfm authored May 15, 2023
1 parent c4f9554 commit a9d4b48
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ spec:
| `SECRETS` | A comma-separated list of `Secrets` namespaced names (`ns1/sec,ns2/sec`) | - |
| `CONFIGMAPS_SELECTOR` | A label selector to match `ConfigMaps` (`foo=bar`) | - |
| `SECRETS_SELECTOR` | A label selector to match `Secrets` (`foo=bar`) | - |
| `IGNORE_NOT_FOUND` | Specifies when not found errors should be ignored | `false` |

# Contributing

Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

Expand All @@ -47,12 +48,18 @@ func main() {
for _, nn := range opts.ConfigMaps {
cm, err := client.ConfigMaps(nn.Namespace).Get(ctx, nn.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) && opts.IgnoreNotFound {
continue
}
log.Fatalln("failed to get ConfigMap:", err)
}
configMaps = append(configMaps, *cm)
}
for _, nn := range opts.Secrets {
sec, err := client.Secrets(nn.Namespace).Get(ctx, nn.Name, metav1.GetOptions{})
if errors.IsNotFound(err) && opts.IgnoreNotFound {
continue
}
if err != nil {
log.Fatalln("failed to get Secret:", err)
}
Expand Down
6 changes: 6 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {

DefaultMode os.FileMode
Dir string
IgnoreNotFound bool
ConfigMaps []types.NamespacedName
Secrets []types.NamespacedName
ConfigMapsSelector string
Expand All @@ -46,6 +47,11 @@ func NewFromEnv() *Options {
} else {
opts.Dir = "/tmp"
}
if v, ok := os.LookupEnv("IGNORE_NOT_FOUND"); ok {
opts.IgnoreNotFound, _ = strconv.ParseBool(v)
} else {
opts.IgnoreNotFound = false
}
if v, ok := os.LookupEnv("CONFIGMAPS"); ok {
opts.ConfigMaps = parseNamespacedNames(v)
}
Expand Down

0 comments on commit a9d4b48

Please sign in to comment.