Skip to content

Commit 4285d57

Browse files
LittleFox94kstiehlahmedwaleedmalik
authored
[release/v1.45] Improvements for Anexia (#1405)
* Make anexia provider more resilient against errors (#1175) * add conditions & tests Original commit by @kstiehl, conflicts resolved by @LittleFox94, mainly in pkg/cloudprovider/provider/anexia/provider.go Signed-off-by: kstiehl <[email protected]> Signed-off-by: Mara Sophie Grosch <[email protected]> * Fix various linter errors Signed-off-by: Mara Sophie Grosch <[email protected]> * Add missing boilerplate to files Signed-off-by: Mara Sophie Grosch <[email protected]> * Anexia provider: add dummy updater for tests Signed-off-by: Mara Sophie Grosch <[email protected]> Co-authored-by: Mara Sophie Grosch <[email protected]> * docs(anexia): add info for templates (#1288) * docs(anexia): add info for templates Signed-off-by: Waleed Malik <[email protected]> * Update anexia.md Signed-off-by: Waleed Malik <[email protected]> * anexia: increase HTTP client timeout to 120s (#1331) Signed-off-by: Mara Sophie Grosch <[email protected]> * Anexia: extend disk configuration (#1402) * Anexia: cleanup Config/RawConfig attribute duplication types.Config and types.RawConfig contained the same members, with RawConfig using ConfigVars and Config storing the resolved data. This commit does some cleanup to ease adding new config values in the future. Signed-off-by: Mara Sophie Grosch <[email protected]> * Anexia: extend disk configuration Adds a new config value "Disks", with each entry configuring the size and performance type of a single disk. The config format can be used for configuring multiple disks in the future, which is on our roadmap, but right now only a single disk is supported - with the added feature of defining the performance type. Signed-off-by: Mara Sophie Grosch <[email protected]> * Anexia: update MachineDeployment example Signed-off-by: Mara Sophie Grosch <[email protected]> * CI: disable OperationSystemManager for Anexia Signed-off-by: Mara Sophie Grosch <[email protected]> Signed-off-by: Mara Sophie Grosch <[email protected]> * Anexia: return reserved IP when instance IPs are requested When creating a machine at, we always first reserve an IP address for it. Later when retrieving the addresses of that machine, we rely on the vminfo API, which has some delay as it retrieves the IPs from the running VM. This commit adds the reserved address to the list of addresses returned from an instance, which should reduce provisioning time a bit and make it more stable. Also fixes a long-standing TODO comment: marking internal IPs as internal. Signed-off-by: Mara Sophie Grosch <[email protected]> Signed-off-by: Waleed Malik <[email protected]> Signed-off-by: Mara Sophie Grosch <[email protected]> Co-authored-by: Kevin Stiehl <[email protected]> Co-authored-by: Waleed Malik <[email protected]>
1 parent 81dc767 commit 4285d57

File tree

12 files changed

+1167
-164
lines changed

12 files changed

+1167
-164
lines changed

docs/anexia.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ This provider implementation is currently in **alpha** state.
66

77
Only flatcar linux is currently supported and you explicitly have to set the provisioning mechanism to cloud-init by setting `machine.spec.providerSpec.value.operatingSystemSpec.provisioningUtility` to "cloud-init".
88

9-
An example machine deployment can be found here: [examples/anexia-machinedeployment.yaml](../examples/anexia-machinedeployment.yaml)
9+
An example machine deployment can be found here: [examples/anexia-machinedeployment.yaml](../examples/anexia-machinedeployment.yaml)
10+
11+
## Templates
12+
13+
To retrieve all available templates against a given location:
14+
15+
```
16+
https://engine.anexia-it.com/api/vsphere/v1/provisioning/templates.json/<location identifier>/templates?page=1&limit=50&api_key=<API Key>
17+
```
18+
19+
Templates are rotated pretty often, to include updates and latest security patches. Outdated versions of templates are not retained as a result and they get removed after some time.

examples/anexia-machinedeployment.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ spec:
3636
locationID: "<< ANEXIA_LOCATION_ID >>"
3737
cpus: 2
3838
memory: 2048
39-
diskSize: 60
39+
40+
# only a single disk is currently supported, but support for multiple disks is planned already
41+
disks:
42+
- size: 60
43+
performanceType: ENT6
44+
45+
# You may have this old disk config attribute in your config - please migrate to the disks attribute.
46+
# For now it is still recognized though.
47+
#diskSize: 60
4048
# Flatcar is the only supported operating system
4149
operatingSystem: "flatcar"
4250
operatingSystemSpec:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/Masterminds/semver/v3 v3.1.1
1313
github.com/Masterminds/sprig/v3 v3.2.2
1414
github.com/aliyun/alibaba-cloud-sdk-go v1.61.751
15-
github.com/anexia-it/go-anxcloud v0.3.8
15+
github.com/anexia-it/go-anxcloud v0.3.26
1616
github.com/aws/aws-sdk-go v1.36.2
1717
github.com/coreos/container-linux-config-transpiler v0.9.0
1818
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ github.com/aliyun/alibaba-cloud-sdk-go v1.61.751 h1:PX0jCn9kBBgaybsFltpmQ8F7O74h
172172
github.com/aliyun/alibaba-cloud-sdk-go v1.61.751/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
173173
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
174174
github.com/andygrunwald/go-gerrit v0.0.0-20190120104749-174420ebee6c/go.mod h1:0iuRQp6WJ44ts+iihy5E/WlPqfg5RNeQxOmzRkxCdtk=
175-
github.com/anexia-it/go-anxcloud v0.3.8 h1:+ZOVqUHwINTm9Q68GPVh+Q/c794Fe+2GahIVagNLjDg=
176175
github.com/anexia-it/go-anxcloud v0.3.8/go.mod h1:cevqezsbOJ4GBlAWaztfLKl9w4VzxJBt4ipgHORi3gw=
176+
github.com/anexia-it/go-anxcloud v0.3.26 h1:uStosj8srS6OA1OsPsMJBFqd4Znzl6fEhUv8b3+G8FU=
177+
github.com/anexia-it/go-anxcloud v0.3.26/go.mod h1:fiEBxEtBXx78/OWBJvL7+2o4TESrnEcrDYjLeonGkDw=
177178
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
178179
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
179180
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
@@ -319,6 +320,7 @@ github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
319320
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
320321
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
321322
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
323+
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
322324
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
323325
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
324326
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
@@ -1003,6 +1005,7 @@ github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
10031005
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
10041006
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
10051007
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
1008+
github.com/onsi/gomega v1.10.4/go.mod h1:g/HbgYopi++010VEqkFgJHKC09uJiW9UkXvMUuKHUCQ=
10061009
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
10071010
github.com/onsi/gomega v1.15.0 h1:WjP/FQ/sk43MRmnEcT+MlDw2TFvkrXlprrPST/IudjU=
10081011
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
@@ -1181,7 +1184,6 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
11811184
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
11821185
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
11831186
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
1184-
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
11851187
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
11861188
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
11871189
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
@@ -1605,7 +1607,6 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
16051607
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16061608
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16071609
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1608-
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16091610
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16101611
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16111612
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -1986,7 +1987,6 @@ k8s.io/api v0.19.2/go.mod h1:IQpK0zFQ1xc5iNIQPqzgoOwuFugaYHK4iCknlAQP9nI=
19861987
k8s.io/api v0.19.4/go.mod h1:SbtJ2aHCItirzdJ36YslycFNzWADYH3tgOhvBEFtZAk=
19871988
k8s.io/api v0.20.1/go.mod h1:KqwcCVogGxQY3nBlRpwt+wpAMF/KjaCc7RpywacvqUo=
19881989
k8s.io/api v0.20.2/go.mod h1:d7n6Ehyzx+S+cE3VhTGfVNNqtGc/oL9DCdYYahlurV8=
1989-
k8s.io/api v0.20.2/go.mod h1:d7n6Ehyzx+S+cE3VhTGfVNNqtGc/oL9DCdYYahlurV8=
19901990
k8s.io/api v0.22.2 h1:M8ZzAD0V6725Fjg53fKeTJxGsJvRbk4TEm/fexHMtfw=
19911991
k8s.io/api v0.22.2/go.mod h1:y3ydYpLJAaDI+BbSe2xmGcqxiWHmWjkEeIbiwHvnPR8=
19921992
k8s.io/apiextensions-apiserver v0.0.0-20190918161926-8f644eb6e783/go.mod h1:xvae1SZB3E17UpV59AWc271W/Ph25N+bjPyR63X6tPY=
@@ -2025,7 +2025,6 @@ k8s.io/apimachinery v0.19.2/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlm
20252025
k8s.io/apimachinery v0.19.4/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
20262026
k8s.io/apimachinery v0.20.1/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
20272027
k8s.io/apimachinery v0.20.2/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
2028-
k8s.io/apimachinery v0.20.2/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
20292028
k8s.io/apimachinery v0.22.2 h1:ejz6y/zNma8clPVfNDLnPbleBo6MpoFy/HBiBqCouVk=
20302029
k8s.io/apimachinery v0.22.2/go.mod h1:O3oNtNadZdeOMxHFVxOreoznohCpy0z6mocxbZr7oJ0=
20312030
k8s.io/apiserver v0.0.0-20190918160949-bfa5e2e684ad/go.mod h1:XPCXEwhjaFN29a8NldXA901ElnKeKLrLtREO9ZhFyhg=
@@ -2111,8 +2110,6 @@ k8s.io/klog/v2 v2.9.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
21112110
k8s.io/kube-aggregator v0.19.0/go.mod h1:1Ln45PQggFAG8xOqWPIYMxUq8WNtpPnYsbUJ39DpF/A=
21122111
k8s.io/kube-aggregator v0.19.4/go.mod h1:cTkvun110194d797AuThyydBBlgm+cKIFUeS2uzGJfU=
21132112
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd h1:sOHNzJIkytDF6qadMNKhhDRpc6ODik8lVC6nOur7B2c=
2114-
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd h1:sOHNzJIkytDF6qadMNKhhDRpc6ODik8lVC6nOur7B2c=
2115-
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
21162113
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
21172114
k8s.io/kubectl v0.19.0/go.mod h1:gPCjjsmE6unJzgaUNXIFGZGafiUp5jh0If3F/x7/rRg=
21182115
k8s.io/kubectl v0.19.4/go.mod h1:XPmlu4DJEYgD83pvZFeKF8+MSvGnYGqunbFSrJsqHv0=
@@ -2185,7 +2182,6 @@ sigs.k8s.io/structured-merge-diff v1.0.1/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8AP
21852182
sigs.k8s.io/structured-merge-diff/v3 v3.0.0/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
21862183
sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
21872184
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
2188-
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
21892185
sigs.k8s.io/structured-merge-diff/v4 v4.1.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
21902186
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 h1:Hr/htKFmJEbtMgS/UD0N+gtgctAqz81t3nu+sPzynno=
21912187
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2022 The Machine Controller Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package anexia
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"testing"
23+
24+
"github.com/anexia-it/go-anxcloud/pkg/vsphere/search"
25+
"github.com/gophercloud/gophercloud/testhelper"
26+
"github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
27+
anxtypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/anexia/types"
28+
"github.com/kubermatic/machine-controller/pkg/providerconfig/types"
29+
"k8s.io/apimachinery/pkg/runtime"
30+
)
31+
32+
type ConfigTestCase struct {
33+
Config anxtypes.RawConfig
34+
Error error
35+
}
36+
37+
type ValidateCallTestCase struct {
38+
Spec v1alpha1.MachineSpec
39+
ExpectedError error
40+
}
41+
42+
func getSpecsForValidationTest(t *testing.T, configCases []ConfigTestCase) []ValidateCallTestCase {
43+
44+
var testCases []ValidateCallTestCase
45+
46+
for _, configCase := range configCases {
47+
jsonConfig, err := json.Marshal(configCase.Config)
48+
testhelper.AssertNoErr(t, err)
49+
jsonProviderConfig, err := json.Marshal(types.Config{
50+
CloudProviderSpec: runtime.RawExtension{Raw: jsonConfig},
51+
OperatingSystemSpec: runtime.RawExtension{Raw: []byte("{}")},
52+
})
53+
testhelper.AssertNoErr(t, err)
54+
testCases = append(testCases, ValidateCallTestCase{
55+
Spec: v1alpha1.MachineSpec{
56+
ProviderSpec: v1alpha1.ProviderSpec{
57+
Value: &runtime.RawExtension{Raw: jsonProviderConfig},
58+
},
59+
},
60+
ExpectedError: configCase.Error,
61+
})
62+
}
63+
return testCases
64+
}
65+
66+
func createSearchHandler(t *testing.T, iterations int) http.HandlerFunc {
67+
counter := 0
68+
return func(writer http.ResponseWriter, request *http.Request) {
69+
test := request.URL.Query().Get("name")
70+
testhelper.AssertEquals(t, "%-TestMachine", test)
71+
testhelper.TestMethod(t, request, http.MethodGet)
72+
if iterations == counter {
73+
encoder := json.NewEncoder(writer)
74+
testhelper.AssertNoErr(t, encoder.Encode(map[string]interface{}{
75+
"data": []search.VM{
76+
{
77+
Name: "543053-TestMachine",
78+
Identifier: TestIdentifier,
79+
},
80+
},
81+
}))
82+
}
83+
counter++
84+
}
85+
}
86+
87+
func newConfigVarString(str string) types.ConfigVarString {
88+
return types.ConfigVarString{
89+
Value: str,
90+
}
91+
}

pkg/cloudprovider/provider/anexia/instance.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package anexia
1818

1919
import (
20+
"net"
21+
2022
"github.com/anexia-it/go-anxcloud/pkg/vsphere/info"
2123

2224
"github.com/kubermatic/machine-controller/pkg/cloudprovider/instance"
@@ -26,7 +28,8 @@ import (
2628
)
2729

2830
type anexiaInstance struct {
29-
info *info.Info
31+
info *info.Info
32+
reservedAddresses []string
3033
}
3134

3235
func (ai *anexiaInstance) Name() string {
@@ -48,19 +51,30 @@ func (ai *anexiaInstance) ID() string {
4851
func (ai *anexiaInstance) Addresses() map[string]v1.NodeAddressType {
4952
addresses := map[string]v1.NodeAddressType{}
5053

51-
if ai.info == nil {
52-
return addresses
54+
if ai.reservedAddresses != nil {
55+
for _, reservedIP := range ai.reservedAddresses {
56+
addresses[reservedIP] = v1.NodeExternalIP
57+
}
5358
}
5459

55-
for _, network := range ai.info.Network {
56-
for _, ip := range network.IPv4 {
57-
addresses[ip] = v1.NodeExternalIP
60+
if ai.info != nil {
61+
for _, network := range ai.info.Network {
62+
for _, ip := range network.IPv4 {
63+
addresses[ip] = v1.NodeExternalIP
64+
}
65+
for _, ip := range network.IPv6 {
66+
addresses[ip] = v1.NodeExternalIP
67+
}
5868
}
59-
for _, ip := range network.IPv6 {
69+
}
70+
71+
for ip := range addresses {
72+
parsed := net.ParseIP(ip)
73+
if parsed.IsPrivate() {
74+
addresses[ip] = v1.NodeInternalIP
75+
} else {
6076
addresses[ip] = v1.NodeExternalIP
6177
}
62-
63-
// TODO mark RFC1918 and RFC4193 addresses as internal
6478
}
6579

6680
return addresses
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright 2022 The Machine Controller Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package anexia
18+
19+
import (
20+
"testing"
21+
22+
"github.com/gophercloud/gophercloud/testhelper"
23+
24+
"github.com/anexia-it/go-anxcloud/pkg/vsphere/info"
25+
v1 "k8s.io/api/core/v1"
26+
)
27+
28+
func TestAnexiaInstance(t *testing.T) {
29+
addressCheck := func(t *testing.T, testcase string, instance *anexiaInstance, expected map[string]v1.NodeAddressType) {
30+
t.Run(testcase, func(t *testing.T) {
31+
addresses := instance.Addresses()
32+
33+
testhelper.AssertDeepEquals(t, expected, addresses)
34+
})
35+
}
36+
37+
t.Run("empty instance", func(t *testing.T) {
38+
instance := anexiaInstance{}
39+
addressCheck(t, "no addresses", &instance, map[string]v1.NodeAddressType{})
40+
})
41+
42+
t.Run("instance with only reservedAddresses set", func(t *testing.T) {
43+
instance := anexiaInstance{
44+
reservedAddresses: []string{"10.0.0.2", "fda0:23::2", "8.8.8.8", "2001:db8::2"},
45+
}
46+
47+
addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
48+
"10.0.0.2": v1.NodeInternalIP,
49+
"fda0:23::2": v1.NodeInternalIP,
50+
"8.8.8.8": v1.NodeExternalIP,
51+
"2001:db8::2": v1.NodeExternalIP,
52+
})
53+
})
54+
55+
t.Run("instance with only info set", func(t *testing.T) {
56+
instance := anexiaInstance{
57+
info: &info.Info{
58+
Network: []info.Network{
59+
{
60+
IPv4: []string{"10.0.0.2"},
61+
IPv6: []string{"fda0:23::2"},
62+
},
63+
{
64+
IPv4: []string{"8.8.8.8"},
65+
IPv6: []string{"2001:db8::2"},
66+
},
67+
},
68+
},
69+
}
70+
71+
addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
72+
"10.0.0.2": v1.NodeInternalIP,
73+
"fda0:23::2": v1.NodeInternalIP,
74+
"8.8.8.8": v1.NodeExternalIP,
75+
"2001:db8::2": v1.NodeExternalIP,
76+
})
77+
})
78+
79+
t.Run("instance with both reservedAddresses and info set, full overlapping set", func(t *testing.T) {
80+
instance := anexiaInstance{
81+
reservedAddresses: []string{"10.0.0.2", "fda0:23::2", "8.8.8.8", "2001:db8::2"},
82+
info: &info.Info{
83+
Network: []info.Network{
84+
{
85+
IPv4: []string{"10.0.0.2"},
86+
IPv6: []string{"fda0:23::2"},
87+
},
88+
{
89+
IPv4: []string{"8.8.8.8"},
90+
IPv6: []string{"2001:db8::2"},
91+
},
92+
},
93+
},
94+
}
95+
96+
addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
97+
"10.0.0.2": v1.NodeInternalIP,
98+
"fda0:23::2": v1.NodeInternalIP,
99+
"8.8.8.8": v1.NodeExternalIP,
100+
"2001:db8::2": v1.NodeExternalIP,
101+
})
102+
})
103+
104+
t.Run("instance with both reservedAddresses and info set, some overlap, each adding some", func(t *testing.T) {
105+
instance := anexiaInstance{
106+
reservedAddresses: []string{"10.0.0.2", "8.8.8.8", "2001:db8::2"},
107+
info: &info.Info{
108+
Network: []info.Network{
109+
{
110+
IPv4: []string{"10.0.0.2"},
111+
IPv6: []string{"fda0:23::2"},
112+
},
113+
{
114+
IPv6: []string{"2001:db8::2"},
115+
},
116+
},
117+
},
118+
}
119+
120+
addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
121+
"10.0.0.2": v1.NodeInternalIP,
122+
"fda0:23::2": v1.NodeInternalIP,
123+
"8.8.8.8": v1.NodeExternalIP,
124+
"2001:db8::2": v1.NodeExternalIP,
125+
})
126+
})
127+
}

0 commit comments

Comments
 (0)