github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/debug/clusterinfo/clusterinfo.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 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 clusterinfo 16 17 import ( 18 "context" 19 20 "github.com/pkg/errors" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 corev1client "k8s.io/client-go/kubernetes/typed/core/v1" 23 ) 24 25 // GetPodsIP returns the pods IP without duplicates. 26 func GetPodsIP(ctx context.Context, client corev1client.CoreV1Interface, namespace string) ([]string, error) { 27 ipList := []string{} 28 podList, err := client.Pods(namespace).List(ctx, metav1.ListOptions{}) 29 if err != nil { 30 return nil, errors.Wrapf(err, "failed to get pods ip") 31 } 32 33 for i := range podList.Items { 34 ipList = append(ipList, podList.Items[i].Status.PodIP) 35 } 36 37 ipList = removeDuplicatesAndEmpty(ipList) 38 39 return ipList, nil 40 } 41 42 // GetNodesIP returns the nodes IP. 43 func GetNodesIP(ctx context.Context, client corev1client.CoreV1Interface) ([]string, error) { 44 ipList := []string{} 45 nodeList, err := client.Nodes().List(ctx, metav1.ListOptions{}) 46 if err != nil { 47 return nil, errors.Wrapf(err, "failed to get nodes ip") 48 } 49 50 for i := range nodeList.Items { 51 ipList = append(ipList, nodeList.Items[i].Status.Addresses[0].Address) 52 } 53 54 return ipList, nil 55 } 56 57 // GetDNSServiceAll return the DNS Service domain、DNS service cluster IP、DNS service endpoints. 58 func GetDNSServiceAll(ctx context.Context, client corev1client.CoreV1Interface) (string, string, []string, error) { 59 namespace := "kube-system" 60 serviceName := "kube-dns" 61 domain := "kube-dns.kube-system.svc" 62 63 service, err := client.Services(namespace).Get(ctx, serviceName, metav1.GetOptions{}) 64 if err != nil { 65 return "", "", nil, errors.Wrapf(err, "failed to get DNS service") 66 } 67 serviceClusterIP := service.Spec.ClusterIP 68 69 endpoints, err := client.Endpoints(namespace).Get(ctx, serviceName, metav1.GetOptions{}) 70 if err != nil { 71 return "", "", nil, errors.Wrapf(err, "failed to get DNS service endpoint") 72 } 73 endpointsIPs := []string{} 74 for _, address := range endpoints.Subsets[0].Addresses { 75 endpointsIPs = append(endpointsIPs, address.IP) 76 } 77 78 return domain, serviceClusterIP, endpointsIPs, nil 79 } 80 81 func removeDuplicatesAndEmpty(ss []string) (res []string) { 82 if len(ss) == 0 { 83 return ss 84 } 85 86 sMap := map[string]bool{} 87 88 for _, v := range ss { 89 if _, ok := sMap[v]; !ok && len(v) != 0 { 90 sMap[v] = true 91 res = append(res, v) 92 } 93 } 94 95 return res 96 }