Skip to content

Commit 130c897

Browse files
authored
Merge pull request #51 from akshaymankar/kube-config-loader
Kube config loader
2 parents 01b367b + a30745f commit 130c897

File tree

21 files changed

+1047
-184
lines changed

21 files changed

+1047
-184
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ matrix:
9696
# compiler: ": #stack 8.0.2"
9797
# addons: {apt: {packages: [libgmp-dev]}}
9898

99-
- env: BUILD=stack ARGS="--resolver lts-11"
99+
- env: BUILD=stack ARGS="--resolver lts-11 --stack-yaml stack-8.2.2.yaml"
100100
compiler: ": #stack 8.2.2"
101101
addons: {apt: {packages: [libgmp-dev]}}
102102

103-
- env: BUILD=stack ARGS="--resolver lts-12"
103+
- env: BUILD=stack ARGS="--resolver lts-12 --stack-yaml stack-8.4.4.yaml"
104104
compiler: ": #stack 8.4.4"
105105
addons: {apt: {packages: [libgmp-dev]}}
106106

@@ -139,11 +139,11 @@ matrix:
139139
# compiler: ": #stack 8.0.2 osx"
140140
# os: osx
141141

142-
- env: BUILD=stack ARGS="--resolver lts-11"
142+
- env: BUILD=stack ARGS="--resolver lts-11 --stack-yaml stack-8.2.2.yaml"
143143
compiler: ": #stack 8.2.2 osx"
144144
os: osx
145145

146-
- env: BUILD=stack ARGS="--resolver lts-12"
146+
- env: BUILD=stack ARGS="--resolver lts-12 --stack-yaml stack-8.4.4.yaml"
147147
compiler: ": #stack 8.4.4 osx"
148148
os: osx
149149

@@ -223,7 +223,7 @@ script:
223223
set -ex
224224
case "$BUILD" in
225225
stack)
226-
travis_wait 30 stack --no-terminal $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps
226+
stack --no-terminal $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps
227227
;;
228228
cabal)
229229
cabal install --enable-tests --enable-benchmarks --force-reinstalls --ghc-options=-O0 --reorder-goals --max-backjumps=-1 $CABALARGS $PACKAGES

kubernetes-client/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,52 @@
22

33
## Example
44

