Skip to content

Commit

Permalink
Update V4.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ws410 committed Sep 9, 2023
1 parent e628258 commit cdd6ad2
Show file tree
Hide file tree
Showing 18 changed files with 1,864 additions and 12 deletions.
157 changes: 157 additions & 0 deletions cli/client/client_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package client

import (
"encoding/json"
"errors"
"fmt"
"reflect"

corev1 "k8s.io/api/core/v1"
k8string "k8s.io/utils/strings"

"huawei-csi-driver/cli/helper"
xuanwuV1 "huawei-csi-driver/client/apis/xuanwu/v1"
"huawei-csi-driver/utils/log"
)

// CommonCallHandler common call handler
type CommonCallHandler[T any] struct {
client KubernetesClient
}

// ListResult list query result
type ListResult[T any] struct {
Items []T `json:"items"`
}

// NewCommonCallHandler init common call handler
func NewCommonCallHandler[T any](client KubernetesClient) *CommonCallHandler[T] {
return &CommonCallHandler[T]{client: client}
}

// Create resource
func (r *CommonCallHandler[T]) Create(t T) error {
return r.commonOperateResource(t, Create)
}

// Update resource
func (r *CommonCallHandler[T]) Update(t T) error {
return r.commonOperateResource(t, Apply)
}

// QueryByName query resource by name
func (r *CommonCallHandler[T]) QueryByName(namespace, name string) (T, error) {
return commonQuery[T, T](r.client, namespace, name)
}

// QueryList query resource list
func (r *CommonCallHandler[T]) QueryList(namespace string, names ...string) ([]T, error) {
if len(names) == 1 {
t, err := r.QueryByName(namespace, names[0])
if err != nil {
return []T{}, err
}
return safeToArray(t), nil
}

result, err := commonQuery[ListResult[T], T](r.client, namespace, names...)
if err != nil {
return []T{}, err
}
return result.Items, nil
}

// DeleteByNames delete resource by names
func (r *CommonCallHandler[T]) DeleteByNames(namespace string, names ...string) error {
var qualifiedNames []string
resourceType, err := GetResourceTypeByT[T]()
if err != nil {
return err
}
for _, name := range names {
qualifiedNames = append(qualifiedNames, k8string.JoinQualifiedName(string(resourceType), name))
}
_, err = r.client.DeleteResourceByQualifiedNames(qualifiedNames, namespace)
return err
}

// commonQuery common query resource
// T is return Type
// R is Resource struct
func commonQuery[T any, R any](client KubernetesClient, namespace string, names ...string) (T, error) {
var t T
resourceType, err := GetResourceTypeByT[R]()
if err != nil {
return t, err
}
jsonBytes, err := client.GetResource(names, namespace, "json", resourceType)
if err != nil || len(jsonBytes) == 0 {
return t, err
}

if err := json.Unmarshal(jsonBytes, &t); err != nil {
return t, err
}

return t, nil
}

// commonQuery common query resource
// T is resource struct
func (r *CommonCallHandler[T]) commonOperateResource(t T, operateType string) error {
bytes, err := helper.StructToYAML(t)
if err != nil {
log.Errorf("%s resource failed, error: %v", operateType, err)
return err
}
return r.client.OperateResourceByYaml(string(bytes), operateType, false)
}

// GetResourceTypeByT get resource type
func GetResourceTypeByT[T any]() (ResourceType, error) {
var t T
resourceType := parseType(t)
if resourceType == "" {
return "", errors.New(fmt.Sprintf("Unsupported query type: %s", reflect.TypeOf(t).Name()))
}
return resourceType, nil
}

func parseType(target interface{}) ResourceType {
switch target.(type) {
case corev1.Secret:
return Secret
case corev1.ConfigMap:
return ConfigMap
case xuanwuV1.StorageBackendClaim:
return Storagebackendclaim
case xuanwuV1.StorageBackendContent:
return StoragebackendclaimContent
default:
return ""
}
}

func safeToArray[T any](t T) []T {
var emptyStruct T
if reflect.DeepEqual(t, emptyStruct) {
return []T{}
}
return []T{t}
}
2 changes: 1 addition & 1 deletion cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
//CliVersion oceanctl version
CliVersion = "v4.1.0"
CliVersion = "v4.1.1"

// DefaultMaxClientThreads default max client threads
DefaultMaxClientThreads = "30"
Expand Down
Loading

0 comments on commit cdd6ad2

Please sign in to comment.