Skip to content

Commit

Permalink
inject ClusterConfig to validating-webhook
Browse files Browse the repository at this point in the history
Specifically, it is injected to VMICreateAdmitter, VMsAdmitter, and VMIRSAdmitter.

Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Apr 29, 2019
1 parent 5896d2c commit 4b6aba6
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 134 deletions.
40 changes: 26 additions & 14 deletions pkg/virt-api/webhooks/validating-webhook/validating-webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func ValidateVirtualMachineInstanceMandatoryFields(field *k8sfield.Path, spec *v
return causes
}

func ValidateVirtualMachineInstanceSpec(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec) []metav1.StatusCause {
func ValidateVirtualMachineInstanceSpec(field *k8sfield.Path, spec *v1.VirtualMachineInstanceSpec, config *virtconfig.ClusterConfig) []metav1.StatusCause {
var causes []metav1.StatusCause
volumeNameMap := make(map[string]*v1.Volume)
networkNameMap := make(map[string]*v1.Network)
Expand Down Expand Up @@ -745,9 +745,6 @@ func ValidateVirtualMachineInstanceSpec(field *k8sfield.Path, spec *v1.VirtualMa
// Validate emulated machine
if len(spec.Domain.Machine.Type) > 0 {
machine := spec.Domain.Machine.Type
informers := webhooks.GetInformers()
namespace, _ := util.GetNamespace()
config := virtconfig.NewClusterConfig(informers.ConfigMapInformer.GetStore(), namespace)
supportedMachines := config.GetEmulatedMachines()
var match = false
for _, val := range supportedMachines {
Expand Down Expand Up @@ -1464,7 +1461,7 @@ func ValidateVirtualMachineInstanceMetadata(field *k8sfield.Path, vmi *v1.Virtua
return causes
}

func ValidateVirtualMachineSpec(field *k8sfield.Path, spec *v1.VirtualMachineSpec) []metav1.StatusCause {
func ValidateVirtualMachineSpec(field *k8sfield.Path, spec *v1.VirtualMachineSpec, config *virtconfig.ClusterConfig) []metav1.StatusCause {
var causes []metav1.StatusCause

if spec.Template == nil {
Expand All @@ -1475,7 +1472,7 @@ func ValidateVirtualMachineSpec(field *k8sfield.Path, spec *v1.VirtualMachineSpe
})
}

causes = append(causes, ValidateVirtualMachineInstanceSpec(field.Child("template", "spec"), &spec.Template.Spec)...)
causes = append(causes, ValidateVirtualMachineInstanceSpec(field.Child("template", "spec"), &spec.Template.Spec, config)...)

if len(spec.DataVolumeTemplates) > 0 {

Expand Down Expand Up @@ -1568,7 +1565,7 @@ func ValidateVMIPresetSpec(field *k8sfield.Path, spec *v1.VirtualMachineInstance
return causes
}

func ValidateVMIRSSpec(field *k8sfield.Path, spec *v1.VirtualMachineInstanceReplicaSetSpec) []metav1.StatusCause {
func ValidateVMIRSSpec(field *k8sfield.Path, spec *v1.VirtualMachineInstanceReplicaSetSpec, config *virtconfig.ClusterConfig) []metav1.StatusCause {
var causes []metav1.StatusCause

if spec.Template == nil {
Expand All @@ -1578,7 +1575,7 @@ func ValidateVMIRSSpec(field *k8sfield.Path, spec *v1.VirtualMachineInstanceRepl
Field: field.Child("template").String(),
})
}
causes = append(causes, ValidateVirtualMachineInstanceSpec(field.Child("template", "spec"), &spec.Template.Spec)...)
causes = append(causes, ValidateVirtualMachineInstanceSpec(field.Child("template", "spec"), &spec.Template.Spec, config)...)

selector, err := metav1.LabelSelectorAsSelector(spec.Selector)
if err != nil {
Expand Down Expand Up @@ -1641,6 +1638,7 @@ func getAdmissionReviewVMI(ar *v1beta1.AdmissionReview) (new *v1.VirtualMachineI
}

type VMICreateAdmitter struct {
clusterConfig *virtconfig.ClusterConfig
}

func (admitter *VMICreateAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
Expand All @@ -1653,7 +1651,7 @@ func (admitter *VMICreateAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.A
return webhooks.ToAdmissionResponseError(err)
}

causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("spec"), &vmi.Spec)
causes := ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("spec"), &vmi.Spec, admitter.clusterConfig)
causes = append(causes, ValidateVirtualMachineInstanceMandatoryFields(k8sfield.NewPath("spec"), &vmi.Spec)...)
causes = append(causes, ValidateVirtualMachineInstanceMetadata(k8sfield.NewPath("spec"), vmi)...)

Expand All @@ -1667,7 +1665,11 @@ func (admitter *VMICreateAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.A
}

func ServeVMICreate(resp http.ResponseWriter, req *http.Request) {
serve(resp, req, &VMICreateAdmitter{})
informers := webhooks.GetInformers()
namespace, _ := util.GetNamespace()
serve(resp, req, &VMICreateAdmitter{
clusterConfig: virtconfig.NewClusterConfig(informers.ConfigMapInformer.GetStore(), namespace),
})
}

type VMIUpdateAdmitter struct {
Expand Down Expand Up @@ -1704,6 +1706,7 @@ func ServeVMIUpdate(resp http.ResponseWriter, req *http.Request) {
}

type VMsAdmitter struct {
clusterConfig *virtconfig.ClusterConfig
}

func (admitter *VMsAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
Expand All @@ -1724,7 +1727,7 @@ func (admitter *VMsAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.Admissi
return webhooks.ToAdmissionResponseError(err)
}

causes := ValidateVirtualMachineSpec(k8sfield.NewPath("spec"), &vm.Spec)
causes := ValidateVirtualMachineSpec(k8sfield.NewPath("spec"), &vm.Spec, admitter.clusterConfig)
if len(causes) > 0 {
return webhooks.ToAdmissionResponse(causes)
}
Expand All @@ -1735,10 +1738,15 @@ func (admitter *VMsAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.Admissi
}

func ServeVMs(resp http.ResponseWriter, req *http.Request) {
serve(resp, req, &VMsAdmitter{})
informers := webhooks.GetInformers()
namespace, _ := util.GetNamespace()
serve(resp, req, &VMsAdmitter{
clusterConfig: virtconfig.NewClusterConfig(informers.ConfigMapInformer.GetStore(), namespace),
})
}

type VMIRSAdmitter struct {
clusterConfig *virtconfig.ClusterConfig
}

func (admitter *VMIRSAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
Expand All @@ -1759,7 +1767,7 @@ func (admitter *VMIRSAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.Admis
return webhooks.ToAdmissionResponseError(err)
}

causes := ValidateVMIRSSpec(k8sfield.NewPath("spec"), &vmirs.Spec)
causes := ValidateVMIRSSpec(k8sfield.NewPath("spec"), &vmirs.Spec, admitter.clusterConfig)
if len(causes) > 0 {
return webhooks.ToAdmissionResponse(causes)
}
Expand All @@ -1770,7 +1778,11 @@ func (admitter *VMIRSAdmitter) admit(ar *v1beta1.AdmissionReview) *v1beta1.Admis
}

func ServeVMIRS(resp http.ResponseWriter, req *http.Request) {
serve(resp, req, &VMIRSAdmitter{})
informers := webhooks.GetInformers()
namespace, _ := util.GetNamespace()
serve(resp, req, &VMIRSAdmitter{
clusterConfig: virtconfig.NewClusterConfig(informers.ConfigMapInformer.GetStore(), namespace),
})
}

type VMIPresetAdmitter struct {
Expand Down
Loading

0 comments on commit 4b6aba6

Please sign in to comment.