Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for attaching multiple networks to a vApp #12996

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *vmwareCloudDirectorScenario) Cluster(secrets types.Secrets) *kubermatic
Organization: secrets.VMwareCloudDirector.Organization,
VDC: secrets.VMwareCloudDirector.VDC,
OVDCNetwork: secrets.VMwareCloudDirector.OVDCNetwork,
OVDCNetworks: secrets.VMwareCloudDirector.OVDCNetworks,
CSI: &kubermaticv1.VMwareCloudDirectorCSIConfig{
StorageProfile: vmwareCloudDirectorStorageProfile,
},
Expand Down
1 change: 1 addition & 0 deletions cmd/conformance-tester/pkg/types/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type Secrets struct {
Organization string
VDC string
OVDCNetwork string
OVDCNetworks []string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should keep both the old and the new syntax in the conformance-tester's CLI flags. There is no BC guarantee with the tester anyway. We should just have 1 flag, preferably the new one.

Also, this new flag is never set by our code, so the new code isn't tested. Please adjust the hack/ci/run-conformance-tests.sh accordingly.

}
RHEL struct {
SubscriptionUser string
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/kubermatic/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,11 @@ type VMwareCloudDirectorCloudSpec struct {
VDC string `json:"vdc,omitempty"`

// The name of organizational virtual data center network that will be associated with the VMs and vApp.
OVDCNetwork string `json:"ovdcNetwork"`
// Deprecated: OVDCNetwork has been deprecated starting with KKP 2.25 and will be removed in KKP 2.27+. It is recommended to use OVDCNetworks instead.
OVDCNetwork string `json:"ovdcNetwork,omitempty"`

// OVDCNetworks is the list of organizational virtual data center networks that will be attached to the vApp and can be consumed the VMs.
OVDCNetworks []string `json:"ovdcNetworks,omitempty"`

// VApp used for isolation of VMs and their associated network
// +optional
Expand Down
5 changes: 4 additions & 1 deletion pkg/apis/kubermatic/v1/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ type VMwareCloudDirector struct {
// The name of organization to use.
Organization string `json:"organization"`
// The name of organizational virtual data center network that will be associated with the VMs and vApp.
OVDCNetwork string `json:"ovdcNetwork"`
// Deprecated: OVDCNetwork has been deprecated starting with KKP 2.25 and will be removed in KKP 2.27+. It is recommended to use OVDCNetworks instead.
OVDCNetwork string `json:"ovdcNetwork,omitempty"`
// OVDCNetworks is the list of organizational virtual data center networks that will be attached to the vApp and can be consumed the VMs.
OVDCNetworks []string `json:"ovdcNetworks,omitempty"`
}

func (s VMwareCloudDirector) IsValid() bool {
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/kubermatic/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions pkg/crd/k8c.io/kubermatic.k8c.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,13 @@ spec:
description: The name of organization to use.
type: string
ovdcNetwork:
description: The name of organizational virtual data center network that will be associated with the VMs and vApp.
description: 'The name of organizational virtual data center network that will be associated with the VMs and vApp. Deprecated: OVDCNetwork has been deprecated starting with KKP 2.25 and will be removed in KKP 2.27+. It is recommended to use OVDCNetworks instead.'
type: string
ovdcNetworks:
description: OVDCNetworks is the list of organizational virtual data center networks that will be attached to the vApp and can be consumed the VMs.
items:
type: string
type: array
password:
description: The VMware Cloud Director user password.
type: string
Expand All @@ -928,7 +933,6 @@ spec:
type: string
required:
- csi
- ovdcNetwork
type: object
vsphere:
description: VSphere defines the configuration data of the vSphere.
Expand Down
8 changes: 6 additions & 2 deletions pkg/crd/k8c.io/kubermatic.k8c.io_clustertemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,13 @@ spec:
description: The name of organization to use.
type: string
ovdcNetwork:
description: The name of organizational virtual data center network that will be associated with the VMs and vApp.
description: 'The name of organizational virtual data center network that will be associated with the VMs and vApp. Deprecated: OVDCNetwork has been deprecated starting with KKP 2.25 and will be removed in KKP 2.27+. It is recommended to use OVDCNetworks instead.'
type: string
ovdcNetworks:
description: OVDCNetworks is the list of organizational virtual data center networks that will be attached to the vApp and can be consumed the VMs.
items:
type: string
type: array
password:
description: The VMware Cloud Director user password.
type: string
Expand All @@ -923,7 +928,6 @@ spec:
type: string
required:
- csi
- ovdcNetwork
type: object
vsphere:
description: VSphere defines the configuration data of the vSphere.
Expand Down
8 changes: 6 additions & 2 deletions pkg/crd/k8c.io/kubermatic.k8c.io_presets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,13 @@ spec:
description: The name of organization to use.
type: string
ovdcNetwork:
description: The name of organizational virtual data center network that will be associated with the VMs and vApp.
description: 'The name of organizational virtual data center network that will be associated with the VMs and vApp. Deprecated: OVDCNetwork has been deprecated starting with KKP 2.25 and will be removed in KKP 2.27+. It is recommended to use OVDCNetworks instead.'
type: string
ovdcNetworks:
description: OVDCNetworks is the list of organizational virtual data center networks that will be attached to the vApp and can be consumed the VMs.
items:
type: string
type: array
password:
description: The VMware Cloud Director user password.
type: string
Expand All @@ -448,7 +453,6 @@ spec:
type: string
required:
- organization
- ovdcNetwork
- vdc
type: object
vsphere:
Expand Down
7 changes: 6 additions & 1 deletion pkg/machine/provider/vmware_cloud_director.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but in line 106 can you change the "AWS" to "VCD"?

Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func CompleteVMwareCloudDirectorProviderSpec(config *vmwareclouddirector.RawConf
}

if config.Network.Value == "" {
config.Network.Value = cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetwork
if len(cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetworks) > 0 {
// As a default, we attach the first network to the VMs.
config.Network.Value = cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetworks[0]
} else if cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetwork != "" {
config.Network.Value = cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetwork
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/provider/cloud/vmwareclouddirector/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"

"github.com/vmware/go-vcloud-director/v2/govcd"

kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
)

func deleteVApp(vdc *govcd.Vdc, vapp *govcd.VApp) error {
Expand All @@ -42,3 +44,23 @@ func deleteVApp(vdc *govcd.Vdc, vapp *govcd.VApp) error {
}
return nil
}

func getOrgVDCNetworks(vdc *govcd.Vdc, spec kubermaticv1.VMwareCloudDirectorCloudSpec) ([]*govcd.OrgVDCNetwork, error) {
var networks []string

if spec.OVDCNetworks != nil && len(spec.OVDCNetworks) > 0 {
networks = spec.OVDCNetworks
} else {
networks = []string{spec.OVDCNetwork}
}

orgVDCNetworks := make([]*govcd.OrgVDCNetwork, 0, len(networks))
for _, network := range networks {
orgVDCNetwork, err := vdc.GetOrgVdcNetworkByNameOrId(network, true)
if err != nil {
return nil, fmt.Errorf("failed to get OrgVDCNetwork '%s': %w", network, err)
}
orgVDCNetworks = append(orgVDCNetworks, orgVDCNetwork)
}
return orgVDCNetworks, nil
}
13 changes: 10 additions & 3 deletions pkg/provider/cloud/vmwareclouddirector/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (p *Provider) ValidateCloudSpec(_ context.Context, spec kubermaticv1.CloudS
}

// Ensure that the network exists
if spec.VMwareCloudDirector.OVDCNetwork != "" {
_, err := vdc.GetOrgVdcNetworkByNameOrId(spec.VMwareCloudDirector.OVDCNetwork, true)
if spec.VMwareCloudDirector.OVDCNetwork != "" || spec.VMwareCloudDirector.OVDCNetworks != nil {
_, err := getOrgVDCNetworks(vdc, *spec.VMwareCloudDirector)
if err != nil {
return fmt.Errorf("failed to get organization VDC network '%s': %w", client.Auth.VDC, err)
return fmt.Errorf("failed to get organization VDC networks '%s': %w", client.Auth.VDC, err)
}
}

Expand Down Expand Up @@ -160,6 +160,13 @@ func (p *Provider) ValidateCloudSpecUpdate(_ context.Context, oldSpec kubermatic
if oldSpec.VMwareCloudDirector.OVDCNetwork != newSpec.VMwareCloudDirector.OVDCNetwork {
return fmt.Errorf("updating VMware Cloud Director OVDCNetwork is not supported (was %s, updated to %s)", oldSpec.VMwareCloudDirector.OVDCNetwork, newSpec.VMwareCloudDirector.OVDCNetwork)
}

if oldSpec.VMwareCloudDirector.OVDCNetworks != nil && newSpec.VMwareCloudDirector.OVDCNetworks != nil {
if len(oldSpec.VMwareCloudDirector.OVDCNetworks) != len(newSpec.VMwareCloudDirector.OVDCNetworks) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this only checking the length? Is changing [a,b] to [c,d] valid? Maybe using a string set's Equal() function would work better here, if not even using a semantic equality. But I'd probably just use a set.

return fmt.Errorf("updating VMware Cloud Director OVDCNetworks is not supported (was %v, updated to %v)", oldSpec.VMwareCloudDirector.OVDCNetworks, newSpec.VMwareCloudDirector.OVDCNetworks)
}
}

return nil
}

Expand Down
29 changes: 20 additions & 9 deletions pkg/provider/cloud/vmwareclouddirector/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,43 @@ func reconcileVApp(ctx context.Context, cluster *kubermaticv1.Cluster, update pr
func reconcileNetwork(ctx context.Context, cluster *kubermaticv1.Cluster, update provider.ClusterUpdater, vdc *govcd.Vdc) (*kubermaticv1.Cluster, error) {
var err error

// Ensure that ovdc network is attached to the vApp
ovdcNetwork, err := vdc.GetOrgVdcNetworkByNameOrId(cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetwork, true)
// Ensure that all the ovdc networks are attached to the vApp
ovdcNetworks, err := getOrgVDCNetworks(vdc, *cluster.Spec.Cloud.VMwareCloudDirector)
if err != nil {
return nil, fmt.Errorf("failed to get organization VDC network '%s': %w", cluster.Spec.Cloud.VMwareCloudDirector.OVDCNetwork, err)
return nil, fmt.Errorf("failed to get organization VDC networks: %w", err)
}

// Check if the network is already present in vApp
for _, ovdcNetwork := range ovdcNetworks {
if err := reconcileNetworkForVApp(ctx, cluster, update, vdc, ovdcNetwork); err != nil {
return nil, fmt.Errorf("failed to reconcile network for vApp: %w", err)
}
}

return cluster, nil
}

func reconcileNetworkForVApp(ctx context.Context, cluster *kubermaticv1.Cluster, update provider.ClusterUpdater, vdc *govcd.Vdc, network *govcd.OrgVDCNetwork) error {
// Check if the network is already present in vApp.
vApp, err := vdc.GetVAppByNameOrId(cluster.Spec.Cloud.VMwareCloudDirector.VApp, true)
if err != nil {
return nil, err
return err
}

exists := false
if vApp.VApp.NetworkConfigSection != nil && vApp.VApp.NetworkConfigSection.NetworkConfig != nil {
for _, net := range vApp.VApp.NetworkConfigSection.NetworkNames() {
if net == ovdcNetwork.OrgVDCNetwork.Name {
if net == network.OrgVDCNetwork.Name {
exists = true
}
}
}

// We need to attach the network to vApp
if !exists {
if _, err := vApp.AddOrgNetwork(&govcd.VappNetworkSettings{}, ovdcNetwork.OrgVDCNetwork, false); err != nil {
return nil, fmt.Errorf("failed to attach organization VDC network '%s' to vApp '%s': %w", ovdcNetwork.OrgVDCNetwork.Name, cluster.Spec.Cloud.VMwareCloudDirector.VApp, err)
if _, err := vApp.AddOrgNetwork(&govcd.VappNetworkSettings{}, network.OrgVDCNetwork, false); err != nil {
return fmt.Errorf("failed to attach organization VDC network '%s' to vApp '%s': %w", network.OrgVDCNetwork.Name, cluster.Spec.Cloud.VMwareCloudDirector.VApp, err)
}
}
return cluster, nil

return nil
}
1 change: 1 addition & 0 deletions pkg/resources/test/load_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ var (
Organization: "vcd-organization",
VDC: "vcd-vdc",
OVDCNetwork: "vcd-ovdc-network",
OVDCNetworks: []string{"vcd-ovdc-network1", "vcd-ovdc-network2"},
VApp: "vcd-vapp",
},
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,12 @@ func validateVMwareCloudDirectorCloudSpec(spec *kubermaticv1.VMwareCloudDirector
}
}

if spec.OVDCNetwork == "" && len(spec.OVDCNetworks) == 0 {
return errors.New("one of ovdcNetwork or ovdcNetworks needs to be specified")
} else if spec.OVDCNetwork != "" && len(spec.OVDCNetworks) > 0 {
return errors.New("ovdcNetwork and ovdcNetworks cannot be set at the same time")
}

return nil
}

Expand Down