kubernetes-webhook-haskell: Create Kubernetes Admission Webhooks in Haskell

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

This library lets you create Kubernetes Admission Webhooks in Haskell.


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.2.0.0, 0.2.0.1, 0.2.0.1, 0.2.0.2, 0.2.0.3
Change log None available
Dependencies aeson (>=1.4.6 && <1.5), base (>=4.7 && <5), base64-bytestring (>=1.0.0 && <1.2), binary (>=0.8.6 && <0.9), bytestring (>=0.10.8 && <0.11), text (>=1.2.3 && <1.3), unordered-containers (>=0.2.10 && <0.3) [details]
License MIT
Copyright 2020 Earnest Research
Author Alessandro Marrella
Maintainer amarrella@earnestresearch.com
Category Web
Home page https://github.com/EarnestResearch/kubernetes-webhook-haskell#readme
Source repo head: git clone https://github.com/EarnestResearch/kubernetes-webhook-haskell
Uploaded by amarrella at 2020-04-25T20:37:22Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for kubernetes-webhook-haskell-0.2.0.1

[back to package description]

kubernetes-webhook-haskell

This library lets you create Kubernetes Admission Webhooks in Haskell.

Using webhooks in Kubernetes requires some configuration, in the dhall directory you can find some useful templates that you can reuse to deploy your webhooks. cert-manager is required to be installed in the cluster to use the templates.

Example webhook using Servant:

module Kubernetes.Example
    ( startApp,
    app,
    )
where

import Control.Monad.IO.Class
import qualified Data.Aeson as A
import qualified Data.ByteString as BS
import qualified Data.HashMap.Strict as HM
import Data.Text
import GHC.Generics
import qualified Kubernetes.Webhook as W
import Network.Wai
import Network.Wai.Handler.Warp
import Network.Wai.Handler.WarpTLS
import Servant
import System.Environment

type API =
"mutate" :> ReqBody '[JSON] W.AdmissionReviewRequest :> Post '[JSON] W.AdmissionReviewResponse

data Toleration
= Toleration
    { effect :: Maybe TolerationEffect,
        key :: Maybe Text,
        operator :: Maybe TolerationOperator,
        tolerationSeconds :: Maybe Integer,
        value :: Maybe Text
    }
deriving (Generic, A.ToJSON)

data TolerationEffect = NoSchedule | PreferNoSchedule | NoExecute deriving (Generic, A.ToJSON)

data TolerationOperator = Exists | Equal deriving (Generic, A.ToJSON)

testToleration :: Toleration
testToleration =
Toleration
    { effect = Just NoSchedule,
    key = Just "dedicated",
    operator = Just Equal,
    tolerationSeconds = Nothing,
    value = Just "test"
    }

startApp :: IO ()
startApp = do
let tlsOpts = tlsSettings "/certs/tls.crt" "/certs/tls.key"
    warpOpts = setPort 8080 defaultSettings
runTLS tlsOpts warpOpts app

app :: Application
app = serve api server

api :: Proxy API
api = Proxy

server :: Server API
server = mutate

mutate :: W.AdmissionReviewRequest -> Handler W.AdmissionReviewResponse
mutate req = pure $ W.mutatingWebhook req (\_ -> Right addToleration)

addToleration :: W.Patch
addToleration = 
W.Patch
    [ W.PatchOperation
        { op = W.Add,
        path = "/spec/tolerations/-",
        from = Nothing,
        value = Just $ A.toJSON testToleration
        }
    ]