forked from hetznercloud/hcloud-cloud-controller-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.go
224 lines (189 loc) · 6.33 KB
/
cloud.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright 2018 Hetzner Cloud GmbH.
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 hcloud
import (
"context"
"errors"
"fmt"
"io"
"os"
"strconv"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops"
"github.com/hetznercloud/hcloud-go/hcloud"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
)
const (
hcloudTokenENVVar = "HCLOUD_TOKEN"
hcloudEndpointENVVar = "HCLOUD_ENDPOINT"
hcloudNetworkENVVar = "HCLOUD_NETWORK"
hcloudDebugENVVar = "HCLOUD_DEBUG"
hcloudLoadBalancersEnabledENVVar = "HCLOUD_LOAD_BALANCERS_ENABLED"
hcloudLoadBalancersLocation = "HCLOUD_LOAD_BALANCERS_LOCATION"
hcloudLoadBalancersNetworkZone = "HCLOUD_LOAD_BALANCERS_NETWORK_ZONE"
hcloudLoadBalancersDisablePrivateIngress = "HCLOUD_LOAD_BALANCERS_DISABLE_PRIVATE_INGRESS"
hcloudLoadBalancersUsePrivateIP = "HCLOUD_LOAD_BALANCERS_USE_PRIVATE_IP"
nodeNameENVVar = "NODE_NAME"
providerName = "hcloud"
providerVersion = "v1.9.1"
)
type cloud struct {
client *hcloud.Client
instances *instances
zones *zones
routes *routes
loadBalancer *loadBalancers
networkID int
}
func newCloud(config io.Reader) (cloudprovider.Interface, error) {
const op = "hcloud/newCloud"
token := os.Getenv(hcloudTokenENVVar)
if token == "" {
return nil, fmt.Errorf("environment variable %q is required", hcloudTokenENVVar)
}
if len(token) != 64 {
return nil, fmt.Errorf("entered token is invalid (must be exactly 64 characters long)")
}
nodeName := os.Getenv(nodeNameENVVar)
if nodeName == "" {
return nil, fmt.Errorf("environment variable %q is required", nodeNameENVVar)
}
opts := []hcloud.ClientOption{
hcloud.WithToken(token),
hcloud.WithApplication("hcloud-cloud-controller", providerVersion),
}
if os.Getenv(hcloudDebugENVVar) == "true" {
opts = append(opts, hcloud.WithDebugWriter(os.Stderr))
}
if endpoint := os.Getenv(hcloudEndpointENVVar); endpoint != "" {
opts = append(opts, hcloud.WithEndpoint(endpoint))
}
client := hcloud.NewClient(opts...)
var networkID int
if v, ok := os.LookupEnv(hcloudNetworkENVVar); ok {
n, _, err := client.Network.Get(context.Background(), v)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
if n == nil {
return nil, fmt.Errorf("%s: Network %s not found", op, v)
}
networkID = n.ID
}
if networkID == 0 {
klog.Infof("%s: %s empty", op, hcloudNetworkENVVar)
}
_, _, err := client.Server.List(context.Background(), hcloud.ServerListOpts{})
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
lbOpsDefaults, lbDisablePrivateIngress, err := loadBalancerDefaultsFromEnv()
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
fmt.Printf("Hetzner Cloud k8s cloud controller %s started\n", providerVersion)
lbOps := &hcops.LoadBalancerOps{
LBClient: &client.LoadBalancer,
CertOps: &hcops.CertificateOps{CertClient: &client.Certificate},
ActionClient: &client.Action,
NetworkClient: &client.Network,
NetworkID: networkID,
Defaults: lbOpsDefaults,
}
loadBalancers := newLoadBalancers(lbOps, &client.Action, lbDisablePrivateIngress)
if os.Getenv(hcloudLoadBalancersEnabledENVVar) == "false" {
loadBalancers = nil
}
return &cloud{
client: client,
zones: newZones(client, nodeName),
instances: newInstances(client),
loadBalancer: loadBalancers,
routes: nil,
networkID: networkID,
}, nil
}
func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
}
func (c *cloud) Instances() (cloudprovider.Instances, bool) {
return c.instances, true
}
func (c *cloud) Zones() (cloudprovider.Zones, bool) {
return c.zones, true
}
func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
if c.loadBalancer == nil {
return nil, false
}
return c.loadBalancer, true
}
func (c *cloud) Clusters() (cloudprovider.Clusters, bool) {
return nil, false
}
func (c *cloud) Routes() (cloudprovider.Routes, bool) {
if c.networkID > 0 {
r, err := newRoutes(c.client, c.networkID)
if err != nil {
klog.ErrorS(err, "create routes provider", "networkID", c.networkID)
return nil, false
}
return r, true
}
return nil, false // If no network is configured, disable the routes part
}
func (c *cloud) ProviderName() string {
return providerName
}
func (c *cloud) ScrubDNS(nameservers, searches []string) (nsOut, srchOut []string) {
return nil, nil
}
func (c *cloud) HasClusterID() bool {
return false
}
func loadBalancerDefaultsFromEnv() (hcops.LoadBalancerDefaults, bool, error) {
defaults := hcops.LoadBalancerDefaults{
Location: os.Getenv(hcloudLoadBalancersLocation),
NetworkZone: os.Getenv(hcloudLoadBalancersNetworkZone),
}
if defaults.Location != "" && defaults.NetworkZone != "" {
return defaults, false, errors.New(
"HCLOUD_LOAD_BALANCERS_LOCATION/HCLOUD_LOAD_BALANCERS_NETWORK_ZONE: Only one of these can be set")
}
disablePrivateIngress, err := getEnvBool(hcloudLoadBalancersDisablePrivateIngress)
if err != nil {
return defaults, false, err
}
defaults.UsePrivateIP, err = getEnvBool(hcloudLoadBalancersUsePrivateIP)
if err != nil {
return defaults, false, err
}
return defaults, disablePrivateIngress, nil
}
// getEnvBool returns the boolean parsed from the environment variable with the given key and a potential error
// parsing the var. Returns false if the env var is unset.
func getEnvBool(key string) (bool, error) {
v, ok := os.LookupEnv(key)
if !ok {
return false, nil
}
b, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("%s: %v", key, err)
}
return b, nil
}
func init() {
cloudprovider.RegisterCloudProvider(providerName, func(config io.Reader) (cloudprovider.Interface, error) {
return newCloud(config)
})
}