...
1
16
17 package discovery
18
19 import (
20 "net/url"
21
22 "github.com/pkg/errors"
23
24 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
25 "k8s.io/klog/v2"
26
27 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
28 kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
29 "k8s.io/kubernetes/cmd/kubeadm/app/discovery/file"
30 "k8s.io/kubernetes/cmd/kubeadm/app/discovery/https"
31 "k8s.io/kubernetes/cmd/kubeadm/app/discovery/token"
32 kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
33 )
34
35
36 const TokenUser = "tls-bootstrap-token-user"
37
38
39
40 func For(cfg *kubeadmapi.JoinConfiguration) (*clientcmdapi.Config, error) {
41
42
43 config, err := DiscoverValidatedKubeConfig(cfg)
44 if err != nil {
45 return nil, errors.Wrap(err, "couldn't validate the identity of the API Server")
46 }
47
48
49
50
51 if len(cfg.Discovery.TLSBootstrapToken) != 0 {
52 klog.V(1).Info("[discovery] Using provided TLSBootstrapToken as authentication credentials for the join process")
53
54 _, clusterinfo := kubeconfigutil.GetClusterFromKubeConfig(config)
55 return kubeconfigutil.CreateWithToken(
56 clusterinfo.Server,
57 kubeadmapiv1.DefaultClusterName,
58 TokenUser,
59 clusterinfo.CertificateAuthorityData,
60 cfg.Discovery.TLSBootstrapToken,
61 ), nil
62 }
63
64
65 if kubeconfigutil.HasAuthenticationCredentials(config) {
66 return config, nil
67 }
68
69
70 return nil, errors.New("couldn't find authentication credentials for the TLS boostrap process. Please use Token discovery, a discovery file with embedded authentication credentials or a discovery file without authentication credentials but with the TLSBootstrapToken flag")
71 }
72
73
74 func DiscoverValidatedKubeConfig(cfg *kubeadmapi.JoinConfiguration) (*clientcmdapi.Config, error) {
75 timeout := cfg.Timeouts.Discovery.Duration
76 switch {
77 case cfg.Discovery.File != nil:
78 kubeConfigPath := cfg.Discovery.File.KubeConfigPath
79 if isHTTPSURL(kubeConfigPath) {
80 return https.RetrieveValidatedConfigInfo(kubeConfigPath, timeout)
81 }
82 return file.RetrieveValidatedConfigInfo(kubeConfigPath, timeout)
83 case cfg.Discovery.BootstrapToken != nil:
84 return token.RetrieveValidatedConfigInfo(&cfg.Discovery, timeout)
85 default:
86 return nil, errors.New("couldn't find a valid discovery configuration")
87 }
88 }
89
90
91 func isHTTPSURL(s string) bool {
92 u, err := url.Parse(s)
93 return err == nil && u.Scheme == "https"
94 }
95
View as plain text