github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/class/utils.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 "context" 24 "fmt" 25 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/client-go/dynamic" 29 30 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 31 "github.com/1aal/kubeblocks/pkg/class" 32 "github.com/1aal/kubeblocks/pkg/cli/types" 33 "github.com/1aal/kubeblocks/pkg/constant" 34 ) 35 36 // GetManager gets a class manager which manages default classes and user custom classes 37 func GetManager(client dynamic.Interface, cdName string) (*class.Manager, error) { 38 selector := fmt.Sprintf("%s=%s,%s", constant.ClusterDefLabelKey, cdName, types.ClassProviderLabelKey) 39 classObjs, err := client.Resource(types.ComponentClassDefinitionGVR()).Namespace("").List(context.TODO(), metav1.ListOptions{ 40 LabelSelector: selector, 41 }) 42 if err != nil { 43 return nil, err 44 } 45 var classDefinitionList v1alpha1.ComponentClassDefinitionList 46 if err = runtime.DefaultUnstructuredConverter.FromUnstructured(classObjs.UnstructuredContent(), &classDefinitionList); err != nil { 47 return nil, err 48 } 49 50 constraintObjs, err := client.Resource(types.ComponentResourceConstraintGVR()).Namespace("").List(context.TODO(), metav1.ListOptions{}) 51 if err != nil { 52 return nil, err 53 } 54 var resourceConstraintList v1alpha1.ComponentResourceConstraintList 55 if err = runtime.DefaultUnstructuredConverter.FromUnstructured(constraintObjs.UnstructuredContent(), &resourceConstraintList); err != nil { 56 return nil, err 57 } 58 return class.NewManager(classDefinitionList, resourceConstraintList) 59 } 60 61 // GetResourceConstraints gets all resource constraints 62 func GetResourceConstraints(dynamic dynamic.Interface) (map[string]*v1alpha1.ComponentResourceConstraint, error) { 63 objs, err := dynamic.Resource(types.ComponentResourceConstraintGVR()).List(context.TODO(), metav1.ListOptions{ 64 // LabelSelector: types.ResourceConstraintProviderLabelKey, 65 }) 66 if err != nil { 67 return nil, err 68 } 69 var constraintsList v1alpha1.ComponentResourceConstraintList 70 if err = runtime.DefaultUnstructuredConverter.FromUnstructured(objs.UnstructuredContent(), &constraintsList); err != nil { 71 return nil, err 72 } 73 74 result := make(map[string]*v1alpha1.ComponentResourceConstraint) 75 for idx := range constraintsList.Items { 76 cf := constraintsList.Items[idx] 77 if _, ok := cf.GetLabels()[constant.ResourceConstraintProviderLabelKey]; !ok { 78 continue 79 } 80 result[cf.GetName()] = &cf 81 } 82 return result, nil 83 }