github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/client/k8s/client.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 k8s
    16  
    17  import (
    18  	"context"
    19  	"path/filepath"
    20  
    21  	"github.com/pkg/errors"
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/client-go/kubernetes"
    25  	v12 "k8s.io/client-go/kubernetes/typed/core/v1"
    26  	"k8s.io/client-go/tools/clientcmd"
    27  	"k8s.io/client-go/util/homedir"
    28  
    29  	"github.com/sealerio/sealer/common"
    30  )
    31  
    32  type Client struct {
    33  	client *kubernetes.Clientset
    34  }
    35  
    36  type NamespacePod struct {
    37  	Namespace v1.Namespace
    38  	PodList   *v1.PodList
    39  }
    40  
    41  type NamespaceSvc struct {
    42  	Namespace   v1.Namespace
    43  	ServiceList *v1.ServiceList
    44  }
    45  
    46  func NewK8sClient() (*Client, error) {
    47  	kubeconfig := filepath.Join(common.DefaultKubeConfigDir(), "config")
    48  	if home := homedir.HomeDir(); home != "" {
    49  		kubeconfig = filepath.Join(home, ".kube", "config")
    50  	}
    51  
    52  	// use the current context in kubeconfig
    53  	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    54  	if err != nil {
    55  		return nil, errors.Wrap(err, "failed to build kube config")
    56  	}
    57  
    58  	clientSet, err := kubernetes.NewForConfig(config)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return &Client{
    64  		client: clientSet,
    65  	}, nil
    66  }
    67  
    68  func (c *Client) ConfigMap(ns string) v12.ConfigMapInterface {
    69  	return c.client.CoreV1().ConfigMaps(ns)
    70  }
    71  
    72  func (c *Client) ListNodes() (*v1.NodeList, error) {
    73  	nodes, err := c.client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
    74  	if err != nil {
    75  		return nil, errors.Wrapf(err, "failed to get cluster nodes")
    76  	}
    77  	return nodes, nil
    78  }
    79  
    80  func (c *Client) DeleteNode(name string) error {
    81  	if err := c.client.CoreV1().Nodes().Delete(context.TODO(), name, metav1.DeleteOptions{}); err != nil {
    82  		return errors.Wrapf(err, "failed to delete cluster node(%s)", name)
    83  	}
    84  	return nil
    85  }
    86  
    87  func (c *Client) listNamespaces() (*v1.NamespaceList, error) {
    88  	namespaceList, err := c.client.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
    89  	if err != nil {
    90  		return nil, errors.Wrapf(err, "failed to get namespaces")
    91  	}
    92  	return namespaceList, nil
    93  }
    94  
    95  func (c *Client) ListAllNamespacesPods() ([]*NamespacePod, error) {
    96  	namespaceList, err := c.listNamespaces()
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	var namespacePodList []*NamespacePod
   101  	for _, ns := range namespaceList.Items {
   102  		pods, err := c.client.CoreV1().Pods(ns.Name).List(context.TODO(), metav1.ListOptions{})
   103  		if err != nil {
   104  			return nil, errors.Wrapf(err, "failed to get all namespace pods")
   105  		}
   106  		namespacePod := NamespacePod{
   107  			Namespace: ns,
   108  			PodList:   pods,
   109  		}
   110  		namespacePodList = append(namespacePodList, &namespacePod)
   111  	}
   112  
   113  	return namespacePodList, nil
   114  }
   115  
   116  func (c *Client) ListAllNamespacesSvcs() ([]*NamespaceSvc, error) {
   117  	namespaceList, err := c.listNamespaces()
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	var namespaceSvcList []*NamespaceSvc
   122  	for _, ns := range namespaceList.Items {
   123  		svcs, err := c.client.CoreV1().Services(ns.Name).List(context.TODO(), metav1.ListOptions{})
   124  		if err != nil {
   125  			return nil, errors.Wrapf(err, "failed to get all namespace pods")
   126  		}
   127  		namespaceSvc := NamespaceSvc{
   128  			Namespace:   ns,
   129  			ServiceList: svcs,
   130  		}
   131  		namespaceSvcList = append(namespaceSvcList, &namespaceSvc)
   132  	}
   133  	return namespaceSvcList, nil
   134  }
   135  
   136  func (c *Client) GetEndpointsList(namespace string) (*v1.EndpointsList, error) {
   137  	endpointsList, err := c.client.CoreV1().Endpoints(namespace).List(context.TODO(), metav1.ListOptions{})
   138  	if err != nil {
   139  		return nil, errors.Wrapf(err, "failed to get endpoint in namespace %s", namespace)
   140  	}
   141  	return endpointsList, nil
   142  }