ARO-RP/pkg/util/restconfig/restconfig.go

68 строки
2.0 KiB
Go
Исходник Обычный вид История

2019-10-19 00:59:21 +03:00
package restconfig
2019-12-17 04:16:50 +03:00
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
2019-10-19 00:59:21 +03:00
import (
2019-12-26 17:29:30 +03:00
"context"
"errors"
2019-12-29 17:49:38 +03:00
"fmt"
2019-12-26 17:29:30 +03:00
"net"
"net/http"
2019-12-26 17:29:30 +03:00
machnet "k8s.io/apimachinery/pkg/util/net"
2019-10-19 00:59:21 +03:00
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
2019-12-26 17:29:30 +03:00
"github.com/Azure/ARO-RP/pkg/api"
2020-09-18 18:30:33 +03:00
"github.com/Azure/ARO-RP/pkg/proxy"
2019-10-19 00:59:21 +03:00
)
// RestConfig returns the Kubernetes *rest.Config for a kubeconfig
2020-09-18 18:30:33 +03:00
func RestConfig(dialer proxy.Dialer, oc *api.OpenShiftCluster) (*rest.Config, error) {
// must not proceed if PrivateEndpointIP is not set. In
// k8s.io/client-go/transport/cache.go, k8s caches our transport, and it
// can't tell if data in the restconfig.Dial closure has changed. We don't
// want it to cache a transport that can never work.
if oc.Properties.NetworkProfile.APIServerPrivateEndpointIP == "" {
return nil, errors.New("privateEndpointIP is empty")
}
2020-03-20 03:39:51 +03:00
kubeconfig := oc.Properties.AROServiceKubeconfig
if kubeconfig == nil {
kubeconfig = oc.Properties.AdminKubeconfig
}
config, err := clientcmd.Load(kubeconfig)
2019-10-19 00:59:21 +03:00
if err != nil {
2020-03-20 03:39:51 +03:00
return nil, err
2019-10-19 00:59:21 +03:00
}
2019-12-26 17:29:30 +03:00
restconfig, err := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, err
}
2020-09-26 01:20:03 +03:00
restconfig.Dial = DialContext(dialer, oc)
// https://github.com/kubernetes/kubernetes/issues/118703#issuecomment-1595072383
// TODO: Revert or adapt when upstream fix is available
restconfig.Proxy = machnet.NewProxierWithNoProxyCIDR(http.ProxyFromEnvironment)
2020-09-26 01:20:03 +03:00
return restconfig, nil
}
func DialContext(dialer proxy.Dialer, oc *api.OpenShiftCluster) func(ctx context.Context, network, address string) (net.Conn, error) {
return func(ctx context.Context, network, address string) (net.Conn, error) {
2019-12-29 17:49:38 +03:00
if network != "tcp" {
return nil, fmt.Errorf("unimplemented network %q", network)
}
_, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
return dialer.DialContext(ctx, network, oc.Properties.NetworkProfile.APIServerPrivateEndpointIP+":"+port)
2019-12-26 17:29:30 +03:00
}
2019-10-19 00:59:21 +03:00
}