generated from kedacore/github-template
-
Notifications
You must be signed in to change notification settings - Fork 97
/
scaled_object.go
145 lines (128 loc) · 3.67 KB
/
scaled_object.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
package http
import (
"context"
"fmt"
"github.com/go-logr/logr"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
httpv1alpha1 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
"github.com/kedacore/http-add-on/pkg/k8s"
)
// createOrUpdateScaledObject attempts to create a new ScaledObject
// according to the given parameters. If the create failed because the
// ScaledObject already exists, attempts to patch the scaledobject.
// otherwise, fails.
func (r *HTTPScaledObjectReconciler) createOrUpdateScaledObject(
ctx context.Context,
cl client.Client,
logger logr.Logger,
externalScalerHostName string,
httpso *httpv1alpha1.HTTPScaledObject,
) error {
logger.Info("Creating scaled objects", "external scaler host name", externalScalerHostName)
var minReplicaCount *int32
var maxReplicaCount *int32
if replicas := httpso.Spec.Replicas; replicas != nil {
minReplicaCount = replicas.Min
maxReplicaCount = replicas.Max
}
appScaledObject := k8s.NewScaledObject(
httpso.GetNamespace(),
httpso.GetName(), // HTTPScaledObject name is the same as the ScaledObject name
httpso.Spec.ScaleTargetRef.Deployment,
externalScalerHostName,
httpso.Spec.Hosts,
httpso.Spec.PathPrefixes,
minReplicaCount,
maxReplicaCount,
httpso.Spec.CooldownPeriod,
)
// Set HTTPScaledObject instance as the owner and controller
if err := controllerutil.SetControllerReference(httpso, appScaledObject, r.Scheme); err != nil {
return err
}
logger.Info("Creating App ScaledObject", "ScaledObject", *appScaledObject)
if err := cl.Create(ctx, appScaledObject); err != nil {
if errors.IsAlreadyExists(err) {
existingSOKey := client.ObjectKey{
Namespace: httpso.GetNamespace(),
Name: appScaledObject.GetName(),
}
var fetchedSO kedav1alpha1.ScaledObject
if err := cl.Get(ctx, existingSOKey, &fetchedSO); err != nil {
logger.Error(
err,
"failed to fetch existing ScaledObject for patching",
)
return err
}
if err := cl.Patch(ctx, appScaledObject, client.Merge); err != nil {
logger.Error(
err,
"failed to patch existing ScaledObject",
)
return err
}
} else {
AddCondition(
httpso,
*SetMessage(
CreateCondition(
httpv1alpha1.Error,
v1.ConditionFalse,
httpv1alpha1.ErrorCreatingAppScaledObject,
),
err.Error(),
),
)
logger.Error(err, "Creating ScaledObject")
return err
}
}
AddCondition(
httpso,
*SetMessage(
CreateCondition(
httpv1alpha1.Created,
v1.ConditionTrue,
httpv1alpha1.AppScaledObjectCreated,
),
"App ScaledObject created",
),
)
return r.purgeLegacySO(ctx, cl, logger, httpso)
}
// TODO(pedrotorres): delete this on v0.6.0
func (r *HTTPScaledObjectReconciler) purgeLegacySO(
ctx context.Context,
cl client.Client,
logger logr.Logger,
httpso *httpv1alpha1.HTTPScaledObject,
) error {
legacyName := fmt.Sprintf("%s-app", httpso.GetName())
legacyKey := client.ObjectKey{
Namespace: httpso.GetNamespace(),
Name: legacyName,
}
var legacySO kedav1alpha1.ScaledObject
if err := cl.Get(ctx, legacyKey, &legacySO); err != nil {
if errors.IsNotFound(err) {
logger.Info("legacy ScaledObject not found")
return nil
}
logger.Error(err, "failed getting legacy ScaledObject")
return err
}
if err := cl.Delete(ctx, &legacySO); err != nil {
if errors.IsNotFound(err) {
logger.Info("legacy ScaledObject not found")
return nil
}
logger.Error(err, "failed deleting legacy ScaledObject")
return err
}
return nil
}