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

Enabled pprof + upgraded controller runtime #88

Merged
merged 30 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3a9fbc9
Client Registry introduced and refactored code to handle deployments/…
deefreak Oct 3, 2023
6724f3f
HPA support
deefreak Oct 13, 2023
874fed6
feature flag to enable rollout
deefreak Oct 17, 2023
495c64a
Refactoring of code to support both scaledobjects and hpa for autosca…
deefreak Oct 18, 2023
b3c98b3
minor change
deefreak Oct 27, 2023
374cb8a
minor change
deefreak Oct 27, 2023
165fa96
refactored code and added support for autoscaling/v2 api
deefreak Oct 29, 2023
82bffa9
minor test fix
deefreak Oct 29, 2023
e37f6e6
refactored code
deefreak Oct 29, 2023
4b6dfc5
refactored code
deefreak Oct 29, 2023
deaa72d
refactored code
deefreak Oct 29, 2023
f4db816
refactored code
deefreak Oct 29, 2023
43d2475
upgrading controller-runtime to 0.14.7
bharathguvvala Oct 30, 2023
a8bf4d0
upgrading controller-runtime to 0.15.0 along with code changes to com…
bharathguvvala Oct 30, 2023
f06e257
upgrading controller-runtime to 0.15.3 along with code changes to com…
bharathguvvala Oct 30, 2023
e32c495
added finalizer and handled delete/update operations for policies
deefreak Nov 6, 2023
4a267b8
added finalizer and handled delete/update operations for policies
deefreak Nov 6, 2023
9520002
added finalizer and handled delete/update operations for policies
deefreak Nov 6, 2023
202817f
added finalizer and handled delete/update operations for policies
deefreak Nov 6, 2023
555e52b
added finalizer and handled delete/update operations for policies
deefreak Nov 6, 2023
78c3a3d
test cases added for client registry
deefreak Nov 7, 2023
a6c4797
added test for autoscaler clients
deefreak Nov 8, 2023
aa53de3
added test for autoscaler clients
deefreak Nov 8, 2023
3135f73
added unit tests for policy controller reconciliation
deefreak Nov 8, 2023
7f81af7
added unit tests for policy controller reconciliation
deefreak Nov 8, 2023
f843db9
Merge pull request #81 from deefreak/ossrefactoring
deefreak Nov 9, 2023
d494660
merge conflict resolved
deefreak Nov 9, 2023
14fb7dd
Merge pull request #90 from deefreak/policyControllerChanges
deefreak Nov 9, 2023
0cd5551
Merge pull request #84 from deefreak/ossrefactoringhpa
deefreak Nov 9, 2023
63c13ec
merge conflict resolved
deefreak Nov 9, 2023
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
37 changes: 32 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"context"
"flag"
argov1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/flipkart-incubator/ottoscalr/pkg/autoscaler"
"github.com/flipkart-incubator/ottoscalr/pkg/controller"
"github.com/flipkart-incubator/ottoscalr/pkg/integration"
"github.com/flipkart-incubator/ottoscalr/pkg/metrics"
"github.com/flipkart-incubator/ottoscalr/pkg/policy"
"github.com/flipkart-incubator/ottoscalr/pkg/reco"
"github.com/flipkart-incubator/ottoscalr/pkg/registry"
"github.com/flipkart-incubator/ottoscalr/pkg/transformer"
"github.com/flipkart-incubator/ottoscalr/pkg/trigger"
kedaapi "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
Expand Down Expand Up @@ -127,6 +129,11 @@ type Config struct {
EventScaleUpBufferPeriodInHours int `yaml:"eventScaleUpBufferPeriodInHours"`
CustomEventDataConfigMapName string `yaml:"customEventDataConfigMapName"`
} `yaml:"eventCallIntegration"`
AutoscalerClient struct {
EnableScaledObject *bool `yaml:"enableScaledObject"`
HpaAPIVersion string `yaml:"hpaAPIVersion"`
} `yaml:"autoscalerClient"`
EnableArgoRolloutsSupport *bool `yaml:"enableArgoRolloutsSupport"`
}

