Skip to content

Commit

Permalink
feat(operator): add fallback behavior when no keptn annotations are s…
Browse files Browse the repository at this point in the history
…et (#171)
  • Loading branch information
thschue committed Oct 17, 2022
1 parent eebe420 commit b6cc674
Show file tree
Hide file tree
Showing 11 changed files with 468 additions and 16 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ For this reason, the Keptn Lifecycle Controller is agnostic to deployment tools

## How to use

The Keptn Lifecycle Controller monitors manifests that have been applied against the Kubernetes API and reacts if it finds a workload with special annotations.
The Keptn Lifecycle Controller monitors manifests that have been applied against the Kubernetes API and reacts if it finds a workload with special annotations/labels.
For this, you should annotate your [Workload](https://kubernetes.io/docs/concepts/workloads/) with (at least) the following two annotations:

```yaml
keptn.sh/app: myAwesomeAppName
keptn.sh/workload: myAwesomeWorkload
keptn.sh/version: myAwesomeWorkloadVersion
```
Alternatively, you can use Kubernetes [Recommended Labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/) to annotate your workload:
```yaml
app.kubernetes.io/part-of: myAwesomeAppName
app.kubernetes.io/name: myAwesomeWorkload
app.kubernetes.io/version: myAwesomeWorkloadVersion
```
In general, the Keptn Annotations/Labels take precedence over the Kubernetes recommended labels. If there is no version annotation/label and there is only one container in the pod, the Lifecycle Controller will take the image tag as version (if it is not "latest").
In case you want to run pre- and post-deployment checks, further annotations are necessary:
```yaml
Expand Down
1 change: 1 addition & 0 deletions examples/podtatohead-deployment-k8s-labels/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/slack-secret.yaml
5 changes: 5 additions & 0 deletions examples/podtatohead-deployment-k8s-labels/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Create Secret for Slack here

```
kubectl create secret generic slack-notification --from-literal=SECURE_DATA='{"slack_hook":"<WebHook>","text":"Deployed PodTatoHead Application"}' -n podtato-kubectl -oyaml --dry-run > slack-secret.yaml
```
22 changes: 22 additions & 0 deletions examples/podtatohead-deployment-k8s-labels/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: lifecycle.keptn.sh/v1alpha1
kind: KeptnApp
metadata:
name: podtato-head
namespace: podtato-kubectl
spec:
version: "1.3"
workloads:
- name: podtato-head-left-arm
version: 0.2.7
- name: podtato-head-left-leg
version: 0.2.7
- name: podtato-head-entry
version: 0.2.7
- name: podtato-head-right-arm
version: 0.1.0
- name: podtato-head-left-arm
version: 0.2.7
- name: podtato-head-hat
version: 0.1.0
postDeploymentTasks:
- post-deployment-hello
12 changes: 12 additions & 0 deletions examples/podtatohead-deployment-k8s-labels/check_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: lifecycle.keptn.sh/v1alpha1
kind: KeptnTaskDefinition
metadata:
name: check-entry-service
namespace: podtato-kubectl
spec:
function:
httpRef:
url: https://raw.githubusercontent.com/keptn-sandbox/lifecycle-controller/main/functions-runtime/samples/ts/http.ts
parameters:
map:
url: http://podtato-head-entry.podtato-kubectl.svc.cluster.local:9000
299 changes: 299 additions & 0 deletions examples/podtatohead-deployment-k8s-labels/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: podtato-kubectl
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: podtato-head-entry
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
matchLabels:
component: podtato-head-entry
template:
metadata:
labels:
component: podtato-head-entry
keptn.sh/app: podtato-head
keptn.sh/workload: podtato-head-entry
spec:
terminationGracePeriodSeconds: 5
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'sleep 30']
containers:
- name: server
image: ghcr.io/podtato-head/entry:0.2.7
imagePullPolicy: Always
ports:
- containerPort: 9000
env:
- name: PODTATO_PORT
value: "9000"
---
apiVersion: v1
kind: Service
metadata:
name: podtato-head-entry
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
component: podtato-head-entry
ports:
- name: http
port: 9000
protocol: TCP
targetPort: 9000
type: LoadBalancer
# change to NodePort if no LoadBalancer controller is available
# type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: podtato-head-hat
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
matchLabels:
component: podtato-head-hat
template:
metadata:
labels:
component: podtato-head-hat
app.kubernetes.io/version: 0.1.0
annotations:
app.kubernetes.io/part-of: podtato-head
app.kubernetes.io/name: podtato-head-hat
keptn.sh/pre-deployment-tasks: check-entry-service
spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: ghcr.io/podtato-head/hat:0.2.7
imagePullPolicy: Always
ports:
- containerPort: 9000
env:
- name: PODTATO_PORT
value: "9000"
---
apiVersion: v1
kind: Service
metadata:
name: podtato-head-hat
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
component: podtato-head-hat
ports:
- name: http
port: 9001
protocol: TCP
targetPort: 9000
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: podtato-head-left-leg
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
matchLabels:
component: podtato-head-left-leg
template:
metadata:
labels:
component: podtato-head-left-leg
annotations:
app.kubernetes.io/part-of: podtato-head
app.kubernetes.io/name: podtato-head-left-leg
keptn.sh/pre-deployment-tasks: check-entry-service
spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: ghcr.io/podtato-head/left-leg:0.2.7
imagePullPolicy: Always
ports:
- containerPort: 9000
env:
- name: PODTATO_PORT
value: "9000"
---
apiVersion: v1
kind: Service
metadata:
name: podtato-head-left-leg
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
component: podtato-head-left-leg
ports:
- name: http
port: 9002
protocol: TCP
targetPort: 9000
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: podtato-head-left-arm
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
matchLabels:
component: podtato-head-left-arm
template:
metadata:
labels:
component: podtato-head-left-arm
app.kubernetes.io/part-of: podtato-head
annotations:
app.kubernetes.io/name: podtato-head-left-arm
keptn.sh/pre-deployment-tasks: check-entry-service
spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: ghcr.io/podtato-head/left-arm:0.2.7
imagePullPolicy: Always
ports:
- containerPort: 9000
env:
- name: PODTATO_PORT
value: "9000"
---
apiVersion: v1
kind: Service
metadata:
name: podtato-head-left-arm
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
component: podtato-head-left-arm
ports:
- name: http
port: 9003
protocol: TCP
targetPort: 9000
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: podtato-head-right-leg
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
matchLabels:
component: podtato-head-right-leg
template:
metadata:
labels:
component: podtato-head-right-leg
annotations:
app.kubernetes.io/part-of: podtato-head
app.kubernetes.io/name: podtato-head-right-leg
keptn.sh/pre-deployment-tasks: check-entry-service
spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: ghcr.io/podtato-head/right-leg:0.2.7
imagePullPolicy: Always
ports:
- containerPort: 9000
env:
- name: PODTATO_PORT
value: "9000"
---
apiVersion: v1
kind: Service
metadata:
name: podtato-head-right-leg
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
component: podtato-head-right-leg
ports:
- name: http
port: 9004
protocol: TCP
targetPort: 9000
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: podtato-head-right-arm
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
matchLabels:
component: podtato-head-right-arm
template:
metadata:
labels:
component: podtato-head-right-arm
annotations:
app.kubernetes.io/part-of: podtato-head
keptn.sh/workload: podtato-head-right-arm
keptn.sh/version: 0.1.0
keptn.sh/pre-deployment-tasks: check-entry-service
spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: ghcr.io/podtato-head/right-arm:0.2.7
imagePullPolicy: Always
ports:
- containerPort: 9000
env:
- name: PODTATO_PORT
value: "9000"
---
apiVersion: v1
kind: Service
metadata:
name: podtato-head-right-arm
namespace: podtato-kubectl
labels:
app: podtato-head
spec:
selector:
component: podtato-head-right-arm
ports:
- name: http
port: 9005
protocol: TCP
targetPort: 9000
type: ClusterIP
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: lifecycle.keptn.sh/v1alpha1
kind: KeptnTaskDefinition
metadata:
name: post-deployment-hello
namespace: podtato-kubectl
spec:
function:
inline:
code: |
let text = Deno.env.get("SECURE_DATA");
let data;
if (text != undefined) {
data = JSON.parse(text);
}
const body = `{"text": "${data.text}"}`;
console.log(body)
let resp = await fetch("https://hooks.slack.com/services/" + data.slack_hook, {
method: "POST",
body,
});
console.log(resp)
secureParameters:
secret: slack-notification
Loading

0 comments on commit b6cc674

Please sign in to comment.