Repository: kubermatic/machine-controller
Pull request #1451 (2022-09-29 14:01:57)

Add an option to enable nested virtualization and CPU platform for GCP
**What this PR does / why we need it**:
Allows to use nested virtualization on GCP

**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?**
/kind feature
<!--
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
-->

**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
Enable nested virtualization for GCP cloud provider
```

**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: @mfranczy: The following test **failed**, say `/retest` to rerun all failed tests or `/retest-required` to rerun all mandatory failed tests:

Test name | Commit | Details | Required | Rerun command
--- | --- | --- | --- | ---
pull-machine-controller-lint | 3b26a5ba908b6b78f9431f7d2a426f6f65685244 | [link](https://public-prow.loodse.com/view/gs/prow-dev-public-data/pr-logs/pull/kubermatic_machine-controller/1451/pull-machine-controller-lint/1575486064821276672) | true | `/test pull-machine-controller-lint`

[Full PR test history](https://public-prow.loodse.com/pr-history?org=kubermatic&repo=machine-controller&pr=1451)

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
kubermatic-bot: LGTM label has been added.  <details>Git tree hash: a4a511d4be36c0611f3d4e82cb1091f89701d0eb</details>
kubermatic-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubermatic/machine-controller/pull/1451#pullrequestreview-1126249774" title="Approved">ahmedwaleedmalik</a>*, *<a href="https://github.com/kubermatic/machine-controller/pull/1451#" title="Author self-approved">mfranczy</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/master/OWNERS)~~ [ahmedwaleedmalik,mfranczy]
- ~~[pkg/cloudprovider/provider/OWNERS](https://github.com/kubermatic/machine-controller/blob/master/pkg/cloudprovider/provider/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":[]} -->

Patch:
diff --git a/examples/gce-machinedeployment.yaml b/examples/gce-machinedeployment.yaml
index c7ab56ff..84f24707 100644
--- a/examples/gce-machinedeployment.yaml
+++ b/examples/gce-machinedeployment.yaml
@@ -69,6 +69,8 @@ spec:
             assignPublicIPAddress: true
             customImage: "myCustomImage"
             disableMachineServiceAccount: false
+            enableNestedVirtualization: false
+            minCPUPlatform: "Intel Haswell"
           # Can be 'ubuntu' or 'rhel'
           operatingSystem: "ubuntu"
           operatingSystemSpec:
diff --git a/pkg/cloudprovider/provider/gce/config.go b/pkg/cloudprovider/provider/gce/config.go
index 9901b7af..28777faa 100644
--- a/pkg/cloudprovider/provider/gce/config.go
+++ b/pkg/cloudprovider/provider/gce/config.go
@@ -112,6 +112,8 @@ type config struct {
 	regional                     bool
 	customImage                  string
 	disableMachineServiceAccount bool
+	enableNestedVirtualization   bool
+	minCPUPlatform               string
 }
 
 // newConfig creates a Provider configuration out of the passed resolver and spec.
@@ -220,6 +222,16 @@ func newConfig(resolver *providerconfig.ConfigVarResolver, spec v1alpha1.Provide
 		return nil, fmt.Errorf("failed to retrieve disable machine service account: %w", err)
 	}
 
+	cfg.enableNestedVirtualization, _, err = resolver.GetConfigVarBoolValue(cpSpec.EnableNestedVirtualization)
+	if err != nil {
+		return nil, fmt.Errorf("failed to retrieve enable nested virtualization: %w", err)
+	}
+
+	cfg.minCPUPlatform, err = resolver.GetConfigVarStringValue(cpSpec.MinCPUPlatform)
+	if err != nil {
+		return nil, fmt.Errorf("failed to retrieve min cpu platform: %w", err)
+	}
+
 	return cfg, nil
 }
 
diff --git a/pkg/cloudprovider/provider/gce/provider.go b/pkg/cloudprovider/provider/gce/provider.go
index 58245db0..28e7cc3f 100644
--- a/pkg/cloudprovider/provider/gce/provider.go
+++ b/pkg/cloudprovider/provider/gce/provider.go
@@ -280,6 +280,16 @@ func (p *Provider) Create(ctx context.Context, machine *clusterv1alpha1.Machine,
 		inst.Scheduling.ProvisioningModel = *cfg.provisioningModel
 	}
 
+	if cfg.enableNestedVirtualization {
+		inst.AdvancedMachineFeatures = &compute.AdvancedMachineFeatures{
+			EnableNestedVirtualization: true,
+		}
+	}
+
+	if cfg.minCPUPlatform != "" {
+		inst.MinCpuPlatform = cfg.minCPUPlatform
+	}
+
 	op, err := svc.Instances.Insert(cfg.projectID, cfg.zone, inst).Do()
 	if err != nil {
 		return nil, newError(common.InvalidConfigurationMachineError, errInsertInstance, err)
diff --git a/pkg/cloudprovider/provider/gce/types/types.go b/pkg/cloudprovider/provider/gce/types/types.go
index 0ac0f333..0bc95de0 100644
--- a/pkg/cloudprovider/provider/gce/types/types.go
+++ b/pkg/cloudprovider/provider/gce/types/types.go
@@ -48,6 +48,8 @@ type CloudProviderSpec struct {
 	Regional                     providerconfigtypes.ConfigVarBool    `json:"regional"`
 	CustomImage                  providerconfigtypes.ConfigVarString  `json:"customImage,omitempty"`
 	DisableMachineServiceAccount providerconfigtypes.ConfigVarBool    `json:"disableMachineServiceAccount,omitempty"`
+	EnableNestedVirtualization   providerconfigtypes.ConfigVarBool    `json:"enableNestedVirtualization,omitempty"`
+	MinCPUPlatform               providerconfigtypes.ConfigVarString  `json:"MinCPUPlatform,omitempty"`
 }
 
 // UpdateProviderSpec updates the given provider spec with changed


Test patch:
diff --git a/pkg/cloudprovider/provider/gce/provider_test.go b/pkg/cloudprovider/provider/gce/provider_test.go
index c792c655..f3ffeddc 100644
--- a/pkg/cloudprovider/provider/gce/provider_test.go
+++ b/pkg/cloudprovider/provider/gce/provider_test.go
@@ -55,6 +55,8 @@ func testProviderSpec() map[string]interface{} {
 			},
 			"zone":                         "europe-west2-a",
 			"disableMachineServiceAccount": false,
+			"enableNestedVirtualization":   true,
+			"minCPUPlatform":               "Intel Haswell",
 		},
 		"operatingSystem": "ubuntu",
 		"operatingSystemSpec": map[string]interface{}{