Skip to content

Commit

Permalink
Extract out K8s metrics to external proj, Go 1.17 (#171)
Browse files Browse the repository at this point in the history
Internal K8s metric gathering removed in favour of using jthomperoo/k8shorizmetrics instead.
Upgrade to Go v1.17.
Upgrade K8s client libraries to v0.24.0.
  • Loading branch information
jthomperoo committed May 14, 2022
1 parent 9e1799a commit d73f173
Show file tree
Hide file tree
Showing 41 changed files with 722 additions and 6,663 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.16
- name: Set up Go 1.17
uses: actions/setup-go@v1
with:
go-version: 1.16
go-version: 1.17
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Lint, test and build
run: |
# Get golint
# Get staticcheck
export PATH=$PATH:$(go env GOPATH)/bin
go install golang.org/x/lint/golint@v0.0.0-20201208152925-83fdc39ff7b5
go install honnef.co/go/tools/cmd/staticcheck@v0.3.0
# Lint and test
make vendor_modules
make lint
make beautify
make format
# Exit if after beautification there are any code differences
# Exit if after formatting there are any code differences
git diff --exit-code
make test
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ 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
- Upgraded K8s client libraries to `v0.24.0`.
- Upgraded to Go `v1.17`.
- Extracted out internal K8s metrics calculations to use
[jthomperoo/k8shorizmetrics](https://github.com/jthomperoo/k8shorizmetrics) instead.

## [v2.6.0] - 2022-04-17
### Added
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ the other projects that solved it better and which could serve as inspiration.

Developing this project requires these dependencies:

* [Go](https://golang.org/doc/install) >= `1.16`
* [Golint](https://github.com/golang/lint) == `v0.0.0-20201208152925-83fdc39ff7b5`
* [Go](https://golang.org/doc/install) >= `1.17`
* [staticcheck](https://staticcheck.io/docs/getting-started/) == `v0.3.0 (2022.1)`
* [Docker](https://docs.docker.com/install/)

To view the docs, you need Python 3 installed:
Expand Down
25 changes: 11 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ REGISTRY = custompodautoscaler
NAME = custom-pod-autoscaler
VERSION = latest

default: vendor_modules
default:
@echo "=============Building============="
CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'main.Version=$(VERSION)'" -mod vendor -o dist/$(NAME) main.go
CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'main.Version=$(VERSION)'" -o dist/$(NAME) main.go
cp LICENSE dist/LICENSE

test: vendor_modules
@echo "=============Running unit tests============="
go test -mod vendor ./... -cover -coverprofile unit_cover.out
test:
@echo "=============Running tests============="
go test ./... -cover -coverprofile coverage.out

lint: vendor_modules
lint:
@echo "=============Linting============="
go list -mod vendor ./... | grep -v /vendor/ | xargs -L1 golint -set_exit_status
staticcheck ./...

beautify: vendor_modules
@echo "=============Beautifying============="
format:
@echo "=============Formatting============="
gofmt -s -w .
go mod tidy

Expand All @@ -33,9 +33,6 @@ doc:
@echo "=============Serving docs============="
mkdocs serve

view_coverage:
coverage:
@echo "=============Loading coverage HTML============="
go tool cover -html=unit_cover.out

vendor_modules:
go mod vendor
go tool cover -html=coverage.out
36 changes: 7 additions & 29 deletions config/k8smetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,29 @@ limitations under the License.
package config

import (
autoscaling "k8s.io/api/autoscaling/v2beta2"
v1 "k8s.io/api/core/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
)

// K8sMetricSpec defines which metrics to query from the metrics server
type K8sMetricSpec struct {
Type autoscaling.MetricSourceType `json:"type"`
Object *K8sObjectMetricSource `json:"object,omitempty"`
Pods *K8sPodsMetricSource `json:"pods,omitempty"`
Resource *K8sResourceMetricSource `json:"resource,omitempty"`
External *K8sExternalMetricSource `json:"external,omitempty"`
}
type K8sMetricSpec autoscalingv2.MetricSpec

// K8sMetricTarget defines the type of metric gathering, either target value, average value, or average utilization of a
// specific metric
type K8sMetricTarget struct {
Type autoscaling.MetricTargetType `json:"type"`
}
type K8sMetricTarget autoscalingv2.MetricTarget

// K8sObjectMetricSource defines gathering metrics for a kubernetes object (for example, hits-per-second on an Ingress
// object).
type K8sObjectMetricSource struct {
DescribedObject autoscaling.CrossVersionObjectReference `json:"describedObject"`
Metric autoscaling.MetricIdentifier `json:"metric"`
Target K8sMetricTarget `json:"target"`
}
type K8sObjectMetricSource autoscalingv2.ObjectMetricSource

// K8sPodsMetricSource defines gathering metrics describing each pod in the current scale target (for example,
// transactions-processed-per-second).
type K8sPodsMetricSource struct {
Metric autoscaling.MetricIdentifier `json:"metric"`
Target K8sMetricTarget `json:"target"`
}
type K8sPodsMetricSource autoscalingv2.PodsMetricSource

// K8sResourceMetricSource defines gathering metrics for a resource metric known to Kubernetes, as specified in requests
// and limits, describing each pod in the current scale target (e.g. CPU or memory). Such metrics are built in to
// Kubernetes.
type K8sResourceMetricSource struct {
Name v1.ResourceName `json:"name" protobuf:"bytes,1,name=name"`
Target K8sMetricTarget `json:"target"`
}
type K8sResourceMetricSource autoscalingv2.ResourceMetricSource

// K8sExternalMetricSource defines gathering metrics for a metric not associated with any Kubernetes object (for example
// length of queue in cloud messaging service, or QPS from loadbalancer running outside of cluster).
type K8sExternalMetricSource struct {
Metric autoscaling.MetricIdentifier `json:"metric"`
Target K8sMetricTarget `json:"target"`
}
type K8sExternalMetricSource autoscalingv2.ExternalMetricSource
54 changes: 44 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
module github.com/jthomperoo/custom-pod-autoscaler/v2

go 1.16
go 1.17

require (
github.com/go-chi/chi v4.0.2+incompatible
github.com/golang/glog v1.0.0
github.com/google/go-cmp v0.5.5
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/google/go-cmp v0.5.8
github.com/jthomperoo/k8shorizmetrics v1.0.0
k8s.io/api v0.24.0
k8s.io/apimachinery v0.24.0
k8s.io/client-go v0.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.15.0+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo v1.14.1 // indirect
github.com/onsi/gomega v1.10.2 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.6 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.21.8
k8s.io/apimachinery v0.21.8
k8s.io/client-go v0.21.8
k8s.io/metrics v0.21.8
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220413171646-5e7f5fdc6da6 // indirect
k8s.io/metrics v0.24.0 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

0 comments on commit d73f173

Please sign in to comment.