From fc9676192715cc9c3a2352c69e426745405fd488 Mon Sep 17 00:00:00 2001 From: David Markey Date: Mon, 13 May 2019 00:06:42 +0100 Subject: [PATCH] Fix bugs and add sample deployment --- deploy/deployment.yaml | 29 +++++++++++++++++++++++++++++ deploy/rbac.yaml | 37 +++++++++++++++++++++++++++++++++++++ lookout.py | 16 +++++++--------- 3 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 deploy/deployment.yaml create mode 100644 deploy/rbac.yaml diff --git a/deploy/deployment.yaml b/deploy/deployment.yaml new file mode 100644 index 0000000..b7aa6ac --- /dev/null +++ b/deploy/deployment.yaml @@ -0,0 +1,29 @@ +apiVersion: extensions/v1beta1 # for versions before 1.8.0 use apps/v1beta1 +kind: Deployment +metadata: + name: kube-lookout + labels: + app: kube-lookout + namespace: kube-system +spec: + replicas: 1 + selector: + matchLabels: + app: kube-lookout + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + template: + metadata: + labels: + app: kube-lookout + spec: + serviceAccount: kube-lookout + containers: + - name: kube-lookout + image: davidjmarkey/kube-lookout:latest + imagePullPolicy: Never + env: + - name: SLACK_TOKEN + value: "insert_token_here" diff --git a/deploy/rbac.yaml b/deploy/rbac.yaml new file mode 100644 index 0000000..8942cc8 --- /dev/null +++ b/deploy/rbac.yaml @@ -0,0 +1,37 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + labels: + k8s-app: kube-lookout + name: kube-lookout + namespace: kube-system +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: kube-lookout + labels: + k8s-app: kube-lookout +rules: +- apiGroups: + - extensions + resources: + - deployments + verbs: + - list + - get + - watch +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: kube-lookout +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: kube-lookout +subjects: + - kind: ServiceAccount + name: kube-lookout + namespace: kube-system diff --git a/lookout.py b/lookout.py index 7716cf7..e17e917 100644 --- a/lookout.py +++ b/lookout.py @@ -78,6 +78,10 @@ def _handle_deployment_change(self, deployment): metadata = deployment.metadata deployment_key = f"{metadata.namespace}/{metadata.name}" + ready_replicas = 0 + if deployment.status.ready_replicas is not None: + ready_replicas = deployment.status.ready_replicas + if deployment_key not in self.rollouts and \ deployment.status.updated_replicas is None: blocks = self._generate_deployment_rollout_block(deployment) @@ -88,7 +92,7 @@ def _handle_deployment_change(self, deployment): rollout_complete = ( deployment.status.updated_replicas == deployment.status.replicas == - deployment.status.ready_replicas) + ready_replicas) blocks = self._generate_deployment_rollout_block(deployment, rollout_complete) self.rollouts[deployment_key] = self._send_slack_block( @@ -97,14 +101,13 @@ def _handle_deployment_change(self, deployment): if rollout_complete: self.rollouts.pop(deployment_key) - elif deployment.status.ready_replicas < deployment.spec.replicas: + elif ready_replicas < deployment.spec.replicas: blocks = self._generate_deployment_degraded_block(deployment) self._send_slack_block(blocks, self.slack_channel) self.degraded.add(deployment_key) elif (deployment_key in self.degraded and - deployment.status.ready_replicas - >= deployment.spec.replicas): + ready_replicas >= deployment.spec.replicas): self.degraded.remove(deployment_key) blocks = self._generate_deployment_not_degraded_block(deployment) self._send_slack_block(blocks, self.slack_channel) @@ -124,11 +127,6 @@ def main_loop(self): print("Waiting for deployment events to come in..") for event in stream: deployment = event['object'] - obj = event["object"] - code = obj.get("code") - if code == 410: - print("Received HTTP 410, restarting..") - break self._handle_event(deployment) def _generate_deployment_rollout_block(self, deployment,