Skip to content

Commit

Permalink
Remove use of github.com/pkg/errors (#4)
Browse files Browse the repository at this point in the history
Remove use of github.com/pkg/errors
  • Loading branch information
witomlin authored Jan 31, 2024
1 parent c1266c6 commit 9bb7fd2
Show file tree
Hide file tree
Showing 42 changed files with 251 additions and 217 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@
| 1.29 | ✔️ | Alpha |
| 1.28 | ✔️ | Alpha |
| 1.27 || Alpha |

## 0.2.0
2024-02-01

### Removed
- https://github.com/pkg/errors in favor of the `errors` package from the Go standard library.

### Helm Chart
[1.1.0](charts/container-startup-autoscaler/CHANGELOG.md#110)

### Kubernetes Compatibility
| Kube Version | Compatible? | `In-place Update of Pod Resources` Maturity |
|:------------:|:-----------:|:-------------------------------------------:|
| 1.29 | ✔️ | Alpha |
| 1.28 | ✔️ | Alpha |
| 1.27 || Alpha |
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ Each message includes a number of keys that originate from controller-runtime an
- `targetname`: the name of the container to target.
- `targetstates`: the states of the target container, per [status](#status).

Regardless of configured logging verbosity, `error`-level messages are always displayed and additionally include a
stack trace key (`stacktrace`), if available.
Regardless of configured logging verbosity, `error`-level messages are always displayed.

## Metrics
Additional CSA-specific metrics are registered to the Prometheus registry exposed by controller-runtime and exposed
Expand Down
9 changes: 9 additions & 0 deletions charts/container-startup-autoscaler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@

### CSA Version
[0.1.0](../../CHANGELOG.md#010)

## 1.1.0
2024-02-01

### Changed
- CSA version only.

### CSA Version
[0.2.0](../../CHANGELOG.md#020)
4 changes: 2 additions & 2 deletions charts/container-startup-autoscaler/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: container-startup-autoscaler
description: >
container-startup-autoscaler is a Kubernetes controller that modifies the CPU and/or memory resources of containers
depending on whether they're starting up.
version: 1.0.0
appVersion: "0.1.0"
version: 1.1.0
appVersion: "0.2.0"
home: https://github.com/ExpediaGroup/container-startup-autoscaler/README.md
sources:
- https://github.com/ExpediaGroup/container-startup-autoscaler
Expand Down
14 changes: 7 additions & 7 deletions cmd/container-startup-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package main
import (
"os"

"github.com/ExpediaGroup/container-startup-autoscaler/internal/common"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/controller"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/controller/controllercommon"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/logging"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod/podcommon"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -94,29 +94,29 @@ func run(_ *cobra.Command, _ []string) {
// Uses KUBECONFIG env var if set, otherwise tries in-cluster config.
restConfig, err := config.GetConfig()
if err != nil {
logging.Fatale(nil, errors.Wrap(err, "unable to get rest config"))
logging.Fatale(nil, common.WrapErrorf(err, "unable to get rest config"))
}

runtimeManager, err := manager.New(restConfig, options)
if err != nil {
logging.Fatale(nil, errors.Wrap(err, "unable to create controller-runtime manager"))
logging.Fatale(nil, common.WrapErrorf(err, "unable to create controller-runtime manager"))
}

if err = runtimeManager.AddHealthzCheck("liveness", healthz.Ping); err != nil {
logging.Fatale(nil, errors.Wrap(err, "unable to add healthz check"))
logging.Fatale(nil, common.WrapErrorf(err, "unable to add healthz check"))
}

csaController, err := controller.NewController(controllerConfig, runtimeManager)
if err != nil {
logging.Fatale(nil, errors.Wrap(err, "unable to create controller"))
logging.Fatale(nil, common.WrapErrorf(err, "unable to create controller"))
}

if err = csaController.Initialize(); err != nil {
logging.Fatale(nil, errors.Wrap(err, "unable to initialize controller"))
logging.Fatale(nil, common.WrapErrorf(err, "unable to initialize controller"))
}

// Blocks.
if err = runtimeManager.Start(signals.SetupSignalHandler()); err != nil {
logging.Fatale(nil, errors.Wrap(err, "unable to start controller-runtime manager"))
logging.Fatale(nil, common.WrapErrorf(err, "unable to start controller-runtime manager"))
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/go-logr/zerologr v1.2.3
github.com/google/uuid v1.4.0
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.7.0
Expand Down Expand Up @@ -50,6 +49,7 @@ require (
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/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
Expand Down
12 changes: 7 additions & 5 deletions internal/logging/stacktracer.go → internal/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package logging
package common

import "github.com/pkg/errors"
import "fmt"

// stackTracer is implemented within errors that include a stack trace.
type stackTracer interface {
StackTrace() errors.StackTrace
// WrapErrorf returns an error with the supplied format that wraps err. The supplied format is appended with ': %w',
// with %w as err.
func WrapErrorf(err error, format string, a ...any) error {
wrapFormat := fmt.Sprintf("%s: %%w", format)
return fmt.Errorf(wrapFormat, append(a, err)...)
}
48 changes: 48 additions & 0 deletions internal/common/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 Expedia Group, Inc.
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.
*/

package common

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestWrapErrorf(t *testing.T) {
t.Run("WithFormatString", func(t *testing.T) {
err := WrapErrorf(errors.New("non-wrapped err message"), "format (%s)", "test")
assert.Equal(t, "format (test): non-wrapped err message", err.Error())
})

t.Run("WithoutFormatString", func(t *testing.T) {
err := WrapErrorf(errors.New("non-wrapped err message"), "format")
assert.Equal(t, "format: non-wrapped err message", err.Error())
})

t.Run("ChainWithFormatString", func(t *testing.T) {
errInner := WrapErrorf(errors.New("non-wrapped err message"), "errInner format (%s)", "errInner test")
errOuter := WrapErrorf(errInner, "errOuter format (%s)", "errOuter test")
assert.Equal(t, "errOuter format (errOuter test): errInner format (errInner test): non-wrapped err message", errOuter.Error())
})

t.Run("ChainWithoutFormatString", func(t *testing.T) {
errInner := WrapErrorf(errors.New("non-wrapped err message"), "errInner format")
errOuter := WrapErrorf(errInner, "errOuter format")
assert.Equal(t, "errOuter format: errInner format: non-wrapped err message", errOuter.Error())
})
}
2 changes: 1 addition & 1 deletion internal/context/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package context

import (
"context"
"errors"

"github.com/ExpediaGroup/container-startup-autoscaler/internal/context/contextcommon"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod/podcommon"
"github.com/pkg/errors"
)

// WithStandardRetryAttempts adds or replaces contextcommon.KeyStandardRetryAttempts to/in ctx.
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package controller

import (
"errors"

"github.com/ExpediaGroup/container-startup-autoscaler/internal/common"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/controller/controllercommon"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/logging"
csametrics "github.com/ExpediaGroup/container-startup-autoscaler/internal/metrics"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
runtimecontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -97,7 +99,7 @@ func (c *controller) Initialize(runtimeController ...runtimecontroller.Controlle
},
)
if err != nil {
return errors.Wrap(err, "unable to create controller-runtime controller")
return common.WrapErrorf(err, "unable to create controller-runtime controller")
}
} else {
actualRuntimeController = runtimeController[0]
Expand All @@ -114,7 +116,7 @@ func (c *controller) Initialize(runtimeController ...runtimecontroller.Controlle
GenericFunc: PredicateGenericFunc,
},
); err != nil {
return errors.Wrap(err, "unable to watch pods")
return common.WrapErrorf(err, "unable to watch pods")
}

csametrics.RegisterAllMetrics(metrics.Registry, Name)
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package controller

import (
"context"
"errors"
"net/http"
"testing"

"github.com/ExpediaGroup/container-startup-autoscaler/internal/controller/controllercommon"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down
13 changes: 7 additions & 6 deletions internal/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ package controller
import (
"context"
"encoding/json"
"errors"
"sync"

"github.com/ExpediaGroup/container-startup-autoscaler/internal/common"
ccontext "github.com/ExpediaGroup/container-startup-autoscaler/internal/context"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/controller/controllercommon"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/logging"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/metrics/reconciler"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod/podcommon"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
// Reconcilation will still operate correctly in this case as current conditions are always examined.
podExists, kubePod, err := r.pod.KubeHelper.Get(ctx, request.NamespacedName)
if err != nil {
wrappedErr := errors.Wrap(err, "unable to get pod (will requeue)")
wrappedErr := common.WrapErrorf(err, "unable to get pod (will requeue)")
logging.Errore(ctx, wrappedErr)
reconciler.FailureUnableToGetPod().Inc()
return reconcile.Result{RequeueAfter: r.controllerConfig.RequeueDurationSecsDuration()}, nil
Expand All @@ -106,7 +107,7 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
var podJson []byte
podJson, err = json.Marshal(kubePod)
if err != nil {
logging.Errore(ctx, errors.Wrap(err, "unable to marshal pod to json for trace logging"))
logging.Errore(ctx, common.WrapErrorf(err, "unable to marshal pod to json for trace logging"))
} else {
logging.Infof(ctx, logging.VTrace, "reconciling pod: %s", string(podJson))
}
Expand All @@ -121,7 +122,7 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
}
err = r.pod.Validation.Validate(ctx, kubePod, config, afterScaleConfigPopulatedFunc)
if err != nil {
wrappedErr := errors.Wrap(err, "unable to validate pod (won't requeue)")
wrappedErr := common.WrapErrorf(err, "unable to validate pod (won't requeue)")
logging.Errore(ctx, wrappedErr)
reconciler.FailureValidation().Inc()
return reconcile.Result{}, reconcile.TerminalError(wrappedErr)
Expand All @@ -130,7 +131,7 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
// Determine target container states.
states, err := r.pod.TargetContainerState.States(ctx, kubePod, config)
if err != nil {
wrappedErr := errors.Wrap(err, "unable to determine target container states (won't requeue)")
wrappedErr := common.WrapErrorf(err, "unable to determine target container states (won't requeue)")
logging.Errore(ctx, wrappedErr)
reconciler.FailureStatesDetermination().Inc()
return reconcile.Result{}, reconcile.TerminalError(wrappedErr)
Expand All @@ -140,7 +141,7 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
// Execute action for determined target container states.
err = r.pod.TargetContainerAction.Execute(ctx, states, kubePod, config)
if err != nil {
wrappedErr := errors.Wrap(err, "unable to action target container states (won't requeue)")
wrappedErr := common.WrapErrorf(err, "unable to action target container states (won't requeue)")
logging.Errore(ctx, wrappedErr)
reconciler.FailureStatesAction().Inc()
return reconcile.Result{}, reconcile.TerminalError(wrappedErr)
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"bytes"
"errors"
"testing"
"time"

Expand All @@ -28,7 +29,6 @@ import (
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod/podcommon"
"github.com/ExpediaGroup/container-startup-autoscaler/internal/pod/podtest"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
v1 "k8s.io/api/core/v1"
Expand Down
1 change: 0 additions & 1 deletion internal/logging/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ package logging
const (
KeyTargetContainerName = "targetname"
KeyTargetContainerStates = "targetstates"
KeyStackTrace = "stacktrace"
)
Loading

0 comments on commit 9bb7fd2

Please sign in to comment.