Skip to content

Commit

Permalink
fix: improve cmp + msg + constant
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Oct 29, 2024
1 parent e9b30ad commit 263271e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/cleaner/cleanup-vpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/SocialGouv/oblik/pkg/client"
"github.com/SocialGouv/oblik/pkg/config"
ovpa "github.com/SocialGouv/oblik/pkg/vpa"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -27,7 +28,7 @@ func CleanUpVPAs(ctx context.Context, kubeClients *client.KubeClients) {
}

for _, vpa := range vpaList.Items {
if strings.HasPrefix(vpa.Name, "oblik-") {
if strings.HasPrefix(vpa.Name, config.VpaPrefix) {
if err := processVPA(ctx, kubeClients, &vpa); err != nil {
klog.Errorf("Error processing VPA %s: %s\n", vpa.Name, err.Error())
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
const defaultCron = "0 2 * * *"
const defaultCronAddRandomMax = "120m"

const VpaPrefix = "oblik-"

type ApplyMode int

const (
Expand Down
11 changes: 10 additions & 1 deletion pkg/config/strategy-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"strings"
"time"

"github.com/SocialGouv/oblik/pkg/calculator"
Expand All @@ -11,7 +12,15 @@ import (
)

func CreateStrategyConfig(configurable *Configurable) *StrategyConfig {
key := fmt.Sprintf("%s/%s", configurable.GetNamespace(), configurable.GetName())
workloadName := configurable.GetName()

if strings.HasPrefix(workloadName, VpaPrefix) {
workloadName = strings.TrimPrefix(workloadName, VpaPrefix)
workloadKind := strings.Split(workloadName, "-")[0]
workloadName = strings.TrimPrefix(workloadName, workloadKind+"-")
}

key := fmt.Sprintf("%s/%s", configurable.GetNamespace(), workloadName)
cfg := &StrategyConfig{
Key: key,
LoadCfg: &LoadCfg{
Expand Down
8 changes: 4 additions & 4 deletions pkg/logical/set-containers-resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func setContainerCpuRequest(container *corev1.Container, containerRequestRecomme
if scfg.GetRequestCpuScaleDirection(containerName) == config.ScaleDirectionUp && newCPURequest.Cmp(cpuRequest) == -1 {
newCPURequest = cpuRequest
}
if scfg.GetRequestCPUApplyMode(containerName) == config.ApplyModeEnforce && newCPURequest.String() != cpuRequest.String() {
if scfg.GetRequestCPUApplyMode(containerName) == config.ApplyModeEnforce && newCPURequest.Cmp(cpuRequest) != 0 {
changes = append(changes, reporting.Change{
Old: cpuRequest,
New: newCPURequest,
Expand Down Expand Up @@ -83,7 +83,7 @@ func setContainerCpuLimit(container *corev1.Container, containerRequestRecommend
if scfg.GetLimitCpuScaleDirection(containerName) == config.ScaleDirectionUp && newCPULimit.Cmp(cpuLimit) == -1 {
newCPULimit = cpuLimit
}
if scfg.GetLimitCPUApplyMode(containerName) == config.ApplyModeEnforce && newCPULimit.String() != cpuLimit.String() {
if scfg.GetLimitCPUApplyMode(containerName) == config.ApplyModeEnforce && newCPULimit.Cmp(cpuLimit) != 0 {
changes = append(changes, reporting.Change{
Old: cpuLimit,
New: newCPULimit,
Expand Down Expand Up @@ -128,7 +128,7 @@ func setContainerMemoryRequest(container *corev1.Container, containerRequestReco
if scfg.GetRequestMemoryScaleDirection(containerName) == config.ScaleDirectionUp && newMemoryRequest.Cmp(memoryRequest) == -1 {
newMemoryRequest = memoryRequest
}
if scfg.GetRequestMemoryApplyMode(containerName) == config.ApplyModeEnforce && newMemoryRequest.String() != memoryRequest.String() {
if scfg.GetRequestMemoryApplyMode(containerName) == config.ApplyModeEnforce && newMemoryRequest.Cmp(memoryRequest) != 0 {
changes = append(changes, reporting.Change{
Old: memoryRequest,
New: newMemoryRequest,
Expand Down Expand Up @@ -175,7 +175,7 @@ func setContainerMemoryLimit(container *corev1.Container, containerRequestRecomm
if scfg.GetLimitMemoryScaleDirection(containerName) == config.ScaleDirectionUp && newMemoryLimit.Cmp(memoryLimit) == -1 {
newMemoryLimit = memoryLimit
}
if scfg.GetLimitMemoryApplyMode(containerName) == config.ApplyModeEnforce && newMemoryLimit.String() != memoryLimit.String() {
if scfg.GetLimitMemoryApplyMode(containerName) == config.ApplyModeEnforce && newMemoryLimit.Cmp(memoryLimit) != 0 {
changes = append(changes, reporting.Change{
Old: memoryLimit,
New: newMemoryLimit,
Expand Down
3 changes: 2 additions & 1 deletion pkg/vpa/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"github.com/SocialGouv/oblik/pkg/config"
"github.com/SocialGouv/oblik/pkg/utils"
autoscaling "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -18,7 +19,7 @@ import (
)

func GenerateVPAName(kind, name string) string {
vpaName := fmt.Sprintf("oblik-%s-%s", strings.ToLower(kind), name)
vpaName := fmt.Sprintf("%s%s-%s", config.VpaPrefix, strings.ToLower(kind), name)
if len(vpaName) > 63 {
hash := sha256.Sum256([]byte(vpaName))
truncatedHash := fmt.Sprintf("%x", hash)[:8]
Expand Down

0 comments on commit 263271e

Please sign in to comment.