Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(platform): reduce apiserver locaion input #1694

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/platform/registry/cluster/storage/cronhpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
platformv1 "tkestack.io/tke/api/platform/v1"

"tkestack.io/tke/api/platform"
"tkestack.io/tke/pkg/platform/util"
Expand Down Expand Up @@ -74,7 +75,13 @@ func (r *CronHPAREST) Connect(ctx context.Context, clusterName string, opts runt
}
proxyOpts := opts.(*platform.CronHPAProxyOptions)

location, transport, token, err := util.APIServerLocationByCluster(ctx, cluster, r.platformClient)
clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
if err != nil {
return nil, err
}

location, transport, token, err := util.APIServerLocationByCluster(ctx, clusterv1)
if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/platform/registry/cluster/storage/csioperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
platformv1 "tkestack.io/tke/api/platform/v1"

"tkestack.io/tke/api/platform"
"tkestack.io/tke/pkg/platform/util"
Expand Down Expand Up @@ -70,7 +71,13 @@ func (r *CSIREST) Connect(ctx context.Context, clusterName string, opts runtime.
}
proxyOpts := opts.(*platform.CSIProxyOptions)

location, transport, token, err := util.APIServerLocationByCluster(ctx, cluster, r.platformClient)
clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
if err != nil {
return nil, err
}

location, transport, token, err := util.APIServerLocationByCluster(ctx, clusterv1)
if err != nil {
return nil, err
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/platform/registry/cluster/storage/tappcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,22 @@ func (r *TappControllerREST) Connect(ctx context.Context, clusterName string, op
}
proxyOpts := opts.(*platform.TappControllerProxyOptions)

location, transport, token, err := util.APIServerLocationByCluster(ctx, cluster, r.platformClient)
clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
if err != nil {
return nil, err
}
provider, err := clusterprovider.GetProvider(cluster.Spec.Type)

location, transport, token, err := util.APIServerLocationByCluster(ctx, clusterv1)
if err != nil {
return nil, err
}

username, _ := authentication.UsernameAndTenantID(ctx)
clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
provider, err := clusterprovider.GetProvider(cluster.Spec.Type)
if err != nil {
return nil, err
}

username, _ := authentication.UsernameAndTenantID(ctx)
config, err := provider.GetRestConfig(ctx, clusterv1, username)
if err != nil {
return nil, err
Expand Down
18 changes: 9 additions & 9 deletions pkg/platform/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,20 @@ func BuildExternalClientSetWithName(ctx context.Context, platformClient platform
return clientset, nil
}

func clusterAddress(cluster *platform.Cluster) (*platform.ClusterAddress, error) {
addrs := make(map[platform.AddressType][]platform.ClusterAddress)
func clusterAddress(cluster *platformv1.Cluster) (*platformv1.ClusterAddress, error) {
addrs := make(map[platformv1.AddressType][]platformv1.ClusterAddress)
for _, one := range cluster.Status.Addresses {
addrs[one.Type] = append(addrs[one.Type], one)
}

var address *platform.ClusterAddress
if len(addrs[platform.AddressInternal]) != 0 {
address = &addrs[platform.AddressInternal][rand.Intn(len(addrs[platform.AddressInternal]))]
} else if len(addrs[platform.AddressAdvertise]) != 0 {
address = &addrs[platform.AddressAdvertise][rand.Intn(len(addrs[platform.AddressAdvertise]))]
var address *platformv1.ClusterAddress
if len(addrs[platformv1.AddressInternal]) != 0 {
address = &addrs[platformv1.AddressInternal][rand.Intn(len(addrs[platformv1.AddressInternal]))]
} else if len(addrs[platformv1.AddressAdvertise]) != 0 {
address = &addrs[platformv1.AddressAdvertise][rand.Intn(len(addrs[platformv1.AddressAdvertise]))]
} else {
if len(addrs[platform.AddressReal]) != 0 {
address = &addrs[platform.AddressReal][rand.Intn(len(addrs[platform.AddressReal]))]
if len(addrs[platformv1.AddressReal]) != 0 {
address = &addrs[platformv1.AddressReal][rand.Intn(len(addrs[platformv1.AddressReal]))]
}
}
if address == nil {
Expand Down
20 changes: 10 additions & 10 deletions pkg/platform/util/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ import (

// APIServerLocationByCluster returns a URL and transport which one can use to
// send traffic for the specified cluster api server.
func APIServerLocationByCluster(ctx context.Context, cluster *platform.Cluster, platformClient platforminternalclient.PlatformInterface) (*url.URL, http.RoundTripper, string, error) {
func APIServerLocationByCluster(ctx context.Context, cluster *platformv1.Cluster) (*url.URL, http.RoundTripper, string, error) {
username, tenantID := authentication.UsernameAndTenantID(ctx)
if len(tenantID) > 0 && cluster.Spec.TenantID != tenantID {
return nil, nil, "", errors.NewNotFound(platform.Resource("clusters"), cluster.ObjectMeta.Name)
}
if cluster.Status.Phase != platform.ClusterRunning {
if cluster.Status.Phase != platformv1.ClusterRunning {
return nil, nil, "", errors.NewServiceUnavailable(fmt.Sprintf("cluster %s status is abnormal", cluster.ObjectMeta.Name))
}

Expand All @@ -58,13 +58,7 @@ func APIServerLocationByCluster(ctx context.Context, cluster *platform.Cluster,
return nil, nil, "", errors.NewInternalError(err)
}

clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}

restconfig, err := provider.GetRestConfig(ctx, clusterv1, username)
restconfig, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
Expand Down Expand Up @@ -106,7 +100,13 @@ func APIServerLocation(ctx context.Context, platformClient platforminternalclien
return nil, nil, "", err
}

location, transport, token, err := APIServerLocationByCluster(ctx, cluster, platformClient)
clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}

location, transport, token, err := APIServerLocationByCluster(ctx, clusterv1)
if err != nil {
return nil, nil, "", err
}
Expand Down