@@ -11,9 +11,11 @@ import qualified Data.ByteString as B
11
11
import qualified Data.ByteString.Lazy as LazyB
12
12
import Data.Default.Class (def )
13
13
import Data.Either (rights )
14
+ import Data.Monoid ((<>) )
14
15
import Data.PEM (pemContent , pemParseBS )
15
16
import qualified Data.Text as T
16
17
import qualified Data.Text.Encoding as T
18
+ import qualified Data.Text.IO as T
17
19
import Data.Typeable (Typeable )
18
20
import Data.X509 (SignedCertificate ,
19
21
decodeSignedCertificate )
@@ -27,6 +29,7 @@ import Network.HTTP.Client.TLS (mkManagerSettings)
27
29
import Network.TLS (Credential , defaultParamsClient )
28
30
import qualified Network.TLS as TLS
29
31
import qualified Network.TLS.Extra as TLS
32
+ import System.Environment (getEnv )
30
33
import System.X509 (getSystemCertificateStore )
31
34
32
35
-- | Sets the master URI in the 'K.KubernetesConfig'.
@@ -47,7 +50,7 @@ setTokenAuth
47
50
-> K. KubernetesConfig
48
51
-> K. KubernetesConfig
49
52
setTokenAuth token kcfg = kcfg
50
- { K. configAuthMethods = [K. AnyAuthMethod (K. AuthApiKeyBearerToken token)]
53
+ { K. configAuthMethods = [K. AnyAuthMethod (K. AuthApiKeyBearerToken $ " Bearer " <> token)]
51
54
}
52
55
53
56
-- | Creates a 'NH.Manager' that can handle TLS.
@@ -117,3 +120,17 @@ loadPEMCerts p = do
117
120
liftIO (B. readFile p)
118
121
>>= either (throwM . ParsePEMCertsException ) return
119
122
. parsePEMCerts
123
+
124
+ serviceAccountDir :: FilePath
125
+ serviceAccountDir = " /var/run/secrets/kubernetes.io/serviceaccount"
126
+
127
+ cluster :: (MonadIO m , MonadThrow m ) => m (NH. Manager , K. KubernetesConfig )
128
+ cluster = do
129
+ caStore <- loadPEMCerts $ serviceAccountDir ++ " /ca.crt"
130
+ defTlsParams <- liftIO defaultTLSClientParams
131
+ mgr <- liftIO . newManager . setCAStore caStore $ disableServerNameValidation defTlsParams
132
+ tok <- liftIO . T. readFile $ serviceAccountDir ++ " /token"
133
+ host <- liftIO $ getEnv " KUBERNETES_SERVICE_HOST"
134
+ port <- liftIO $ getEnv " KUBERNETES_SERVICE_PORT"
135
+ config <- setTokenAuth tok . setMasterURI (T. pack $ " https://" ++ host ++ " :" ++ port) <$> liftIO K. newConfig
136
+ return (mgr, config)
0 commit comments