istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/completion/completion.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package completion 16 17 import ( 18 "context" 19 "strings" 20 21 "github.com/spf13/cobra" 22 corev1 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 25 "istio.io/istio/istioctl/pkg/cli" 26 "istio.io/istio/pkg/kube" 27 ) 28 29 func getPodsNameInDefaultNamespace(ctx cli.Context, toComplete string) ([]string, error) { 30 client, err := ctx.CLIClient() 31 if err != nil { 32 return nil, err 33 } 34 ns := ctx.NamespaceOrDefault(ctx.Namespace()) 35 podList, err := client.Kube().CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{}) 36 if err != nil { 37 return nil, err 38 } 39 40 var podsName []string 41 for _, pod := range podList.Items { 42 if toComplete == "" || strings.HasPrefix(pod.Name, toComplete) { 43 podsName = append(podsName, pod.Name) 44 } 45 } 46 47 return podsName, nil 48 } 49 50 func ValidPodsNameArgs(ctx cli.Context) func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 51 return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 52 if len(args) != 0 { 53 return nil, cobra.ShellCompDirectiveNoFileComp 54 } 55 56 podsName, err := getPodsNameInDefaultNamespace(ctx, toComplete) 57 if err != nil { 58 return nil, cobra.ShellCompDirectiveNoFileComp 59 } 60 return podsName, cobra.ShellCompDirectiveNoFileComp 61 } 62 } 63 64 func getServicesName(ctx cli.Context, toComplete string) ([]string, error) { 65 client, err := ctx.CLIClient() 66 if err != nil { 67 return nil, err 68 } 69 ns := ctx.NamespaceOrDefault(ctx.Namespace()) 70 serviceList, err := client.Kube().CoreV1().Services(ns).List(context.Background(), metav1.ListOptions{}) 71 if err != nil { 72 return nil, err 73 } 74 75 var serviceNameList []string 76 for _, service := range serviceList.Items { 77 if toComplete == "" || strings.HasPrefix(service.Name, toComplete) { 78 serviceNameList = append(serviceNameList, service.Name) 79 } 80 } 81 82 return serviceNameList, nil 83 } 84 85 func ValidServiceArgs(_ *cobra.Command, ctx cli.Context, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 86 if len(args) != 0 { 87 return nil, cobra.ShellCompDirectiveNoFileComp 88 } 89 90 servicesName, err := getServicesName(ctx, toComplete) 91 if err != nil { 92 return nil, cobra.ShellCompDirectiveNoFileComp 93 } 94 return servicesName, cobra.ShellCompDirectiveNoFileComp 95 } 96 97 func getNamespacesName(kubeClient kube.CLIClient, toComplete string) ([]string, error) { 98 ctx := context.Background() 99 nsList, err := getNamespaces(ctx, kubeClient) 100 if err != nil { 101 return nil, err 102 } 103 104 var nsNameList []string 105 for _, ns := range nsList { 106 if toComplete == "" || strings.HasPrefix(ns.Name, toComplete) { 107 nsNameList = append(nsNameList, ns.Name) 108 } 109 } 110 111 return nsNameList, nil 112 } 113 114 func getNamespaces(ctx context.Context, client kube.CLIClient) ([]corev1.Namespace, error) { 115 nslist, err := client.Kube().CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) 116 if err != nil { 117 return []corev1.Namespace{}, err 118 } 119 return nslist.Items, nil 120 } 121 122 func ValidNamespaceArgs(_ *cobra.Command, ctx cli.Context, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 123 if len(args) != 0 { 124 return nil, cobra.ShellCompDirectiveNoFileComp 125 } 126 client, err := ctx.CLIClient() 127 if err != nil { 128 return nil, cobra.ShellCompDirectiveNoFileComp 129 } 130 nsName, err := getNamespacesName(client, toComplete) 131 if err != nil { 132 return nil, cobra.ShellCompDirectiveNoFileComp 133 } 134 return nsName, cobra.ShellCompDirectiveNoFileComp 135 } 136 137 func ValidServiceAccountArgs(_ *cobra.Command, ctx cli.Context, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 138 if len(args) != 0 { 139 return nil, cobra.ShellCompDirectiveNoFileComp 140 } 141 client, err := ctx.CLIClient() 142 if err != nil { 143 return nil, cobra.ShellCompDirectiveNoFileComp 144 } 145 saName, err := getServiceAccountsName(client, toComplete, ctx.NamespaceOrDefault(ctx.Namespace())) 146 if err != nil { 147 return nil, cobra.ShellCompDirectiveNoFileComp 148 } 149 return saName, cobra.ShellCompDirectiveNoFileComp 150 } 151 152 func getServiceAccountsName(kubeClient kube.CLIClient, toComplete, ns string) ([]string, error) { 153 ctx := context.Background() 154 saList, err := kubeClient.Kube().CoreV1().ServiceAccounts(ns).List(ctx, metav1.ListOptions{}) 155 if err != nil { 156 return nil, err 157 } 158 159 var saNameList []string 160 for _, sa := range saList.Items { 161 if toComplete == "" || strings.HasPrefix(sa.Name, toComplete) { 162 saNameList = append(saNameList, sa.Name) 163 } 164 } 165 166 return saNameList, nil 167 }