github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/apps/v1alpha1/componentclassdefinition_types.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1alpha1
    18  
    19  import (
    20  	corev1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/api/resource"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  )
    24  
    25  // ComponentClassDefinitionSpec defines the desired state of ComponentClassDefinition
    26  type ComponentClassDefinitionSpec struct {
    27  	// group defines a list of class series that conform to the same constraint.
    28  	// +optional
    29  	Groups []ComponentClassGroup `json:"groups,omitempty"`
    30  }
    31  
    32  type ComponentClassGroup struct {
    33  	// template is a class definition template that uses the Go template syntax and allows for variable declaration.
    34  	// When defining a class in Series, specifying the variable's value is sufficient, as the complete class
    35  	// definition will be generated through rendering the template.
    36  	//
    37  	// For example:
    38  	//	template: |
    39  	//	  cpu: "{{ or .cpu 1 }}"
    40  	//	  memory: "{{ or .memory 4 }}Gi"
    41  	//
    42  	// +optional
    43  	Template string `json:"template,omitempty"`
    44  
    45  	// vars defines the variables declared in the template and will be used to generating the complete class definition by
    46  	// render the template.
    47  	// +listType=set
    48  	// +optional
    49  	Vars []string `json:"vars,omitempty"`
    50  
    51  	// series is a series of class definitions.
    52  	// +optional
    53  	Series []ComponentClassSeries `json:"series,omitempty"`
    54  }
    55  
    56  type ComponentClassSeries struct {
    57  	// namingTemplate is a template that uses the Go template syntax and allows for referencing variables defined
    58  	// in ComponentClassGroup.Template. This enables dynamic generation of class names.
    59  	// For example:
    60  	// name: "general-{{ .cpu }}c{{ .memory }}g"
    61  	// +optional
    62  	NamingTemplate string `json:"namingTemplate,omitempty"`
    63  
    64  	// classes are definitions of classes that come in two forms. In the first form, only ComponentClass.Args
    65  	// need to be defined, and the complete class definition is generated by rendering the ComponentClassGroup.Template
    66  	// and Name. In the second form, the Name, CPU and Memory must be defined.
    67  	// +optional
    68  	Classes []ComponentClass `json:"classes,omitempty"`
    69  }
    70  
    71  type ComponentClass struct {
    72  	// name is the class name
    73  	// +optional
    74  	Name string `json:"name,omitempty"`
    75  
    76  	// args are variable's value
    77  	// +optional
    78  	Args []string `json:"args,omitempty"`
    79  
    80  	// the CPU of the class
    81  	// +optional
    82  	CPU resource.Quantity `json:"cpu,omitempty"`
    83  
    84  	// the memory of the class
    85  	// +optional
    86  	Memory resource.Quantity `json:"memory,omitempty"`
    87  }
    88  
    89  // ComponentClassDefinitionStatus defines the observed state of ComponentClassDefinition
    90  type ComponentClassDefinitionStatus struct {
    91  	// observedGeneration is the most recent generation observed for this
    92  	// ComponentClassDefinition. It corresponds to the ComponentClassDefinition's generation, which is
    93  	// updated on mutation by the API Server.
    94  	// +optional
    95  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
    96  
    97  	// classes is the list of classes that have been observed for this ComponentClassDefinition
    98  	Classes []ComponentClass `json:"classes,omitempty"`
    99  }
   100  
   101  // +genclient
   102  // +genclient:nonNamespaced
   103  // +k8s:openapi-gen=true
   104  // +kubebuilder:object:root=true
   105  // +kubebuilder:subresource:status
   106  // +kubebuilder:resource:categories={kubeblocks},scope=Cluster,shortName=ccd
   107  
   108  // ComponentClassDefinition is the Schema for the componentclassdefinitions API
   109  type ComponentClassDefinition struct {
   110  	metav1.TypeMeta   `json:",inline"`
   111  	metav1.ObjectMeta `json:"metadata,omitempty"`
   112  
   113  	Spec   ComponentClassDefinitionSpec   `json:"spec,omitempty"`
   114  	Status ComponentClassDefinitionStatus `json:"status,omitempty"`
   115  }
   116  
   117  // +kubebuilder:object:root=true
   118  
   119  // ComponentClassDefinitionList contains a list of ComponentClassDefinition
   120  type ComponentClassDefinitionList struct {
   121  	metav1.TypeMeta `json:",inline"`
   122  	metav1.ListMeta `json:"metadata,omitempty"`
   123  	Items           []ComponentClassDefinition `json:"items"`
   124  }
   125  
   126  func init() {
   127  	SchemeBuilder.Register(&ComponentClassDefinition{}, &ComponentClassDefinitionList{})
   128  }
   129  
   130  func (r *ComponentClass) ToResourceRequirements() corev1.ResourceRequirements {
   131  	requests := corev1.ResourceList{
   132  		corev1.ResourceCPU:    r.CPU,
   133  		corev1.ResourceMemory: r.Memory,
   134  	}
   135  	return corev1.ResourceRequirements{Requests: requests, Limits: requests}
   136  }
   137  
   138  func (r *ComponentClass) Cmp(b *ComponentClass) int {
   139  	if out := r.CPU.Cmp(b.CPU); out != 0 {
   140  		return out
   141  	}
   142  
   143  	if out := r.Memory.Cmp(b.Memory); out != 0 {
   144  		return out
   145  	}
   146  
   147  	return 0
   148  }