in cmd/memberagent/main.go [184:272]
func buildHubConfig(hubURL string, useCertificateAuth bool, tlsClientInsecure bool) (*rest.Config, error) {
var hubConfig = &rest.Config{
Host: hubURL,
}
if useCertificateAuth {
keyFilePath := os.Getenv("IDENTITY_KEY")
certFilePath := os.Getenv("IDENTITY_CERT")
if keyFilePath == "" {
err := errors.New("identity key file path cannot be empty")
klog.ErrorS(err, "Failed to retrieve identity key")
return nil, err
}
if certFilePath == "" {
err := errors.New("identity certificate file path cannot be empty")
klog.ErrorS(err, "Failed to retrieve identity certificate")
return nil, err
}
hubConfig.TLSClientConfig.CertFile = certFilePath
hubConfig.TLSClientConfig.KeyFile = keyFilePath
} else {
tokenFilePath := os.Getenv("CONFIG_PATH")
if tokenFilePath == "" {
err := errors.New("hub token file path cannot be empty if CA auth not used")
klog.ErrorS(err, "Failed to retrieve token file")
return nil, err
}
err := retry.OnError(retry.DefaultRetry, func(e error) bool {
return true
}, func() error {
// Stat returns file info. It will return
// an error if there is no file.
_, err := os.Stat(tokenFilePath)
return err
})
if err != nil {
klog.ErrorS(err, "Failed to retrieve token file from the path %s", tokenFilePath)
return nil, err
}
hubConfig.BearerTokenFile = tokenFilePath
}
hubConfig.TLSClientConfig.Insecure = tlsClientInsecure
if !tlsClientInsecure {
caBundle, ok := os.LookupEnv("CA_BUNDLE")
if ok && caBundle == "" {
err := errors.New("environment variable CA_BUNDLE should not be empty")
klog.ErrorS(err, "Failed to validate system variables")
return nil, err
}
hubCA, ok := os.LookupEnv("HUB_CERTIFICATE_AUTHORITY")
if ok && hubCA == "" {
err := errors.New("environment variable HUB_CERTIFICATE_AUTHORITY should not be empty")
klog.ErrorS(err, "Failed to validate system variables")
return nil, err
}
if caBundle != "" && hubCA != "" {
err := errors.New("environment variables CA_BUNDLE and HUB_CERTIFICATE_AUTHORITY should not be set at same time")
klog.ErrorS(err, "Failed to validate system variables")
return nil, err
}
if caBundle != "" {
hubConfig.TLSClientConfig.CAFile = caBundle
} else if hubCA != "" {
caData, err := base64.StdEncoding.DecodeString(hubCA)
if err != nil {
klog.ErrorS(err, "Failed to decode hub cluster certificate authority data")
return nil, err
}
hubConfig.TLSClientConfig.CAData = caData
}
}
// Sometime the hub cluster need additional http header for authentication or authorization.
// the "HUB_KUBE_HEADER" to allow sending custom header to hub's API Server for authentication and authorization.
if header, ok := os.LookupEnv("HUB_KUBE_HEADER"); ok {
r := textproto.NewReader(bufio.NewReader(strings.NewReader(header)))
h, err := r.ReadMIMEHeader()
if err != nil && !errors.Is(err, io.EOF) {
klog.ErrorS(err, "Failed to parse HUB_KUBE_HEADER %q", header)
return nil, err
}
hubConfig.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return httpclient.NewCustomHeadersRoundTripper(http.Header(h), rt)
}
}
return hubConfig, nil
}