Skip to content

Commit 18dbb83

Browse files
committed
Re-introduce flags that are used to configure Addon Resizer resources if not specified by config map
1 parent 7ca6aab commit 18dbb83

3 files changed

Lines changed: 49 additions & 14 deletions

File tree

addon-resizer/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ The cluster size is periodically checked, and used to calculate the expected res
1515
Usage of pod_nanny:
1616
--config-dir="": The name of directory used to specify resources for scaled container.
1717
--container="pod-nanny": The name of the container to watch. This defaults to the nanny itself.
18+
--cpu="MISSING": The base CPU resource requirement.
1819
--deployment="": The name of the deployment being monitored. This is required.
20+
--extra-cpu="0": The amount of CPU to add per node.
21+
--extra-memory="0Mi": The amount of memory to add per node.
1922
--extra-storage="0Gi": The amount of storage to add per node.
2023
--log-flush-frequency=5s: Maximum number of seconds between log flushes
24+
--memory="MISSING": The base memory resource requirement.
2125
--namespace=$MY_POD_NAMESPACE: The namespace of the ward. This defaults to the nanny's own pod.
2226
--pod=$MY_POD_NAME: The name of the pod to watch. This defaults to the nanny's own pod.
2327
--poll-period=10000: The time, in milliseconds, to poll the dependent container.
@@ -39,10 +43,6 @@ data:
3943
NannyConfiguration: |-
4044
apiVersion: nannyconfig/v1alpha1
4145
kind: NannyConfiguration
42-
baseCPU: "80m"
43-
cpuPerNode: "0.5"
44-
baseMemory: "140Mi"
45-
memoryPerNode: "4"
4646
---
4747
apiVersion: extensions/v1beta1
4848
kind: Deployment
@@ -91,6 +91,10 @@ spec:
9191
command:
9292
- /pod_nanny
9393
- --config-dir=/etc/config
94+
- --cpu=300m
95+
- --extra-cpu=20m
96+
- --memory=200Mi
97+
- --extra-memory=10Mi
9498
- --threshold=5
9599
- --deployment=nanny-v1
96100
volumes:

addon-resizer/nanny/apis/nannyconfig/v1alpha1/defaults.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,18 @@ func SetDefaults_NannyConfiguration(obj *NannyConfiguration) {
3939
obj.MemoryPerNode = "0"
4040
}
4141
}
42+
43+
func FillInDefaults_NannyConfiguration(obj *NannyConfiguration, defaults *NannyConfiguration) {
44+
if obj.BaseCPU == nannyconfig.NoValue {
45+
obj.BaseCPU = defaults.BaseCPU
46+
}
47+
if obj.CPUPerNode == "0" {
48+
obj.CPUPerNode = defaults.CPUPerNode
49+
}
50+
if obj.BaseMemory == nannyconfig.NoValue {
51+
obj.BaseMemory = defaults.BaseMemory
52+
}
53+
if obj.MemoryPerNode == "0" {
54+
obj.MemoryPerNode = defaults.MemoryPerNode
55+
}
56+
}

