-
Notifications
You must be signed in to change notification settings - Fork 838
/
settings.go
139 lines (121 loc) · 4.21 KB
/
settings.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package settings
import (
"context"
"encoding/json"
"fmt"
"time"
v1 "k8s.io/api/core/v1"
"knative.dev/pkg/configmap"
coresettings "github.com/aws/karpenter-core/pkg/apis/settings"
)
type settingsKeyType struct{}
var ContextKey = settingsKeyType{}
var defaultSettings = &Settings{
AssumeRoleARN: "",
AssumeRoleDuration: time.Minute * 15,
ClusterCABundle: "",
ClusterName: "",
ClusterEndpoint: "",
DefaultInstanceProfile: "",
EnablePodENI: false,
EnableENILimitedPodDensity: true,
IsolatedVPC: false,
VMMemoryOverheadPercent: 0.075,
InterruptionQueueName: "",
Tags: map[string]string{},
ReservedENIs: 0,
}
// +k8s:deepcopy-gen=true
type Settings struct {
AssumeRoleARN string
AssumeRoleDuration time.Duration
ClusterCABundle string
ClusterName string
ClusterEndpoint string
DefaultInstanceProfile string
EnablePodENI bool
EnableENILimitedPodDensity bool
IsolatedVPC bool
VMMemoryOverheadPercent float64
InterruptionQueueName string
Tags map[string]string
ReservedENIs int
}
func (*Settings) ConfigMap() string {
return "karpenter-global-settings"
}
// Inject creates a Settings from the supplied ConfigMap
func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context, error) {
s := defaultSettings.DeepCopy()
if cm == nil {
return ToContext(ctx, s), nil
}
if err := configmap.Parse(cm.Data,
configmap.AsString("aws.assumeRoleARN", &s.AssumeRoleARN),
configmap.AsDuration("aws.assumeRoleDuration", &s.AssumeRoleDuration),
configmap.AsString("aws.clusterCABundle", &s.ClusterCABundle),
configmap.AsString("aws.clusterName", &s.ClusterName),
configmap.AsString("aws.clusterEndpoint", &s.ClusterEndpoint),
configmap.AsString("aws.defaultInstanceProfile", &s.DefaultInstanceProfile),
configmap.AsBool("aws.enablePodENI", &s.EnablePodENI),
configmap.AsBool("aws.enableENILimitedPodDensity", &s.EnableENILimitedPodDensity),
configmap.AsBool("aws.isolatedVPC", &s.IsolatedVPC),
configmap.AsFloat64("aws.vmMemoryOverheadPercent", &s.VMMemoryOverheadPercent),
configmap.AsString("aws.interruptionQueueName", &s.InterruptionQueueName),
AsStringMap("aws.tags", &s.Tags),
configmap.AsInt("aws.reservedENIs", &s.ReservedENIs),
); err != nil {
return ctx, fmt.Errorf("parsing settings, %w", err)
}
if err := s.Validate(); err != nil {
return ctx, fmt.Errorf("validating settings, %w", err)
}
return ToContext(ctx, s), nil
}
func (*Settings) FromContext(ctx context.Context) coresettings.Injectable {
return FromContext(ctx)
}
func ToContext(ctx context.Context, s *Settings) context.Context {
return context.WithValue(ctx, ContextKey, s)
}
func FromContext(ctx context.Context) *Settings {
data := ctx.Value(ContextKey)
if data == nil {
// This is developer error if this happens, so we should panic
panic("settings doesn't exist in context")
}
return data.(*Settings)
}
// AsTypedString passes the value at key through into the target, if it exists.
func AsTypedString[T ~string](key string, target *T) configmap.ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
*target = T(raw)
}
return nil
}
}
// AsStringMap parses a value as a JSON map of map[string]string.
func AsStringMap(key string, target *map[string]string) configmap.ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
m := map[string]string{}
if err := json.Unmarshal([]byte(raw), &m); err != nil {
return err
}
*target = m
}
return nil
}
}