forked from target/pod-reaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
121 lines (110 loc) · 3.51 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"github.com/target/pod-reaper/rules"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"os"
"strings"
"time"
)
// environment variable names
const ENV_NAMESPACE = "NAMESPACE"
const ENV_POLL_INTERVAL = "POLL_INTERVAL"
const ENV_RUN_DURATION = "RUN_DURATION"
const ENV_EXCLUDE_LABEL_KEY = "EXCLUDE_LABEL_KEY"
const ENV_EXCLUDE_LABEL_VALUES = "EXCLUDE_LABEL_VALUES"
const ENV_REQUIRE_LABEL_KEY = "REQUIRE_LABEL_KEY"
const ENV_REQUIRE_LABEL_VALUES = "REQUIRE_LABEL_VALUES"
type options struct {
namespace string
pollInterval time.Duration
runDuration time.Duration
labelExclusion *labels.Requirement
labelRequirement *labels.Requirement
rules rules.Rules
}
func namespace() string {
return os.Getenv(ENV_NAMESPACE)
}
func envDuration(key string, defValue string) (time.Duration, error) {
envDuration, exists := os.LookupEnv(key)
if !exists {
envDuration = defValue
}
duration, err := time.ParseDuration(envDuration)
if err != nil {
return duration, fmt.Errorf("invalid %s: %s", key, err)
}
return duration, nil
}
func pollInterval() (time.Duration, error) {
return envDuration(ENV_POLL_INTERVAL, "1m")
}
func runDuration() (time.Duration, error) {
return envDuration(ENV_RUN_DURATION, "0s")
}
func labelExclusion() (*labels.Requirement, error) {
labelKey, labelKeyExists := os.LookupEnv(ENV_EXCLUDE_LABEL_KEY)
labelValue, labelValuesExist := os.LookupEnv(ENV_EXCLUDE_LABEL_VALUES)
if labelKeyExists && !labelValuesExist {
return nil, fmt.Errorf("specified %s but not %s", ENV_EXCLUDE_LABEL_KEY, ENV_EXCLUDE_LABEL_VALUES)
} else if !labelKeyExists && labelValuesExist {
return nil, fmt.Errorf("did not specify %s but did specify %s", ENV_EXCLUDE_LABEL_KEY, ENV_EXCLUDE_LABEL_VALUES)
} else if !labelKeyExists && !labelValuesExist {
return nil, nil
}
labelValues := strings.Split(labelValue, ",")
labelExclusion, err := labels.NewRequirement(labelKey, selection.NotIn, labelValues)
if err != nil {
return nil, fmt.Errorf("could not create exclusion label: %s", err)
}
return labelExclusion, nil
}
func labelRequirement() (*labels.Requirement, error) {
labelKey, labelKeyExists := os.LookupEnv(ENV_REQUIRE_LABEL_KEY)
labelValue, labelValuesExist := os.LookupEnv(ENV_REQUIRE_LABEL_VALUES)
if labelKeyExists && !labelValuesExist {
return nil, fmt.Errorf("specified %s but not %s", ENV_REQUIRE_LABEL_KEY, ENV_REQUIRE_LABEL_VALUES)
} else if !labelKeyExists && labelValuesExist {
return nil, fmt.Errorf("did not specify %s but did specify %s", ENV_REQUIRE_LABEL_KEY, ENV_REQUIRE_LABEL_VALUES)
} else if !labelKeyExists && !labelValuesExist {
return nil, nil
}
labelValues := strings.Split(labelValue, ",")
labelRequirement, err := labels.NewRequirement(labelKey, selection.In, labelValues)
if err != nil {
return nil, fmt.Errorf("could not create requirement label: %s", err)
}
return labelRequirement, nil
}
func loadOptions() (options options, err error) {
// namespace
options.namespace = namespace()
// poll interval
options.pollInterval, err = pollInterval()
if err != nil {
return options, err
}
// run duration
options.runDuration, err = runDuration()
if err != nil {
return options, err
}
// label exclusion
options.labelExclusion, err = labelExclusion()
if err != nil {
return options, err
}
// label requirement
options.labelRequirement, err = labelRequirement()
if err != nil {
return options, err
}
// rules
options.rules, err = rules.LoadRules()
if err != nil {
return options, err
}
return options, err
}