github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/componentresourceconstraint_factory.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 apps
    21  
    22  import (
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/util/yaml"
    25  
    26  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    27  )
    28  
    29  type ResourceConstraintTplType string
    30  
    31  const (
    32  	GeneralResourceConstraint         ResourceConstraintTplType = "general"
    33  	MemoryOptimizedResourceConstraint ResourceConstraintTplType = "memory-optimized"
    34  	ProductionResourceConstraint      ResourceConstraintTplType = "production"
    35  
    36  	generalResourceConstraintTemplate = `
    37  - name: c1
    38    cpu:
    39      min: 0.5
    40      max: 2
    41      step: 0.5
    42    memory:
    43      sizePerCPU: 1Gi
    44  - name: c2
    45    cpu:
    46      min: 2
    47      max: 2
    48    memory:
    49      sizePerCPU: 2Gi
    50  - name: c3
    51    cpu:
    52      slots: [1, 2, 4, 8, 16, 24, 32, 48, 64, 96, 128]
    53    memory:
    54      sizePerCPU: 4Gi
    55  - name: c4
    56    cpu:
    57      slots: [100, 500]
    58    memory:
    59      sizePerCPU: 2Gi
    60  `
    61  
    62  	memoryResourceConstraintTemplate = `
    63  - name: c1
    64    cpu:
    65      slots: [2, 4, 8, 12, 24, 48]
    66    memory:
    67      sizePerCPU: 8Gi
    68  - name: c2
    69    cpu:
    70      min: 2
    71      max: 128
    72      step: 2
    73    memory:
    74      sizePerCPU: 16Gi
    75  `
    76  
    77  	productionResourceConstraintTemplate = `
    78  - name: c1
    79    cpu:
    80      min: "0.5"
    81      max: 64
    82      step: "0.5"
    83    memory:
    84      minPerCPU: 1Gi
    85      maxPerCPU: 32Gi
    86    storage:
    87      min: 1Gi
    88      max: 10Ti
    89  `
    90  )
    91  
    92  type MockComponentResourceConstraintFactory struct {
    93  	BaseFactory[appsv1alpha1.ComponentResourceConstraint, *appsv1alpha1.ComponentResourceConstraint, MockComponentResourceConstraintFactory]
    94  }
    95  
    96  func NewComponentResourceConstraintFactory(name string) *MockComponentResourceConstraintFactory {
    97  	f := &MockComponentResourceConstraintFactory{}
    98  	f.Init("", name, &appsv1alpha1.ComponentResourceConstraint{
    99  		ObjectMeta: metav1.ObjectMeta{
   100  			Name: name,
   101  			Labels: map[string]string{
   102  				"resourceconstraint.kubeblocks.io/provider": "kubeblocks",
   103  			},
   104  		},
   105  	}, f)
   106  	return f
   107  }
   108  
   109  func (factory *MockComponentResourceConstraintFactory) AddConstraints(constraintTplType ResourceConstraintTplType) *MockComponentResourceConstraintFactory {
   110  	var (
   111  		tpl            string
   112  		newConstraints []appsv1alpha1.ResourceConstraintRule
   113  		constraints    = factory.Get().Spec.Rules
   114  	)
   115  	switch constraintTplType {
   116  	case GeneralResourceConstraint:
   117  		tpl = generalResourceConstraintTemplate
   118  	case MemoryOptimizedResourceConstraint:
   119  		tpl = memoryResourceConstraintTemplate
   120  	case ProductionResourceConstraint:
   121  		tpl = productionResourceConstraintTemplate
   122  	}
   123  	if err := yaml.Unmarshal([]byte(tpl), &newConstraints); err != nil {
   124  		panic(err)
   125  	}
   126  	constraints = append(constraints, newConstraints...)
   127  	factory.Get().Spec.Rules = constraints
   128  	return factory
   129  }
   130  
   131  func (factory *MockComponentResourceConstraintFactory) AddSelector(selector appsv1alpha1.ClusterResourceConstraintSelector) *MockComponentResourceConstraintFactory {
   132  	factory.Get().Spec.Selector = append(factory.Get().Spec.Selector, selector)
   133  	return factory
   134  }