1
1
{-# LANGUAGE OverloadedStrings #-}
2
2
3
- module Kubernetes.Client.Config where
3
+ module Kubernetes.Client.Config
4
+ ( KubeConfigSource (.. )
5
+ , addCACertData
6
+ , addCACertFile
7
+ , applyAuthSettings
8
+ , clientHooksL
9
+ , cluster
10
+ , defaultTLSClientParams
11
+ , disableServerCertValidation
12
+ , disableServerNameValidation
13
+ , disableValidateAuthMethods
14
+ , kubeClient
15
+ , loadPEMCerts
16
+ , newManager
17
+ , onCertificateRequestL
18
+ , onServerCertificateL
19
+ , parsePEMCerts
20
+ , serviceAccountDir
21
+ , setCAStore
22
+ , setClientCert
23
+ , setMasterURI
24
+ , setTokenAuth
25
+ , tlsValidation
26
+ )
27
+ where
4
28
5
29
import qualified Kubernetes.OpenAPI.Core as K
6
30
@@ -23,43 +47,23 @@ import Kubernetes.Client.Auth.GCP
23
47
import Kubernetes.Client.Auth.OIDC
24
48
import Kubernetes.Client.Auth.Token
25
49
import Kubernetes.Client.Internal.TLSUtils
26
- import Kubernetes.Client.KubeConfig
50
+ import Kubernetes.Client.KubeConfig hiding ( cluster )
27
51
import Network.Connection (TLSSettings (.. ))
28
52
import qualified Network.HTTP.Client as NH
29
53
import Network.HTTP.Client.TLS (mkManagerSettings )
30
54
import qualified Network.TLS as TLS
31
55
import System.Environment (getEnv )
32
56
import System.FilePath
33
57
34
- -- | Sets the master URI in the 'K.KubernetesClientConfig'.
35
- setMasterURI
36
- :: T. Text -- ^ Master URI
37
- -> K. KubernetesClientConfig
38
- -> K. KubernetesClientConfig
39
- setMasterURI masterURI kcfg =
40
- kcfg { K. configHost = (LazyB. fromStrict . T. encodeUtf8) masterURI }
41
-
42
- -- | Creates a 'NH.Manager' that can handle TLS.
43
- newManager :: TLS. ClientParams -> IO NH. Manager
44
- newManager cp = NH. newManager (mkManagerSettings (TLSSettings cp) Nothing )
45
-
46
- serviceAccountDir :: FilePath
47
- serviceAccountDir = " /var/run/secrets/kubernetes.io/serviceaccount"
48
-
49
- cluster :: (MonadIO m , MonadThrow m ) => m (NH. Manager , K. KubernetesClientConfig )
50
- cluster = do
51
- caStore <- loadPEMCerts $ serviceAccountDir ++ " /ca.crt"
52
- defTlsParams <- liftIO defaultTLSClientParams
53
- mgr <- liftIO . newManager . setCAStore caStore $ disableServerNameValidation defTlsParams
54
- tok <- liftIO . T. readFile $ serviceAccountDir ++ " /token"
55
- host <- liftIO $ getEnv " KUBERNETES_SERVICE_HOST"
56
- port <- liftIO $ getEnv " KUBERNETES_SERVICE_PORT"
57
- cfg <- setTokenAuth tok . setMasterURI (T. pack $ " https://" ++ host ++ " :" ++ port) <$> liftIO K. newConfig
58
- return (mgr, cfg)
59
-
60
58
data KubeConfigSource = KubeConfigFile FilePath
61
59
| KubeConfigCluster
62
60
61
+ {-|
62
+ Creates 'NH.Manager' and 'K.KubernetesClientConfig' for a given
63
+ 'KubeConfigSource'. It is recommended that multiple 'kubeClient' invocations
64
+ across an application share an 'OIDCCache', this makes sure updation of OAuth
65
+ token is synchronized across all the different clients being used.
66
+ -}
63
67
kubeClient
64
68
:: OIDCCache
65
69
-> KubeConfigSource
@@ -80,7 +84,34 @@ kubeClient oidcCache (KubeConfigFile f) = do
80
84
Right (_, auth)-> applyAuthSettings oidcCache auth (t, c)
81
85
mgr <- newManager tlsParams
82
86
return (mgr, cfg)
83
- kubeClient _ (KubeConfigCluster ) = Kubernetes.Client.Config. cluster
87
+ kubeClient _ (KubeConfigCluster ) = cluster
88
+
89
+ -- | Creates 'NH.Manager' and 'K.KubernetesClientConfig' assuming it is being executed in a pod
90
+ cluster :: (MonadIO m , MonadThrow m ) => m (NH. Manager , K. KubernetesClientConfig )
91
+ cluster = do
92
+ caStore <- loadPEMCerts $ serviceAccountDir ++ " /ca.crt"
93
+ defTlsParams <- liftIO defaultTLSClientParams
94
+ mgr <- liftIO . newManager . setCAStore caStore $ disableServerNameValidation defTlsParams
95
+ tok <- liftIO . T. readFile $ serviceAccountDir ++ " /token"
96
+ host <- liftIO $ getEnv " KUBERNETES_SERVICE_HOST"
97
+ port <- liftIO $ getEnv " KUBERNETES_SERVICE_PORT"
98
+ cfg <- setTokenAuth tok . setMasterURI (T. pack $ " https://" ++ host ++ " :" ++ port) <$> liftIO K. newConfig
99
+ return (mgr, cfg)
100
+
101
+ -- | Sets the master URI in the 'K.KubernetesClientConfig'.
102
+ setMasterURI
103
+ :: T. Text -- ^ Master URI
104
+ -> K. KubernetesClientConfig
105
+ -> K. KubernetesClientConfig
106
+ setMasterURI masterURI kcfg =
107
+ kcfg { K. configHost = (LazyB. fromStrict . T. encodeUtf8) masterURI }
108
+
109
+ -- | Creates a 'NH.Manager' that can handle TLS.
110
+ newManager :: TLS. ClientParams -> IO NH. Manager
111
+ newManager cp = NH. newManager (mkManagerSettings (TLSSettings cp) Nothing )
112
+
113
+ serviceAccountDir :: FilePath
114
+ serviceAccountDir = " /var/run/secrets/kubernetes.io/serviceaccount"
84
115
85
116
tlsValidation :: Config -> TLS. ClientParams -> TLS. ClientParams
86
117
tlsValidation cfg t = case getCluster cfg of
0 commit comments