Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log simplification and consistency #5

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
- Based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.1.0
2024-01-05
## 0.3.0
2024-02-01

### Added
- Initial version.
### Changed
- Some aspects of logging for simplification and consistency purposes.

### Helm Chart
[1.0.0](charts/container-startup-autoscaler/CHANGELOG.md#100)
[1.2.0](charts/container-startup-autoscaler/CHANGELOG.md#120)

### Kubernetes Compatibility
| Kube Version | Compatible? | `In-place Update of Pod Resources` Maturity |
Expand All @@ -33,3 +33,20 @@
| 1.29 | ✔️ | Alpha |
nikos912000 marked this conversation as resolved.
Show resolved Hide resolved
| 1.28 | ✔️ | Alpha |
| 1.27 | ❌ | Alpha |

## 0.1.0
2024-01-05

### Added
- Initial version.

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

### Kubernetes Compatibility
| Kube Version | Compatible? | `In-place Update of Pod Resources` Maturity |
|:------------:|:-----------:|:-------------------------------------------:|
| 1.29 | ✔️ | Alpha |
| 1.28 | ✔️ | Alpha |
| 1.27 | ❌ | Alpha |

21 changes: 15 additions & 6 deletions charts/container-startup-autoscaler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
- Based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0.0
nikos912000 marked this conversation as resolved.
Show resolved Hide resolved
2024-01-05
## 1.2.0
2024-02-01

### Added
- Initial version.
### Changed
- CSA version only.

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

## 1.1.0
2024-02-01
Expand All @@ -18,4 +18,13 @@
- CSA version only.

### CSA Version
[0.2.0](../../CHANGELOG.md#020)
[0.2.0](../../CHANGELOG.md#020)

## 1.0.0
2024-01-05

### Added
- Initial version.

### CSA Version
[0.1.0](../../CHANGELOG.md#010)
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.1.0
appVersion: "0.2.0"
version: 1.2.0
appVersion: "0.3.0"
home: https://github.com/ExpediaGroup/container-startup-autoscaler/README.md
sources:
- https://github.com/ExpediaGroup/container-startup-autoscaler
Expand Down
13 changes: 6 additions & 7 deletions cmd/container-startup-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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"
Expand Down Expand Up @@ -94,29 +93,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, common.WrapErrorf(err, "unable to get rest config"))
logging.Fatalf(nil, err, "unable to get rest config")
}

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

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

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

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

// Blocks.
if err = runtimeManager.Start(signals.SetupSignalHandler()); err != nil {
logging.Fatale(nil, common.WrapErrorf(err, "unable to start controller-runtime manager"))
logging.Fatalf(nil, err, "unable to start controller-runtime manager")
}
}
25 changes: 12 additions & 13 deletions internal/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,14 @@ 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 := common.WrapErrorf(err, "unable to get pod (will requeue)")
logging.Errore(ctx, wrappedErr)
logging.Errorf(ctx, err, "unable to get pod (will requeue)")
reconciler.FailureUnableToGetPod().Inc()
return reconcile.Result{RequeueAfter: r.controllerConfig.RequeueDurationSecsDuration()}, nil
}

if !podExists {
err = errors.New("pod doesn't exist (won't requeue)")
logging.Errore(ctx, err)
logging.Errorf(ctx, err, err.Error())
reconciler.FailurePodDoesntExist().Inc()
return reconcile.Result{}, reconcile.TerminalError(err)
}
Expand All @@ -107,7 +106,7 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
var podJson []byte
podJson, err = json.Marshal(kubePod)
if err != nil {
logging.Errore(ctx, common.WrapErrorf(err, "unable to marshal pod to json for trace logging"))
logging.Errorf(ctx, err, "unable to marshal pod to json for trace logging")
} else {
logging.Infof(ctx, logging.VTrace, "reconciling pod: %s", string(podJson))
}
Expand All @@ -122,29 +121,29 @@ func (r *containerStartupAutoscalerReconciler) Reconcile(
}
err = r.pod.Validation.Validate(ctx, kubePod, config, afterScaleConfigPopulatedFunc)
if err != nil {
wrappedErr := common.WrapErrorf(err, "unable to validate pod (won't requeue)")
logging.Errore(ctx, wrappedErr)
msg := "unable to validate pod (won't requeue)"
logging.Errorf(ctx, err, msg)
reconciler.FailureValidation().Inc()
return reconcile.Result{}, reconcile.TerminalError(wrappedErr)
return reconcile.Result{}, reconcile.TerminalError(common.WrapErrorf(err, msg))
}

// Determine target container states.
states, err := r.pod.TargetContainerState.States(ctx, kubePod, config)
if err != nil {
wrappedErr := common.WrapErrorf(err, "unable to determine target container states (won't requeue)")
logging.Errore(ctx, wrappedErr)
msg := "unable to determine target container states (won't requeue)"
logging.Errorf(ctx, err, msg)
reconciler.FailureStatesDetermination().Inc()
return reconcile.Result{}, reconcile.TerminalError(wrappedErr)
return reconcile.Result{}, reconcile.TerminalError(common.WrapErrorf(err, msg))
}
ctx = ccontext.WithTargetContainerStates(ctx, states)

// Execute action for determined target container states.
err = r.pod.TargetContainerAction.Execute(ctx, states, kubePod, config)
if err != nil {
wrappedErr := common.WrapErrorf(err, "unable to action target container states (won't requeue)")
logging.Errore(ctx, wrappedErr)
msg := "unable to action target container states (won't requeue)"
logging.Errorf(ctx, err, msg)
reconciler.FailureStatesAction().Inc()
return reconcile.Result{}, reconcile.TerminalError(wrappedErr)
return reconcile.Result{}, reconcile.TerminalError(common.WrapErrorf(err, msg))
}

return reconcile.Result{}, nil
Expand Down
10 changes: 0 additions & 10 deletions internal/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,12 @@ func configureLogger(w io.Writer, v V, addCaller bool) {
CurrentV = v
}

// Errore logs err. The resulting message is the err's message.
func Errore(ctx context.Context, err error) {
Errorf(ctx, err, "%s", err)
}

// Errorf logs err with a formatted message.
func Errorf(ctx context.Context, err error, format string, args ...any) {
validateFormat(format)
configuredLogger(ctx).Error(err, buildMessage(format, args, false))
}

// Fatale logs err and exits with a non-0 return code. The resulting message is the err's message.
func Fatale(ctx context.Context, err error) {
Fatalf(ctx, err, "%s", err)
}

// Fatalf logs err with a formatted message and exits with a non-0 return code.
func Fatalf(ctx context.Context, err error, format string, args ...any) {
validateFormat(format)
Expand Down
46 changes: 0 additions & 46 deletions internal/logging/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,6 @@ func TestInit(t *testing.T) {
})
}

