github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/k8s/k8sclient.go (about)

     1  /*
     2  Copyright 2019 The OpenEBS 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 k8s
    18  
    19  import (
    20  	apis "github.com/openebs/node-disk-manager/api/v1alpha1"
    21  	"github.com/openebs/node-disk-manager/integration_tests/utils"
    22  	apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    23  	"k8s.io/client-go/rest"
    24  	"sigs.k8s.io/controller-runtime/pkg/manager"
    25  	"time"
    26  
    27  	//"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/client-go/kubernetes"
    29  	"k8s.io/client-go/tools/clientcmd"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  )
    32  
    33  const (
    34  	// Namespace is the default namespace
    35  	Namespace = "openebs"
    36  	// WaitDuration is the default wait duration
    37  	WaitDuration time.Duration = 5 * time.Second
    38  	// Running is the active/running status of pod
    39  	Running = "Running"
    40  )
    41  
    42  // K8sClient is the client used for etcd operations
    43  type K8sClient struct {
    44  	config        *rest.Config
    45  	ClientSet     *kubernetes.Clientset
    46  	APIextClient  *apiextensionsclient.Clientset
    47  	RunTimeClient client.Client
    48  }
    49  
    50  // GetClientSet generates the client-set from the config file on the host
    51  // Three clients are generated by the function:
    52  // 1. Client-set from client-go which is used for operations on pods/nodes and
    53  //    other first-class k8s objects. While using runtime client for this operations,
    54  //    the podList etc retrieved were not up-to-date
    55  // 2. Client from apiextensions which is used for CRUD operations on CRDs
    56  // 3. Runtime client from the controller-runtime which will be used for
    57  //    CRUD operations related to custom resources
    58  func GetClientSet() (K8sClient, error) {
    59  	clientSet := K8sClient{}
    60  	kubeConfigPath, err := utils.GetConfigPath()
    61  	if err != nil {
    62  		return clientSet, err
    63  	}
    64  	config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
    65  	if err != nil {
    66  		return clientSet, err
    67  	}
    68  	clientSet.config = config
    69  	// client-go clientSet
    70  	clientSet.ClientSet, err = kubernetes.NewForConfig(config)
    71  	if err != nil {
    72  		return clientSet, err
    73  	}
    74  
    75  	// client for creating CRDs
    76  	clientSet.APIextClient, err = apiextensionsclient.NewForConfig(config)
    77  	if err != nil {
    78  		return clientSet, err
    79  	}
    80  
    81  	// controller-runtime client
    82  	mgr, err := manager.New(config, manager.Options{Namespace: Namespace, MetricsBindAddress: "0"})
    83  	if err != nil {
    84  		return clientSet, err
    85  	}
    86  
    87  	// add to scheme
    88  	scheme := mgr.GetScheme()
    89  	if err = apis.AddToScheme(scheme); err != nil {
    90  		return clientSet, err
    91  	}
    92  
    93  	clientSet.RunTimeClient, err = client.New(config, client.Options{Scheme: scheme})
    94  	if err != nil {
    95  		return clientSet, err
    96  	}
    97  	return clientSet, nil
    98  }
    99  
   100  func (k *K8sClient) RegenerateClient() error {
   101  	// controller-runtime client
   102  	mgr, err := manager.New(k.config, manager.Options{Namespace: Namespace, MetricsBindAddress: "0"})
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	// add to scheme
   108  	scheme := mgr.GetScheme()
   109  	if err = apis.AddToScheme(scheme); err != nil {
   110  		return err
   111  	}
   112  
   113  	k.RunTimeClient, err = client.New(k.config, client.Options{Scheme: scheme})
   114  	return err
   115  }