@@ -17,10 +17,13 @@ limitations under the License.
17
17
package clusterapi
18
18
19
19
import (
20
+ "fmt"
21
+ "net/http"
20
22
"reflect"
21
23
22
24
corev1 "k8s.io/api/core/v1"
23
25
"k8s.io/apimachinery/pkg/api/resource"
26
+ utilnet "k8s.io/apimachinery/pkg/util/net"
24
27
"k8s.io/client-go/discovery"
25
28
"k8s.io/client-go/discovery/cached/memory"
26
29
"k8s.io/client-go/dynamic"
@@ -158,13 +161,22 @@ func BuildClusterAPI(opts config.AutoscalingOptions, do cloudprovider.NodeGroupD
158
161
if err != nil {
159
162
klog .Fatalf ("cannot build management cluster config: %v" , err )
160
163
}
164
+ if managementConfig .BearerToken != "" && ! opts .ClusterAPICloudConfigAuthoritative {
165
+ managementConfig .Wrap (func (rt http.RoundTripper ) http.RoundTripper {
166
+ return & bearerAuthRoundTripper {rt : rt , kubeconfigPath : managementKubeconfig }
167
+ })
168
+ }
161
169
162
170
workloadKubeconfig := opts .KubeConfigPath
163
-
164
171
workloadConfig , err := clientcmd .BuildConfigFromFlags ("" , workloadKubeconfig )
165
172
if err != nil {
166
173
klog .Fatalf ("cannot build workload cluster config: %v" , err )
167
174
}
175
+ if workloadConfig .BearerToken != "" {
176
+ workloadConfig .Wrap (func (rt http.RoundTripper ) http.RoundTripper {
177
+ return & bearerAuthRoundTripper {rt : rt , kubeconfigPath : workloadKubeconfig }
178
+ })
179
+ }
168
180
169
181
// Grab a dynamic interface that we can create informers from
170
182
managementClient , err := dynamic .NewForConfig (managementConfig )
@@ -207,3 +219,20 @@ func BuildClusterAPI(opts config.AutoscalingOptions, do cloudprovider.NodeGroupD
207
219
208
220
return newProvider (cloudprovider .ClusterAPIProviderName , rl , controller )
209
221
}
222
+
223
+ type bearerAuthRoundTripper struct {
224
+ kubeconfigPath string
225
+ rt http.RoundTripper
226
+ }
227
+
228
+ func (rt bearerAuthRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
229
+ req = utilnet .CloneRequest (req )
230
+ kubeConfig , err := clientcmd .BuildConfigFromFlags ("" , rt .kubeconfigPath )
231
+ if err != nil {
232
+ return nil , fmt .Errorf ("cannot build kube cluster config: %w" , err )
233
+ }
234
+ req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , kubeConfig .BearerToken ))
235
+ return rt .rt .RoundTrip (req )
236
+ }
237
+
238
+ var _ http.RoundTripper = & bearerAuthRoundTripper {}
0 commit comments