-
Notifications
You must be signed in to change notification settings - Fork 84
/
config.go
104 lines (85 loc) · 3.16 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package kubernetes
import (
"fmt"
"net"
"os"
"time"
"github.com/zalando/skipper/secrets"
)
// Config holds the common attributes that can be passed to a
// Kubernetes client on initialization.
//
// Mostly copied from https://github.com/kubernetes/client-go/blob/master/rest/config.go
type Config struct {
// BaseURL must be a URL to the base of the apiserver.
BaseURL string
// Server requires Bearer authentication. This client will not
// attempt to use refresh tokens for an OAuth2 flow.
// TODO: demonstrate an OAuth2 compatible client.
TokenProvider secrets.SecretsProvider
// TLSClientConfig contains settings to enable transport layer
// security
TLSClientConfig
// Server should be accessed without verifying the TLS
// certificate. For testing only.
Insecure bool
// UserAgent is an optional field that specifies the caller of
// this request.
UserAgent string
// The maximum length of time to wait before giving up on a
// server request. A value of zero means no timeout.
Timeout time.Duration
}
// TLSClientConfig contains settings to enable transport layer security
type TLSClientConfig struct {
// Trusted root certificates for server
CAFile string
}
const (
serviceAccountDir = "/var/run/secrets/kubernetes.io/serviceaccount/"
serviceAccountTokenKey = "token"
serviceAccountRootCAKey = "ca.crt"
serviceHostEnvVar = "KUBERNETES_SERVICE_HOST"
servicePortEnvVar = "KUBERNETES_SERVICE_PORT"
)
var serviceAccountLocator = defaultServiceAccountLocator
func defaultServiceAccountLocator() string { return serviceAccountDir }
// InClusterConfig creates a configuration for the Kubernetes Adapter
// that will communicate with the API server using TLS and
// authenticate with the cluster provide Bearer token.
// The environment should contain variables KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT.
// The CA certificate and Bearer token will also be taken from the Kubernetes environment.
func InClusterConfig() (*Config, error) {
host, port := os.Getenv(serviceHostEnvVar), os.Getenv(servicePortEnvVar)
if len(host) == 0 || len(port) == 0 {
return nil, ErrMissingKubernetesEnv
}
dir := serviceAccountLocator()
tokenProvider := secrets.NewSecretPaths(time.Minute)
err := tokenProvider.Add(dir + serviceAccountTokenKey)
if err != nil {
return nil, fmt.Errorf("error when adding token file to token provider: %w", err)
}
rootCAFile := dir + serviceAccountRootCAKey
if _, err := os.Stat(rootCAFile); os.IsNotExist(err) {
return nil, err
}
return &Config{
BaseURL: "https://" + net.JoinHostPort(host, port),
UserAgent: "kube-ingress-aws-controller",
TokenProvider: tokenProvider,
Timeout: 10 * time.Second,
TLSClientConfig: TLSClientConfig{CAFile: rootCAFile},
}, nil
}
// InsecureConfig creates a configuration for the Kubernetes Adapter
// that won't use any encryption or authentication mechanisms to
// communicate with the API Server. This should be used only for local
// development, as usually provided by the kubectl proxy
func InsecureConfig(apiServerBaseURL string) *Config {
return &Config{
BaseURL: apiServerBaseURL,
UserAgent: defaultControllerUserAgent,
Timeout: 10 * time.Second,
}
}