github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/general/flags.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 general 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 24 v1 "k8s.io/api/core/v1" 25 "k8s.io/apimachinery/pkg/api/resource" 26 ) 27 28 type ResourceList v1.ResourceList 29 30 func (r *ResourceList) String() string { 31 var pairs []string 32 for k, v := range *r { 33 pairs = append(pairs, fmt.Sprintf("%s=%s", k, v.String())) 34 } 35 sort.Strings(pairs) 36 return strings.Join(pairs, ",") 37 } 38 39 func (r *ResourceList) Set(value string) error { 40 for _, s := range strings.Split(value, ",") { 41 if len(s) == 0 { 42 continue 43 } 44 arr := strings.SplitN(s, "=", 2) 45 if len(arr) == 2 { 46 parseQuantity, err := resource.ParseQuantity(arr[1]) 47 if err != nil { 48 return err 49 } 50 (*r)[v1.ResourceName(strings.TrimSpace(arr[0]))] = parseQuantity 51 } 52 } 53 return nil 54 } 55 56 func (r *ResourceList) Type() string { 57 return "resourceList" 58 }