github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/class/types.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package class
    21  
    22  import (
    23  	"sort"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  
    27  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    28  )
    29  
    30  var _ sort.Interface = ByResourceList{}
    31  
    32  type ByResourceList []corev1.ResourceList
    33  
    34  func (b ByResourceList) Len() int {
    35  	return len(b)
    36  }
    37  
    38  func (b ByResourceList) Less(i, j int) bool {
    39  	switch b[i].Cpu().Cmp(*b[j].Cpu()) {
    40  	case 1:
    41  		return false
    42  	case -1:
    43  		return true
    44  	}
    45  	switch b[i].Memory().Cmp(*b[j].Memory()) {
    46  	case 1:
    47  		return false
    48  	case -1:
    49  		return true
    50  	}
    51  	return false
    52  }
    53  
    54  func (b ByResourceList) Swap(i, j int) {
    55  	b[i], b[j] = b[j], b[i]
    56  }
    57  
    58  var _ sort.Interface = ByRuleList{}
    59  
    60  type ByRuleList []appsv1alpha1.ResourceConstraintRule
    61  
    62  func (m ByRuleList) Len() int {
    63  	return len(m)
    64  }
    65  
    66  func (m ByRuleList) Less(i, j int) bool {
    67  	var (
    68  		resource1 = m[i].GetMinimalResources()
    69  		resource2 = m[j].GetMinimalResources()
    70  	)
    71  	switch resource1.Cpu().Cmp(*resource2.Cpu()) {
    72  	case 1:
    73  		return false
    74  	case -1:
    75  		return true
    76  	}
    77  	switch resource1.Memory().Cmp(*resource2.Memory()) {
    78  	case 1:
    79  		return false
    80  	case -1:
    81  		return true
    82  	}
    83  	return false
    84  }
    85  
    86  func (m ByRuleList) Swap(i, j int) {
    87  	m[i], m[j] = m[j], m[i]
    88  }
    89  
    90  var _ sort.Interface = ByClassResource{}
    91  
    92  type ByClassResource []*ComponentClassWithRef
    93  
    94  func (b ByClassResource) Len() int {
    95  	return len(b)
    96  }
    97  
    98  func (b ByClassResource) Less(i, j int) bool {
    99  	if out := b[i].CPU.Cmp(b[j].CPU); out != 0 {
   100  		return out < 0
   101  	}
   102  
   103  	if out := b[i].Memory.Cmp(b[j].Memory); out != 0 {
   104  		return out < 0
   105  	}
   106  
   107  	return false
   108  }
   109  
   110  func (b ByClassResource) Swap(i, j int) {
   111  	b[i], b[j] = b[j], b[i]
   112  }
   113  
   114  type ComponentClassWithRef struct {
   115  	appsv1alpha1.ComponentClass
   116  
   117  	ClassDefRef appsv1alpha1.ClassDefRef
   118  }