forked from kubernetes/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
101 lines (80 loc) · 2.81 KB
/
config.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
/*
Copyright 2016 The Kubernetes Authors.
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 dynamic
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"k8s.io/api/core/v1"
)
// Config which represents not static but dynamic configuration of cluster-autoscaler which would be updated periodically at runtime
type Config struct {
Settings
resourceVersion string
}
// Settings of cluster-autoscaler contained in the latest config, which should be consumed by cluster-autoscaler
type Settings struct {
NodeGroups []NodeGroupSpec `json:"nodeGroups"`
}
// NewDefaultConfig builds a new config object
func NewDefaultConfig() Config {
return Config{
Settings: Settings{
NodeGroups: []NodeGroupSpec{},
},
resourceVersion: "",
}
}
// ConfigFromConfigMap returns the configuration read from a configmap
func ConfigFromConfigMap(configmap *v1.ConfigMap) (*Config, error) {
settingsInJson := configmap.Data["settings"]
if settingsInJson == "" {
return nil, fmt.Errorf(`invalid format of configmap: missing the key named "nodeGroups" in config = %v`, settingsInJson)
}
settings := Settings{}
if err := json.Unmarshal([]byte(settingsInJson), &settings); err != nil {
return nil, fmt.Errorf(`failed to parse configmap data: %v`, err)
}
config := &Config{
Settings: settings,
resourceVersion: configmap.ResourceVersion,
}
glog.V(5).Infof("json=%v settings=%v config=%v", settingsInJson, settings, config)
if err := config.validate(); err != nil {
return nil, fmt.Errorf("invalid config : %v", err)
}
return config, nil
}
// VersionMismatchesAgainst returns true if versions between two configs don't match i.e. the config should be updated.
func (c Config) VersionMismatchesAgainst(other Config) bool {
return c.resourceVersion != other.resourceVersion
}
// NodeGroupSpecStrings returns node group specs represented in the form of `<minSize>:<maxSize>:<name>` to be passed to cloudprovider impls.
func (c Config) NodeGroupSpecStrings() []string {
return c.nodeGroupSpecStrings()
}
func (c Config) validate() error {
for _, g := range c.NodeGroups {
if err := g.Validate(); err != nil {
return fmt.Errorf("invalid node group: %v", err)
}
}
return nil
}
func (s Settings) nodeGroupSpecStrings() []string {
result := []string{}
for _, spec := range s.NodeGroups {
result = append(result, spec.String())
}
return result
}