From 0ae944f8a6962f80ae579360e5968f92fb72e29c Mon Sep 17 00:00:00 2001 From: Todd Baert Date: Fri, 8 Nov 2024 16:44:26 -0500 Subject: [PATCH] add kubernetes setup (#11) Signed-off-by: Todd Baert --- README.md | 34 ++++++++ kubernetes/cluster.yaml | 17 ++++ kubernetes/toggle-shop.yaml | 169 ++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 kubernetes/cluster.yaml create mode 100644 kubernetes/toggle-shop.yaml diff --git a/README.md b/README.md index 2fe0ed9..4da0dc7 100644 --- a/README.md +++ b/README.md @@ -76,3 +76,37 @@ To simulate the migration yourself, follow these steps: If all goes well, you should see the migration complete without any issues. ![telemetry](./public/img/telemetry.png) + +## Kubernetes + +To run the ToggleShop in a Kind cluster: + +Create the cluster with the supplied definition: + +```sh +kind create cluster --config kubernetes/cluster.yaml +``` + +Install cert manager in the cluster (required for the OpenFeature operator): + +```sh +kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.2/cert-manager.yaml && kubectl wait --timeout=60s --for condition=Available=True deploy --all -n 'cert-manager' +``` + +Install the nginx controller: + +```sh +kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml && kubectl wait --timeout=60s --for condition=Available=True deploy --all -n 'ingress-nginx' +``` + +Install the OpenFeature operator: + +```sh +helm repo add openfeature https://open-feature.github.io/open-feature-operator/ && helm repo update && helm upgrade --install open-feature-operator openfeature/open-feature-operator +``` + +Deploy the ToggleShop and supporting infrastructure: + +```sh +kubectl -n default apply -f kubernetes/toggle-shop.yaml && kubectl wait --timeout=60s deployment --for condition=Available=True -l 'app=toggle-shop' -n 'default' +``` diff --git a/kubernetes/cluster.yaml b/kubernetes/cluster.yaml new file mode 100644 index 0000000..e87f416 --- /dev/null +++ b/kubernetes/cluster.yaml @@ -0,0 +1,17 @@ +apiVersion: kind.x-k8s.io/v1alpha4 +kind: Cluster +nodes: +- role: control-plane + kubeadmConfigPatches: + - | + kind: InitConfiguration + nodeRegistration: + kubeletExtraArgs: + node-labels: "ingress-ready=true" + extraPortMappings: + - containerPort: 80 + hostPort: 80 + protocol: TCP +- role: worker +- role: worker +- role: worker diff --git a/kubernetes/toggle-shop.yaml b/kubernetes/toggle-shop.yaml new file mode 100644 index 0000000..e1af4ce --- /dev/null +++ b/kubernetes/toggle-shop.yaml @@ -0,0 +1,169 @@ +# Flags for our UI +apiVersion: core.openfeature.dev/v1beta1 +kind: FeatureFlag +metadata: + name: ui-flags + labels: + app: toggle-shop +spec: + flagSpec: + flags: + offer-free-shipping: + state: ENABLED + variants: + 'on': true + 'off': false + defaultVariant: 'on' +--- +# Flags for our backend application +apiVersion: core.openfeature.dev/v1beta1 +kind: FeatureFlag +metadata: + name: app-flags + labels: + app: toggle-shop +spec: + flagSpec: + flags: + use-distributed-db: + state: ENABLED + variants: + 'on': true + 'off': false + defaultVariant: 'off' + use-secure-protocol: + state: ENABLED + variants: + 'on': true + 'off': false + defaultVariant: 'off' +--- +# Feature flag source custom resource, configuring flagd to source flags from FeatureFlag CRDs +apiVersion: core.openfeature.dev/v1beta1 +kind: FeatureFlagSource +metadata: + name: ui-flag-source + labels: + app: toggle-shop +spec: + sources: + - source: ui-flags + provider: kubernetes +--- +# Feature flag source custom resource, configuring flagd to source flags from FeatureFlag CRDs +apiVersion: core.openfeature.dev/v1beta1 +kind: FeatureFlagSource +metadata: + name: app-flag-source + labels: + app: toggle-shop +spec: + sources: + - source: app-flags + provider: kubernetes +--- +# Standalone flagd for serving UI +apiVersion: core.openfeature.dev/v1beta1 +kind: Flagd +metadata: + name: flagd-ui +spec: + replicas: 1 + serviceAccountName: default + featureFlagSource: ui-flag-source + ingress: + enabled: true + annotations: + nginx.ingress.kubernetes.io/use-regex: 'true' + nginx.ingress.kubernetes.io/rewrite-target: /ofrep/$1 + nginx.ingress.kubernetes.io/force-ssl-redirect: 'false' + hosts: + - localhost + - '' + ingressClassName: nginx + pathType: ImplementationSpecific + ofrepPath: /api/ofrep/(.*) +--- +# Standalone flagd for serving in-process provider +apiVersion: core.openfeature.dev/v1beta1 +kind: Flagd +metadata: + name: flagd-in-process +spec: + replicas: 1 + serviceType: ClusterIP + serviceAccountName: default + featureFlagSource: app-flag-source +--- +# In-process provider configuration +apiVersion: core.openfeature.dev/v1beta1 +kind: InProcessConfiguration +metadata: + name: in-process-config +spec: + host: flagd-in-process +--- +# Deployment for our application +apiVersion: apps/v1 +kind: Deployment +metadata: + name: toggle-shop-deployment + labels: + app: toggle-shop +spec: + replicas: 1 + selector: + matchLabels: + app: toggle-shop + template: + metadata: + labels: + app: toggle-shop + annotations: + openfeature.dev/enabled: 'true' + openfeature.dev/inprocessconfiguration: 'in-process-config' + spec: + containers: + - name: toggle-shop + image: ghcr.io/open-feature/toggle-shop:main # x-release-please-version + ports: + - containerPort: 3000 + name: app-port + env: + - name: FLAGD_HOST + value: flagd-in-process +--- +# Service to expose our application +apiVersion: v1 +kind: Service +metadata: + name: toggle-shop-app-service + labels: + app: toggle-shop +spec: + type: NodePort + selector: + app: toggle-shop + ports: + - port: 3000 + targetPort: app-port + nodePort: 30000 +--- +# Ingress for our application +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: toggle-shop-ingress +spec: + ingressClassName: nginx + rules: + - host: localhost + http: + paths: + - pathType: Prefix + path: / + backend: + service: + name: toggle-shop-app-service + port: + number: 3000