* Copyright (c) 2024 Huawei Technologies Co., Ltd.
* openFuyao is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package cluster
import (
"context"
"github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type OperationsInterface interface {
JoinHost(ctx context.Context)
Join(ctx context.Context, opts JoinOptions) error
Unjoin(ctx context.Context, opts UnjoinOptions) error
Edit(ctx context.Context, opts EditOptions) error
Inspect(ctx context.Context, opts InspectOptions) (*ListOfCluster, error)
Authorize(ctx context.Context, opts AuthorizeOptions) error
}
type OperationClient struct {
MgrClient client.Client
KarmadaClient *kubernetes.Clientset
KarmadaVersionedClient *versioned.Clientset
}
func NewClusteClient(mgr client.Client, kClient *kubernetes.Clientset,
kVClient *versioned.Clientset) OperationsInterface {
return &OperationClient{
MgrClient: mgr,
KarmadaClient: kClient,
KarmadaVersionedClient: kVClient,
}
}
func Transfer2JoinOpts(clusterinfo InfoOfCluster) JoinOptions {
labels := deepcopyLabels(clusterinfo.ClusterLabels)
return JoinOptions{
ClusterName: clusterinfo.Name,
Kubeconfig: clusterinfo.Kubeconfig,
KubeconfigPath: clusterinfo.KubeconfigPath,
ClusterLabels: labels,
}
}
func Transfer2UnjoinOpts(clusterTag string) UnjoinOptions {
return UnjoinOptions{
ClusterName: clusterTag,
}
}
func Transfer2InspectOpts(clusterTag string) InspectOptions {
return InspectOptions{
ClusterName: clusterTag,
}
}
func Transfer2EditOpts(clusterTag string, tags TagOfCluster) EditOptions {
labels := deepcopyLabels(tags.ClusterLabels)
return EditOptions{
ClusterName: clusterTag,
ClusterLabels: labels,
}
}
func Transfer2AuthorizeOpts(identityInfo IdentityCollection) AuthorizeOptions {
var users []*IdentityDescriptor
for _, user := range identityInfo.UserInfo {
users = append(users, user)
}
return AuthorizeOptions{
Users: users,
}
}
func deepcopyLabels(data map[string]string) map[string]string {
labels := make(map[string]string)
for key, value := range data {
labels[key] = value
}
return labels
}