-
Notifications
You must be signed in to change notification settings - Fork 132
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
vSphere: support for VM Groups #1847
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ type Config struct { | |
MemoryMB int64 | ||
DiskSizeGB *int64 | ||
Tags []tags.Tag | ||
VMGroup string | ||
} | ||
|
||
// Ensures that Server implements Instance interface. | ||
|
@@ -210,6 +211,11 @@ func (p *provider) getConfig(provSpec clusterv1alpha1.ProviderSpec) (*Config, *p | |
return nil, nil, nil, err | ||
} | ||
|
||
c.VMGroup, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.VMGroup) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
|
||
c.CPUs = rawConfig.CPUs | ||
c.MemoryMB = rawConfig.MemoryMB | ||
c.DiskSizeGB = rawConfig.DiskSizeGB | ||
|
@@ -311,10 +317,13 @@ func (p *provider) Validate(ctx context.Context, log *zap.SugaredLogger, spec cl | |
} | ||
} | ||
|
||
if config.VMAntiAffinity { | ||
if config.Cluster == "" { | ||
return fmt.Errorf("cluster is required for vm anti affinity") | ||
} | ||
if config.VMAntiAffinity && config.Cluster == "" { | ||
return fmt.Errorf("cluster is required for vm anti affinity") | ||
} else if config.VMGroup != "" && config.Cluster == "" { | ||
return fmt.Errorf("cluster is required for vm group") | ||
} | ||
|
||
if config.Cluster != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way that we validate the VMGroup exist? |
||
_, err = session.Finder.ClusterComputeResource(ctx, config.Cluster) | ||
if err != nil { | ||
return fmt.Errorf("failed to get cluster %q, %w", config.Cluster, err) | ||
|
@@ -376,6 +385,12 @@ func (p *provider) create(ctx context.Context, log *zap.SugaredLogger, machine * | |
return nil, fmt.Errorf("failed to attach tags: %w", err) | ||
} | ||
|
||
if config.VMGroup != "" { | ||
if err := p.addToVMGroup(ctx, log, session, machine, config); err != nil { | ||
return nil, fmt.Errorf("failed to add VM to VM group: %w", err) | ||
} | ||
} | ||
|
||
if config.VMAntiAffinity { | ||
if err := p.createOrUpdateVMAntiAffinityRule(ctx, log, session, machine, config); err != nil { | ||
return nil, fmt.Errorf("failed to add VM to anti affinity rule: %w", err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright 2024 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 vsphere | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1" | ||
|
||
"github.com/vmware/govmomi/vim25/types" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (p *provider) addToVMGroup(ctx context.Context, log *zap.SugaredLogger, session *Session, machine *clusterv1alpha1.Machine, config *Config) error { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
|
||
// Check if the VM group exists | ||
vmGroup, err := findVMGroup(ctx, session, config.Cluster, config.VMGroup) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// We have to find all VMs in the folder and add them to the VM group. VMGroup only contains VM reference ID which is not enough to | ||
// identify the VM by name. | ||
machineSetName := machine.Name[:strings.LastIndex(machine.Name, "-")] | ||
vmsInFolder, err := session.Finder.VirtualMachineList(ctx, strings.Join([]string{config.Folder, "*"}, "/")) | ||
if err != nil { | ||
return fmt.Errorf("failed to find VMs in folder: %w", err) | ||
} | ||
|
||
var vmRefs []types.ManagedObjectReference | ||
for _, vm := range vmsInFolder { | ||
// Only add VMs with the same machineSetName to the rule and exclude the machine itself if it is being deleted | ||
if strings.HasPrefix(vm.Name(), machineSetName) && !(vm.Name() == machine.Name && machine.DeletionTimestamp != nil) { | ||
vmRefs = append(vmRefs, vm.Reference()) | ||
} | ||
} | ||
|
||
var vmRefsToAdd []types.ManagedObjectReference | ||
for _, vm := range vmRefs { | ||
found := false | ||
for _, existingVM := range vmGroup.Vm { | ||
if existingVM.Value == vm.Value { | ||
log.Debugf("VM %s already in VM group %s", machine.Name, config.VMGroup) | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
vmRefsToAdd = append(vmRefsToAdd, vm) | ||
} | ||
} | ||
|
||
// Add the VM to the VM group | ||
vmGroup.Vm = append(vmGroup.Vm, vmRefsToAdd...) | ||
cluster, err := session.Finder.ClusterComputeResource(ctx, config.Cluster) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
spec := &types.ClusterConfigSpecEx{ | ||
GroupSpec: []types.ClusterGroupSpec{ | ||
{ | ||
ArrayUpdateSpec: types.ArrayUpdateSpec{ | ||
Operation: types.ArrayUpdateOperationEdit, | ||
}, | ||
Info: vmGroup, | ||
}, | ||
}, | ||
} | ||
|
||
log.Debugf("Adding VM %s in VM group %s", machine.Name, config.VMGroup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically if something went wrong on vSphere side, this could result in adding multiple VMs to the group at the same time. We should eventually improve logging to reflect that edge case. |
||
task, err := cluster.Reconfigure(ctx, spec, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
taskResult, err := task.WaitForResultEx(ctx) | ||
if err != nil { | ||
return fmt.Errorf("error waiting for cluster %v reconfiguration to complete", cluster.Name()) | ||
} | ||
if taskResult.State != types.TaskInfoStateSuccess { | ||
return fmt.Errorf("cluster %v reconfiguration task was not successful", cluster.Name()) | ||
} | ||
log.Debugf("Successfully added VM %s in VM group %s", machine.Name, config.VMGroup) | ||
return nil | ||
} | ||
|
||
func findVMGroup(ctx context.Context, session *Session, clusterName, vmGroup string) (*types.ClusterVmGroup, error) { | ||
cluster, err := session.Finder.ClusterComputeResource(ctx, clusterName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
clusterConfigInfoEx, err := cluster.Configuration(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, group := range clusterConfigInfoEx.Group { | ||
if clusterVMGroup, ok := group.(*types.ClusterVmGroup); ok { | ||
if clusterVMGroup.Name == vmGroup { | ||
return clusterVMGroup, nil | ||
} | ||
} | ||
} | ||
return nil, fmt.Errorf("cannot find VM group %s", vmGroup) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if both VMAntiAffinity and VMGroup are set, is that supported?