5+
### Load KubeConfig file
6+
7+
```haskell
8+
import Control.Concurrent.STM (atomically, newTVar)
9+
import Kubernetes.Client (KubeConfigSource (..), mkKubeClientConfig)
10+
import Kubernetes.OpenAPI (Accept (..), MimeJSON (..), dispatchMime)
11+
12+
import qualified Data.Map as Map
13+
import qualified Kubernetes.OpenAPI.API.CoreV1 as CoreV1
14+
15+
main :: IO ()
16+
main = do
17+
oidcCache <- atomically $ newTVar $ Map.fromList []
18+
(mgr, kcfg) <- mkKubeClientConfig oidcCache $ KubeConfigFile "/path/to/kubeconfig"
19+
dispatchMime
20+
mgr
21+
kcfg
22+
(CoreV1.listPodForAllNamespaces (Accept MimeJSON))
23+
>>= print
24+
```
25+
26+
### Load InCluster Config
27+
28+
```haskell
29+
import Control.Concurrent.STM (atomically, newTVar)
30+
import Data.Function ((&))
31+
import Kubernetes.Client (KubeConfigSource (..), mkKubeClientConfig)
32+
import Kubernetes.OpenAPI (Accept (..), MimeJSON (..), dispatchMime)
33+
import Network.TLS (credentialLoadX509)
34+
35+
import qualified Data.Map as Map
36+
import qualified Kubernetes.OpenAPI.API.CoreV1 as CoreV1
37+
38+
main :: IO ()
39+
main = do
40+
oidcCache <- atomically $ newTVar $ Map.fromList []
41+
(mgr, kcfg) <- mkKubeClientConfig oidcCache KubeConfigCluster
42+
dispatchMime
43+
mgr
44+
kcfg
45+
(CoreV1.listPodForAllNamespaces (Accept MimeJSON))
46+
>>= print
47+
```
48+
49+
### Load config from URL and paths
50+
551
```haskell
652
{-# LANGUAGE OverloadedStrings #-}
753

kubernetes-client/example/App.hs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
module Main where
44

5-
import Data.Function ((&))
6-
import Kubernetes.Client (defaultTLSClientParams,
7-
disableServerCertValidation,
8-
disableServerNameValidation,
9-
disableValidateAuthMethods,
10-
loadPEMCerts, newManager,
11-
setCAStore, setClientCert,
12-
setMasterURI, setTokenAuth)
13-
import Kubernetes.OpenAPI (Accept (..), MimeJSON (..),
14-
dispatchMime, newConfig)
5+
import Control.Concurrent.STM (atomically, newTVar)
6+
import Data.Function ((&))
7+
import Kubernetes.Client (KubeConfigSource (..), defaultTLSClientParams,
8+
disableServerCertValidation,
9+
disableServerNameValidation,
10+
disableValidateAuthMethods, mkKubeClientConfig,
11+
loadPEMCerts, newManager, setCAStore,
12+
setClientCert, setMasterURI, setTokenAuth)
13+
import Kubernetes.OpenAPI (Accept (..), MimeJSON (..), dispatchMime,
14+
newConfig)
15+
import Network.TLS (credentialLoadX509)
16+
17+
import qualified Data.Map as Map
1518
import qualified Kubernetes.OpenAPI.API.CoreV1 as CoreV1
16-
import Network.TLS (credentialLoadX509)
1719

1820
example :: IO ()
1921
example = do
@@ -42,5 +44,25 @@ example = do
4244
(CoreV1.listPodForAllNamespaces (Accept MimeJSON))
4345
>>= print
4446

47+
exampleWithKubeConfig :: IO ()
48+
exampleWithKubeConfig = do
49+
oidcCache <- atomically $ newTVar $ Map.fromList []
50+
(mgr, kcfg) <- mkKubeClientConfig oidcCache $ KubeConfigFile "/path/to/kubeconfig"
51+
dispatchMime
52+
mgr
53+
kcfg
54+
(CoreV1.listPodForAllNamespaces (Accept MimeJSON))
55+
>>= print
56+
57+
exampleWithInClusterConfig :: IO ()
58+
exampleWithInClusterConfig = do
59+
oidcCache <- atomically $ newTVar $ Map.fromList []
60+
(mgr, kcfg) <- mkKubeClientConfig oidcCache KubeConfigCluster
61+
dispatchMime
62+
mgr
63+
kcfg
64+
(CoreV1.listPodForAllNamespaces (Accept MimeJSON))
65+
>>= print
66+
4567
main :: IO ()
4668
main = return ()

kubernetes-client/package.yaml

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,58 @@ license: Apache-2.0
1313
license-file: LICENSE
1414
library:
1515
source-dirs: src
16+
ghc-options:
17+
- -Wall
1618
tests:
1719
spec:
1820
main: Spec.hs
1921
source-dirs: test
2022
dependencies:
2123
- kubernetes-client
2224
- hspec
25+
- hspec-attoparsec
2326
- yaml
27+
- file-embed
2428
example:
2529
main: App.hs
2630
source-dirs: example
2731
dependencies:
2832
- kubernetes-client
2933
extra-source-files:
30-
- test/testdata/*
34+
- test/testdata/**/*
3135
- README.md
3236
dependencies:
3337
- base >=4.7 && <5.0
34-
- bytestring >=0.10.0 && <0.11
35-
- aeson >=1.2.2 && <1.5
36-
- connection >=0.2.8
37-
- containers >= 0.6.0.1
38-
- data-default-class >=0.1.2.0
38+
- base64-bytestring
39+
- bytestring >=0.10 && <0.11
40+
- aeson >=1.2 && <1.5
41+
- attoparsec >=0.13 && <0.14
42+
- jsonpath >=0.1 && <0.2
43+
- connection >=0.2
44+
- containers >= 0.5
45+
- data-default-class >=0.1
46+
- either >=5.0
47+
- filepath >=1.4
48+
- hoauth2 >=1.8
3949
- http-client >=0.5 && <0.7
40-
- http-client-tls >=0.3.5.3
50+
- http-client-tls >=0.3
51+
- jose-jwt >=0.8
4152
- kubernetes-client-core ==0.1.0.1
42-
- microlens >=0.4.3 && <0.5
43-
- mtl >=2.2.1
44-
- pem >=0.2.4
53+
- microlens >=0.4 && <0.5
54+
- mtl >=2.2
55+
- oidc-client >=0.4
56+
- pem >=0.2
4557
- safe-exceptions >=0.1.0.0
46-
- streaming-bytestring >= 0.1.5 && < 0.2.0
58+
- stm >=2.4
59+
- streaming-bytestring >= 0.1 && < 0.2.0
4760
- text >=0.11 && <1.3
61+
- time >=1.8
62+
- timerep >=2.0
4863
- tls >=1.4.1
49-
- x509 >=1.7.5
50-
- x509-system >=1.6.6
51-
- x509-store >=1.6.7
52-
- x509-validation >=1.6.11
64+
- typed-process >=0.2
65+
- uri-bytestring >=0.3
66+
- x509 >=1.7
67+
- x509-system >=1.6
68+
- x509-store >=1.6
69+
- x509-validation >=1.6
70+
- yaml >=0.8.32
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Kubernetes.Client.Auth.ClientCert where
2+
3+
import Control.Exception.Safe (Exception, throwM)
4+
import Data.Text.Encoding
5+
import Kubernetes.Client.Auth.Internal.Types
6+
import Kubernetes.Client.Internal.TLSUtils
7+
import Kubernetes.Client.KubeConfig
8+
import Kubernetes.OpenAPI (KubernetesClientConfig (..))
9+
import Network.TLS
10+
11+
-- | Detects if kuebconfig file provides 'client-certificate', if it configures TLS client params with the client certificate
12+
clientCertFileAuth :: DetectAuth
13+
clientCertFileAuth auth (tlsParams, cfg) = do
14+
certFile <- clientCertificate auth
15+
keyFile <- clientKey auth
16+
return $ do
17+
cert <- credentialLoadX509 certFile keyFile
18+
>>= either (throwM . CredentialLoadException) return
19+
let newParams = (setClientCert cert tlsParams)
20+
newCfg = (disableValidateAuthMethods cfg)
21+
return (newParams, newCfg)
22+
23+
-- | Detects if kuebconfig file provides 'client-certificate-data', if it configures TLS client params with the client certificate
24+
clientCertDataAuth :: DetectAuth
25+
clientCertDataAuth auth (tlsParams, cfg) = do
26+
certB64 <- encodeUtf8 <$> clientCertificateData auth
27+
keyB64 <- encodeUtf8 <$> clientKeyData auth
28+
Just $ do
29+
cert <- loadB64EncodedCert certB64 keyB64
30+
let newParams = (setClientCert cert tlsParams)
31+
newCfg = (disableValidateAuthMethods cfg)
32+
return (newParams, newCfg)
33+
34+
-- |Disables the client-side auth methods validation. This is necessary if you are using client cert authentication.
35+
disableValidateAuthMethods :: KubernetesClientConfig -> KubernetesClientConfig
36+
disableValidateAuthMethods kcfg = kcfg { configValidateAuthMethods = False }
37+
38+
data CredentialLoadException = CredentialLoadException String
39+
deriving Show
40+
41+
instance Exception CredentialLoadException

0 commit comments

Comments
 (0)