1 Star 0 Fork 0

kubeship/prism

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
reader.go 4.07 KB
一键复制 编辑 原始数据 按行查看 历史
raikyou 提交于 2024-07-02 11:42 . 11111
/*
Copyright 2022 The KubeVela Authors.
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 v1alpha1
import (
"context"
"fmt"
"strings"
clustergatewayv1alpha1 "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/v1alpha1"
clustergatewaycommon "github.com/oam-dev/cluster-gateway/pkg/common"
clustergatewayconfig "github.com/oam-dev/cluster-gateway/pkg/config"
corev1 "k8s.io/api/core/v1"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ocmclusterv1 "open-cluster-management.io/api/cluster/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"gitee.com/kubeship/pkg/util/apiserver"
"gitee.com/kubeship/pkg/util/singleton"
)
// Get finds a resource in the storage by name and returns it.
func (in *Cluster) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return NewClusterClient(singleton.KubeClient.Get()).Get(ctx, name)
}
// List selects resources in the storage which match to the selector. 'options' can be nil.
func (in *Cluster) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
return NewClusterClient(singleton.KubeClient.Get()).List(ctx, apiserver.NewMatchingLabelSelectorFromInternalVersionListOptions(options))
}
func extractLabels(labels map[string]string) map[string]string {
_labels := make(map[string]string)
for k, v := range labels {
if !strings.HasPrefix(k, clustergatewayconfig.MetaApiGroupName) {
_labels[k] = v
}
}
return _labels
}
func newCluster(obj client.Object) *Cluster {
cluster := &Cluster{}
cluster.SetGroupVersionKind(ClusterGroupVersionKind)
if obj != nil {
cluster.SetName(obj.GetName())
cluster.SetCreationTimestamp(obj.GetCreationTimestamp())
cluster.SetLabels(extractLabels(obj.GetLabels()))
if annotations := obj.GetAnnotations(); annotations != nil {
cluster.Spec.Alias = annotations[AnnotationClusterAlias]
}
}
cluster.Spec.Accepted = true
cluster.Spec.Endpoint = ClusterBlankEndpoint
metav1.SetMetaDataLabel(&cluster.ObjectMeta, LabelClusterControlPlane, fmt.Sprintf("%t", obj == nil))
return cluster
}
// NewLocalCluster return the local cluster
func NewLocalCluster() *Cluster {
cluster := newCluster(nil)
cluster.SetName(ClusterLocalName)
cluster.Spec.CredentialType = CredentialTypeInternal
return cluster
}
// NewClusterFromSecret extract cluster from cluster secret
func NewClusterFromSecret(secret *corev1.Secret) (*Cluster, error) {
cluster := newCluster(secret)
cluster.Spec.Endpoint = string(secret.Data["endpoint"])
if metav1.HasLabel(secret.ObjectMeta, clustergatewaycommon.LabelKeyClusterEndpointType) {
cluster.Spec.Endpoint = secret.GetLabels()[clustergatewaycommon.LabelKeyClusterEndpointType]
}
if cluster.Spec.Endpoint == "" {
return nil, NewEmptyEndpointClusterSecretError()
}
if !metav1.HasLabel(secret.ObjectMeta, clustergatewaycommon.LabelKeyClusterCredentialType) {
return nil, NewEmptyCredentialTypeClusterSecretError()
}
cluster.Spec.CredentialType = clustergatewayv1alpha1.CredentialType(
secret.GetLabels()[clustergatewaycommon.LabelKeyClusterCredentialType])
return cluster, nil
}
// NewClusterFromManagedCluster extract cluster from ocm managed cluster
func NewClusterFromManagedCluster(managedCluster *ocmclusterv1.ManagedCluster) (*Cluster, error) {
if len(managedCluster.Spec.ManagedClusterClientConfigs) == 0 {
return nil, NewInvalidManagedClusterError()
}
cluster := newCluster(managedCluster)
cluster.Spec.Accepted = managedCluster.Spec.HubAcceptsClient
cluster.Spec.CredentialType = CredentialTypeOCMManagedCluster
return cluster, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kubeship/prism.git
git@gitee.com:kubeship/prism.git
kubeship
prism
prism
v0.0.1

搜索帮助