Skip to content

Commit

Permalink
feat(cluster): support path in address
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChenglong authored and tke-robot committed Jun 28, 2020
1 parent 1210c09 commit bbb8cb6
Show file tree
Hide file tree
Showing 10 changed files with 427 additions and 349 deletions.
8 changes: 7 additions & 1 deletion api/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/platform/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ type ClusterAddress struct {
// The cluster address.
Host string
Port int32
Path string
}

// +genclient
Expand Down
714 changes: 377 additions & 337 deletions api/platform/v1/generated.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/platform/v1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/platform/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ type ClusterAddress struct {
// The cluster address.
Host string `json:"host" protobuf:"bytes,2,opt,name=host"`
Port int32 `json:"port" protobuf:"varint,3,name=port"`
Path string `json:"path" protobuf:"bytes,4,opt,name=path"`
}

// +genclient
Expand Down
2 changes: 2 additions & 0 deletions api/platform/v1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion pkg/platform/provider/imported/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -76,7 +77,15 @@ func ValidatClusterAddresses(addresses []platform.ClusterAddress, fldPath *field
for _, msg := range validation.IsValidPortNum(int(address.Port)) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("port"), address.Port, msg))
}
err := utilvalidation.IsHTTPSReachle(address.Host, address.Port, 5*time.Second)
if address.Path != "" && !strings.HasPrefix(address.Path, "/") {
allErrs = append(allErrs, field.Invalid(fldPath.Child("path"), address.Path, "must start by `/`"))
}

url := fmt.Sprintf("https://%s:%d", address.Host, address.Port)
if address.Path != "" {
url = fmt.Sprintf("%s%s", url, address.Path)
}
err := utilvalidation.IsValiadURL(url, 5*time.Second)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, address, err.Error()))
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/platform/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"math/rand"
"net"
"net/http"
"path"
"strings"
"time"

Expand Down Expand Up @@ -528,6 +529,20 @@ func BuildClientSet(ctx context.Context, cluster *platform.Cluster, credential *

// ClusterHost returns host and port for kube-apiserver of cluster.
func ClusterHost(cluster *platform.Cluster) (string, error) {
address, err := ClusterAddress(cluster)
if err != nil {
return "", err
}

result := fmt.Sprintf("%s:%d", address.Host, address.Port)
if address.Path != "" {
result = path.Join(result, address.Path)
}

return result, nil
}

func ClusterAddress(cluster *platform.Cluster) (*platform.ClusterAddress, error) {
addrs := make(map[platform.AddressType][]platform.ClusterAddress)
for _, one := range cluster.Status.Addresses {
addrs[one.Type] = append(addrs[one.Type], one)
Expand All @@ -543,12 +558,11 @@ func ClusterHost(cluster *platform.Cluster) (string, error) {
address = &addrs[platform.AddressReal][rand.Intn(len(addrs[platform.AddressReal]))]
}
}

if address == nil {
return "", pkgerrors.New("no valid address for the cluster")
return nil, pkgerrors.New("no valid address for the cluster")
}

return fmt.Sprintf("%s:%d", address.Host, address.Port), nil
return address, nil
}

// ClusterV1Host returns host and port for kube-apiserver of versioned cluster resource.
Expand Down
11 changes: 8 additions & 3 deletions pkg/platform/util/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net/http"
"net/url"
"path"

"tkestack.io/tke/pkg/apiserver/authentication"
"tkestack.io/tke/pkg/platform/apiserver/filter"
Expand Down Expand Up @@ -63,7 +64,7 @@ func APIServerLocationByCluster(ctx context.Context, cluster *platform.Cluster,
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
host, err := ClusterHost(cluster)
address, err := ClusterAddress(cluster)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
Expand All @@ -72,12 +73,16 @@ func APIServerLocationByCluster(ctx context.Context, cluster *platform.Cluster,
if clusterCredential.Token != nil {
token = *clusterCredential.Token
}
urlPath := requestInfo.Path
if address.Path != "" {
urlPath = path.Join(address.Path, requestInfo.Path)
}

// Otherwise, return the requested scheme and port, and the proxy transport
return &url.URL{
Scheme: "https",
Host: host,
Path: requestInfo.Path,
Host: fmt.Sprintf("%v:%v", address.Host, address.Port),
Path: urlPath,
}, transport, token, nil
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/util/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package validation

import (
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
Expand All @@ -29,8 +28,8 @@ import (
"tkestack.io/tke/pkg/util/ipallocator"
)

// IsHTTPSReachle tests that https://host:port is reachble in timeout.
func IsHTTPSReachle(host string, port int32, timeout time.Duration) error {
// IsValiadURL tests that https://host:port is reachble in timeout.
func IsValiadURL(url string, timeout time.Duration) error {
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Expand All @@ -41,7 +40,6 @@ func IsHTTPSReachle(host string, port int32, timeout time.Duration) error {
},
}

url := fmt.Sprintf("https://%s:%d", host, port)
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
Expand Down

0 comments on commit bbb8cb6

Please sign in to comment.