func main() {
Expand Down Expand Up @@ -164,6 +171,7 @@ func main() {
MetricsBindAddress: config.MetricBindAddress,
Port: config.Port,
HealthProbeBindAddress: config.HealthProbeBindAddress,
PprofBindAddress: config.MetricBindAddress,
LeaderElection: config.EnableLeaderElection,
LeaderElectionID: config.LeaderElectionID,
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
Expand Down Expand Up @@ -244,7 +252,14 @@ func main() {

metricsTransformer = append(metricsTransformer, outlierInterpolatorTransformer)
}
deploymentClientRegistryBuilder := registry.NewDeploymentClientRegistryBuilder().
WithK8sClient(mgr.GetClient()).
WithCustomDeploymentClient(registry.NewDeploymentClient(mgr.GetClient()))

if *config.EnableArgoRolloutsSupport {
deploymentClientRegistryBuilder = deploymentClientRegistryBuilder.WithCustomDeploymentClient(registry.NewRolloutClient(mgr.GetClient()))
}
deploymentClientRegistry := deploymentClientRegistryBuilder.Build()
cpuUtilizationBasedRecommender := reco.NewCpuUtilizationBasedRecommender(mgr.GetClient(),
config.BreachMonitor.CpuRedLine,
time.Duration(config.CpuUtilizationBasedRecommender.MetricWindowInDays)*24*time.Hour,
Expand All @@ -254,6 +269,7 @@ func main() {
config.CpuUtilizationBasedRecommender.MinTarget,
config.CpuUtilizationBasedRecommender.MaxTarget,
config.CpuUtilizationBasedRecommender.MetricsPercentageThreshold,
*deploymentClientRegistry,
logger)

breachAnalyzer, err := reco.NewBreachAnalyzer(mgr.GetClient(), scraper, config.BreachMonitor.CpuRedLine, time.Duration(config.BreachMonitor.StepSec)*time.Second)
Expand All @@ -278,7 +294,7 @@ func main() {
os.Exit(1)
}

deploymentTriggerReconciler := controller.NewDeploymentTriggerController(mgr.GetClient(), mgr.GetScheme())
deploymentTriggerReconciler := controller.NewDeploymentTriggerController(mgr.GetClient(), mgr.GetScheme(), *deploymentClientRegistry)
if err = deploymentTriggerReconciler.
SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DeploymentController")
Expand All @@ -305,9 +321,19 @@ func main() {
hpaEnforcerExcludedNamespaces := parseCommaSeparatedValues(config.HPAEnforcer.ExcludedNamespaces)
hpaEnforcerIncludedNamespaces := parseCommaSeparatedValues(config.HPAEnforcer.IncludedNamespaces)

var autoscalerClient autoscaler.AutoscalerClient
if *config.AutoscalerClient.EnableScaledObject {
autoscalerClient = autoscaler.NewScaledobjectClient(mgr.GetClient())
} else {
if config.AutoscalerClient.HpaAPIVersion == "v2" {
autoscalerClient = autoscaler.NewHPAClientV2(mgr.GetClient())
} else {
autoscalerClient = autoscaler.NewHPAClient(mgr.GetClient())
}
}
hpaEnforcementController, err := controller.NewHPAEnforcementController(mgr.GetClient(),
mgr.GetScheme(), mgr.GetEventRecorderFor(controller.HPAEnforcementCtrlName),
config.HPAEnforcer.MaxConcurrentReconciles, config.HPAEnforcer.IsDryRun, &hpaEnforcerExcludedNamespaces, &hpaEnforcerIncludedNamespaces, config.HPAEnforcer.WhitelistMode, config.HPAEnforcer.MinRequiredReplicas)
mgr.GetScheme(),*deploymentClientRegistry, mgr.GetEventRecorderFor(controller.HPAEnforcementCtrlName),
config.HPAEnforcer.MaxConcurrentReconciles, config.HPAEnforcer.IsDryRun, &hpaEnforcerExcludedNamespaces, &hpaEnforcerIncludedNamespaces, config.HPAEnforcer.WhitelistMode, config.HPAEnforcer.MinRequiredReplicas, autoscalerClient)
if err != nil {
setupLog.Error(err, "Unable to initialize HPA enforcement controller")
os.Exit(1)
Expand All @@ -323,15 +349,16 @@ func main() {
mgr.GetScheme(),
config.PolicyRecommendationRegistrar.RequeueDelayMs,
monitorManager,
policyStore, excludedNamespaces, includedNamespaces).SetupWithManager(mgr); err != nil {
policyStore, *deploymentClientRegistry, excludedNamespaces, includedNamespaces).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller",
"controller", "PolicyRecommendationRegistration")
os.Exit(1)
}

if err = controller.NewPolicyWatcher(mgr.GetClient(),
mgr.GetScheme(),
triggerHandler.QueueAllForExecution).SetupWithManager(mgr); err != nil {
triggerHandler.QueueAllForExecution,
triggerHandler.QueueForExecution).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Policy")
os.Exit(1)
}
Expand Down
28 changes: 11 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/flipkart-incubator/ottoscalr

go 1.19
go 1.20

require (
github.com/argoproj/argo-rollouts v1.4.1
Expand All @@ -12,21 +12,15 @@ require (
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/common v0.44.0
github.com/spf13/viper v1.15.0
golang.org/x/net v0.11.0
golang.org/x/net v0.17.0
golang.org/x/sync v0.2.0
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
sigs.k8s.io/controller-runtime v0.14.6
k8s.io/api v0.27.7
k8s.io/apimachinery v0.27.7
k8s.io/client-go v0.27.7
sigs.k8s.io/controller-runtime v0.15.3

)

replace (
k8s.io/api v0.27.2 => k8s.io/api v0.26.3
k8s.io/apimachinery v0.27.2 => k8s.io/apimachinery v0.26.3
k8s.io/client-go v0.27.2 => k8s.io/client-go v0.26.3
)

require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -74,9 +68,9 @@ require (
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/oauth2 v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
Expand All @@ -86,8 +80,8 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.27.2 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/apiextensions-apiserver v0.27.7 // indirect
k8s.io/component-base v0.27.7 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
Expand Down
42 changes: 21 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
Expand Down Expand Up @@ -379,8 +379,8 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -443,11 +443,11 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -456,8 +456,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -647,16 +647,16 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.26.3 h1:emf74GIQMTik01Aum9dPP0gAypL8JTLl/lHa4V9RFSU=
k8s.io/api v0.26.3/go.mod h1:PXsqwPMXBSBcL1lJ9CYDKy7kIReUydukS5JiRlxC3qE=
k8s.io/apiextensions-apiserver v0.27.2 h1:iwhyoeS4xj9Y7v8YExhUwbVuBhMr3Q4bd/laClBV6Bo=
k8s.io/apiextensions-apiserver v0.27.2/go.mod h1:Oz9UdvGguL3ULgRdY9QMUzL2RZImotgxvGjdWRq6ZXQ=
k8s.io/apimachinery v0.26.3 h1:dQx6PNETJ7nODU3XPtrwkfuubs6w7sX0M8n61zHIV/k=
k8s.io/apimachinery v0.26.3/go.mod h1:ats7nN1LExKHvJ9TmwootT00Yz05MuYqPXEXaVeOy5I=
k8s.io/client-go v0.26.3 h1:k1UY+KXfkxV2ScEL3gilKcF7761xkYsSD6BC9szIu8s=
k8s.io/client-go v0.26.3/go.mod h1:ZPNu9lm8/dbRIPAgteN30RSXea6vrCpFvq+MateTUuQ=
k8s.io/component-base v0.27.2 h1:neju+7s/r5O4x4/txeUONNTS9r1HsPbyoPBAtHsDCpo=
k8s.io/component-base v0.27.2/go.mod h1:5UPk7EjfgrfgRIuDBFtsEFAe4DAvP3U+M8RTzoSJkpo=
k8s.io/api v0.27.7 h1:7yG4D3t/q4utJe2ptlRw9aPuxcSmroTsYxsofkQNl/A=
k8s.io/api v0.27.7/go.mod h1:ZNExI/Lhrs9YrLgVWx6jjHZdoWCTXfBXuFjt1X6olro=
k8s.io/apiextensions-apiserver v0.27.7 h1:YqIOwZAUokzxJIjunmUd4zS1v3JhK34EPXn+pP0/bsU=
k8s.io/apiextensions-apiserver v0.27.7/go.mod h1:x0p+b5a955lfPz9gaDeBy43obM12s+N9dNHK6+dUL+g=
k8s.io/apimachinery v0.27.7 h1:Gxgtb7Y/Rsu8ymgmUEaiErkxa6RY4oTd8kNUI6SUR58=
k8s.io/apimachinery v0.27.7/go.mod h1:jBGQgTjkw99ef6q5hv1YurDd3BqKDk9YRxmX0Ozo0i8=
k8s.io/client-go v0.27.7 h1:+Xgh9OOKv6A3qdD4Dnl/0VOI5EvAv+0s/OseDxVVTwQ=
k8s.io/client-go v0.27.7/go.mod h1:dZ2kqcalYp5YZ2EV12XIMc77G6PxHWOJp/kclZr4+5Q=
k8s.io/component-base v0.27.7 h1:kngM58HR9W9Nqpv7e4rpdRyWnKl/ABpUhLAZ+HoliMs=
k8s.io/component-base v0.27.7/go.mod h1:YGjlCVL1oeKvG3HSciyPHFh+LCjIEqsxz4BDR3cfHRs=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
Expand All @@ -668,8 +668,8 @@ knative.dev/pkg v0.0.0-20230616134650-eb63a40adfb0/go.mod h1:dqC6IrvyBE7E+oZocs5
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/controller-runtime v0.14.6 h1:oxstGVvXGNnMvY7TAESYk+lzr6S3V5VFxQ6d92KcwQA=
sigs.k8s.io/controller-runtime v0.14.6/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0=
sigs.k8s.io/controller-runtime v0.15.3 h1:L+t5heIaI3zeejoIyyvLQs5vTVu/67IU2FfisVzFlBc=
sigs.k8s.io/controller-runtime v0.15.3/go.mod h1:kp4jckA4vTx281S/0Yk2LFEEQe67mjg+ev/yknv47Ds=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
Expand Down
22 changes: 22 additions & 0 deletions pkg/autoscaler/autoscaler_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package autoscaler

import (
"context"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
trueBool = true
)

type AutoscalerClient interface {
CreateOrUpdateAutoscaler(ctx context.Context, workload client.Object, labels map[string]string, max int32, min int32, targetCPUUtilization int32) (string, error)
DeleteAutoscaler(ctx context.Context, obj client.Object) error
GetType() client.Object
GetList(ctx context.Context, labelSelector labels.Selector, namespace string, fieldSelector fields.Selector) ([]client.Object, error)
GetMaxReplicaCount(obj client.Object) int32
GetScaleTargetName(obj client.Object) string
GetName() string
}
111 changes: 111 additions & 0 deletions pkg/autoscaler/hpa_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package autoscaler

import (
"context"
autoscalingv1 "k8s.io/api/autoscaling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

type HPAClient struct {
k8sClient client.Client
}

func NewHPAClient(k8sClient client.Client) *HPAClient {
return &HPAClient{
k8sClient: k8sClient,
}
}

func (hc *HPAClient) GetMaxReplicaCount(obj client.Object) int32 {
hpa := obj.(*autoscalingv1.HorizontalPodAutoscaler)
maxPods := hpa.Spec.MaxReplicas
return maxPods
}

func (hc *HPAClient) GetName() string {
return "HPA"
}

func (hc *HPAClient) GetList(ctx context.Context, labelSelector labels.Selector, namespace string, fieldSelector fields.Selector) ([]client.Object, error) {
hpas := &autoscalingv1.HorizontalPodAutoscalerList{}
if err := hc.k8sClient.List(ctx, hpas, &client.ListOptions{
FieldSelector: fieldSelector,
LabelSelector: labelSelector,
Namespace: namespace,
}); err != nil {
return nil, err
}

var result []client.Object

for _, hpa := range hpas.Items {
result = append(result, &hpa)
}

return result, nil

}

func (hc *HPAClient) GetType() client.Object {
return &autoscalingv1.HorizontalPodAutoscaler{}
}

func (hc *HPAClient) DeleteAutoscaler(ctx context.Context, obj client.Object) error {
deletePropagationPolicy := metav1.DeletePropagationForeground
err := hc.k8sClient.Delete(ctx, obj, &client.DeleteOptions{
PropagationPolicy: &deletePropagationPolicy,
})
if err != nil {
return err
}
return nil
}

func (hc *HPAClient) GetScaleTargetName(obj client.Object) string {
hpa := obj.(*autoscalingv1.HorizontalPodAutoscaler)
return hpa.Spec.ScaleTargetRef.Name
}

func (hc *HPAClient) CreateOrUpdateAutoscaler(ctx context.Context, workload client.Object, labels map[string]string,
max int32, min int32, targetCPUUtilization int32) (string, error) {
hpa := autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: workload.GetName(),
Namespace: workload.GetNamespace(),
Labels: labels,
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscalingv1.CrossVersionObjectReference{
Name: workload.GetName(),
APIVersion: workload.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Kind: workload.GetObjectKind().GroupVersionKind().Kind,
},
MinReplicas: &min,
MaxReplicas: max,
TargetCPUUtilizationPercentage: &targetCPUUtilization,
},
}

result, err := controllerutil.CreateOrUpdate(ctx, hc.k8sClient, &hpa, func() error {
hpa.Spec = autoscalingv1.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscalingv1.CrossVersionObjectReference{
Name: workload.GetName(),
APIVersion: workload.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Kind: workload.GetObjectKind().GroupVersionKind().Kind,
},
MinReplicas: &min,
MaxReplicas: max,
TargetCPUUtilizationPercentage: &targetCPUUtilization,
}
return nil
})
if err != nil {
return "", err
}

return string(result), nil
}
Loading