/
instances_config.go
92 lines (78 loc) · 2.49 KB
/
instances_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
package exoscale
import (
"crypto/sha256"
"fmt"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/types"
)
// Instances configuration (<-> cloud-config file)
type instancesConfig struct {
Disabled bool // if true, disables this controller
Overrides []instancesOverrideConfig
ExternalOnly bool `yaml:"externalOnly"` // if true, ignore Exoscale API (use only static overrides, if any)
}
type instancesOverrideConfig struct {
Name string // considered a regexp if '/.../'
External bool
ExternalID string `yaml:"externalID"`
Type string
Addresses []instancesOverrideAddressConfig
Region string
}
type instancesOverrideAddressConfig struct {
Type string // Hostname, ExternalIP, InternalIP, ExternalDNS, InternalDNS (v1.NodeAddressType)
Address string
}
func defaultInstanceOverrideExternalID(overrideName string) string {
return fmt.Sprintf("external-%x", sha256.Sum256([]byte(overrideName)))
}
// Return statically-configured instance override
func (c *instancesConfig) getInstanceOverride(nodeName types.NodeName) *instancesOverrideConfig {
var config *instancesOverrideConfig
// first try an exact match on name
for _, candidate := range c.Overrides {
if candidate.Name != "" && nodeName == types.NodeName(candidate.Name) {
config = &candidate //nolint:exportloopref
break
}
}
// then regexp match on "name"
if config == nil {
for _, candidate := range c.Overrides {
if strings.HasPrefix(candidate.Name, "/") && strings.HasSuffix(candidate.Name, "/") {
match, err := regexp.Match(strings.Trim(candidate.Name, "/"), []byte(nodeName))
if err != nil {
errorf("invalid regular expression: %s", candidate.Name)
continue
}
if match {
config = &candidate //nolint:exportloopref
break
}
}
}
}
return config
}
func (c *instancesConfig) getInstanceOverrideByProviderID(providerID string) *instancesOverrideConfig {
var config *instancesOverrideConfig
instanceID := strings.TrimPrefix(providerID, providerPrefix)
// first try an exact match on externalID
for _, candidate := range c.Overrides {
if candidate.ExternalID != "" && instanceID == candidate.ExternalID {
config = &candidate //nolint:exportloopref
break
}
}
// then try a match on the internally-built, name-based one
if config == nil {
for _, candidate := range c.Overrides {
if candidate.ExternalID == "" && instanceID == defaultInstanceOverrideExternalID(candidate.Name) {
config = &candidate //nolint:exportloopref
break
}
}
}
return config
}