addon-resizer/nanny/main/pod_nanny.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ import (
4040

4141
var (
4242
// Flags to define the resource requirements.
43-
configDir = flag.String("config-dir", nannyconfig.NoValue, "Path of configuration containing base resource requirements.")
43+
configDir = flag.String("config-dir", nannyconfig.NoValue, "Path of configuration containing base resource requirements.")
44+
// Following empty values ("") will be overwritten by defaults specified in apis/nannyconfig/v1alpha1/defaults.go
45+
baseCPU = flag.String("cpu", "", "The base CPU resource requirement.")
46+
cpuPerNode = flag.String("extra-cpu", "", "The amount of CPU to add per node.")
47+
baseMemory = flag.String("memory", "", "The base memory resource requirement.")
48+
memoryPerNode = flag.String("extra-memory", "", "The amount of memory to add per node.")
4449
baseStorage = flag.String("storage", nannyconfig.NoValue, "The base storage resource requirement.")
4550
storagePerNode = flag.String("extra-storage", "0Gi", "The amount of storage to add per node.")
4651
threshold = flag.Int("threshold", 0, "A number between 0-100. The dependent's resources are rewritten when they deviate from expected by more than threshold.")
@@ -83,7 +88,13 @@ func main() {
8388
}
8489
k8s := nanny.NewKubernetesClient(*podNamespace, *deployment, *podName, *containerName, clientset)
8590

86-
nannycfg, err := loadNannyConfiguration(*configDir)
91+
nannyConfigurationFromFlags := &nannyconfigalpha.NannyConfiguration{
92+
BaseCPU: *baseCPU,
93+
CPUPerNode: *cpuPerNode,
94+
BaseMemory: *baseMemory,
95+
MemoryPerNode: *memoryPerNode,
96+
}
97+
nannycfg, err := loadNannyConfiguration(*configDir, nannyConfigurationFromFlags)
8798
if err != nil {
8899
glog.Fatal(err)
89100
}
@@ -136,21 +147,26 @@ func main() {
136147
nanny.PollAPIServer(k8s, est, *containerName, pollPeriod, uint64(*threshold))
137148
}
138149

139-
func loadNannyConfiguration(configDir string) (*nannyconfig.NannyConfiguration, error) {
150+
func loadNannyConfiguration(configDir string, defaultConfig *nannyconfigalpha.NannyConfiguration) (*nannyconfig.NannyConfiguration, error) {
140151
path := filepath.Join(configDir, "NannyConfiguration")
141152
scheme, codecs, err := nannyscheme.NewSchemeAndCodecs()
142153
if err != nil {
143154
return nil, err
144155
}
156+
// overwrite defaults with flag-specified parameters
157+
nannyconfigalpha.SetDefaults_NannyConfiguration(defaultConfig)
158+
// retrieve config map parameters if present
159+
configMapConfig := &nannyconfigalpha.NannyConfiguration{}
145160
data, err := ioutil.ReadFile(path)
146161
if err != nil {
147162
glog.V(0).Infof("Failed to read data from config file %q: %v, using default parameters", path, err)
148-
config := &nannyconfigalpha.NannyConfiguration{}
149-
nannyconfigalpha.SetDefaults_NannyConfiguration(config)
150-
return convertNannyConfiguration(config, scheme)
163+
} else if configMapConfig, err = decodeNannyConfiguration(data, scheme, codecs); err != nil {
164+
glog.V(0).Infof("Unable to decode Nanny Configuration from config map, using default parameters")
151165
}
152-
153-
return decodeNannyConfiguration(data, scheme, codecs)
166+
glog.Infof("%s", configMapConfig.BaseCPU)
167+
// overwrite defaults with config map parameters
168+
nannyconfigalpha.FillInDefaults_NannyConfiguration(configMapConfig, defaultConfig)
169+
return convertNannyConfiguration(configMapConfig, scheme)
154170
}
155171

156172
func convertNannyConfiguration(configAlpha *nannyconfigalpha.NannyConfiguration, scheme *runtime.Scheme) (*nannyconfig.NannyConfiguration, error) {
@@ -162,7 +178,7 @@ func convertNannyConfiguration(configAlpha *nannyconfigalpha.NannyConfiguration,
162178
return config, nil
163179
}
164180

165-
func decodeNannyConfiguration(data []byte, scheme *runtime.Scheme, codecs *serializer.CodecFactory) (*nannyconfig.NannyConfiguration, error) {
181+
func decodeNannyConfiguration(data []byte, scheme *runtime.Scheme, codecs *serializer.CodecFactory) (*nannyconfigalpha.NannyConfiguration, error) {
166182
obj, err := runtime.Decode(codecs.UniversalDecoder(nannyconfigalpha.SchemeGroupVersion), data)
167183
if err != nil {
168184
return nil, fmt.Errorf("failed to decode, error: %v", err)
@@ -171,5 +187,5 @@ func decodeNannyConfiguration(data []byte, scheme *runtime.Scheme, codecs *seria
171187
if !ok {
172188
return nil, fmt.Errorf("failed to cast object to NannyConfiguration, object: %#v", obj)
173189
}
174-
return convertNannyConfiguration(externalHC, scheme)
190+
return externalHC, nil
175191
}

0 commit comments

Comments
 (0)