Skip to content

Commit 2f861c1

Browse files
committed
Cabal file and gitignore tweaks
1 parent a77091d commit 2f861c1

File tree

3 files changed

+97
-54
lines changed

3 files changed

+97
-54
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
*.pem
33
*.csr
44
result
5-
*.lock
5+
*.lock
6+
.pre-commit-config.yaml
7+
dist-newstyle/
8+
dist/

.pre-commit-config.yaml

Lines changed: 0 additions & 44 deletions
This file was deleted.

kubernetes-webhook-haskell.cabal

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
name: kubernetes-webhook-haskell
22
version: 0.1.0.0
3-
-- synopsis:
4-
-- description:
3+
synopsis: Create Kubernetes Admission Webhooks in Haskell
54
homepage: https://github.com/githubuser/kubernetes-webhook-haskell#readme
65
license: MIT
76
license-file: LICENSE
@@ -12,17 +11,102 @@ category: Web
1211
build-type: Simple
1312
extra-source-files: README.md
1413
cabal-version: >=1.10
14+
description:
15+
This library lets you create [Kubernetes Admission Webhooks](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/) in Haskell.
16+
17+
Using webhooks in Kubernetes requires some configuration, in the [dhall](./dhall) directory you can find some useful templates that you can reuse to deploy your webhooks. [cert-manager](https://cert-manager.io/docs/) is required to be installed in the cluster to use the templates.
18+
19+
Example webhook using Servant:
20+
```hs
21+
module Kubernetes.Example
22+
( startApp,
23+
app,
24+
)
25+
where
26+
27+
import Control.Monad.IO.Class
28+
import qualified Data.Aeson as A
29+
import qualified Data.ByteString as BS
30+
import qualified Data.HashMap.Strict as HM
31+
import Data.Text
32+
import GHC.Generics
33+
import qualified Kubernetes.Webhook as W
34+
import Network.Wai
35+
import Network.Wai.Handler.Warp
36+
import Network.Wai.Handler.WarpTLS
37+
import Servant
38+
import System.Environment
39+
40+
type API =
41+
"mutate" :> ReqBody '[JSON] W.AdmissionReviewRequest :> Post '[JSON] W.AdmissionReviewResponse
42+
43+
data Toleration
44+
= Toleration
45+
{ effect :: Maybe TolerationEffect,
46+
key :: Maybe Text,
47+
operator :: Maybe TolerationOperator,
48+
tolerationSeconds :: Maybe Integer,
49+
value :: Maybe Text
50+
}
51+
deriving (Generic, A.ToJSON)
52+
53+
data TolerationEffect = NoSchedule | PreferNoSchedule | NoExecute deriving (Generic, A.ToJSON)
54+
55+
data TolerationOperator = Exists | Equal deriving (Generic, A.ToJSON)
56+
57+
testToleration :: Toleration
58+
testToleration =
59+
Toleration
60+
{ effect = Just NoSchedule,
61+
key = Just "dedicated",
62+
operator = Just Equal,
63+
tolerationSeconds = Nothing,
64+
value = Just "test"
65+
}
66+
67+
startApp :: IO ()
68+
startApp = do
69+
let tlsOpts = tlsSettings "/certs/tls.crt" "/certs/tls.key"
70+
warpOpts = setPort 8080 defaultSettings
71+
runTLS tlsOpts warpOpts app
72+
73+
app :: Application
74+
app = serve api server
75+
76+
api :: Proxy API
77+
api = Proxy
78+
79+
server :: Server API
80+
server = mutate
81+
82+
mutate :: W.AdmissionReviewRequest -> Handler W.AdmissionReviewResponse
83+
mutate req = pure $ W.mutatingWebhook req (\_ -> Right W.Allowed) addToleration
84+
85+
addToleration :: W.Patch
86+
addToleration =
87+
W.Patch
88+
[ W.PatchOperation
89+
{ op = W.Add,
90+
path = "/spec/tolerations/-",
91+
from = Nothing,
92+
value = Just $ A.toJSON testToleration
93+
}
94+
]
95+
```
96+
97+
1598

1699
library
17100
hs-source-dirs: src
18101
exposed-modules: Kubernetes.Webhook, Kubernetes.Webhook.Types
19-
build-depends: base >= 4.7 && < 5
20-
, aeson
21-
, base64-bytestring
22-
, binary
23-
, bytestring
24-
, text
25-
, unordered-containers
102+
build-depends: aeson >= 1.4.6 && < 1.5,
103+
base >= 4.7 && < 5,
104+
base64-bytestring >= 1.0.0 && < 1.1,
105+
binary >= 0.8.6 && < 0.9,
106+
bytestring >= 0.10.8 && < 0.11,
107+
text >= 1.2.3 && < 1.3,
108+
unordered-containers >= 0.2.10 && < 0.3
109+
26110
default-language: Haskell2010
27111

28112
source-repository head

0 commit comments

Comments
 (0)