@@ -3,7 +3,6 @@ package kube
33import (
44 "encoding/base64"
55 "fmt"
6- "io/ioutil"
76 "os"
87 "path/filepath"
98 "strings"
@@ -81,6 +80,9 @@ type KubeConfigOptions struct {
8180 ConfigPath string
8281 ConfigDataBase64 string
8382 ConfigPathMergeList []string
83+
84+ BearerToken string
85+ BearerTokenFile string
8486}
8587
8688type KubeConfig struct {
@@ -91,8 +93,9 @@ type KubeConfig struct {
9193
9294func GetKubeConfig (opts KubeConfigOptions ) (* KubeConfig , error ) {
9395 // Try to load from kubeconfig in flags or from ~/.kube/config
94- config , outOfClusterErr := getOutOfClusterConfig (opts .Context , opts .ConfigPath , opts .ConfigDataBase64 , opts .ConfigPathMergeList )
95-
96+ config , outOfClusterErr := getOutOfClusterConfig (
97+ opts ,
98+ )
9699 if config == nil {
97100 if hasInClusterConfig () {
98101 // Try to configure as inCluster
@@ -135,8 +138,11 @@ type ContextClient struct {
135138func GetAllContextsClients (opts GetAllContextsClientsOptions ) ([]* ContextClient , error ) {
136139 // Try to load contexts from kubeconfig in flags or from ~/.kube/config
137140 var outOfClusterErr error
138- contexts , outOfClusterErr := getOutOfClusterContextsClients (opts .ConfigPath , opts .ConfigDataBase64 , opts .ConfigPathMergeList )
139- // return if contexts are loaded successfully
141+ contexts , outOfClusterErr := getOutOfClusterContextsClients (KubeConfigOptions {
142+ ConfigPath : opts .ConfigPath ,
143+ ConfigDataBase64 : opts .ConfigDataBase64 ,
144+ ConfigPathMergeList : opts .ConfigPathMergeList ,
145+ })
140146 if len (contexts ) > 0 {
141147 return contexts , nil
142148 }
@@ -229,17 +235,22 @@ func parseConfigDataBase64(configDataBase64 string) ([]byte, error) {
229235 return configData , nil
230236}
231237
232- func getOutOfClusterConfig (context , configPath , configDataBase64 string , configPathMergeList [] string ) (* KubeConfig , error ) {
238+ func getOutOfClusterConfig (opts KubeConfigOptions ) (* KubeConfig , error ) {
233239 res := & KubeConfig {}
234240
235- configData , err := parseConfigDataBase64 (configDataBase64 )
241+ configData , err := parseConfigDataBase64 (opts . ConfigDataBase64 )
236242 if err != nil {
237243 return nil , fmt .Errorf ("unable to parse base64 config data: %w" , err )
238244 }
239245
240- clientConfig , err := GetClientConfig (context , configPath , configData , configPathMergeList )
246+ clientConfig , err := GetClientConfig (
247+ opts .Context ,
248+ opts .ConfigPath ,
249+ configData ,
250+ opts .ConfigPathMergeList ,
251+ )
241252 if err != nil {
242- return nil , makeOutOfClusterClientConfigError (configPath , context , err )
253+ return nil , makeOutOfClusterClientConfigError (opts . ConfigDataBase64 , opts . Context , err )
243254 }
244255
245256 if ns , _ , err := clientConfig .Namespace (); err != nil {
@@ -250,35 +261,38 @@ func getOutOfClusterConfig(context, configPath, configDataBase64 string, configP
250261
251262 config , err := clientConfig .ClientConfig ()
252263 if err != nil {
253- return nil , makeOutOfClusterClientConfigError (configPath , context , err )
264+ return nil , makeOutOfClusterClientConfigError (opts . ConfigDataBase64 , opts . Context , err )
254265 }
255266 if config == nil {
256267 return nil , nil
257268 }
269+
270+ applyBearerToken (config , opts )
271+
258272 res .Config = config
259273
260- if context == "" {
274+ if opts . Context == "" {
261275 if rc , err := clientConfig .RawConfig (); err != nil {
262276 return nil , fmt .Errorf ("cannot get raw kubernetes config: %w" , err )
263277 } else {
264278 res .Context = rc .CurrentContext
265279 }
266280 } else {
267- res .Context = context
281+ res .Context = opts . Context
268282 }
269283
270284 return res , nil
271285}
272286
273- func getOutOfClusterContextsClients (configPath , configDataBase64 string , configPathMergeList [] string ) ([]* ContextClient , error ) {
287+ func getOutOfClusterContextsClients (opts KubeConfigOptions ) ([]* ContextClient , error ) {
274288 var res []* ContextClient
275289
276- configData , err := parseConfigDataBase64 (configDataBase64 )
290+ configData , err := parseConfigDataBase64 (opts . ConfigDataBase64 )
277291 if err != nil {
278292 return nil , fmt .Errorf ("unable to parse base64 config data: %w" , err )
279293 }
280294
281- clientConfig , err := GetClientConfig ("" , configPath , configData , configPathMergeList )
295+ clientConfig , err := GetClientConfig ("" , opts . ConfigPath , configData , opts . ConfigPathMergeList )
282296 if err != nil {
283297 return nil , err
284298 }
@@ -289,16 +303,18 @@ func getOutOfClusterContextsClients(configPath, configDataBase64 string, configP
289303 }
290304
291305 for contextName , context := range rc .Contexts {
292- clientConfig , err := GetClientConfig (contextName , configPath , configData , configPathMergeList )
306+ clientConfig , err := GetClientConfig (contextName , opts . ConfigPath , configData , opts . ConfigPathMergeList )
293307 if err != nil {
294- return nil , makeOutOfClusterClientConfigError (configPath , contextName , err )
308+ return nil , makeOutOfClusterClientConfigError (opts . ConfigPath , contextName , err )
295309 }
296310
297311 config , err := clientConfig .ClientConfig ()
298312 if err != nil {
299- return nil , makeOutOfClusterClientConfigError (configPath , contextName , err )
313+ return nil , makeOutOfClusterClientConfigError (opts . ConfigPath , contextName , err )
300314 }
301315
316+ applyBearerToken (config , opts )
317+
302318 clientset , err := kubernetes .NewForConfig (config )
303319 if err != nil {
304320 return nil , err
@@ -323,7 +339,7 @@ func getInClusterConfig() (*KubeConfig, error) {
323339 res .Config = config
324340 }
325341
326- if data , err := ioutil .ReadFile (kubeNamespaceFilePath ); err != nil {
342+ if data , err := os .ReadFile (kubeNamespaceFilePath ); err != nil {
327343 return nil , fmt .Errorf ("in-cluster configuration problem: cannot determine default kubernetes namespace: error reading %s: %w" , kubeNamespaceFilePath , err )
328344 } else {
329345 res .DefaultNamespace = string (data )
@@ -403,3 +419,13 @@ func restMapper(cachedDiscoveryClient *discovery.CachedDiscoveryInterface) meta.
403419 fmt .Printf (s )
404420 })
405421}
422+
423+ func applyBearerToken (config * rest.Config , opts KubeConfigOptions ) {
424+ if opts .BearerToken != "" {
425+ config .BearerToken = opts .BearerToken
426+ config .BearerTokenFile = ""
427+ } else if opts .BearerTokenFile != "" {
428+ config .BearerTokenFile = opts .BearerTokenFile
429+ config .BearerToken = ""
430+ }
431+ }
0 commit comments