github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/resource-recommend/resource/k8s_resource.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 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 resource 18 19 import ( 20 "context" 21 "encoding/json" 22 23 "github.com/pkg/errors" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 k8stypes "k8s.io/apimachinery/pkg/types" 27 "k8s.io/klog/v2" 28 k8sclient "sigs.k8s.io/controller-runtime/pkg/client" 29 30 "github.com/kubewharf/katalyst-api/pkg/apis/recommendation/v1alpha1" 31 ) 32 33 func ConvertAndGetResource(ctx context.Context, client k8sclient.Client, namespace string, targetRef v1alpha1.CrossVersionObjectReference) (*unstructured.Unstructured, error) { 34 klog.V(5).Infof("Get resource in", "targetRef", targetRef, "namespace", namespace) 35 obj := &unstructured.Unstructured{} 36 obj.SetAPIVersion(targetRef.APIVersion) 37 obj.SetKind(targetRef.Kind) 38 if err := client.Get(ctx, k8stypes.NamespacedName{Namespace: namespace, Name: targetRef.Name}, obj); err != nil { 39 return nil, err 40 } 41 return obj, nil 42 } 43 44 func GetAllClaimedContainers(controller *unstructured.Unstructured) ([]string, error) { 45 klog.V(5).InfoS("Get all controller claimed containers", "controller", controller) 46 templateSpec, found, err := unstructured.NestedMap(controller.Object, "spec", "template", "spec") 47 if err != nil { 48 return nil, errors.Wrapf(err, "unstructured.NestedMap err") 49 } 50 if !found { 51 return nil, errors.Errorf("spec.template.spec not found in the controller") 52 } 53 containersList, found, err := unstructured.NestedSlice(templateSpec, "containers") 54 if err != nil { 55 return nil, errors.Wrapf(err, "unstructured.NestedSlice err") 56 } 57 if !found { 58 return nil, errors.Errorf("failure to find containers in the controller") 59 } 60 61 containerNames := make([]string, 0, len(containersList)) 62 for _, container := range containersList { 63 containerMap, ok := container.(map[string]interface{}) 64 if !ok { 65 klog.Errorf("Unable to convert container:%v to map[string]interface{}", container) 66 continue 67 } 68 name, found, err := unstructured.NestedString(containerMap, "name") 69 if err != nil || !found { 70 klog.Errorf("Container name not found or get container name err:%v in the containerMap %v", err, containerMap) 71 continue 72 } 73 containerNames = append(containerNames, name) 74 } 75 return containerNames, nil 76 } 77 78 type patchRecord struct { 79 Op string `json:"op,inline"` 80 Path string `json:"path,inline"` 81 Value interface{} `json:"value"` 82 } 83 84 func PatchUpdateResourceRecommend(client k8sclient.Client, namespaceName k8stypes.NamespacedName, 85 resourceRecommend *v1alpha1.ResourceRecommend, 86 ) error { 87 obj := &v1alpha1.ResourceRecommend{ 88 ObjectMeta: metav1.ObjectMeta{ 89 Name: namespaceName.Name, 90 Namespace: namespaceName.Namespace, 91 }, 92 } 93 patches := []patchRecord{{ 94 Op: "replace", 95 Path: "/status", 96 Value: resourceRecommend.Status, 97 }} 98 99 patch, err := json.Marshal(patches) 100 if err != nil { 101 return errors.Wrapf(err, "failed to Marshal resourceRecommend: %+v", resourceRecommend) 102 } 103 patchDate := k8sclient.RawPatch(k8stypes.JSONPatchType, patch) 104 err = client.Status().Patch(context.TODO(), obj, patchDate) 105 if err != nil { 106 return errors.Wrapf(err, "failed to patch resource") 107 } 108 return nil 109 }