Skip to content

Commit

Permalink
Merge pull request #182 from sunya-ch/v1.2.0
Browse files Browse the repository at this point in the history
fix nil LastSyncTime and remove hostdevicenetwork handling
  • Loading branch information
sunya-ch authored Mar 5, 2024
2 parents 1863bb2 + 751b9bc commit 907a875
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 78 deletions.
4 changes: 4 additions & 0 deletions controllers/multinicnetwork_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 9 additions & 35 deletions plugin/mellanox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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{}
Expand All @@ -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)
}
}
}
Expand All @@ -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) {
Expand All @@ -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
}
33 changes: 0 additions & 33 deletions plugin/mellanox_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\-]+
Expand Down Expand Up @@ -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"`
}
14 changes: 4 additions & 10 deletions unit-test/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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())
})
Expand Down

0 comments on commit 907a875

Please sign in to comment.