Skip to content

Commit 7261011

Browse files
committed
Re-export all the moved functions from Kubernetes.Client.Config
Also: - add haddock description for `kubeClient` - re-order functions so `kubeClient` and `cluster` are on top
1 parent c94ffb9 commit 7261011

File tree

1 file changed

+60
-29
lines changed
  • kubernetes-client/src/Kubernetes/Client

1 file changed

+60
-29
lines changed

kubernetes-client/src/Kubernetes/Client/Config.hs

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

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
428

529
import qualified Kubernetes.OpenAPI.Core as K
630

@@ -23,43 +47,23 @@ import Kubernetes.Client.Auth.GCP
2347
import Kubernetes.Client.Auth.OIDC
2448
import Kubernetes.Client.Auth.Token
2549
import Kubernetes.Client.Internal.TLSUtils
26-
import Kubernetes.Client.KubeConfig
50+
import Kubernetes.Client.KubeConfig hiding (cluster)
2751
import Network.Connection (TLSSettings (..))
2852
import qualified Network.HTTP.Client as NH
2953
import Network.HTTP.Client.TLS (mkManagerSettings)
3054
import qualified Network.TLS as TLS
3155
import System.Environment (getEnv)
3256
import System.FilePath
3357

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-
6058
data KubeConfigSource = KubeConfigFile FilePath
6159
| KubeConfigCluster
6260

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+
-}
6367
kubeClient
6468
:: OIDCCache
6569
-> KubeConfigSource
@@ -80,7 +84,34 @@ kubeClient oidcCache (KubeConfigFile f) = do
8084
Right (_, auth)-> applyAuthSettings oidcCache auth (t, c)
8185
mgr <- newManager tlsParams
8286
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"
84115

85116
tlsValidation :: Config -> TLS.ClientParams -> TLS.ClientParams
86117
tlsValidation cfg t = case getCluster cfg of

0 commit comments

Comments
 (0)