Skip to content

Commit

Permalink
Merge pull request #257 from 3scale-ops/feat/system-controller-improv…
Browse files Browse the repository at this point in the history
…ements

feat: System controller improvements
  • Loading branch information
3scale-robot authored Apr 19, 2023
2 parents b7fabf1 + e102002 commit 0d5d982
Show file tree
Hide file tree
Showing 44 changed files with 6,631 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# To re-generate a bundle for another specific version without changing the standard setup, you can:
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
VERSION ?= 0.18.1
VERSION ?= 0.19.5

# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
Expand Down
47 changes: 47 additions & 0 deletions api/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"time"

jsonpatch "github.com/evanphx/json-patch"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -402,6 +404,11 @@ type HorizontalPodAutoscalerSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
ResourceUtilization *int32 `json:"resourceUtilization,omitempty"`
// Behavior configures the scaling behavior of the target
// in both Up and Down directions (scaleUp and scaleDown fields respectively).
// If not set, the default HPAScalingRules for scale up and scale down are used.
// +optional
Behavior *autoscalingv2.HorizontalPodAutoscalerBehavior `json:"behavior,omitempty"`
}

type defaultHorizontalPodAutoscalerSpec struct {
Expand Down Expand Up @@ -437,6 +444,39 @@ func InitializeHorizontalPodAutoscalerSpec(spec *HorizontalPodAutoscalerSpec, de
return spec
}

type DeploymentStrategySpec struct {
// Type of deployment. Can be "Recreate" or "RollingUpdate". Default is RollingUpdate.
// +optional
Type appsv1.DeploymentStrategyType `json:"type,omitempty"`
// Rolling update config params. Present only if DeploymentStrategyType =
// RollingUpdate.
// +optional
RollingUpdate *appsv1.RollingUpdateDeployment `json:"rollingUpdate,omitempty"`
}

type defaultDeploymentRollingStrategySpec struct {
MaxUnavailable, MaxSurge *intstr.IntOrString
}

// InitializeDeploymentStrategySpec initializes a DeploymentStrategySpec struct
func InitializeDeploymentStrategySpec(spec *DeploymentStrategySpec, def defaultDeploymentRollingStrategySpec) *DeploymentStrategySpec {
if spec == nil {
new := &DeploymentStrategySpec{}
new.Default(def)
return new
}
return spec
}

// Default sets default values for any value not specifically set in the DeploymentStrategySpec struct
func (spec *DeploymentStrategySpec) Default(def defaultDeploymentRollingStrategySpec) {
spec.Type = appsv1.RollingUpdateDeploymentStrategyType
spec.RollingUpdate = &appsv1.RollingUpdateDeployment{
MaxSurge: def.MaxSurge,
MaxUnavailable: def.MaxUnavailable,
}
}

// ResourceRequirementsSpec defines the resource requirements for the component
type ResourceRequirementsSpec struct {
// Limits describes the maximum amount of compute resources allowed.
Expand Down Expand Up @@ -658,6 +698,13 @@ func intOrDefault(value *int32, defValue *int32) *int32 {
return value
}

func int64OrDefault(value *int64, defValue *int64) *int64 {
if value == nil {
return defValue
}
return value
}

func boolOrDefault(value *bool, defValue *bool) *bool {
if value == nil {
return defValue
Expand Down
53 changes: 51 additions & 2 deletions api/v1alpha1/system_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
)

var (

// Common
systemDefaultSandboxProxyOpensslVerifyMode string = "VERIFY_NONE"
systemDefaultForceSSL bool = true
Expand All @@ -55,6 +56,7 @@ var (
SelectorKey: pointer.StringPtr("monitoring-key"),
SelectorValue: pointer.StringPtr("middleware"),
}
systemDefaultTerminationGracePeriodSeconds *int64 = pointer.Int64(60)

// App
systemDefaultAppReplicas int32 = 2
Expand All @@ -78,6 +80,10 @@ var (
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
}
systemDefaultAppDeploymentStrategy defaultDeploymentRollingStrategySpec = defaultDeploymentRollingStrategySpec{
MaxUnavailable: util.IntStrPtr(intstr.FromInt(0)),
MaxSurge: util.IntStrPtr(intstr.FromString("10%")),
}
systemDefaultAppHPA defaultHorizontalPodAutoscalerSpec = defaultHorizontalPodAutoscalerSpec{
MinReplicas: pointer.Int32Ptr(2),
MaxReplicas: pointer.Int32Ptr(4),
Expand Down Expand Up @@ -114,6 +120,10 @@ var (
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
}
systemDefaultSidekiqDeploymentStrategy defaultDeploymentRollingStrategySpec = defaultDeploymentRollingStrategySpec{
MaxUnavailable: util.IntStrPtr(intstr.FromInt(0)),
MaxSurge: util.IntStrPtr(intstr.FromInt(1)),
}
systemDefaultSidekiqHPA defaultHorizontalPodAutoscalerSpec = defaultHorizontalPodAutoscalerSpec{
MinReplicas: pointer.Int32Ptr(2),
MaxReplicas: pointer.Int32Ptr(4),
Expand Down Expand Up @@ -277,7 +287,7 @@ func (spec *SystemSpec) Default() {
if spec.Console == nil {
spec.Console = &SystemRailsConsoleSpec{}
}
spec.Console.Default()
spec.Console.Default(spec.Image)

if spec.Twemproxy != nil {
spec.Twemproxy.Default()
Expand Down Expand Up @@ -585,6 +595,10 @@ func (srs *SystemRailsSpec) Default() {

// SystemAppSpec configures the App component of System
type SystemAppSpec struct {
// The deployment strategy to use to replace existing pods with new ones.
// +optional
// +patchStrategy=retainKeys
DeploymentStrategy *DeploymentStrategySpec `json:"deploymentStrategy,omitempty"`
// Pod Disruption Budget for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Expand Down Expand Up @@ -620,20 +634,33 @@ type SystemAppSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Canary *Canary `json:"canary,omitempty"`
// Configures the TerminationGracePeriodSeconds
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
}

// Default implements defaulting for the system App component
func (spec *SystemAppSpec) Default() {
spec.DeploymentStrategy = InitializeDeploymentStrategySpec(spec.DeploymentStrategy, systemDefaultAppDeploymentStrategy)
spec.HPA = InitializeHorizontalPodAutoscalerSpec(spec.HPA, systemDefaultAppHPA)
spec.Replicas = intOrDefault(spec.Replicas, &systemDefaultAppReplicas)
spec.PDB = InitializePodDisruptionBudgetSpec(spec.PDB, systemDefaultAppPDB)
spec.Resources = InitializeResourceRequirementsSpec(spec.Resources, systemDefaultAppResources)
spec.LivenessProbe = InitializeProbeSpec(spec.LivenessProbe, systemDefaultAppLivenessProbe)
spec.ReadinessProbe = InitializeProbeSpec(spec.ReadinessProbe, systemDefaultAppReadinessProbe)
spec.TerminationGracePeriodSeconds = int64OrDefault(
spec.TerminationGracePeriodSeconds, systemDefaultTerminationGracePeriodSeconds,
)

}

// SystemSidekiqSpec configures the Sidekiq component of System
type SystemSidekiqSpec struct {
// The deployment strategy to use to replace existing pods with new ones.
// +optional
// +patchStrategy=retainKeys
DeploymentStrategy *DeploymentStrategySpec `json:"deploymentStrategy,omitempty"`
// Sidekiq specific configuration options for the component element
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Expand Down Expand Up @@ -673,6 +700,10 @@ type SystemSidekiqSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Canary *Canary `json:"canary,omitempty"`
// Configures the TerminationGracePeriodSeconds
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
}

// SidekiqConfig configures app behavior for System Sidekiq
Expand Down Expand Up @@ -702,12 +733,17 @@ func (cfg *SidekiqConfig) Default(def defaultSidekiqConfig) {

// Default implements defaulting for the system Sidekiq component
func (spec *SystemSidekiqSpec) Default(sidekiqType systemSidekiqType) {
spec.DeploymentStrategy = InitializeDeploymentStrategySpec(spec.DeploymentStrategy, systemDefaultSidekiqDeploymentStrategy)
spec.HPA = InitializeHorizontalPodAutoscalerSpec(spec.HPA, systemDefaultSidekiqHPA)
spec.Replicas = intOrDefault(spec.Replicas, &systemDefaultSidekiqReplicas)
spec.PDB = InitializePodDisruptionBudgetSpec(spec.PDB, systemDefaultSidekiqPDB)
spec.Resources = InitializeResourceRequirementsSpec(spec.Resources, systemDefaultSidekiqResources)
spec.LivenessProbe = InitializeProbeSpec(spec.LivenessProbe, systemDefaultSidekiqLivenessProbe)
spec.ReadinessProbe = InitializeProbeSpec(spec.ReadinessProbe, systemDefaultSidekiqReadinessProbe)
spec.TerminationGracePeriodSeconds = int64OrDefault(
spec.TerminationGracePeriodSeconds, systemDefaultTerminationGracePeriodSeconds,
)

if spec.Config == nil {
spec.Config = &SidekiqConfig{}
}
Expand Down Expand Up @@ -750,6 +786,10 @@ type SystemSphinxSpec struct {
// If specified, the pod's tolerations.
// +optional
Tolerations []corev1.Toleration `json:"tolerations,omitempty" protobuf:"bytes,22,opt,name=tolerations"`
// Configures the TerminationGracePeriodSeconds
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
}

// Default implements defaulting for the system sphinx component
Expand All @@ -763,6 +803,9 @@ func (spec *SystemSphinxSpec) Default(systemDefaultImage *ImageSpec) {
spec.Config = &SphinxConfig{}
}
spec.Config.Default()
spec.TerminationGracePeriodSeconds = int64OrDefault(
spec.TerminationGracePeriodSeconds, systemDefaultTerminationGracePeriodSeconds,
)
}

// SphinxConfig has configuration options for System's sphinx
Expand Down Expand Up @@ -833,6 +876,11 @@ func (tc *ThinkingSpec) Default() {

// SystemRailsConsoleSpec configures the App component of System
type SystemRailsConsoleSpec struct {
// Image specification for the Console component.
// Defaults to system image if not defined.
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Image *ImageSpec `json:"image,omitempty"`
// Resource requirements for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Expand All @@ -846,7 +894,8 @@ type SystemRailsConsoleSpec struct {
}

// Default implements defaulting for the system App component
func (spec *SystemRailsConsoleSpec) Default() {
func (spec *SystemRailsConsoleSpec) Default(systemDefaultImage *ImageSpec) {
spec.Image = InitializeImageSpec(spec.Image, defaultImageSpec(*systemDefaultImage))
spec.Resources = InitializeResourceRequirementsSpec(spec.Resources, systemDefaultRailsConsoleResources)
}

Expand Down
57 changes: 57 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bundle.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LABEL operators.operatorframework.io.bundle.metadata.v1=metadata/
LABEL operators.operatorframework.io.bundle.package.v1=saas-operator
LABEL operators.operatorframework.io.bundle.channels.v1=alpha,stable
LABEL operators.operatorframework.io.bundle.channel.default.v1=alpha
LABEL operators.operatorframework.io.metrics.builder=operator-sdk-v1.25.0
LABEL operators.operatorframework.io.metrics.builder=operator-sdk-v1.27.0
LABEL operators.operatorframework.io.metrics.mediatype.v1=metrics+v1
LABEL operators.operatorframework.io.metrics.project_layout=go.kubebuilder.io/v3

Expand Down
Loading

0 comments on commit 0d5d982

Please sign in to comment.