Skip to content

Commit

Permalink
add default cluster cpu model
Browse files Browse the repository at this point in the history
  • Loading branch information
ksimon1 committed Mar 4, 2019
1 parent 6f1f653 commit 6032ec0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/virt-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,11 +1013,13 @@ func (app *virtAPIApp) Run() {
go webhookInformers.VMIInformer.Run(stopChan)
go webhookInformers.VMIPresetInformer.Run(stopChan)
go webhookInformers.NamespaceLimitsInformer.Run(stopChan)
go webhookInformers.ConfigMapInformer.Run(stopChan)

cache.WaitForCacheSync(stopChan,
webhookInformers.VMIInformer.HasSynced,
webhookInformers.VMIPresetInformer.HasSynced,
webhookInformers.NamespaceLimitsInformer.HasSynced)
webhookInformers.NamespaceLimitsInformer.HasSynced,
webhookInformers.ConfigMapInformer.HasSynced)

// Verify/create webhook endpoint.
err = app.createWebhook()
Expand Down
2 changes: 2 additions & 0 deletions pkg/virt-api/webhooks/mutating-webhook/mutating-webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func mutateVMIs(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
},
}
}
// Apply default cpu model
setDefaultCPUModel(&vmi, informers.ConfigMapInformer.GetStore())

// Apply namespace limits
applyNamespaceLimitRangeValues(&vmi, informers.NamespaceLimitsInformer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ var _ = Describe("Mutating Webhook", func() {
}
namespaceLimitInformer, _ = testutils.NewFakeInformerFor(&k8sv1.LimitRange{})
namespaceLimitInformer.GetIndexer().Add(namespaceLimit)

configMapInformer, _ := testutils.NewFakeInformerFor(&k8sv1.ConfigMap{})
webhooks.SetInformers(
&webhooks.Informers{
VMIPresetInformer: presetInformer,
NamespaceLimitsInformer: namespaceLimitInformer,
ConfigMapInformer: configMapInformer,
},
)
})
Expand Down
25 changes: 24 additions & 1 deletion pkg/virt-api/webhooks/mutating-webhook/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import (
"kubevirt.io/kubevirt/pkg/log"
)

const exclusionMarking = "virtualmachineinstancepresets.admission.kubevirt.io/exclude"
const (
exclusionMarking = "virtualmachineinstancepresets.admission.kubevirt.io/exclude"
namespaceKubevirt = "kubevirt"
configMapName = "kubevirt-config"
defaultCPUModelKey = "default-cpu-model"
)

// listPresets returns all VirtualMachinePresets by namespace
func listPresets(vmiPresetInformer cache.SharedIndexInformer, namespace string) ([]kubev1.VirtualMachineInstancePreset, error) {
Expand Down Expand Up @@ -275,3 +280,21 @@ func isVMIExcluded(vmi *kubev1.VirtualMachineInstance) bool {
}
return false
}

//setDefaultCPUModel sets default cpu model from config if vmi doesn't have cpu model
func setDefaultCPUModel(vmi *kubev1.VirtualMachineInstance, configMapStore cache.Store) {
//if vmi doesn't have cpu topology or cpu model set
if vmi.Spec.Domain.CPU == nil || vmi.Spec.Domain.CPU.Model == "" {
// if default cluster cpu model is defined
if obj, exists, err := configMapStore.GetByKey(namespaceKubevirt + "/" + configMapName); err == nil && exists {
if obj.(*k8sv1.ConfigMap).Data[defaultCPUModelKey] != "" {
// create cpu topology struct
if vmi.Spec.Domain.CPU == nil {
vmi.Spec.Domain.CPU = &kubev1.CPU{}
}
//set is as vmi cpu model
vmi.Spec.Domain.CPU.Model = obj.(*k8sv1.ConfigMap).Data[defaultCPUModelKey]
}
}
}
}
67 changes: 67 additions & 0 deletions pkg/virt-api/webhooks/mutating-webhook/preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"

Expand Down Expand Up @@ -518,6 +519,72 @@ var _ = Describe("Mutating Webhook Presets", func() {
})
})

Context("Apply default cpu model", func() {
var vmi v1.VirtualMachineInstance
var configMapIndexer cache.Indexer
var defaultCPUModel = "Haswell"
var cfgMap k8sv1.ConfigMap

BeforeEach(func() {
configMapIndexer = cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, nil)
vmi = v1.VirtualMachineInstance{Spec: v1.VirtualMachineInstanceSpec{Domain: v1.DomainSpec{}}}
})

It("Should set default cpu model when vmi doesn't have it", func() {
cfgMap = k8sv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubevirt",
Name: "kubevirt-config",
},
Data: map[string]string{
defaultCPUModelKey: defaultCPUModel,
},
}
configMapIndexer.Add(&cfgMap)
setDefaultCPUModel(&vmi, configMapIndexer)

Expect(vmi.Spec.Domain.CPU).ToNot(BeNil())
Expect(vmi.Spec.Domain.CPU.Model).To(Equal(defaultCPUModel))
})

It("Should not set default cpu model when vmi does have it", func() {
cfgMap = k8sv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubevirt",
Name: "kubevirt-config",
},
Data: map[string]string{
defaultCPUModelKey: defaultCPUModel,
},
}
configMapIndexer.Add(&cfgMap)

vmCPUModel := "EPYC"
vmi.Spec.Domain.CPU = &v1.CPU{
Model: vmCPUModel,
}
setDefaultCPUModel(&vmi, configMapIndexer)

Expect(vmi.Spec.Domain.CPU).ToNot(BeNil())
Expect(vmi.Spec.Domain.CPU.Model).To(Equal(vmCPUModel))
})

It("Should has empty cpu model when cpu model is not set", func() {
cfgMap = k8sv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "kubevirt",
Name: "kubevirt-config",
},
Data: map[string]string{},
}
configMapIndexer.Add(&cfgMap)
vmi.Spec.Domain.CPU = &v1.CPU{}
setDefaultCPUModel(&vmi, configMapIndexer)
Expect(vmi.Spec.Domain.CPU).ToNot(BeNil())
Expect(vmi.Spec.Domain.CPU.Model).To(BeEmpty())
})
})

Context("Apply Presets", func() {
var vmi v1.VirtualMachineInstance
var preset *v1.VirtualMachineInstancePreset
Expand Down
2 changes: 2 additions & 0 deletions pkg/virt-api/webhooks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Informers struct {
VMIPresetInformer cache.SharedIndexInformer
NamespaceLimitsInformer cache.SharedIndexInformer
VMIInformer cache.SharedIndexInformer
ConfigMapInformer cache.SharedIndexInformer
}

func GetInformers() *Informers {
Expand Down Expand Up @@ -110,6 +111,7 @@ func newInformers() *Informers {
VMIInformer: kubeInformerFactory.VMI(),
VMIPresetInformer: kubeInformerFactory.VirtualMachinePreset(),
NamespaceLimitsInformer: kubeInformerFactory.LimitRanges(),
ConfigMapInformer: kubeInformerFactory.ConfigMap(),
}
}

Expand Down

0 comments on commit 6032ec0

Please sign in to comment.