...

Source file src/edge-infra.dev/pkg/sds/devices/agent/events/class.go

Documentation: edge-infra.dev/pkg/sds/devices/agent/events

     1  package events
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/containerd/containerd/containers"
     8  
     9  	"edge-infra.dev/pkg/sds/devices/class"
    10  	dsv1 "edge-infra.dev/pkg/sds/devices/k8s/apis/v1"
    11  	plugins "edge-infra.dev/pkg/sds/devices/k8s/device-plugins"
    12  	"edge-infra.dev/pkg/sds/devices/logger"
    13  )
    14  
    15  // ClassEventConstructor returns a constructor function that instantiates a new device class event from a modified/deleted class
    16  func ClassEventConstructor(servers map[string]plugins.Plugin, deviceClasses map[string]*dsv1.DeviceClass, allContainers map[string]*containers.Container) func(ctx context.Context, deviceClass *dsv1.DeviceClass) DeviceEvent {
    17  	return func(ctx context.Context, deviceClass *dsv1.DeviceClass) DeviceEvent {
    18  		log := logger.FromContext(ctx)
    19  		name := deviceClass.ClassName()
    20  
    21  		if deviceClass.Annotations[dsv1.MarkDeletedAnn] == "true" {
    22  			log.Info("device class was deleted", "class", name)
    23  			servers[name].Stop()
    24  			delete(servers, name)
    25  			delete(deviceClasses, name)
    26  			return newClassEvent(deviceClass.ClassName(), allContainers)
    27  		}
    28  
    29  		log.Info("device class was modified", "class", name)
    30  		deviceClasses[name] = deviceClass
    31  		return newClassEvent(deviceClass.ClassName(), allContainers)
    32  	}
    33  }
    34  
    35  // newClassEvent returns a new device event built from the changed class
    36  func newClassEvent(changedClass string, allContainers map[string]*containers.Container) DeviceEvent {
    37  	event := &classEvent{
    38  		event: &event{
    39  			containers: map[string]*containers.Container{},
    40  			postHookFn: func(context.Context) {},
    41  			timestamp:  time.Now(),
    42  		},
    43  	}
    44  
    45  	for _, ctr := range allContainers {
    46  		if _, ok := ctr.Labels[changedClass]; ok {
    47  			ctr.Labels[class.DefaultClass] = requested
    48  			event.event.containers[ctr.ID] = ctr
    49  		}
    50  	}
    51  	return event
    52  }
    53  

View as plain text