diff --git a/controllers/multinicnetwork_handler.go b/controllers/multinicnetwork_handler.go index 254bcc33..f8a80aa9 100644 --- a/controllers/multinicnetwork_handler.go +++ b/controllers/multinicnetwork_handler.go @@ -161,6 +161,10 @@ func (h *MultiNicNetworkHandler) UpdateNetConfigStatus(instance *multinicv1.Mult instance.Status.ComputeResults = []multinicv1.NicNetworkResult{} } instance.Status.NetConfigStatus = netConfigStatus + emptyTime := metav1.Time{} + if instance.Status.LastSyncTime == emptyTime { + instance.Status.LastSyncTime = metav1.Now() + } ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout) defer cancel() err := h.Client.Status().Update(ctx, instance) diff --git a/plugin/mellanox.go b/plugin/mellanox.go index b4fdda84..6b52637f 100644 --- a/plugin/mellanox.go +++ b/plugin/mellanox.go @@ -12,7 +12,6 @@ import ( "github.com/containernetworking/cni/pkg/types" multinicv1 "github.com/foundation-model-stack/multi-nic-cni/api/v1" "github.com/foundation-model-stack/multi-nic-cni/controllers/vars" - k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" @@ -57,21 +56,12 @@ func (p *MellanoxPlugin) GetConfig(net multinicv1.MultiNicNetwork, hifList map[s annotation := make(map[string]string) var err error // get resource from nicclusterpolicy - resourceName := p.GetResourceName() + prefix, resourceName := p.GetResourceName() if resourceName == "" { msg := "failed to get resource name from sriov plugin config" vars.NetworkLog.V(2).Info(msg) return "", annotation, fmt.Errorf(msg) } - - name := net.GetName() - namespace := net.GetNamespace() - netName := GetHolderNetName(name) - hostdevicenetwork, err := p.createHostDeviceNetwork(net.Spec.IPAM, netName, namespace, resourceName) - if err != nil { - return "", annotation, err - } - vars.NetworkLog.V(2).Info(fmt.Sprintf("hostdevicenetwork %s created", hostdevicenetwork.Name)) conf := HostDeviceTypeNetConf{} conf.CNIVersion = net.Spec.MainPlugin.CNIVersion conf.Type = HOST_DEVICE_TYPE @@ -84,24 +74,24 @@ func (p *MellanoxPlugin) GetConfig(net multinicv1.MultiNicNetwork, hifList map[s if err != nil { return "", annotation, err } - annotation[RESOURCE_ANNOTATION] = resourceName + annotation[RESOURCE_ANNOTATION] = prefix + "/" + resourceName return string(confBytes), annotation, nil } // return first resource name found in SriovDevicePlugin -func (p *MellanoxPlugin) GetResourceName() string { +func (p *MellanoxPlugin) GetResourceName() (string, string) { policy, err := p.getPolicy() if err != nil { vars.NetworkLog.V(2).Info(fmt.Sprintf("failed to get policy: %v", err)) } if err != nil { vars.NetworkLog.V(2).Info(fmt.Sprintf("failed to read sriov plugin config: %v", err)) - return "" + return "", "" } sriovPlugin := policy.Spec.SriovDevicePlugin if sriovPlugin == nil || sriovPlugin.Config == nil { vars.NetworkLog.V(2).Info(fmt.Sprintf("no sriov device plugin config set in %s", policy.Name)) - return "" + return "", "" } configStr := *sriovPlugin.Config var config map[string]interface{} @@ -113,9 +103,9 @@ func (p *MellanoxPlugin) GetResourceName() string { if resourceMap, ok := resource.(map[string]interface{}); ok { if resourcePrefix, ok := resourceMap["resourcePrefix"]; ok { if resourceName, ok := resourceMap["resourceName"]; ok { - return resourcePrefix.(string) + "/" + resourceName.(string) + return resourcePrefix.(string), resourceName.(string) } else { - return DEFAULT_MELLANOX_PREFIX + "/" + resourceName.(string) + return DEFAULT_MELLANOX_PREFIX, resourceName.(string) } } } @@ -124,7 +114,7 @@ func (p *MellanoxPlugin) GetResourceName() string { } } vars.NetworkLog.V(2).Info(fmt.Sprintf("cannot read value from sriov config: %v", err)) - return "" + return "", "" } func (p *MellanoxPlugin) getPolicy() (*NicClusterPolicy, error) { @@ -136,22 +126,6 @@ func (p *MellanoxPlugin) getPolicy() (*NicClusterPolicy, error) { return policy, err } -func (p *MellanoxPlugin) createHostDeviceNetwork(ipam string, name string, namespace string, resourceName string) (*HostDeviceNetwork, error) { - spec := &HostDeviceNetworkSpec{} - spec.NetworkNamespace = "default" - spec.ResourceName = resourceName - spec.IPAM = ipam - metaObj := GetMetaObject(name, namespace, make(map[string]string)) - hostDeviceNet := NewHostDeviceNetwork(metaObj, *spec) - result := &HostDeviceNetwork{} - err := p.MellanoxNetworkHandler.Create(metav1.NamespaceAll, hostDeviceNet, result) - if k8serrors.IsAlreadyExists(err) { - return result, nil - } - return result, err -} - func (p *MellanoxPlugin) CleanUp(net multinicv1.MultiNicNetwork) error { - netName := GetHolderNetName(net.Name) - return p.MellanoxNetworkHandler.Delete(netName, metav1.NamespaceAll) + return nil } diff --git a/plugin/mellanox_resource.go b/plugin/mellanox_resource.go index 3f0c10c1..9daf4005 100644 --- a/plugin/mellanox_resource.go +++ b/plugin/mellanox_resource.go @@ -27,17 +27,6 @@ const ( MELLANOX_POLICY_KIND = "NicClusterPolicy" ) -func NewHostDeviceNetwork(metaObj metav1.ObjectMeta, spec HostDeviceNetworkSpec) HostDeviceNetwork { - return HostDeviceNetwork{ - TypeMeta: metav1.TypeMeta{ - APIVersion: MELLANOX_API_VERSION, - Kind: MELLANOX_NETWORK_KIND, - }, - ObjectMeta: metaObj, - Spec: spec, - } -} - // ImageSpec Contains container image specifications type ImageSpec struct { // +kubebuilder:validation:Pattern=[a-zA-Z0-9\-]+ @@ -239,25 +228,3 @@ type NicClusterPolicy struct { Spec NicClusterPolicySpec `json:"spec,omitempty"` Status NicClusterPolicyStatus `json:"status,omitempty"` } - -// HostDeviceNetworkSpec defines the desired state of HostDeviceNetwork -type HostDeviceNetworkSpec struct { - // Namespace of the NetworkAttachmentDefinition custom resource - NetworkNamespace string `json:"networkNamespace,omitempty"` - // Host device resource pool name - ResourceName string `json:"resourceName,omitempty"` - // IPAM configuration to be used for this network - IPAM string `json:"ipam,omitempty"` -} - -type HostDeviceNetworkStatus struct { -} - -// HostDeviceNetwork is the Schema for the hostdevicenetworks API -type HostDeviceNetwork struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - Spec HostDeviceNetworkSpec `json:"spec,omitempty"` - Status HostDeviceNetworkStatus `json:"status,omitempty"` -} diff --git a/unit-test/plugin_test.go b/unit-test/plugin_test.go index 9169e6ca..324a8e9b 100644 --- a/unit-test/plugin_test.go +++ b/unit-test/plugin_test.go @@ -14,7 +14,6 @@ import ( "github.com/foundation-model-stack/multi-nic-cni/plugin" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" //+kubebuilder:scaffold:imports ) @@ -159,16 +158,11 @@ var _ = Describe("Test GetConfig of main plugins", func() { confBytes, annotations, err := mellanoxPlugin.GetConfig(*multinicnetwork, hifList) Expect(err).NotTo(HaveOccurred()) Expect(confBytes).NotTo(Equal("")) - netName := plugin.GetHolderNetName(multinicnetwork.Name) - resourceName := mellanoxPlugin.GetResourceName() + prefix, resourceName := mellanoxPlugin.GetResourceName() Expect(resourceName) - - hostDeviceNet := &plugin.HostDeviceNetwork{} - err = mellanoxPlugin.MellanoxNetworkHandler.Get(netName, metav1.NamespaceAll, hostDeviceNet) - // HostDeviceNetwork is created - Expect(err).NotTo(HaveOccurred()) - Expect(hostDeviceNet.Spec.ResourceName).To(Equal(resourceName)) - Expect(annotations[plugin.RESOURCE_ANNOTATION]).To(Equal(resourceName)) + Expect(prefix) + fullName := prefix + "/" + resourceName + Expect(annotations[plugin.RESOURCE_ANNOTATION]).To(Equal(fullName)) err = mellanoxPlugin.CleanUp(*multinicnetwork) Expect(err).NotTo(HaveOccurred()) })