sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/internal/util/obj_refs.go (about) 1 /* 2 Copyright 2020 The Kubernetes 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 util 18 19 import ( 20 "fmt" 21 "os" 22 "strings" 23 24 corev1 "k8s.io/api/core/v1" 25 ) 26 27 // GetObjectReferences accepts arguments in resource/name form (e.g. 'resource/<resource_name>') and returns a ObjectReference for each resource/name. 28 func GetObjectReferences(namespace string, args ...string) ([]corev1.ObjectReference, error) { 29 var objRefs []corev1.ObjectReference 30 if ok, err := hasCombinedTypeArgs(args); ok { 31 if err != nil { 32 return objRefs, err 33 } 34 for _, s := range args { 35 ref, ok, err := convertToObjectRef(namespace, s) 36 if err != nil { 37 return objRefs, err 38 } 39 if ok { 40 objRefs = append(objRefs, ref) 41 } 42 } 43 } else { 44 return objRefs, fmt.Errorf("arguments must be in resource/name format (e.g. machinedeployment/md-1)") 45 } 46 return objRefs, nil 47 } 48 49 func hasCombinedTypeArgs(args []string) (bool, error) { 50 hasSlash := 0 51 for _, s := range args { 52 if strings.Contains(s, "/") { 53 hasSlash++ 54 } 55 } 56 switch { 57 case hasSlash > 0 && hasSlash == len(args): 58 return true, nil 59 case hasSlash > 0 && hasSlash != len(args): 60 baseCmd := "cmd" 61 if len(os.Args) > 0 { 62 baseCmdSlice := strings.Split(os.Args[0], "/") 63 baseCmd = baseCmdSlice[len(baseCmdSlice)-1] 64 } 65 return true, fmt.Errorf("there is no need to specify a resource type as a separate argument when passing arguments in resource/name form (e.g. '%s get resource/<resource_name>' instead of '%s get resource resource/<resource_name>'", baseCmd, baseCmd) 66 default: 67 return false, nil 68 } 69 } 70 71 // convertToObjectRef handles type/name resource formats and returns a ObjectReference 72 // (empty or not), whether it successfully found one, and an error. 73 func convertToObjectRef(namespace, s string) (corev1.ObjectReference, bool, error) { 74 if !strings.Contains(s, "/") { 75 return corev1.ObjectReference{}, false, nil 76 } 77 seg := strings.Split(s, "/") 78 if len(seg) != 2 { 79 return corev1.ObjectReference{}, false, fmt.Errorf("arguments in resource/name form may not have more than one slash") 80 } 81 resource, name := seg[0], seg[1] 82 if resource == "" || name == "" { 83 return corev1.ObjectReference{}, false, fmt.Errorf("arguments in resource/name form must have a single resource and name") 84 } 85 return corev1.ObjectReference{ 86 Kind: resource, 87 Name: name, 88 Namespace: namespace, 89 }, true, nil 90 }