Skip to content

Commit

Permalink
feat: status and conditions for StartupCPUBoost (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikouaj authored May 21, 2024
1 parent e04806a commit 8678a00
Show file tree
Hide file tree
Showing 17 changed files with 847 additions and 102 deletions.
19 changes: 17 additions & 2 deletions api/v1alpha1/startupcpuboost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,23 @@ type StartupCPUBoostSpec struct {

// StartupCPUBoostStatus defines the observed state of StartupCPUBoost
type StartupCPUBoostStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// activeContainerBoosts is the number of containers which CPU
// resources were increased by the StartupCPUBoost and not yet
// reverted back to the original values
// +kubebuilder:validation:Optional
ActiveContainerBoosts int32 `json:"activeContainerBoosts,omitempty"`
// totalContainerBoosts is the number of containers which CPU
// resources were increased by the StartupCPUBoost
// +kubebuilder:validation:Optional
TotalContainerBoosts int32 `json:"totalContainerBoosts,omitempty"`
// Conditions hold the latest available observations of the StartupCPUBoost
// current state.
// +optional
// +listType=map
// +listMapKey=type
// +patchStrategy=merge
// +patchMergeKey=type
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}

//+kubebuilder:object:root=true
Expand Down
12 changes: 10 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

9 changes: 5 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ func setupControllers(mgr ctrl.Manager, boostMgr boost.Manager, certsReady chan
}
cpuBoostWebHook := boostWebhook.NewPodCPUBoostWebHook(boostMgr, scheme)
mgr.GetWebhookServer().Register("/mutate-v1-pod", cpuBoostWebHook)

if err := (&controller.StartupCPUBoostReconciler{
boostCtrl := &controller.StartupCPUBoostReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("startup-cpu-boost-reconciler"),
Log: ctrl.Log.WithName("boost-reconciler"),
Manager: boostMgr,
}).SetupWithManager(mgr); err != nil {
}
boostMgr.SetStartupCPUBoostReconciler(boostCtrl)
if err := boostCtrl.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "StartupCPUBoost")
os.Exit(1)
}
Expand Down
89 changes: 89 additions & 0 deletions config/crd/bases/autoscaling.x-k8s.io_startupcpuboosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,95 @@ spec:
type: object
status:
description: StartupCPUBoostStatus defines the observed state of StartupCPUBoost
properties:
activeContainerBoosts:
description: |-
activeContainerBoosts is the number of containers which CPU
resources were increased by the StartupCPUBoost and not yet
reverted back to the original values
format: int32
type: integer
conditions:
description: |-
Conditions hold the latest available observations of the StartupCPUBoost
current state.
items:
description: "Condition contains details for one aspect of the current
state of this API Resource.\n---\nThis struct is intended for
direct use as an array at the field path .status.conditions. For
example,\n\n\n\ttype FooStatus struct{\n\t // Represents the
observations of a foo's current state.\n\t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\"\n\t //
+patchMergeKey=type\n\t // +patchStrategy=merge\n\t // +listType=map\n\t
\ // +listMapKey=type\n\t Conditions []metav1.Condition `json:\"conditions,omitempty\"
patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"`\n\n\n\t
\ // other fields\n\t}"
properties:
lastTransitionTime:
description: |-
lastTransitionTime is the last time the condition transitioned from one status to another.
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: |-
message is a human readable message indicating details about the transition.
This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: |-
observedGeneration represents the .metadata.generation that the condition was set based upon.
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: |-
reason contains a programmatic identifier indicating the reason for the condition's last transition.
Producers of specific condition types may define expected values and meanings for this field,
and whether the values are considered a guaranteed API.
The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: |-
type of condition in CamelCase or in foo.example.com/CamelCase.
---
Many .condition.type values are consistent across resources like Available, but because arbitrary conditions can be
useful (see .node.status.conditions), the ability to deconflict is important.
The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
totalContainerBoosts:
description: |-
totalContainerBoosts is the number of containers which CPU
resources were increased by the StartupCPUBoost
format: int32
type: integer
type: object
type: object
served: true
Expand Down
2 changes: 1 addition & 1 deletion hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
45 changes: 41 additions & 4 deletions internal/boost/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/go-logr/logr"
"github.com/google/kube-startup-cpu-boost/internal/boost/duration"
Expand All @@ -47,7 +49,7 @@ type Manager interface {
StartupCPUBoostForPod(ctx context.Context, pod *corev1.Pod) (StartupCPUBoost, bool)
// StartupCPUBoostForPod returns a startup-cpu-boost that matches a given pod
StartupCPUBoost(namespace, name string) (StartupCPUBoost, bool)
// Start runs the manager's control loop
SetStartupCPUBoostReconciler(reconciler reconcile.Reconciler)
Start(ctx context.Context) error
}

Expand Down Expand Up @@ -77,6 +79,7 @@ func newTimeTickerImpl(d time.Duration) TimeTicker {
type managerImpl struct {
sync.RWMutex
client client.Client
reconciler reconcile.Reconciler
ticker TimeTicker
checkInterval time.Duration
startupCPUBoosts map[string]map[string]StartupCPUBoost
Expand Down Expand Up @@ -158,7 +161,10 @@ func (m *managerImpl) StartupCPUBoostForPod(ctx context.Context, pod *corev1.Pod
return nil, false
}

// Start runs the manager's control loop
func (m *managerImpl) SetStartupCPUBoostReconciler(reconciler reconcile.Reconciler) {
m.reconciler = reconciler
}

func (m *managerImpl) Start(ctx context.Context) error {
log := m.loggerFromContext(ctx)
defer m.ticker.Stop()
Expand Down Expand Up @@ -209,6 +215,7 @@ func (m *managerImpl) validateTimePolicyBoosts(ctx context.Context) {
m.RLock()
defer m.RUnlock()
revertTasks := make(chan *podRevertTask, m.maxGoroutines)
reconcileTasks := make(chan *reconcile.Request, m.maxGoroutines)
errors := make(chan error, m.maxGoroutines)
log := m.loggerFromContext(ctx)

Expand All @@ -235,15 +242,33 @@ func (m *managerImpl) validateTimePolicyBoosts(ctx context.Context) {
log.V(5).Info("updating pod with initial resources")
if err := task.boost.RevertResources(ctx, task.pod); err != nil {
errors <- fmt.Errorf("pod %s/%s: %w", task.pod.Namespace, task.pod.Name, err)
} else {
reconcileTasks <- &reconcile.Request{
NamespacedName: types.NamespacedName{
Name: task.boost.Name(),
Namespace: task.boost.Namespace(),
},
}
}
}
}()
}
wg.Wait()
close(reconcileTasks)
close(errors)
}()
for err := range errors {
log.Error(err, "failed to revert resources")

go func() {
for err := range errors {
log.Error(err, "failed to revert resources")
}
}()

reconcileRequests := dedupeReconcileRequests(reconcileTasks)
if m.reconciler != nil {
for _, req := range reconcileRequests {
m.reconciler.Reconcile(ctx, req)
}
}
}

Expand All @@ -252,3 +277,15 @@ func (m *managerImpl) loggerFromContext(ctx context.Context) logr.Logger {
return ctrl.LoggerFrom(ctx).
WithName("boost-manager")
}

func dedupeReconcileRequests(reconcileTasks chan *reconcile.Request) []reconcile.Request {
result := make([]reconcile.Request, 0, len(reconcileTasks))
requests := make(map[reconcile.Request]bool)
for task := range reconcileTasks {
requests[*task] = true
}
for k := range requests {
result = append(result, k)
}
return result
}
17 changes: 12 additions & 5 deletions internal/boost/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("Manager", func() {
Expand Down Expand Up @@ -192,11 +194,12 @@ var _ = Describe("Manager", func() {
})
When("There are startup-cpu-boosts with fixed duration policy", func() {
var (
spec *autoscaling.StartupCPUBoost
boost cpuboost.StartupCPUBoost
pod *corev1.Pod
mockClient *mock.MockClient
c chan time.Time
spec *autoscaling.StartupCPUBoost
boost cpuboost.StartupCPUBoost
pod *corev1.Pod
mockClient *mock.MockClient
mockReconciler *mock.MockReconciler
c chan time.Time
)
BeforeEach(func() {
spec = specTemplate.DeepCopy()
Expand All @@ -209,13 +212,17 @@ var _ = Describe("Manager", func() {
creationTimestamp := time.Now().Add(-1 * time.Duration(seconds) * time.Second).Add(-1 * time.Minute)
pod.CreationTimestamp = metav1.NewTime(creationTimestamp)
mockClient = mock.NewMockClient(mockCtrl)
mockReconciler = mock.NewMockReconciler(mockCtrl)

c = make(chan time.Time, 1)
mockTicker.EXPECT().Tick().MinTimes(1).Return(c)
mockTicker.EXPECT().Stop().Return()
mockClient.EXPECT().Update(gomock.Any(), gomock.Eq(pod)).MinTimes(1).Return(nil)
reconcileReq := reconcile.Request{NamespacedName: types.NamespacedName{Name: spec.Name, Namespace: spec.Namespace}}
mockReconciler.EXPECT().Reconcile(gomock.Any(), gomock.Eq(reconcileReq)).Times(1)
})
JustBeforeEach(func() {
manager.SetStartupCPUBoostReconciler(mockReconciler)
boost, err = cpuboost.NewStartupCPUBoost(mockClient, spec)
Expect(err).ShouldNot(HaveOccurred())
err = boost.UpsertPod(ctx, pod)
Expand Down
Loading

0 comments on commit 8678a00

Please sign in to comment.