Repository: kubermatic/machine-controller
Pull request #1640 (2023-05-12 07:37:31)

[release/v1.56] Anexia Provider: Configure dns servers
**What this PR does / why we need it**:
Backport for https://github.com/kubermatic/machine-controller/pull/1637

**Which issue(s) this PR fixes**:
<!--optional, in `fixes #<issue number>` format, will close the issue(s) when PR gets merged-->
Fixes #

**What type of PR is this?**
<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind design

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
/kind chore
-->
/kind feature

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change? Then add your Release Note here**:
<!--
Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->
```release-note
Anexia Provider: Enable up to 4 custom dns entries
```

**Documentation**:
<!--
Please do one of the following options:
- Add a link to the existing documentation
- Add a link to the kubermatic/docs pull request
- If no documentation change is applicable then add:
  - TBD (documentation will be added later)
  - NONE (no documentation needed for this PR)
-->
```documentation
NONE
```


kubermatic-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubermatic/machine-controller/pull/1640#" title="Author self-approved">ahmedwaleedmalik</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubermatic%2Fmachine-controller).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubermatic/machine-controller/blob/release/v1.56/OWNERS)~~ [ahmedwaleedmalik]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
kron4eg: /lgtm
kubermatic-bot: LGTM label has been added.  <details>Git tree hash: 67af4149fcac69eb00e38bab96fde961cdd3b6e0</details>

Patch:
diff --git a/pkg/cloudprovider/provider/anexia/provider.go b/pkg/cloudprovider/provider/anexia/provider.go
index 47aa46a8..967d9578 100644
--- a/pkg/cloudprovider/provider/anexia/provider.go
+++ b/pkg/cloudprovider/provider/anexia/provider.go
@@ -96,17 +96,18 @@ func (p *provider) Create(ctx context.Context, machine *clusterv1alpha1.Machine,
 	// ensure conditions are present on machine
 	ensureConditions(&status)
 
-	config, _, err := p.getConfig(ctx, machine.Spec.ProviderSpec)
+	config, providerCfg, err := p.getConfig(ctx, machine.Spec.ProviderSpec)
 	if err != nil {
 		return nil, fmt.Errorf("unable to get provider config: %w", err)
 	}
 
 	ctx = createReconcileContext(ctx, reconcileContext{
-		Status:       &status,
-		UserData:     userdata,
-		Config:       *config,
-		ProviderData: data,
-		Machine:      machine,
+		Status:         &status,
+		UserData:       userdata,
+		Config:         *config,
+		ProviderData:   data,
+		ProviderConfig: providerCfg,
+		Machine:        machine,
 	})
 
 	_, client, err := getClient(config.Token)
@@ -166,6 +167,19 @@ func provisionVM(ctx context.Context, client anxclient.Client) error {
 
 		vm.Script = base64.StdEncoding.EncodeToString([]byte(reconcileContext.UserData))
 
+		for index, dnsServer := range reconcileContext.ProviderConfig.Network.DNS.Servers {
+			switch index {
+			case 0:
+				vm.DNS1 = dnsServer
+			case 1:
+				vm.DNS2 = dnsServer
+			case 2:
+				vm.DNS3 = dnsServer
+			case 3:
+				vm.DNS4 = dnsServer
+			}
+		}
+
 		// We generate a fresh SSH key but will never actually use it - we just want a valid public key to disable password authentication for our fresh VM.
 		sshKey, err := ssh.NewKey()
 		if err != nil {
diff --git a/pkg/cloudprovider/provider/anexia/reconcile_context.go b/pkg/cloudprovider/provider/anexia/reconcile_context.go
index dea3577c..2a14d872 100644
--- a/pkg/cloudprovider/provider/anexia/reconcile_context.go
+++ b/pkg/cloudprovider/provider/anexia/reconcile_context.go
@@ -22,6 +22,7 @@ import (
 	"github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
 	anxtypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/anexia/types"
 	cloudprovidertypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/types"
+	providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
 )
 
 type contextKey byte
@@ -29,11 +30,12 @@ type contextKey byte
 const machineReconcileContextKey contextKey = 0
 
 type reconcileContext struct {
-	Machine      *v1alpha1.Machine
-	Status       *anxtypes.ProviderStatus
-	UserData     string
-	Config       resolvedConfig
-	ProviderData *cloudprovidertypes.ProviderData
+	Machine        *v1alpha1.Machine
+	Status         *anxtypes.ProviderStatus
+	UserData       string
+	Config         resolvedConfig
+	ProviderData   *cloudprovidertypes.ProviderData
+	ProviderConfig *providerconfigtypes.Config
 }
 
 func createReconcileContext(ctx context.Context, cc reconcileContext) context.Context {


Test patch:
diff --git a/pkg/cloudprovider/provider/anexia/provider_test.go b/pkg/cloudprovider/provider/anexia/provider_test.go
index 4bda59f2..5ba8e656 100644
--- a/pkg/cloudprovider/provider/anexia/provider_test.go
+++ b/pkg/cloudprovider/provider/anexia/provider_test.go
@@ -44,6 +44,7 @@ import (
 	cloudprovidererrors "github.com/kubermatic/machine-controller/pkg/cloudprovider/errors"
 	anxtypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/anexia/types"
 	cloudprovidertypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/types"
+	providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
 
 	"k8s.io/apimachinery/pkg/api/meta"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -105,6 +106,11 @@ func TestAnexiaProvider(t *testing.T) {
 			testhelper.AssertEquals(t, expectedJSON["cpu_performance_type"], jsonBody["cpu_performance_type"])
 			testhelper.AssertEquals(t, expectedJSON["hostname"], jsonBody["hostname"])
 			testhelper.AssertEquals(t, expectedJSON["memory_mb"], jsonBody["memory_mb"])
+
+			testhelper.AssertEquals(t, jsonBody["dns1"], "1.1.1.1")
+			testhelper.AssertEquals(t, jsonBody["dns2"], nil)
+			testhelper.AssertEquals(t, jsonBody["dns3"], "192.168.0.1")
+			testhelper.AssertEquals(t, jsonBody["dns4"], "192.168.0.2")
 			testhelper.AssertEquals(t, expectedJSON["count"], jsonBody["count"])
 
 			expectedNetwork := expectedJSON["network"].([]jsonObject)[0]
@@ -163,6 +169,19 @@ func TestAnexiaProvider(t *testing.T) {
 					return nil
 				},
 			},
+			ProviderConfig: &providerconfigtypes.Config{
+				Network: &providerconfigtypes.NetworkConfig{
+					DNS: providerconfigtypes.DNSConfig{
+						Servers: []string{
+							"1.1.1.1",
+							"",
+							"192.168.0.1",
+							"192.168.0.2",
+							"192.168.0.3",
+						},
+					},
+				},
+			},
 		})
 
 		err := provisionVM(ctx, client)