Skip to content

Commit

Permalink
Take template name and generateName into account
Browse files Browse the repository at this point in the history
First try to use the name, then the generateName of the template as the
default value for generateName on new VMs. If both are empty, take the
controller name.

Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr committed Sep 21, 2017
1 parent bb24d78 commit 23ffae5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 17 additions & 2 deletions pkg/virt-controller/watch/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *VMReplicaSet) execute(key string) error {
rs := obj.(*virtv1.VirtualMachineReplicaSet)

//TODO default rs if necessary, the aggregated apiserver will do that in the future
if rs.Spec.Template == nil || rs.Spec.Selector == nil || rs.Spec.Selector.Size() == 0 {
if rs.Spec.Template == nil || rs.Spec.Selector == nil || len(rs.Spec.Template.ObjectMeta.Labels) == 0 {
logging.DefaultLogger().Object(rs).Error().Msg("Invalid controller spec, will not retry processing it.")
return nil
}
Expand Down Expand Up @@ -181,11 +181,14 @@ func (c *VMReplicaSet) scale(rs *virtv1.VirtualMachineReplicaSet, vms []virtv1.V

} else if diff < 0 {
// We have to create VMs
basename := c.getVirtualMachineBaseName(rs)
for i := diff; i < 0; i++ {
go func() {
defer wg.Done()
vm := virtv1.NewVMReferenceFromNameWithNS(rs.ObjectMeta.Namespace, "")
vm.ObjectMeta.GenerateName = rs.ObjectMeta.Name + "-"
vm.ObjectMeta = rs.Spec.Template.ObjectMeta
vm.ObjectMeta.Name = ""
vm.ObjectMeta.GenerateName = basename
vm.Spec = rs.Spec.Template.Spec
// TODO check if vm labels exist, and when make sure that they match. For now just override them
vm.ObjectMeta.Labels = rs.Spec.Template.ObjectMeta.Labels
Expand Down Expand Up @@ -355,3 +358,15 @@ func (c *VMReplicaSet) calcDiff(rs *virtv1.VirtualMachineReplicaSet, vms []virtv

return len(vms) - int(wantedReplicas)
}

func (c *VMReplicaSet) getVirtualMachineBaseName(replicaset *virtv1.VirtualMachineReplicaSet) string {

// TODO defaulting should make sure that the right field is set, instead of doing this
if len(replicaset.Spec.Template.ObjectMeta.Name) > 0 {
return replicaset.Spec.Template.ObjectMeta.Name
}
if len(replicaset.Spec.Template.ObjectMeta.GenerateName) > 0 {
return replicaset.Spec.Template.ObjectMeta.GenerateName
}
return replicaset.ObjectMeta.Name
}
4 changes: 3 additions & 1 deletion pkg/virt-controller/watch/replicaset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ var _ = Describe("Replicaset", func() {
sync(stop)

virtClient.EXPECT().VM(vm.ObjectMeta.Namespace).Return(vmInterface).AnyTimes()
vmInterface.EXPECT().Create(gomock.Any()).Times(3)
vmInterface.EXPECT().Create(gomock.Any()).Times(3).Do(func(arg interface{}) {
Expect(arg.(*v1.VM).ObjectMeta.GenerateName).To(Equal("testvm"))
})
virtClient.EXPECT().ReplicaSet(vm.ObjectMeta.Namespace).Return(rsInterface)
rsInterface.EXPECT().Update(expectedRS)

Expand Down

0 comments on commit 23ffae5

Please sign in to comment.