virt-controller, template, multus annotations: introduce struct
Increase code readability by providing a simpler abstraction that
groups together the multus network annotation manipulation
functions.
@@ -33,22 +33,38 @@ type multusNetworkAnnotation struct {
Namespace string `json:"namespace"`
}
-func generateMultusCNIAnnotation(vmi *v1.VirtualMachineInstance) (string, error) {
- var multusNetworkAnnotationPool []multusNetworkAnnotation
+type multusNetworkAnnotationPool struct {
+ pool []multusNetworkAnnotation
+}
- multusNonDefaultNetworks := filterMultusNonDefaultNetworks(vmi.Spec.Networks)
- for i, network := range multusNonDefaultNetworks {
- serializedInterface := newMultusAnnotationData(vmi, network, fmt.Sprintf("net%d", i+1))
- multusNetworkAnnotationPool = append(multusNetworkAnnotationPool, serializedInterface)
+func (mnap *multusNetworkAnnotationPool) add(multusNetworkAnnotation multusNetworkAnnotation) {
+ mnap.pool = append(mnap.pool, multusNetworkAnnotation)
}
- if len(multusNetworkAnnotationPool) > 0 {
- multusNetworksAnnotation, err := json.Marshal(multusNetworkAnnotationPool)
+func (mnap multusNetworkAnnotationPool) isEmpty() bool {
+ return len(mnap.pool) == 0
+}
+
+func (mnap multusNetworkAnnotationPool) toString() (string, error) {
+ multusNetworksAnnotation, err := json.Marshal(mnap.pool)
if err != nil {
- return "", fmt.Errorf("failed to create JSON list from CNI interface map %v", multusNetworkAnnotationPool)
+ return "", fmt.Errorf("failed to create JSON list from multus interface pool %v", mnap.pool)
}
return string(multusNetworksAnnotation), nil
}
+
+func generateMultusCNIAnnotation(vmi *v1.VirtualMachineInstance) (string, error) {
+ multusNetworkAnnotationPool := multusNetworkAnnotationPool{}
+
+ multusNonDefaultNetworks := filterMultusNonDefaultNetworks(vmi.Spec.Networks)
+ for i, network := range multusNonDefaultNetworks {
+ multusNetworkAnnotationPool.add(
+ newMultusAnnotationData(vmi, network, fmt.Sprintf("net%d", i+1)))
+ }
+
+ if !multusNetworkAnnotationPool.isEmpty() {
+ return multusNetworkAnnotationPool.toString()
+ }
return "", nil
}
