Skip to content

Commit

Permalink
[release/v1.45] Improvements for Anexia (#1405)
Browse files Browse the repository at this point in the history
* 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 <kevin.stiehl@numericas.de>
Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* Fix various linter errors

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* Add missing boilerplate to files

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* Anexia provider: add dummy updater for tests

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

Co-authored-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* docs(anexia): add info for templates (#1288)

* docs(anexia): add info for templates

Signed-off-by: Waleed Malik <ahmedwaleedmalik@gmail.com>

* Update anexia.md

Signed-off-by: Waleed Malik <ahmedwaleedmalik@gmail.com>

* anexia: increase HTTP client timeout to 120s (#1331)

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* 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 <mgrosch@anexia-it.com>

* 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 <mgrosch@anexia-it.com>

* Anexia: update MachineDeployment example

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* CI: disable OperationSystemManager for Anexia

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>

* 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 <mgrosch@anexia-it.com>

Signed-off-by: Waleed Malik <ahmedwaleedmalik@gmail.com>
Signed-off-by: Mara Sophie Grosch <mgrosch@anexia-it.com>
Co-authored-by: Kevin Stiehl <kevinstiehl@live.de>
Co-authored-by: Waleed Malik <ahmedwaleedmalik@gmail.com>
  • Loading branch information
3 people authored Aug 23, 2022
1 parent 81dc767 commit 4285d57
Show file tree
Hide file tree
Showing 12 changed files with 1,167 additions and 164 deletions.
12 changes: 11 additions & 1 deletion docs/anexia.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ This provider implementation is currently in **alpha** state.

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".

An example machine deployment can be found here: [examples/anexia-machinedeployment.yaml](../examples/anexia-machinedeployment.yaml)
An example machine deployment can be found here: [examples/anexia-machinedeployment.yaml](../examples/anexia-machinedeployment.yaml)

## Templates

To retrieve all available templates against a given location:

```
https://engine.anexia-it.com/api/vsphere/v1/provisioning/templates.json/<location identifier>/templates?page=1&limit=50&api_key=<API Key>
```

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.
10 changes: 9 additions & 1 deletion examples/anexia-machinedeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ spec:
locationID: "<< ANEXIA_LOCATION_ID >>"
cpus: 2
memory: 2048
diskSize: 60

# only a single disk is currently supported, but support for multiple disks is planned already
disks:
- size: 60
performanceType: ENT6

# You may have this old disk config attribute in your config - please migrate to the disks attribute.
# For now it is still recognized though.
#diskSize: 60
# Flatcar is the only supported operating system
operatingSystem: "flatcar"
operatingSystemSpec:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/Masterminds/sprig/v3 v3.2.2
github.com/aliyun/alibaba-cloud-sdk-go v1.61.751
github.com/anexia-it/go-anxcloud v0.3.8
github.com/anexia-it/go-anxcloud v0.3.26
github.com/aws/aws-sdk-go v1.36.2
github.com/coreos/container-linux-config-transpiler v0.9.0
github.com/davecgh/go-spew v1.1.1
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ github.com/aliyun/alibaba-cloud-sdk-go v1.61.751 h1:PX0jCn9kBBgaybsFltpmQ8F7O74h
github.com/aliyun/alibaba-cloud-sdk-go v1.61.751/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/andygrunwald/go-gerrit v0.0.0-20190120104749-174420ebee6c/go.mod h1:0iuRQp6WJ44ts+iihy5E/WlPqfg5RNeQxOmzRkxCdtk=
github.com/anexia-it/go-anxcloud v0.3.8 h1:+ZOVqUHwINTm9Q68GPVh+Q/c794Fe+2GahIVagNLjDg=
github.com/anexia-it/go-anxcloud v0.3.8/go.mod h1:cevqezsbOJ4GBlAWaztfLKl9w4VzxJBt4ipgHORi3gw=
github.com/anexia-it/go-anxcloud v0.3.26 h1:uStosj8srS6OA1OsPsMJBFqd4Znzl6fEhUv8b3+G8FU=
github.com/anexia-it/go-anxcloud v0.3.26/go.mod h1:fiEBxEtBXx78/OWBJvL7+2o4TESrnEcrDYjLeonGkDw=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
Expand Down Expand Up @@ -319,6 +320,7 @@ github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down Expand Up @@ -1003,6 +1005,7 @@ github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.10.4/go.mod h1:g/HbgYopi++010VEqkFgJHKC09uJiW9UkXvMUuKHUCQ=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/onsi/gomega v1.15.0 h1:WjP/FQ/sk43MRmnEcT+MlDw2TFvkrXlprrPST/IudjU=
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
Expand Down Expand Up @@ -1181,7 +1184,6 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
Expand Down Expand Up @@ -1605,7 +1607,6 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -1986,7 +1987,6 @@ k8s.io/api v0.19.2/go.mod h1:IQpK0zFQ1xc5iNIQPqzgoOwuFugaYHK4iCknlAQP9nI=
k8s.io/api v0.19.4/go.mod h1:SbtJ2aHCItirzdJ36YslycFNzWADYH3tgOhvBEFtZAk=
k8s.io/api v0.20.1/go.mod h1:KqwcCVogGxQY3nBlRpwt+wpAMF/KjaCc7RpywacvqUo=
k8s.io/api v0.20.2/go.mod h1:d7n6Ehyzx+S+cE3VhTGfVNNqtGc/oL9DCdYYahlurV8=
k8s.io/api v0.20.2/go.mod h1:d7n6Ehyzx+S+cE3VhTGfVNNqtGc/oL9DCdYYahlurV8=
k8s.io/api v0.22.2 h1:M8ZzAD0V6725Fjg53fKeTJxGsJvRbk4TEm/fexHMtfw=
k8s.io/api v0.22.2/go.mod h1:y3ydYpLJAaDI+BbSe2xmGcqxiWHmWjkEeIbiwHvnPR8=
k8s.io/apiextensions-apiserver v0.0.0-20190918161926-8f644eb6e783/go.mod h1:xvae1SZB3E17UpV59AWc271W/Ph25N+bjPyR63X6tPY=
Expand Down Expand Up @@ -2025,7 +2025,6 @@ k8s.io/apimachinery v0.19.2/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlm
k8s.io/apimachinery v0.19.4/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
k8s.io/apimachinery v0.20.1/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
k8s.io/apimachinery v0.20.2/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
k8s.io/apimachinery v0.20.2/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
k8s.io/apimachinery v0.22.2 h1:ejz6y/zNma8clPVfNDLnPbleBo6MpoFy/HBiBqCouVk=
k8s.io/apimachinery v0.22.2/go.mod h1:O3oNtNadZdeOMxHFVxOreoznohCpy0z6mocxbZr7oJ0=
k8s.io/apiserver v0.0.0-20190918160949-bfa5e2e684ad/go.mod h1:XPCXEwhjaFN29a8NldXA901ElnKeKLrLtREO9ZhFyhg=
Expand Down Expand Up @@ -2111,8 +2110,6 @@ k8s.io/klog/v2 v2.9.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/kube-aggregator v0.19.0/go.mod h1:1Ln45PQggFAG8xOqWPIYMxUq8WNtpPnYsbUJ39DpF/A=
k8s.io/kube-aggregator v0.19.4/go.mod h1:cTkvun110194d797AuThyydBBlgm+cKIFUeS2uzGJfU=
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd h1:sOHNzJIkytDF6qadMNKhhDRpc6ODik8lVC6nOur7B2c=
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd h1:sOHNzJIkytDF6qadMNKhhDRpc6ODik8lVC6nOur7B2c=
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
k8s.io/kubectl v0.19.0/go.mod h1:gPCjjsmE6unJzgaUNXIFGZGafiUp5jh0If3F/x7/rRg=
k8s.io/kubectl v0.19.4/go.mod h1:XPmlu4DJEYgD83pvZFeKF8+MSvGnYGqunbFSrJsqHv0=
Expand Down Expand Up @@ -2185,7 +2182,6 @@ sigs.k8s.io/structured-merge-diff v1.0.1/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8AP
sigs.k8s.io/structured-merge-diff/v3 v3.0.0/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.1.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 h1:Hr/htKFmJEbtMgS/UD0N+gtgctAqz81t3nu+sPzynno=
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
Expand Down
91 changes: 91 additions & 0 deletions pkg/cloudprovider/provider/anexia/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2022 The Machine Controller 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 anexia

import (
"encoding/json"
"net/http"
"testing"

"github.com/anexia-it/go-anxcloud/pkg/vsphere/search"
"github.com/gophercloud/gophercloud/testhelper"
"github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
anxtypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/anexia/types"
"github.com/kubermatic/machine-controller/pkg/providerconfig/types"
"k8s.io/apimachinery/pkg/runtime"
)

type ConfigTestCase struct {
Config anxtypes.RawConfig
Error error
}

type ValidateCallTestCase struct {
Spec v1alpha1.MachineSpec
ExpectedError error
}

func getSpecsForValidationTest(t *testing.T, configCases []ConfigTestCase) []ValidateCallTestCase {

var testCases []ValidateCallTestCase

for _, configCase := range configCases {
jsonConfig, err := json.Marshal(configCase.Config)
testhelper.AssertNoErr(t, err)
jsonProviderConfig, err := json.Marshal(types.Config{
CloudProviderSpec: runtime.RawExtension{Raw: jsonConfig},
OperatingSystemSpec: runtime.RawExtension{Raw: []byte("{}")},
})
testhelper.AssertNoErr(t, err)
testCases = append(testCases, ValidateCallTestCase{
Spec: v1alpha1.MachineSpec{
ProviderSpec: v1alpha1.ProviderSpec{
Value: &runtime.RawExtension{Raw: jsonProviderConfig},
},
},
ExpectedError: configCase.Error,
})
}
return testCases
}

func createSearchHandler(t *testing.T, iterations int) http.HandlerFunc {
counter := 0
return func(writer http.ResponseWriter, request *http.Request) {
test := request.URL.Query().Get("name")
testhelper.AssertEquals(t, "%-TestMachine", test)
testhelper.TestMethod(t, request, http.MethodGet)
if iterations == counter {
encoder := json.NewEncoder(writer)
testhelper.AssertNoErr(t, encoder.Encode(map[string]interface{}{
"data": []search.VM{
{
Name: "543053-TestMachine",
Identifier: TestIdentifier,
},
},
}))
}
counter++
}
}

func newConfigVarString(str string) types.ConfigVarString {
return types.ConfigVarString{
Value: str,
}
}
32 changes: 23 additions & 9 deletions pkg/cloudprovider/provider/anexia/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package anexia

import (
"net"

"github.com/anexia-it/go-anxcloud/pkg/vsphere/info"

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

type anexiaInstance struct {
info *info.Info
info *info.Info
reservedAddresses []string
}

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

if ai.info == nil {
return addresses
if ai.reservedAddresses != nil {
for _, reservedIP := range ai.reservedAddresses {
addresses[reservedIP] = v1.NodeExternalIP
}
}

for _, network := range ai.info.Network {
for _, ip := range network.IPv4 {
addresses[ip] = v1.NodeExternalIP
if ai.info != nil {
for _, network := range ai.info.Network {
for _, ip := range network.IPv4 {
addresses[ip] = v1.NodeExternalIP
}
for _, ip := range network.IPv6 {
addresses[ip] = v1.NodeExternalIP
}
}
for _, ip := range network.IPv6 {
}

for ip := range addresses {
parsed := net.ParseIP(ip)
if parsed.IsPrivate() {
addresses[ip] = v1.NodeInternalIP
} else {
addresses[ip] = v1.NodeExternalIP
}

// TODO mark RFC1918 and RFC4193 addresses as internal
}

return addresses
Expand Down
127 changes: 127 additions & 0 deletions pkg/cloudprovider/provider/anexia/instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2022 The Machine Controller 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 anexia

import (
"testing"

"github.com/gophercloud/gophercloud/testhelper"

"github.com/anexia-it/go-anxcloud/pkg/vsphere/info"
v1 "k8s.io/api/core/v1"
)

func TestAnexiaInstance(t *testing.T) {
addressCheck := func(t *testing.T, testcase string, instance *anexiaInstance, expected map[string]v1.NodeAddressType) {
t.Run(testcase, func(t *testing.T) {
addresses := instance.Addresses()

testhelper.AssertDeepEquals(t, expected, addresses)
})
}

t.Run("empty instance", func(t *testing.T) {
instance := anexiaInstance{}
addressCheck(t, "no addresses", &instance, map[string]v1.NodeAddressType{})
})

t.Run("instance with only reservedAddresses set", func(t *testing.T) {
instance := anexiaInstance{
reservedAddresses: []string{"10.0.0.2", "fda0:23::2", "8.8.8.8", "2001:db8::2"},
}

addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
"10.0.0.2": v1.NodeInternalIP,
"fda0:23::2": v1.NodeInternalIP,
"8.8.8.8": v1.NodeExternalIP,
"2001:db8::2": v1.NodeExternalIP,
})
})

t.Run("instance with only info set", func(t *testing.T) {
instance := anexiaInstance{
info: &info.Info{
Network: []info.Network{
{
IPv4: []string{"10.0.0.2"},
IPv6: []string{"fda0:23::2"},
},
{
IPv4: []string{"8.8.8.8"},
IPv6: []string{"2001:db8::2"},
},
},
},
}

addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
"10.0.0.2": v1.NodeInternalIP,
"fda0:23::2": v1.NodeInternalIP,
"8.8.8.8": v1.NodeExternalIP,
"2001:db8::2": v1.NodeExternalIP,
})
})

t.Run("instance with both reservedAddresses and info set, full overlapping set", func(t *testing.T) {
instance := anexiaInstance{
reservedAddresses: []string{"10.0.0.2", "fda0:23::2", "8.8.8.8", "2001:db8::2"},
info: &info.Info{
Network: []info.Network{
{
IPv4: []string{"10.0.0.2"},
IPv6: []string{"fda0:23::2"},
},
{
IPv4: []string{"8.8.8.8"},
IPv6: []string{"2001:db8::2"},
},
},
},
}

addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
"10.0.0.2": v1.NodeInternalIP,
"fda0:23::2": v1.NodeInternalIP,
"8.8.8.8": v1.NodeExternalIP,
"2001:db8::2": v1.NodeExternalIP,
})
})

t.Run("instance with both reservedAddresses and info set, some overlap, each adding some", func(t *testing.T) {
instance := anexiaInstance{
reservedAddresses: []string{"10.0.0.2", "8.8.8.8", "2001:db8::2"},
info: &info.Info{
Network: []info.Network{
{
IPv4: []string{"10.0.0.2"},
IPv6: []string{"fda0:23::2"},
},
{
IPv6: []string{"2001:db8::2"},
},
},
},
}

addressCheck(t, "expected addresses", &instance, map[string]v1.NodeAddressType{
"10.0.0.2": v1.NodeInternalIP,
"fda0:23::2": v1.NodeInternalIP,
"8.8.8.8": v1.NodeExternalIP,
"2001:db8::2": v1.NodeExternalIP,
})
})
}
Loading

0 comments on commit 4285d57

Please sign in to comment.