mindxdl--common--k8s_utils.go
// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.
// Package common define common utils
package common
import (
"context"
"fmt"
"os"
"sync"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"volcano.sh/apis/pkg/client/clientset/versioned"
"huawei.com/npu-exporter/hwlog"
"huawei.com/npu-exporter/utils"
)
const (
maxLen = 200
// PVCVolumeName the volume name of storage pvc
PVCVolumeName = "platform"
// StoragePVCName the PVC name of storage
StoragePVCName = "storage-pvc"
// PublicPVCName the PVC name of public dir
PublicPVCName = "storage-pvc-public"
)
var (
k8sClientOnce sync.Once
vcClientOnce sync.Once
kubeClientSet *kubernetes.Clientset
vcClientSet *versioned.Clientset
// Kubeconfig the k8s master config file
Kubeconfig string
)
// K8sBaseClient base k8s client for other component to extend
type K8sBaseClient struct {
Clientset *kubernetes.Clientset
VcClientSet *versioned.Clientset
NameSpace string
}
// K8SController is a controller for k8s client
type K8SController struct {
KubeClientSet kubernetes.Interface
}
// K8sClient Get the internal k8s client of the cluster
func K8sClient(kubeconfig string) (*kubernetes.Clientset, error) {
k8sClientOnce.Do(func() {
if kubeconfig == "" {
configPath := os.Getenv("KUBECONFIG")
if len(configPath) > maxLen {
hwlog.RunLog.Fatal("the path is too long")
}
kubeconfig = configPath
}
cfgPath, err := utils.CheckPath(kubeconfig)
if err != nil {
hwlog.RunLog.Fatal(err)
}
config, err := clientcmd.BuildConfigFromFlags("", cfgPath)
if err != nil {
hwlog.RunLog.Fatal(err)
}
// Create a new k8sClientSet based on the specified config using the current context
kubeClientSet, err = kubernetes.NewForConfig(config)
if err != nil {
hwlog.RunLog.Fatal(err)
}
})
return kubeClientSet, nil
}
// NewK8sController init k8s controller client
func NewK8sController(kc *kubernetes.Clientset) *K8SController {
return &K8SController{
KubeClientSet: kc,
}
}
// VcClient Get the internal volcano client of the cluster
func VcClient(vcConfig string) (*versioned.Clientset, error) {
vcClientOnce.Do(func() {
if vcConfig == "" {
configPath := os.Getenv("KUBECONFIG")
if len(configPath) > maxLen {
hwlog.RunLog.Fatal("the path is too long")
}
vcConfig = configPath
}
cfgPath, err := utils.CheckPath(vcConfig)
if err != nil {
hwlog.RunLog.Fatal(err)
}
config, err := clientcmd.BuildConfigFromFlags("", cfgPath)
if err != nil {
hwlog.RunLog.Fatal(err)
}
// Create a new vcClientSet based on the specified config using the current context
vcClientSet, err = versioned.NewForConfig(config)
if err != nil {
hwlog.RunLog.Fatal(err)
}
})
return vcClientSet, nil
}
// GetDBConnectionPath get db connection path
func (handler *K8sBaseClient) GetDBConnectionPath(retryCount, timeInterval int,
secretName, namespace string) (string, error) {
for {
if retryCount < 0 {
return "", fmt.Errorf("the secret of db information with name(%s) cannot be find in namespace(%s)",
secretName, namespace)
}
secret, err := handler.GetDBSecret(secretName, namespace)
if err != nil {
retryCount--
hwlog.RunLog.Error(err)
time.Sleep(time.Duration(timeInterval) * time.Second)
continue
}
return fmt.Sprintf("%s", secret.Data[DBSecretConnectionPathKey]), nil
}
}
func (handler *K8sBaseClient) getDBSecret(name, namespace string) (*v1.Secret, error) {
secret, err := handler.Clientset.CoreV1().Secrets(namespace).Get(context.TODO(),
name, metav1.GetOptions{})
return secret, err
}
// GetDBSecret get db secret
func (handler *K8sBaseClient) GetDBSecret(name, namespace string) (*v1.Secret, error) {
secret, err := handler.getDBSecret(name, namespace)
if err != nil {
return nil, err
}
if err = handler.validDBSecret(secret); err != nil {
return nil, err
}
return secret, nil
}
func (handler *K8sBaseClient) validDBSecret(secret *v1.Secret) error {
var err error
if secret.Data == nil {
err = fmt.Errorf("the secret data for connect to database is empty")
return err
}
if _, ok := secret.Data[DBSecretConnectionPathKey]; !ok {
err = fmt.Errorf("the %s field for connecting to the database in the secret is incorrect",
DBSecretConnectionPathKey)
return err
}
return nil
}
本文来自博客园,作者:易先讯,转载请注明原文链接:https://www.cnblogs.com/gongxianjin/p/16683168.html