@@ -55,25 +55,29 @@ var (
5555
5656// getPerNodeKubeconfig creates new kubeConfig, based on bootstrap, with new certDir
5757func getPerNodeKubeconfig (bootstrap * rest.Config , certDir string ) * rest.Config {
58- return & rest.Config {
59- Host : bootstrap .Host ,
60- APIPath : bootstrap .APIPath ,
61- ContentConfig : rest.ContentConfig {
62- AcceptContentTypes : "application/vnd.kubernetes.protobuf,application/json" ,
63- ContentType : "application/vnd.kubernetes.protobuf" ,
64- },
65- TLSClientConfig : rest.TLSClientConfig {
66- KeyFile : path .Join (certDir , certNamePrefix + "-current.pem" ),
67- CertFile : path .Join (certDir , certNamePrefix + "-current.pem" ),
68- CAData : bootstrap .TLSClientConfig .CAData ,
69- },
70- // Allow multus (especially in server mode) to make more concurrent requests
71- // to reduce client-side throttling
72- QPS : 50 ,
73- Burst : 50 ,
74- // Set the config timeout to one minute.
75- Timeout : time .Minute ,
76- }
58+ config := rest .CopyConfig (bootstrap )
59+ config .TLSClientConfig .CertFile = path .Join (certDir , certNamePrefix + "-current.pem" )
60+ config .TLSClientConfig .KeyFile = path .Join (certDir , certNamePrefix + "-current.pem" )
61+ config .TLSClientConfig .CertData = nil
62+ config .TLSClientConfig .KeyData = nil
63+
64+ // Switch to the per-node client certificate instead of reusing bootstrap auth.
65+ config .BearerToken = ""
66+ config .BearerTokenFile = ""
67+ config .Username = ""
68+ config .Password = ""
69+ config .ExecProvider = nil
70+ config .AuthProvider = nil
71+
72+ config .AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
73+ config .ContentType = "application/vnd.kubernetes.protobuf"
74+ // Allow multus (especially in server mode) to make more concurrent requests
75+ // to reduce client-side throttling
76+ config .QPS = 50
77+ config .Burst = 50
78+ // Set the config timeout to one minute.
79+ config .Timeout = time .Minute
80+ return config
7781}
7882
7983// PerNodeK8sClient creates/reload new multus kubeconfig per-node.
@@ -84,33 +88,35 @@ func PerNodeK8sClient(nodeName, bootstrapKubeconfigFile string, certDuration tim
8488 }
8589 config := getPerNodeKubeconfig (bootstrapKubeconfig , certDir )
8690
87- // If we have a valid certificate, user that to fetch CSRs.
91+ // If we have a valid certificate, use that to fetch CSRs.
8892 // Otherwise, use the bootstrap credentials from bootstrapKubeconfig
8993 // https://github.com/kubernetes/kubernetes/blob/068ee321bc7bfe1c2cefb87fb4d9e5deea84fbc8/cmd/kubelet/app/server.go#L953-L963
9094 newClientsetFn := func (current * tls.Certificate ) (kubernetes.Interface , error ) {
9195 cfg := bootstrapKubeconfig
9296
93- // validate the kubeconfig
94- tempClient , err := kubernetes .NewForConfig (cfg )
95- if err != nil {
96- logging .Errorf ("failed to read kubeconfig from cert manager: %v" , err )
97- } else {
98- _ , err := tempClient .Discovery ().ServerVersion ()
99- // tls unknown authority error is unrecoverable error with retry
97+ // When a current certificate exists, prefer it for CSR operations but fall back
98+ // to bootstrap credentials if the stored per-node config is no longer trusted.
99+ if current != nil {
100+ cfg = config
101+ tempClient , err := kubernetes .NewForConfig (cfg )
102+ if err != nil {
103+ return nil , logging .Errorf ("failed to create client from per-node kubeconfig: %v" , err )
104+ }
105+ _ , err = tempClient .Discovery ().ServerVersion ()
100106 if err != nil {
101107 if strings .Contains (err .Error (), "x509: certificate signed by unknown authority" ) {
102- logging .Verbosef ("cert mgr gets invalid config. rebuild from bootstrap kubeconfig" )
103- // reload and use bootstrapKubeconfig again
104- newBootstrapKubeconfig , _ := clientcmd .BuildConfigFromFlags ("" , bootstrapKubeconfigFile )
105- cfg = newBootstrapKubeconfig
108+ logging .Verbosef ("cert mgr gets invalid per-node config. rebuild from bootstrap kubeconfig" )
109+ newBootstrapKubeconfig , reloadErr := clientcmd .BuildConfigFromFlags ("" , bootstrapKubeconfigFile )
110+ if reloadErr != nil {
111+ return nil , logging .Errorf ("failed to reload bootstrap kubeconfig: %v" , reloadErr )
112+ }
113+ bootstrapKubeconfig = newBootstrapKubeconfig
114+ config = getPerNodeKubeconfig (bootstrapKubeconfig , certDir )
115+ cfg = bootstrapKubeconfig
106116 } else {
107- logging .Errorf ("failed to validate kubeconfig with new certs : %v" , err )
117+ logging .Errorf ("failed to validate per-node kubeconfig : %v" , err )
108118 }
109119 }
110-
111- if current != nil {
112- cfg = config
113- }
114120 }
115121 return kubernetes .NewForConfig (cfg )
116122 }
0 commit comments