Reduce nestiness of `OnDefineDomain` method
@@ -187,18 +187,36 @@ func (m *Manager) OnDefineDomain(domainSpec *virtwrapApi.DomainSpec, vmi *v1.Vir
if err != nil {
return "", fmt.Errorf("Failed to marshal domain spec: %v", domainSpec)
}
- if callbacks, found := m.CallbacksPerHookPoint[hooksInfo.OnDefineDomainHookPointName]; found {
- for _, callback := range callbacks {
- if callback.Version == hooksV1alpha1.Version || callback.Version == hooksV1alpha2.Version {
+
+ callbacks, found := m.CallbacksPerHookPoint[hooksInfo.OnDefineDomainHookPointName]
+ if !found {
+ return string(domainSpecXML), nil
+ }
+
vmiJSON, err := json.Marshal(vmi)
if err != nil {
return "", fmt.Errorf("Failed to marshal VMI spec: %v", vmi)
}
+ for _, callback := range callbacks {
+ domainSpecXML, err = m.onDefineDomainCallback(callback, domainSpecXML, vmiJSON)
+ if err != nil {
+ return "", err
+ }
+ }
+
+ return string(domainSpecXML), nil
+}
+
+func (m *Manager) onDefineDomainCallback(callback *callBackClient, domainSpecXML, vmiJSON []byte) ([]byte, error) {
+ if callback.Version != hooksV1alpha1.Version && callback.Version != hooksV1alpha2.Version {
+ return domainSpecXML, nil
+ }
+
conn, err := grpcutil.DialSocketWithTimeout(callback.SocketPath, 1)
if err != nil {
log.Log.Reason(err).Infof(dialSockErr, callback.SocketPath)
- return "", err
+ return nil, err
}
defer conn.Close()
@@ -213,7 +231,7 @@ func (m *Manager) OnDefineDomain(domainSpec *virtwrapApi.DomainSpec, vmi *v1.Vir
Vmi: vmiJSON,
})
if err != nil {
- return "", err
+ return nil, err
}
domainSpecXML = result.GetDomainXML()
case hooksV1alpha2.Version:
@@ -223,16 +241,14 @@ func (m *Manager) OnDefineDomain(domainSpec *virtwrapApi.DomainSpec, vmi *v1.Vir
Vmi: vmiJSON,
})
if err != nil {
- return "", err
+ return nil, err
}
domainSpecXML = result.GetDomainXML()
default:
panic("Should never happen, version compatibility check is done during Info call")
}
- }
- }
- }
- return string(domainSpecXML), nil
+
+ return domainSpecXML, nil
}
func (m *Manager) PreCloudInitIso(vmi *v1.VirtualMachineInstance, cloudInitData *cloudinit.CloudInitData) (*cloudinit.CloudInitData, error) {
