/
scale.go
203 lines (181 loc) · 7.89 KB
/
scale.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2022 SAP SE or an SAP affiliate company
//
// 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 scaler
import (
"context"
"fmt"
"strconv"
"github.com/go-logr/logr"
"github.com/gardener/dependency-watchdog/internal/util"
autoscalingv1 "k8s.io/api/autoscaling/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
scalev1 "k8s.io/client-go/scale"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// ignoreScalingAnnotationKey is the key for an annotation if present on a resource will suspend any scaling action for that resource.
ignoreScalingAnnotationKey = "dependency-watchdog.gardener.cloud/ignore-scaling"
// replicasAnnotationKey is the key for an annotation whose value captures the current spec.replicas prior to scale down for that resource.
// This is used when DWD attempts to restore the state of the resource it scale down.
replicasAnnotationKey = "dependency-watchdog.gardener.cloud/replicas"
// defaultScaleUpReplicas is the default value of number of replicas for a scale-up operation by a probe when the external probe transitions from failed to success.
defaultScaleUpReplicas int32 = 1
// defaultScaleDownReplicas is the default value of number of replicas for a scale-down operation by a probe when the external probe transitions from success to failed.
defaultScaleDownReplicas int32 = 0
)
type resourceScaler interface {
scale(ctx context.Context) error
}
type resScaler struct {
client client.Client
scaler scalev1.ScaleInterface
logger logr.Logger
namespace string
resourceInfo scalableResourceInfo
opts *scalerOptions
}
func newResourceScaler(client client.Client, scaler scalev1.ScaleInterface, logger logr.Logger, opts *scalerOptions, namespace string, resourceInfo scalableResourceInfo) resourceScaler {
resLogger := logger.WithValues("resNamespace", namespace, "kind", resourceInfo.ref.Kind, "apiVersion", resourceInfo.ref.APIVersion, "name", resourceInfo.ref.Name, "level", resourceInfo.level)
return &resScaler{
client: client,
scaler: scaler,
logger: resLogger,
namespace: namespace,
resourceInfo: resourceInfo,
opts: opts,
}
}
func (r *resScaler) scale(ctx context.Context) error {
var (
err error
resourceAnnot map[string]string
)
// sleep for initial delay
if err = util.SleepWithContext(ctx, r.resourceInfo.initialDelay); err != nil {
r.logger.Error(err, "Looks like the context has been cancelled. exiting scaling operation")
return err
}
if resourceAnnot, err = util.GetResourceAnnotations(ctx, r.client, r.namespace, r.resourceInfo.ref); err != nil {
if apierrors.IsNotFound(err) && r.resourceInfo.optional {
r.logger.Info("Resource not found. Ignoring this resource as its existence is marked as optional")
return nil
}
r.logger.Error(err, "Error trying to get annotations for resource")
return err
}
if ignoreScaling(resourceAnnot) {
r.logger.Info("Scaling ignored due to explicit instruction via annotation", "annotation", ignoreScalingAnnotationKey)
return nil
}
_, scaleSubRes, err := util.GetScaleResource(ctx, r.client, r.scaler, r.logger, r.resourceInfo.ref, r.resourceInfo.timeout)
if err != nil {
if apierrors.IsNotFound(err) {
r.logger.Error(err, "Resource does not have a scale subresource. Skipping scaling of dependent resources. Invalid config file")
}
return err
}
if r.resourceInfo.operation.shouldScaleReplicas(scaleSubRes.Spec.Replicas) {
if err := r.updateResourceAndScale(ctx, scaleSubRes, resourceAnnot); err != nil {
return err
}
} else {
if r.resourceInfo.operation == scaleUp {
r.logger.Info("Skipping scale-up for resource as current spec replicas > 0")
} else {
r.logger.Info("Skipping scale-down for resource as current spec replicas == 0")
}
}
return r.waitTillMinTargetReplicasReached(ctx)
}
func (r *resScaler) waitTillMinTargetReplicasReached(ctx context.Context) error {
var minTargetReplicas int32
if r.resourceInfo.operation == scaleUp {
minTargetReplicas = 1
}
r.logger.Info("Waiting for resource to reach minimum target replicas", "minTargetReplicas", minTargetReplicas)
opDesc := fmt.Sprintf("wait for resource to reach minimum required target replicas %d", minTargetReplicas)
resMinTargetReached := util.RetryUntilPredicate(ctx, r.logger, opDesc, func() bool {
readyReplicas, err := util.GetResourceReadyReplicas(ctx, r.client, r.namespace, r.resourceInfo.ref)
if err != nil {
return false
}
if r.resourceInfo.operation.minTargetReplicasReached(readyReplicas) {
r.logger.Info("Resource has reached desired replicas", "minTargetReplicas", minTargetReplicas)
return true
}
return false
}, *r.opts.resourceCheckTimeout, *r.opts.resourceCheckInterval)
if !resMinTargetReached {
return fmt.Errorf("timed out waiting for {namespace: %s, resource: %s} to reach minTargetReplicas %d", r.namespace, r.resourceInfo.ref.Name, minTargetReplicas)
}
return nil
}
func (r *resScaler) updateResourceAndScale(ctx context.Context, scaleSubRes *autoscalingv1.Scale, annot map[string]string) error {
childCtx, cancelFn := context.WithTimeout(ctx, r.resourceInfo.timeout)
defer cancelFn()
// update the annotation capturing the current spec.replicas as the annotation value if the operation is scale down.
// This allows restoration of the resource to the same replica count when a subsequent scale up operation is triggered.
if r.resourceInfo.operation == scaleDown {
patchBytes := []byte(fmt.Sprintf("{\"metadata\":{\"annotations\":{\"%s\":\"%s\"}}}", replicasAnnotationKey, strconv.Itoa(int(scaleSubRes.Spec.Replicas))))
err := util.PatchResourceAnnotations(ctx, r.client, r.namespace, r.resourceInfo.ref, patchBytes)
if err != nil {
r.logger.Error(err, "Failed to update annotation to capture the current replicas before scaling it down")
return err
}
}
targetReplicas, err := r.determineTargetReplicas(annot)
if err != nil {
return err
}
// need the updated scale subresource
gr, scaleSubRes, err := util.GetScaleResource(ctx, r.client, r.scaler, r.logger, r.resourceInfo.ref, r.resourceInfo.timeout)
if err != nil {
return err
}
scaleSubRes.Spec.Replicas = targetReplicas
if r.resourceInfo.operation == scaleUp {
r.logger.Info("Scaling up kubernetes resource", "targetReplicas", targetReplicas)
} else {
r.logger.Info("Scaling down kubernetes resource", "targetReplicas", targetReplicas)
}
if _, err = r.scaler.Update(childCtx, *gr, scaleSubRes, metav1.UpdateOptions{}); err != nil {
return err
}
return nil
}
func (r *resScaler) determineTargetReplicas(annotations map[string]string) (int32, error) {
if r.resourceInfo.operation == scaleDown {
return defaultScaleDownReplicas, nil
}
if replicasStr, ok := annotations[replicasAnnotationKey]; ok {
replicas, err := strconv.Atoi(replicasStr)
if err != nil {
return 0, fmt.Errorf("unexpected and invalid replicasStr set as value for annotation: %s for resource, Err: %w", replicasAnnotationKey, err)
}
return int32(replicas), nil
}
r.logger.Info("Replicas annotation not found, falling back to default scale-up replicas", "operation", r.resourceInfo.operation, "annotationKey", replicasAnnotationKey, "default-replicas", defaultScaleUpReplicas)
return defaultScaleUpReplicas, nil
}
func ignoreScaling(annotations map[string]string) bool {
if val, ok := annotations[ignoreScalingAnnotationKey]; ok {
b, err := strconv.ParseBool(val)
if err != nil {
return false
}
return b
}
return false
}