Skip to content

Commit

Permalink
Update k8s provider detector
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 17, 2024
1 parent 82f659e commit 3f70abe
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 73 deletions.
1 change: 1 addition & 0 deletions api/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
HostingProviderAzure HostingProvider = "Azure"
HostingProviderDigitalOcean HostingProvider = "DigitalOcean"
HostingProviderGoogleCloud HostingProvider = "GoogleCloud"
HostingProviderExoscale HostingProvider = "Exoscale"
HostingProviderLinode HostingProvider = "Linode"
HostingProviderPacket HostingProvider = "Packet"
HostingProviderScaleway HostingProvider = "Scaleway"
Expand Down
107 changes: 107 additions & 0 deletions cluster/detector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright AppsCode Inc. and Contributors
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 cluster

import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"strings"
"time"

kmapi "kmodules.xyz/client-go/api/v1"

"k8s.io/client-go/rest"
)

const (
aksDomain = ".azmk8s.io"
eksDomain = ".eks.amazonaws.com"
gkeDomain = ".gke.com"
exoscaleDomain = ".exo.io"
doDomain = ".k8s.ondigitalocean.com"
lkeDomain = ".linodelke.net"
scalewayDomain = ".scw.cloud"
vultrDomain = ".vultr-k8s.com"
)

func APIServerCertificate(cfg *rest.Config) (*x509.Certificate, error) {
err := rest.LoadTLSFiles(cfg)
if err != nil {
return nil, err
}

// create ca cert pool
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM(cfg.CAData)
if !ok {
return nil, fmt.Errorf("can't append caCert to caCertPool")
}

tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{RootCAs: caCertPool},
}
client := &http.Client{Transport: tr}

resp, err := client.Get(cfg.Host)
if err != nil {
return nil, err
}
for i := range resp.TLS.VerifiedChains {
return resp.TLS.VerifiedChains[i][0], nil
}
return nil, fmt.Errorf("no cert found")
}

func DetectProvider(cfg *rest.Config) (kmapi.HostingProvider, error) {
crt, err := APIServerCertificate(cfg)
if err != nil {
return "", err
}

for _, host := range crt.DNSNames {
if strings.HasSuffix(host, eksDomain) {
return kmapi.HostingProviderAWS, nil
} else if strings.HasSuffix(host, aksDomain) {
return kmapi.HostingProviderAzure, nil
} else if strings.HasSuffix(host, doDomain) {
return kmapi.HostingProviderDigitalOcean, nil
} else if strings.HasSuffix(host, exoscaleDomain) {
return kmapi.HostingProviderExoscale, nil
} else if strings.HasSuffix(host, gkeDomain) {
return kmapi.HostingProviderGoogleCloud, nil
} else if strings.HasSuffix(host, lkeDomain) {
return kmapi.HostingProviderLinode, nil
} else if strings.HasSuffix(host, scalewayDomain) {
return kmapi.HostingProviderScaleway, nil
} else if strings.HasSuffix(host, vultrDomain) {
return kmapi.HostingProviderVultr, nil
}
}
return "", nil
}
22 changes: 1 addition & 21 deletions meta/cloud.go → cluster/host_detector.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
/*
Copyright AppsCode Inc. and Contributors
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 meta
package cluster

import (
"crypto/x509"
Expand Down Expand Up @@ -67,8 +51,6 @@ func TestGKE() (string, error) {
return v.(string), nil
}

const aksDomain = ".azmk8s.io"

func TestAKS(cert *x509.Certificate) (string, error) {
for _, host := range cert.DNSNames {
if strings.HasSuffix(host, aksDomain) && isAKS() == nil {
Expand Down Expand Up @@ -98,8 +80,6 @@ func isAKS() error {
return nil
}

const eksDomain = ".eks.amazonaws.com"

func TestEKS(cert *x509.Certificate) (string, error) {
for _, host := range cert.DNSNames {
if strings.HasSuffix(host, eksDomain) && isEKS() == nil {
Expand Down
12 changes: 6 additions & 6 deletions core/v1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,16 @@ func DetectTopology(ctx context.Context, mc metadata.Interface) (*Topology, erro
labels := m.GetLabels()

if first {
if _, ok := labels[core.LabelZoneRegionStable]; ok {
topology.LabelRegion = core.LabelZoneRegionStable
if _, ok := labels[core.LabelTopologyRegion]; ok {
topology.LabelRegion = core.LabelTopologyRegion
} else {
topology.LabelRegion = core.LabelZoneRegion
topology.LabelRegion = core.LabelFailureDomainBetaRegion
}

if _, ok := labels[core.LabelZoneFailureDomainStable]; ok {
topology.LabelZone = core.LabelZoneFailureDomainStable
if _, ok := labels[core.LabelTopologyZone]; ok {
topology.LabelZone = core.LabelTopologyZone
} else {
topology.LabelZone = core.LabelZoneFailureDomain
topology.LabelZone = core.LabelFailureDomainBetaZone
}

if _, ok := labels[core.LabelInstanceTypeStable]; ok {
Expand Down
44 changes: 0 additions & 44 deletions meta/incluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ limitations under the License.
package meta

import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"

core "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
)

// xref: https://kubernetes.io/docs/concepts/workloads/pods/downward-api/
Expand Down Expand Up @@ -71,44 +65,6 @@ func PossiblyInCluster() bool {
err == nil && !fi.IsDir()
}

func APIServerCertificate(cfg *rest.Config) (*x509.Certificate, error) {
err := rest.LoadTLSFiles(cfg)
if err != nil {
return nil, err
}

// create ca cert pool
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM(cfg.CAData)
if !ok {
return nil, fmt.Errorf("can't append caCert to caCertPool")
}

tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{RootCAs: caCertPool},
}
client := &http.Client{Transport: tr}

resp, err := client.Get(cfg.Host)
if err != nil {
return nil, err
}
for i := range resp.TLS.VerifiedChains {
return resp.TLS.VerifiedChains[i][0], nil
}
return nil, fmt.Errorf("no cert found")
}

func ClusterDomain() string {
defaultDomain := func() string {
if v, ok := os.LookupEnv("KUBE_CLUSTER_DOMAIN"); ok {
Expand Down
5 changes: 3 additions & 2 deletions tools/clientcmd/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"os"

"kmodules.xyz/client-go/cluster"
"kmodules.xyz/client-go/meta"

"github.com/pkg/errors"
Expand Down Expand Up @@ -111,10 +112,10 @@ func Fix(cfg *rest.Config) *rest.Config {
in(cfg.Host, "https://"+net.JoinHostPort(host, port), "https://kubernetes.default.svc", "https://kubernetes.default.svc:443") {
// uses service ip or cluster dns

if cert, err := meta.APIServerCertificate(cfg); err == nil {
if cert, err := cluster.APIServerCertificate(cfg); err == nil {
// kube-apiserver cert found

if host, err := meta.TestAKS(cert); err == nil {
if host, err := cluster.TestAKS(cert); err == nil {
// AKS cluster

h := "https://" + host
Expand Down

0 comments on commit 3f70abe

Please sign in to comment.