Skip to content

Commit

Permalink
Update module path to v3 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomperoo authored Mar 21, 2024
1 parent b353e74 commit 0a0d438
Show file tree
Hide file tree
Showing 45 changed files with 195 additions and 155 deletions.
50 changes: 45 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build](https://github.com/jthomperoo/k8shorizmetrics/workflows/main/badge.svg)](https://github.com/jthomperoo/k8shorizmetrics/actions)
[![go.dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/jthomperoo/k8shorizmetrics/v2)
[![go.dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/jthomperoo/k8shorizmetrics/v3)
[![Go Report
Card](https://goreportcard.com/badge/github.com/jthomperoo/k8shorizmetrics/v2)](https://goreportcard.com/report/github.com/jthomperoo/k8shorizmetrics/v2)
Card](https://goreportcard.com/badge/github.com/jthomperoo/k8shorizmetrics/v3)](https://goreportcard.com/report/github.com/jthomperoo/k8shorizmetrics/v3)
[![License](https://img.shields.io/:license-apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)

# k8shorizmetrics
Expand All @@ -13,7 +13,7 @@ to work out the target replica count that the HPA does.
## Install

```bash
go get -u github.com/jthomperoo/k8shorizmetrics/v2@v2.0.2
go get -u github.com/jthomperoo/k8shorizmetrics/v3@v3.0.0
```

## Features
Expand Down Expand Up @@ -89,15 +89,55 @@ func main() {

## Documentation

See the [Go doc](https://pkg.go.dev/github.com/jthomperoo/k8shorizmetrics/v2).
See the [Go doc](https://pkg.go.dev/github.com/jthomperoo/k8shorizmetrics/v3).

## Migration from v1 to v2
## Migration

This section explains how to migrate between versions of the library.

### From v1 to v2

There are two changes you need to make to migrate from `v1` to `v2`:

1. Switch from using `k8s.io/api/autoscaling/v2beta2` to `k8s.io/api/autoscaling/v2`.
2. Switch from using `github.com/jthomperoo/k8shorizmetrics` to `github.com/jthomperoo/k8shorizmetrics/v2`.

### From v2 to v3

The breaking changes introduced by `v3` are:

- Gather now returns the `GathererMultiMetricError` error type if any of the metrics fail to gather. This error is
returned for partial errors, meaning some metrics gathered successfully and others did not. If this partial error
occurs the `GathererMultiMetricError` error will have the `Partial` property set to `true`. This can be checked for
using `errors.As`.
- Evaluate now returns the `EvaluatorMultiMetricError` error type if any of the metrics fail to
evaluate. This error is returned for partial errors, meaning some metrics evaluted successfully and others did not.
If this partial error occurs the `EvaluatorMultiMetricError` error will have the `Partial` property set to `true`. This
can be checked for using `errors.As`.

To update to `v3` you will need to update all references in your code that refer to
`github.com/jthomperoo/k8shorizmetrics/v2` to use `github.com/jthomperoo/k8shorizmetrics/v3`.

If you want the behaviour to stay the same and to swallow partial errors you can use code like this:

```go
metrics, err := gather.Gather(specs, namespace, podMatchSelector)
if err != nil {
gatherErr := &k8shorizmetrics.GathererMultiMetricError{}
if !errors.As(err, &gatherErr) {
log.Fatal(err)
}

if !gatherErr.Partial {
log.Fatal(err)
}

// Not a partial error, just continue as normal
}
```

You can use similar code for the `Evaluate` method of the `Evaluater`.

## Examples

See the [examples directory](./examples/) for some examples, [cpuprint](./examples/cpuprint/) is a good start.
Expand Down
12 changes: 6 additions & 6 deletions evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ package k8shorizmetrics
import (
"fmt"

"github.com/jthomperoo/k8shorizmetrics/v2/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/pods"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/resource"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/pods"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/resource"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
autoscalingv2 "k8s.io/api/autoscaling/v2"
)

Expand Down
6 changes: 3 additions & 3 deletions evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/jthomperoo/k8shorizmetrics/v2"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
"github.com/jthomperoo/k8shorizmetrics/v3"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
v2 "k8s.io/api/autoscaling/v2"
)

Expand Down
4 changes: 2 additions & 2 deletions examples/cpuandmemoryreplicaprint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/jthomperoo/k8shorizmetrics/examples/cpuandmemoryreplicaprint
go 1.21

require (
github.com/jthomperoo/k8shorizmetrics/v2 v2.0.2
github.com/jthomperoo/k8shorizmetrics/v3 v3.0.0
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
Expand Down Expand Up @@ -55,4 +55,4 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/jthomperoo/k8shorizmetrics/v2 => ../../
replace github.com/jthomperoo/k8shorizmetrics/v3 => ../../
6 changes: 3 additions & 3 deletions examples/cpuandmemoryreplicaprint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"path/filepath"
"time"

"github.com/jthomperoo/k8shorizmetrics/v2"
"github.com/jthomperoo/k8shorizmetrics/v2/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v2/podsclient"
"github.com/jthomperoo/k8shorizmetrics/v3"
"github.com/jthomperoo/k8shorizmetrics/v3/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v3/podsclient"
v2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
4 changes: 2 additions & 2 deletions examples/cpuprint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/jthomperoo/k8shorizmetrics/examples/cpuprint
go 1.21

require (
github.com/jthomperoo/k8shorizmetrics/v2 v2.0.2
github.com/jthomperoo/k8shorizmetrics/v3 v3.0.0
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
Expand Down Expand Up @@ -55,4 +55,4 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/jthomperoo/k8shorizmetrics/v2 => ../../
replace github.com/jthomperoo/k8shorizmetrics/v3 => ../../
6 changes: 3 additions & 3 deletions examples/cpuprint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"path/filepath"
"time"

"github.com/jthomperoo/k8shorizmetrics/v2"
"github.com/jthomperoo/k8shorizmetrics/v2/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v2/podsclient"
"github.com/jthomperoo/k8shorizmetrics/v3"
"github.com/jthomperoo/k8shorizmetrics/v3/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v3/podsclient"
v2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down
4 changes: 2 additions & 2 deletions examples/cpureplicaprint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/jthomperoo/k8shorizmetrics/examples/cpureplicaprint
go 1.21

require (
github.com/jthomperoo/k8shorizmetrics/v2 v2.0.2
github.com/jthomperoo/k8shorizmetrics/v3 v3.0.0
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
Expand Down Expand Up @@ -55,4 +55,4 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/jthomperoo/k8shorizmetrics/v2 => ../../
replace github.com/jthomperoo/k8shorizmetrics/v3 => ../../
6 changes: 3 additions & 3 deletions examples/cpureplicaprint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"path/filepath"
"time"

"github.com/jthomperoo/k8shorizmetrics/v2"
"github.com/jthomperoo/k8shorizmetrics/v2/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v2/podsclient"
"github.com/jthomperoo/k8shorizmetrics/v3"
"github.com/jthomperoo/k8shorizmetrics/v3/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v3/podsclient"
v2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
22 changes: 11 additions & 11 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ import (
"fmt"
"time"

"github.com/jthomperoo/k8shorizmetrics/v2/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/pods"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/resource"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/external"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/object"
podsmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/pods"
resourcemetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/resource"
metricsclient "github.com/jthomperoo/k8shorizmetrics/v2/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/pods"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/resource"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/external"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/object"
podsmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/pods"
resourcemetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/resource"
metricsclient "github.com/jthomperoo/k8shorizmetrics/v3/metricsclient"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
20 changes: 10 additions & 10 deletions gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/jthomperoo/k8shorizmetrics/v2"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/testutil"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/object"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/podmetrics"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/pods"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/resource"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/value"
"github.com/jthomperoo/k8shorizmetrics/v3"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/testutil"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/object"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/podmetrics"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/pods"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/resource"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/value"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/jthomperoo/k8shorizmetrics/v2
module github.com/jthomperoo/k8shorizmetrics/v3

go 1.21

Expand Down
4 changes: 2 additions & 2 deletions internal/external/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"fmt"
"math"

"github.com/jthomperoo/k8shorizmetrics/v2/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
)

// Evaluate (external) calculates a replica count evaluation, using the tolerance and calculater provided
Expand Down
14 changes: 7 additions & 7 deletions internal/external/evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/testutil"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/value"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/testutil"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/value"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
)
Expand Down
8 changes: 4 additions & 4 deletions internal/external/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ package external
import (
"fmt"

"github.com/jthomperoo/k8shorizmetrics/v2/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/value"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/value"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

metricsclient "github.com/jthomperoo/k8shorizmetrics/v2/metricsclient"
metricsclient "github.com/jthomperoo/k8shorizmetrics/v3/metricsclient"
"k8s.io/apimachinery/pkg/labels"
)

Expand Down
14 changes: 7 additions & 7 deletions internal/external/gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/testutil"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/value"
metricsclient "github.com/jthomperoo/k8shorizmetrics/v2/metricsclient"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/external"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/podutil"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/testutil"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/external"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/value"
metricsclient "github.com/jthomperoo/k8shorizmetrics/v3/metricsclient"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package fake
import (
"time"

"github.com/jthomperoo/k8shorizmetrics/v2/metrics/podmetrics"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/podmetrics"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down
2 changes: 1 addition & 1 deletion internal/fake/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package fake

import (
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
)

// ExternalEvaluater (fake) provides a way to insert functionality into a ExternalEvaluater
Expand Down
8 changes: 4 additions & 4 deletions internal/fake/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package fake
import (
"time"

externalmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/external"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/object"
podsmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/pods"
resourcemetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/resource"
externalmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/external"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/object"
podsmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/pods"
resourcemetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/resource"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion internal/fake/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package fake

import (
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/podmetrics"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/podmetrics"
"k8s.io/apimachinery/pkg/util/sets"
)

Expand Down
4 changes: 2 additions & 2 deletions internal/object/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"fmt"
"math"

"github.com/jthomperoo/k8shorizmetrics/v2/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
autoscaling "k8s.io/api/autoscaling/v2"
)

Expand Down
14 changes: 7 additions & 7 deletions internal/object/evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v2/internal/testutil"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v2/metrics/object"
"github.com/jthomperoo/k8shorizmetrics/v2/metrics/value"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/fake"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/object"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/replicas"
"github.com/jthomperoo/k8shorizmetrics/v3/internal/testutil"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics"
objectmetrics "github.com/jthomperoo/k8shorizmetrics/v3/metrics/object"
"github.com/jthomperoo/k8shorizmetrics/v3/metrics/value"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
)
Expand Down
Loading

0 comments on commit 0a0d438

Please sign in to comment.