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"
|
2020-04-02 06:17:14 +03:00
|
|
|
"errors"
|
2019-12-29 17:49:38 +03:00
|
|
|
"fmt"
|
2019-12-26 17:29:30 +03:00
|
|
|
"net"
|
2023-10-04 01:47:49 +03:00
|
|
|
"net/http"
|
2019-12-26 17:29:30 +03:00
|
|
|
|
2023-10-04 01:47:49 +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) {
|
2020-04-02 06:17:14 +03:00
|
|
|
// 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.
|
2021-03-07 16:30:21 +03:00
|
|
|
if oc.Properties.NetworkProfile.APIServerPrivateEndpointIP == "" {
|
2020-04-02 06:17:14 +03:00
|
|
|
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)
|
|
|
|
|
2023-10-04 01:47:49 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:30:21 +03:00
|
|
|
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
|
|
|
}
|