github.com/kaisenlinux/docker@v0.0.0-20230510090727-ea55db55fac7/swarmkit/api/genericresource/helpers.go (about)

     1  package genericresource
     2  
     3  import (
     4  	"github.com/docker/swarmkit/api"
     5  )
     6  
     7  // NewSet creates a set object
     8  func NewSet(key string, vals ...string) []*api.GenericResource {
     9  	rs := make([]*api.GenericResource, 0, len(vals))
    10  
    11  	for _, v := range vals {
    12  		rs = append(rs, NewString(key, v))
    13  	}
    14  
    15  	return rs
    16  }
    17  
    18  // NewString creates a String resource
    19  func NewString(key, val string) *api.GenericResource {
    20  	return &api.GenericResource{
    21  		Resource: &api.GenericResource_NamedResourceSpec{
    22  			NamedResourceSpec: &api.NamedGenericResource{
    23  				Kind:  key,
    24  				Value: val,
    25  			},
    26  		},
    27  	}
    28  }
    29  
    30  // NewDiscrete creates a Discrete resource
    31  func NewDiscrete(key string, val int64) *api.GenericResource {
    32  	return &api.GenericResource{
    33  		Resource: &api.GenericResource_DiscreteResourceSpec{
    34  			DiscreteResourceSpec: &api.DiscreteGenericResource{
    35  				Kind:  key,
    36  				Value: val,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  // GetResource returns resources from the "resources" parameter matching the kind key
    43  func GetResource(kind string, resources []*api.GenericResource) []*api.GenericResource {
    44  	var res []*api.GenericResource
    45  
    46  	for _, r := range resources {
    47  		if Kind(r) != kind {
    48  			continue
    49  		}
    50  
    51  		res = append(res, r)
    52  	}
    53  
    54  	return res
    55  }
    56  
    57  // ConsumeNodeResources removes "res" from nodeAvailableResources
    58  func ConsumeNodeResources(nodeAvailableResources *[]*api.GenericResource, res []*api.GenericResource) {
    59  	if nodeAvailableResources == nil {
    60  		return
    61  	}
    62  
    63  	w := 0
    64  
    65  loop:
    66  	for _, na := range *nodeAvailableResources {
    67  		for _, r := range res {
    68  			if Kind(na) != Kind(r) {
    69  				continue
    70  			}
    71  
    72  			if remove(na, r) {
    73  				continue loop
    74  			}
    75  			// If this wasn't the right element then
    76  			// we need to continue
    77  		}
    78  
    79  		(*nodeAvailableResources)[w] = na
    80  		w++
    81  	}
    82  
    83  	*nodeAvailableResources = (*nodeAvailableResources)[:w]
    84  }
    85  
    86  // Returns true if the element is to be removed from the list
    87  func remove(na, r *api.GenericResource) bool {
    88  	switch tr := r.Resource.(type) {
    89  	case *api.GenericResource_DiscreteResourceSpec:
    90  		if na.GetDiscreteResourceSpec() == nil {
    91  			return false // Type change, ignore
    92  		}
    93  
    94  		na.GetDiscreteResourceSpec().Value -= tr.DiscreteResourceSpec.Value
    95  		if na.GetDiscreteResourceSpec().Value <= 0 {
    96  			return true
    97  		}
    98  	case *api.GenericResource_NamedResourceSpec:
    99  		if na.GetNamedResourceSpec() == nil {
   100  			return false // Type change, ignore
   101  		}
   102  
   103  		if tr.NamedResourceSpec.Value != na.GetNamedResourceSpec().Value {
   104  			return false // not the right item, ignore
   105  		}
   106  
   107  		return true
   108  	}
   109  
   110  	return false
   111  }