Skip to content

Commit

Permalink
Rewrite from a CPA to dedicated CR and operator (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomperoo committed Jul 23, 2022
1 parent 25696d6 commit 9872904
Show file tree
Hide file tree
Showing 100 changed files with 6,020 additions and 5,824 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore build and test binaries.
bin/
testbin/
3 changes: 3 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ assignees: ''
**Describe the bug**
A clear and concise description of what the bug is.

**Predictive Horizontal Pod Autoscaler Version**
The version of the Predictive Horizontal Pod Autoscaler the bug has been found on.

**To Reproduce**
Steps to reproduce the behavior:
1. Deploy '...'
Expand Down
63 changes: 48 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,49 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.17

- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.17
id: go
- uses: actions/setup-python@v2
go-version: 1.18

- uses: actions/setup-python@v4
with:
python-version: '3.8.x'

- uses: azure/setup-helm@v1
with:
python-version: '3.8.5'
version: 'v3.9.0'
id: helm

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Lint, test and build
run: |
# Get staticcheck
# Get tooling
export PATH=$PATH:$(go env GOPATH)/bin
go install honnef.co/go/tools/cmd/staticcheck@v0.3.0
go get
# Lint and test
pip install -r requirements-dev.txt
make generate
make lint
make format
# Exit if after formatting there are any code differences
git diff --exit-code
make test
# Build
if [ ${{ github.event_name }} == "release" ]; then
# github.ref is in the form refs/tags/VERSION, so apply regex to just get version
VERSION=$(echo "${{ github.ref }}" | grep -P '([^\/]+$)' -o)
VERSION="${{ github.ref_name }}"
else
VERSION=$(git rev-parse --short ${{ github.sha }})
VERSION="${{ github.sha }}"
fi
make docker VERSION=${VERSION}
- name: Deploy
env:
DOCKER_USER: ${{ secrets.DOCKER_USER }}
Expand All @@ -52,14 +60,39 @@ jobs:
run: |
if [ ${{ github.event_name }} == "release" ]; then
# github.ref is in the form refs/tags/VERSION, so apply regex to just get version
VERSION=$(echo "${{ github.ref }}" | grep -P '([^\/]+$)' -o)
VERSION="${{ github.ref_name }}"
else
VERSION=$(git rev-parse --short ${{ github.sha }})
VERSION="${{ github.sha }}"
fi
echo "$DOCKER_PASS" | docker login --username=$DOCKER_USER --password-stdin
docker push jthomperoo/predictive-horizontal-pod-autoscaler:${VERSION}
if [ ${{ github.event_name }} == "release" ]; then
docker tag jthomperoo/predictive-horizontal-pod-autoscaler:${VERSION} jthomperoo/predictive-horizontal-pod-autoscaler:latest
docker push jthomperoo/predictive-horizontal-pod-autoscaler:latest
fi
- name: Bundle YAML config
if: github.event_name == 'release' && github.repository == 'jthomperoo/predictive-horizontal-pod-autoscaler'
run: |
# Variables to sub into k8s templates
if [ ${{ github.event_name }} == "release" ]; then
VERSION="${{ github.ref_name }}"
else
VERSION="${{ github.sha }}"
fi
export VERSION="${VERSION}"
sed -i "/version: 0.0.0/c\version: ${VERSION}" helm/Chart.yaml
helm package helm/
- name: Deploy helm package
if: github.event_name == 'release' && github.repository == 'jthomperoo/predictive-horizontal-pod-autoscaler'
uses: Shopify/upload-to-release@1.0.0
with:
name: predictive-horizontal-pod-autoscaler-${{ github.ref_name }}.tgz
path: predictive-horizontal-pod-autoscaler-${{ github.ref_name }}.tgz
repo-token: ${{ secrets.GITHUB_TOKEN }}
29 changes: 27 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
/vendor/
/dist/
*.out
__pycache__
.algorithm_coverage
.coverage

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
dist
tmp
testbin/*

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- See the [migration guide from `v0.10.0` here](https://predictive-horizontal-pod-autoscaler.readthedocs.io/en/latest/user-guide/migration/v0_10_0-to-v0_11_0).
- BREAKING CHANGE: Major rewrite converting this project from a Custom Pod Autoscaler to have its own dedicated CRD
and operator.
- Configuration and deployment has changed completely, no longer need to install the Custom Pod Autoscaler Operator,
instead you need to install the Predictive Horizontal Pod Autoscaler as an operator.
- No longer deployed as `CustomPodAutoscaler` custom resources, now deployed as `PredictiveHorizontalPodAutoscaler`
custom resources.
- BREAKING CHANGES: Several configuration options renamed for clarity.
- `LinearModel -> storedValues` renamed to `LinearModel -> historySize`
- `Model -> perInterval` renamed to `Model -> perSyncPeriod`
- BREAKING CHANGES: `perSyncPeriod` behaviour changed slightly, now it will no longer calculate the prediction, but
it will still update the replica history available to make a prediction with and prune the replica history if needed.
- Holt-Winters runtime hooks format changed:
- `evaluations` renamed to `replicaHistory`.
- Format change for `replicaHistory`, now in the format `[{"time": "<timestamp>", "replicas": <replica count>}]`.
- Upgrade to Go `v1.18`.
- No longer SQLite based storage, instead using Kubernetes configmaps which give persistent storage by default with
resiliency when the autoscaler operator restarts.
### Removed
- BREAKING CHANGES: Removed some no longer relevant configuration options.
- `DBPath`
- `MigrationPath`
- BREAKING CHANGE: Since no longer built as a `CustomPodAutoscaler` the `startTime` configuration is no longer
available: <https://custom-pod-autoscaler.readthedocs.io/en/latest/reference/configuration/#starttime>.
- BREAKING CHANGE: Holt-Winters runtime hooks limited to only support HTTP, shell support dropped.

## [v0.10.0] - 2022-05-14
### Changed
Expand Down
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2022 The Predictive Horizontal Pod Autoscaler Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Build the manager binary
FROM golang:1.18 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY . .

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

FROM python:3.8-slim-buster
WORKDIR /app

# Install Python dependencies
COPY algorithms/requirements.txt ./algorithms/requirements.txt
RUN python -m pip install -r algorithms/requirements.txt

COPY algorithms/ ./algorithms
COPY --from=builder /workspace/manager .
USER 65532:65532

ENTRYPOINT ["/app/manager"]
54 changes: 37 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ REGISTRY = jthomperoo
NAME = predictive-horizontal-pod-autoscaler
VERSION = latest

default:
@echo "=============Building============="
go build -o dist/$(NAME) main.go
cp LICENSE dist/LICENSE
LOCAL_HELM_CHART_NAME=predictive-horizontal-pod-autoscaler-operator

test:
@echo "=============Running tests============="
go test ./... -cover -coverprofile application_coverage.out
pytest algorithms/ --cov-report term --cov-report=xml:algorithm_coverage.out --cov-report=html:.algorithm_coverage --cov=algorithms/
run: deploy py_dependencies
go run github.com/cosmtrek/air

py_dependencies:
python -m pip install -r requirements-dev.txt

undeploy: generate
helm uninstall $(LOCAL_HELM_CHART_NAME)

lint:
deploy: generate
helm upgrade --install --set mode=development $(LOCAL_HELM_CHART_NAME) helm/

lint: generate
@echo "=============Linting============="
staticcheck ./...
go run honnef.co/go/tools/cmd/staticcheck ./...
pylint algorithms --rcfile=.pylintrc

format:
Expand All @@ -23,15 +27,31 @@ format:
go mod tidy
find algorithms -name '*.py' -print0 | xargs -0 yapf -i

test: generate
@echo "=============Running tests============="
go test ./... -cover -coverprofile unit_cover.out
pytest algorithms/ --cov-report term --cov-report=xml:algorithm_coverage.out --cov-report=html:.algorithm_coverage --cov=algorithms/

docker:
@echo "=============Building docker images============="
docker build -f build/Dockerfile -t $(REGISTRY)/$(NAME):$(VERSION) .
docker build . -t $(REGISTRY)/$(NAME):$(VERSION)

doc:
@echo "=============Serving docs============="
mkdocs serve
generate: get_controller-gen
@echo "=============Generating Golang and YAML============="
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
controller-gen rbac:roleName=predictive-horizontal-pod-autoscaler webhook crd:allowDangerousTypes=true \
paths="./..." \
output:crd:artifacts:config=helm/templates/crd \
output:rbac:artifacts:config=helm/templates/cluster \
output:webhook:artifacts:config=helm/templates/cluster

coverage:
view_coverage:
@echo "=============Loading coverage HTML============="
go tool cover -html=application_coverage.out
go tool cover -html=unit_cover.out
python -m webbrowser file://$(shell pwd)/.algorithm_coverage/index.html

get_controller-gen:
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.9.2

doc:
@echo "=============Serving docs============="
mkdocs serve
23 changes: 23 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
domain: jamiethompson.me
layout:
- go.kubebuilder.io/v3
plugins:
manifests.sdk.operatorframework.io/v2: {}
scorecard.sdk.operatorframework.io/v2: {}
projectName: predictive-horizontal-pod-autoscaler
repo: github.com/jthomperoo/predictive-horizontal-pod-autoscaler
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: jamiethompson.me
group: jamiethompson.me
kind: PredictiveHorizontalPodAutoscaler
path: github.com/jthomperoo/predictive-horizontal-pod-autoscaler/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
version: "3"

0 comments on commit 9872904

Please sign in to comment.