generated from knative-extensions/sample-controller
-
Notifications
You must be signed in to change notification settings - Fork 6
/
stagepodautoscaler.go
86 lines (73 loc) · 3.41 KB
/
stagepodautoscaler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Copyright 2023 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stagepodautoscaler
import (
"context"
"fmt"
apierrs "k8s.io/apimachinery/pkg/api/errors"
corev1listers "k8s.io/client-go/listers/core/v1"
"knative.dev/pkg/ptr"
"knative.dev/serving/pkg/apis/serving"
resourceutil "knative.dev/serving/pkg/resources"
pkgreconciler "knative.dev/pkg/reconciler"
v1 "knative.dev/serving-progressive-rollout/pkg/apis/serving/v1"
spareconciler "knative.dev/serving-progressive-rollout/pkg/client/injection/reconciler/serving/v1/stagepodautoscaler"
clientset "knative.dev/serving/pkg/client/clientset/versioned"
palisters "knative.dev/serving/pkg/client/listers/autoscaling/v1alpha1"
)
// Reconciler implements controller.Reconciler for StagePodAutoscaler resources.
type Reconciler struct {
client clientset.Interface
podAutoscalerLister palisters.PodAutoscalerLister
podsLister corev1listers.PodLister
}
// Check that our Reconciler implements soreconciler.Interface
var _ spareconciler.Interface = (*Reconciler)(nil)
// ReconcileKind implements Interface.ReconcileKind.
func (c *Reconciler) ReconcileKind(_ context.Context, spa *v1.StagePodAutoscaler) pkgreconciler.Event {
pa, err := c.podAutoscalerLister.PodAutoscalers(spa.Namespace).Get(spa.Name)
if apierrs.IsNotFound(err) {
message := fmt.Sprintf("The PodAutoscaler %v/%v was not found.", spa.Namespace, spa.Name)
spa.Status.MarkPodAutoscalerStageNotReady(message)
return nil
} else if err != nil {
spa.Status.MarkPodAutoscalerStageNotReady(err.Error())
return err
}
podCounter := resourceutil.NewPodAccessor(c.podsLister, pa.Namespace, pa.Labels[serving.RevisionLabelKey])
_, _, _, terminating, err := podCounter.PodCountsByState()
if err != nil {
return fmt.Errorf("error getting pod counts for the revision %s under the namespace %s: %w",
pa.Labels[serving.RevisionLabelKey], pa.Namespace, err)
}
if spa.Status.ReplicasTerminating == nil || *spa.Status.ReplicasTerminating != int32(terminating) {
spa.Status.ReplicasTerminating = ptr.Int32(int32(terminating))
}
// As long as the PodAutoscaler with the same name as StagePodAutoscaler exists, and both of the DesiredScale
// the ActualScale are available, we propagate the values to StagePodAutoscaler.
// When these values are set in StagePodAutoscaler, it will kick off the reconciliation loop of the
// RolloutOrchestrator.
//
// The purpose of assigning these values to make sure the changes on PodAutoscaler can trigger the
// reconciliation loop of the RolloutOrchestrator.
if pa.Status.DesiredScale != nil && pa.Status.ActualScale != nil {
spa.Status.ActualScale = pa.Status.ActualScale
spa.Status.DesiredScale = pa.Status.DesiredScale
spa.Status.MarkPodAutoscalerStageReady()
} else {
message := fmt.Sprintf("The ActualScale or DesiredScale for the PodAutoscaler %v/%v was not ready.",
spa.Namespace, spa.Name)
spa.Status.MarkPodAutoscalerStageNotReady(message)
}
return nil
}