func TestErrore(t *testing.T) {
tests := []test{
{
name: "PodInfo",
args: args{
ctx: testContextPodInfo(),
err: errors.New(testErrorMsg),
},
wantLogRxConfig: wantLogRxConfig{
wantLevelRx: wantErrorLevelRx,
wantMsgRx: testErrorMsg,
wantTargetNameRx: testTargetContainerName,
wantTargetStates: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
runTest(t, tt, func() { Errore(tt.args.ctx, tt.args.err) })
})
}
}

func TestErrorf(t *testing.T) {
tests := []test{
{
Expand Down Expand Up @@ -171,29 +148,6 @@ func TestErrorf(t *testing.T) {
}
}

func TestFatale(t *testing.T) {
tests := []test{
{
name: "PodInfo",
args: args{
ctx: testContextPodInfo(),
err: errors.New(testErrorMsg),
},
wantLogRxConfig: wantLogRxConfig{
wantLevelRx: wantErrorLevelRx,
wantMsgRx: testErrorMsg + fatalSuffixRx,
wantTargetNameRx: testTargetContainerName,
wantTargetStates: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
runTest(t, tt, func() { Fatale(tt.args.ctx, tt.args.err) })
})
}
}

func TestFatalf(t *testing.T) {
tests := []test{
{
Expand Down
Loading