@@ -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 )
@@ -26,6 +28,7 @@ import Network.HTTP.Client.TLS (mkManagerSettings)
26
28
import Network.TLS (Credential , defaultParamsClient )
27
29
import qualified Network.TLS as TLS
28
30
import qualified Network.TLS.Extra as TLS
31
+ import System.Environment (getEnv )
29
32
import System.X509 (getSystemCertificateStore )
30
33
31
34
-- | Sets the master URI in the 'K.KubernetesConfig'.
@@ -46,7 +49,7 @@ setTokenAuth
46
49
-> K. KubernetesConfig
47
50
-> K. KubernetesConfig
48
51
setTokenAuth token kcfg = kcfg
49
- { K. configAuthMethods = [K. AnyAuthMethod (K. AuthApiKeyBearerToken token)]
52
+ { K. configAuthMethods = [K. AnyAuthMethod (K. AuthApiKeyBearerToken $ " Bearer " <> token)]
50
53
}
51
54
52
55
-- | Creates a 'NH.Manager' that can handle TLS.
@@ -119,3 +122,17 @@ loadPEMCerts p = do
119
122
liftIO (B. readFile p)
120
123
>>= either (throwM . ParsePEMCertsException ) return
121
124
. parsePEMCerts
125
+
126
+ serviceAccountDir :: FilePath
127
+ serviceAccountDir = " /var/run/secrets/kubernetes.io/serviceaccount"
128
+
129
+ cluster :: (MonadIO m , MonadThrow m ) => m (NH. Manager , K. KubernetesConfig )
130
+ cluster = do
131
+ caStore <- loadPEMCerts $ serviceAccountDir ++ " /ca.crt"
132
+ defTlsParams <- liftIO defaultTLSClientParams
133
+ mgr <- liftIO . newManager . setCAStore caStore $ disableServerNameValidation defTlsParams
134
+ tok <- liftIO . T. readFile $ serviceAccountDir ++ " /token"
135
+ host <- liftIO $ getEnv " KUBERNETES_SERVICE_HOST"
136
+ port <- liftIO $ getEnv " KUBERNETES_SERVICE_PORT"
137
+ config <- setTokenAuth tok . setMasterURI (T. pack $ " https://" ++ host ++ " :" ++ port) <$> liftIO K. newConfig
138
+ return (mgr, config)
0 commit comments