Skip to content

Commit

Permalink
Add pre-commit hooks (hjacobs#82)
Browse files Browse the repository at this point in the history
* code formatting to match pre-commit hooks

* use latest reorder dependency

* bump deps, add pre-commit dep

* bump poetry deps

* pre-commit autoupdate
  • Loading branch information
hjacobs authored Jan 13, 2020
1 parent 8350694 commit 95af6a5
Show file tree
Hide file tree
Showing 20 changed files with 368 additions and 133 deletions.
131 changes: 131 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
minimum_pre_commit_version: 1.21.0
fail_fast: false
default_stages: [commit, push]
repos:
# meta

- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes

# formatters

- repo: https://github.com/asottile/reorder_python_imports
rev: v1.9.0
hooks:
- id: reorder-python-imports

- repo: https://github.com/ambv/black
rev: 19.10b0
hooks:
- id: black

# - repo: https://github.com/pre-commit/mirrors-yapf
# rev: v0.28.0
# hooks:
# - id: yapf

- repo: https://github.com/asottile/pyupgrade
rev: v1.26.0
hooks:
- id: pyupgrade
stages: [push]

# linters

- repo: https://github.com/PyCQA/bandit
rev: 1.6.2
hooks:
- id: bandit
args: ["-x", "tests"]
stages: [push]

- repo: https://github.com/PyCQA/pydocstyle
rev: 5.0.2
hooks:
- id: pydocstyle
args: ["--ignore=D10,D21,D202"]


- repo: local
hooks:

- id: safety
name: safety
entry: safety
language: system
pass_filenames: false
args: ["check", "--bare"]
stages: [push]

- id: poetry
name: poetry
description: Validates the structure of the pyproject.toml file
entry: poetry check
language: system
pass_filenames: false
files: ^pyproject.toml$
stages: [push]

- repo: https://github.com/adrienverge/yamllint
rev: v1.20.0
hooks:
- id: yamllint
args: ["--strict", "-d", "{rules: {line-length: {max: 180}}}"]
#
exclude: >
(?x)^(
^{{.*}}.*\.yaml$|
^helm-chart/templates/.*$
)
#

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.761
hooks:
- id: mypy

- repo: https://github.com/pryorda/dockerfilelint-precommit-hooks
rev: v0.1.0
hooks:
- id: dockerfilelint
stages: [commit] # required

# miscellaneous

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-added-large-files
- id: check-docstring-first
- id: debug-statements
- id: end-of-file-fixer
- id: flake8
additional_dependencies: ["flake8-bugbear"]
- id: trailing-whitespace
- id: check-ast
- id: check-builtin-literals
- id: detect-private-key
- id: mixed-line-ending
- id: name-tests-test
args: ["--django"]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.4.4
hooks:
# - id: rst-backticks
- id: python-use-type-annotations
- id: python-no-log-warn
- id: python-no-eval
- id: python-check-mock-methods
- id: python-check-blanket-noqa

# commit-msg
# http://jorisroovers.com/gitlint/#using-gitlint-through-pre-commit

- repo: https://github.com/jorisroovers/gitlint
rev: v0.12.0
hooks:
- id: gitlint
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ default: docker
install:
poetry install

test: install
poetry run flake8
poetry run black --check kube_downscaler
poetry run mypy --ignore-missing-imports kube_downscaler
.PHONY: lint
lint: install
poetry run pre-commit run --all-files


test: lint install
poetry run coverage run --source=kube_downscaler -m py.test -v
poetry run coverage report

docker:
docker:
docker build --build-arg "VERSION=$(VERSION)" -t "$(IMAGE):$(TAG)" .
@echo 'Docker image $(IMAGE):$(TAG) can now be used.'

Expand Down
1 change: 0 additions & 1 deletion helm-chart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
0.1.0 - 10/03/2018
---
- Initial chart

1 change: 0 additions & 1 deletion helm-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ keywords:
- scheduled lifecycle
sources:
- https://github.com/hjacobs/kube-downscaler

4 changes: 2 additions & 2 deletions helm-chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The diagram below depicts how a Kube-downscaler agent control applications.
![Alt text](images/architecture.png?raw=true "Kube Kube-downscaler diagram")

## Quick Start
Below are instructions to quickly install and configure Kube-downscaler.
Below are instructions to quickly install and configure Kube-downscaler.

### Installing Kube-downscaler

Expand Down Expand Up @@ -66,7 +66,7 @@ kubectl get events -w


### Deploying sample applications using Kube-downscaler
In this tutorial we will show how to deploy Kube-downscaler and test with sample Flask application.
In this tutorial we will show how to deploy Kube-downscaler and test with sample Flask application.

1. Deploy Flask applications:
```
Expand Down
3 changes: 1 addition & 2 deletions kube_downscaler/cmd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import argparse
import os

VALID_RESOURCES = frozenset(["deployments", "statefulsets", "stacks", "cronjobs"])

Expand Down
9 changes: 5 additions & 4 deletions kube_downscaler/helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
import os
import re
from typing import Match

import datetime
import pykube
import pytz
import re

WEEKDAYS = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]

Expand Down Expand Up @@ -39,7 +40,7 @@ def matches_time_spec(time: datetime.datetime, spec: str):
return False


def _matches_recurring_time_spec(time: datetime.datetime, match: re.Match):
def _matches_recurring_time_spec(time: datetime.datetime, match: Match):
tz = pytz.timezone(match.group("tz"))
local_time = tz.fromutc(time.replace(tzinfo=tz))
day_from = WEEKDAYS.index(match.group(1).upper())
Expand All @@ -52,7 +53,7 @@ def _matches_recurring_time_spec(time: datetime.datetime, match: re.Match):
return day_matches and time_matches


def _matches_absolute_time_spec(time: datetime.datetime, match: re.Match):
def _matches_absolute_time_spec(time: datetime.datetime, match: Match):
time_from = datetime.datetime.fromisoformat(match.group(1))
time_to = datetime.datetime.fromisoformat(match.group(2))
return time_from <= time <= time_to
Expand Down
8 changes: 4 additions & 4 deletions kube_downscaler/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python3

import time

import logging
import time

from kube_downscaler import __version__, cmd, shutdown
from kube_downscaler import __version__
from kube_downscaler import cmd
from kube_downscaler import shutdown
from kube_downscaler.scaler import scale

logger = logging.getLogger("downscaler")
Expand Down
9 changes: 5 additions & 4 deletions kube_downscaler/resources/stack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from pykube.objects import NamespacedAPIObject, ReplicatedMixin, ScalableMixin
from pykube.objects import NamespacedAPIObject
from pykube.objects import ReplicatedMixin
from pykube.objects import ScalableMixin


class Stack(NamespacedAPIObject, ReplicatedMixin, ScalableMixin):
"""
Support the Stack resource (https://github.com/zalando-incubator/stackset-controller)
"""

"""Support the Stack resource (https://github.com/zalando-incubator/stackset-controller)."""

version = "zalando.org/v1"
endpoint = "stacks"
Expand Down
9 changes: 6 additions & 3 deletions kube_downscaler/scaler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime
import logging
import pykube
from typing import FrozenSet

import pykube
from pykube import CronJob
from pykube import Deployment
from pykube import StatefulSet

from kube_downscaler import helper
from pykube import Deployment, StatefulSet, CronJob
from kube_downscaler.resources.stack import Stack

logger = logging.getLogger(__name__)
Expand All @@ -27,7 +30,7 @@ def within_grace_period(deploy, grace_period: int, now: datetime.datetime):


def pods_force_uptime(api, namespace: str):
"""Returns True if there are any running pods which require the deployments to be scaled back up"""
"""Return True if there are any running pods which require the deployments to be scaled back up."""
for pod in pykube.Pod.objects(api).filter(namespace=(namespace or pykube.all)):
if pod.obj.get("status", {}).get("phase") in ("Succeeded", "Failed"):
continue
Expand Down
3 changes: 1 addition & 2 deletions kube_downscaler/shutdown.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys

import contextlib
import signal
import sys


class GracefulShutdown:
Expand Down
Loading

0 comments on commit 95af6a5

Please sign in to comment.