• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zalando-incubator / stackset-controller / 6507834314

13 Oct 2023 11:48AM UTC coverage: 73.186% (-0.03%) from 73.218%
6507834314

Pull #536

github

katyanna
Remove deprecated linters

deadcode and varcheck are deprecated. They're currenlty unused
throughout the code, so no need for replacement.
Pull Request #536: Remove deprecated linters

2249 of 3073 relevant lines covered (73.19%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.4
/controller/stack_resources.go
1
package controller
2

3
import (
4
        "context"
5
        rgv1 "github.com/szuecs/routegroup-client/apis/zalando.org/v1"
6
        zv1 "github.com/zalando-incubator/stackset-controller/pkg/apis/zalando.org/v1"
7
        "github.com/zalando-incubator/stackset-controller/pkg/core"
8
        apps "k8s.io/api/apps/v1"
9
        v2 "k8s.io/api/autoscaling/v2"
10
        apiv1 "k8s.io/api/core/v1"
11
        networking "k8s.io/api/networking/v1"
12
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
)
14

15
func pint32Equal(p1, p2 *int32) bool {
1✔
16
        if p1 == nil && p2 == nil {
1✔
17
                return true
×
18
        }
×
19
        if p1 != nil && p2 != nil {
2✔
20
                return *p1 == *p2
1✔
21
        }
1✔
22
        return false
×
23
}
24

25
// There are HPA metrics that depend on annotations to work properly,
26
// e.g. External RPS metric, this verification provides a way to verify
27
// all relevant annotations are actually up to date.
28
func areHPAAnnotationsUpToDate(updated, existing *v2.HorizontalPodAutoscaler) bool {
1✔
29
        if len(updated.Annotations) != len(existing.Annotations) {
1✔
30
                return false
×
31
        }
×
32

33
        for k, v := range updated.Annotations {
2✔
34
                if k == "stackset-controller.zalando.org/stack-generation" {
2✔
35
                        continue
1✔
36
                }
37

38
                existingValue, ok := existing.Annotations[k]
1✔
39
                if ok && existingValue == v {
1✔
40
                        continue
×
41
                }
42

43
                return false
1✔
44
        }
45

46
        return true
1✔
47
}
48

49
// syncObjectMeta copies metadata elements such as labels or annotations from source to target
50
func syncObjectMeta(target, source metav1.Object) {
1✔
51
        target.SetLabels(source.GetLabels())
1✔
52
        target.SetAnnotations(source.GetAnnotations())
1✔
53
}
1✔
54

55
func (c *StackSetController) ReconcileStackDeployment(ctx context.Context, stack *zv1.Stack, existing *apps.Deployment, generateUpdated func() *apps.Deployment) error {
1✔
56
        deployment := generateUpdated()
1✔
57

1✔
58
        // Create new deployment
1✔
59
        if existing == nil {
2✔
60
                _, err := c.client.AppsV1().Deployments(deployment.Namespace).Create(ctx, deployment, metav1.CreateOptions{})
1✔
61
                if err != nil {
1✔
62
                        return err
×
63
                }
×
64
                c.recorder.Eventf(
1✔
65
                        stack,
1✔
66
                        apiv1.EventTypeNormal,
1✔
67
                        "CreatedDeployment",
1✔
68
                        "Created Deployment %s",
1✔
69
                        deployment.Name)
1✔
70
                return nil
1✔
71
        }
72

73
        // Check if we need to update the deployment
74
        if core.IsResourceUpToDate(stack, existing.ObjectMeta) && pint32Equal(existing.Spec.Replicas, deployment.Spec.Replicas) {
2✔
75
                return nil
1✔
76
        }
1✔
77

78
        updated := existing.DeepCopy()
1✔
79
        syncObjectMeta(updated, deployment)
1✔
80
        updated.Spec = deployment.Spec
1✔
81
        updated.Spec.Selector = existing.Spec.Selector
1✔
82

1✔
83
        _, err := c.client.AppsV1().Deployments(updated.Namespace).Update(ctx, updated, metav1.UpdateOptions{})
1✔
84
        if err != nil {
1✔
85
                return err
×
86
        }
×
87
        c.recorder.Eventf(
1✔
88
                stack,
1✔
89
                apiv1.EventTypeNormal,
1✔
90
                "UpdatedDeployment",
1✔
91
                "Updated Deployment %s",
1✔
92
                deployment.Name)
1✔
93
        return nil
1✔
94
}
95

96
func (c *StackSetController) ReconcileStackHPA(ctx context.Context, stack *zv1.Stack, existing *v2.HorizontalPodAutoscaler, generateUpdated func() (*v2.HorizontalPodAutoscaler, error)) error {
1✔
97
        hpa, err := generateUpdated()
1✔
98
        if err != nil {
1✔
99
                return err
×
100
        }
×
101

102
        // HPA removed
103
        if hpa == nil {
2✔
104
                if existing != nil {
2✔
105
                        err := c.client.AutoscalingV2().HorizontalPodAutoscalers(existing.Namespace).Delete(ctx, existing.Name, metav1.DeleteOptions{})
1✔
106
                        if err != nil {
1✔
107
                                return err
×
108
                        }
×
109
                        c.recorder.Eventf(
1✔
110
                                stack,
1✔
111
                                apiv1.EventTypeNormal,
1✔
112
                                "DeletedHPA",
1✔
113
                                "Deleted HPA %s",
1✔
114
                                existing.Namespace)
1✔
115
                }
116
                return nil
1✔
117
        }
118

119
        // Create new HPA
120
        if existing == nil {
2✔
121
                _, err := c.client.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Create(ctx, hpa, metav1.CreateOptions{})
1✔
122
                if err != nil {
1✔
123
                        return err
×
124
                }
×
125
                c.recorder.Eventf(
1✔
126
                        stack,
1✔
127
                        apiv1.EventTypeNormal,
1✔
128
                        "CreatedHPA",
1✔
129
                        "Created HPA %s",
1✔
130
                        hpa.Name)
1✔
131
                return nil
1✔
132
        }
133

134
        // Check if we need to update the HPA
135
        if core.IsResourceUpToDate(stack, existing.ObjectMeta) &&
1✔
136
                pint32Equal(existing.Spec.MinReplicas, hpa.Spec.MinReplicas) &&
1✔
137
                areHPAAnnotationsUpToDate(hpa, existing) {
2✔
138
                return nil
1✔
139
        }
1✔
140

141
        updated := existing.DeepCopy()
1✔
142
        syncObjectMeta(updated, hpa)
1✔
143
        updated.Spec = hpa.Spec
1✔
144

1✔
145
        _, err = c.client.AutoscalingV2().HorizontalPodAutoscalers(updated.Namespace).Update(ctx, updated, metav1.UpdateOptions{})
1✔
146
        if err != nil {
1✔
147
                return err
×
148
        }
×
149
        c.recorder.Eventf(
1✔
150
                stack,
1✔
151
                apiv1.EventTypeNormal,
1✔
152
                "UpdatedHPA",
1✔
153
                "Updated HPA %s",
1✔
154
                hpa.Name)
1✔
155
        return nil
1✔
156
}
157

158
func (c *StackSetController) ReconcileStackService(ctx context.Context, stack *zv1.Stack, existing *apiv1.Service, generateUpdated func() (*apiv1.Service, error)) error {
1✔
159
        service, err := generateUpdated()
1✔
160
        if err != nil {
1✔
161
                return err
×
162
        }
×
163

164
        // Create new service
165
        if existing == nil {
2✔
166
                _, err := c.client.CoreV1().Services(service.Namespace).Create(ctx, service, metav1.CreateOptions{})
1✔
167
                if err != nil {
1✔
168
                        return err
×
169
                }
×
170
                c.recorder.Eventf(
1✔
171
                        stack,
1✔
172
                        apiv1.EventTypeNormal,
1✔
173
                        "CreatedService",
1✔
174
                        "Created Service %s",
1✔
175
                        service.Name)
1✔
176
                return nil
1✔
177
        }
178

179
        // Check if we need to update the service
180
        if core.IsResourceUpToDate(stack, existing.ObjectMeta) {
2✔
181
                return nil
1✔
182
        }
1✔
183

184
        updated := existing.DeepCopy()
1✔
185
        syncObjectMeta(updated, service)
1✔
186
        updated.Spec = service.Spec
1✔
187
        updated.Spec.ClusterIP = existing.Spec.ClusterIP // ClusterIP is immutable
1✔
188

1✔
189
        _, err = c.client.CoreV1().Services(updated.Namespace).Update(ctx, updated, metav1.UpdateOptions{})
1✔
190
        if err != nil {
1✔
191
                return err
×
192
        }
×
193
        c.recorder.Eventf(
1✔
194
                stack,
1✔
195
                apiv1.EventTypeNormal,
1✔
196
                "UpdatedService",
1✔
197
                "Updated Service %s",
1✔
198
                service.Name)
1✔
199
        return nil
1✔
200
}
201

202
func (c *StackSetController) ReconcileStackIngress(ctx context.Context, stack *zv1.Stack, existing *networking.Ingress, generateUpdated func() (*networking.Ingress, error)) error {
1✔
203
        ingress, err := generateUpdated()
1✔
204
        if err != nil {
1✔
205
                return err
×
206
        }
×
207

208
        // Ingress removed
209
        if ingress == nil {
2✔
210
                if existing != nil {
2✔
211
                        err := c.client.NetworkingV1().Ingresses(existing.Namespace).Delete(ctx, existing.Name, metav1.DeleteOptions{})
1✔
212
                        if err != nil {
1✔
213
                                return err
×
214
                        }
×
215
                        c.recorder.Eventf(
1✔
216
                                stack,
1✔
217
                                apiv1.EventTypeNormal,
1✔
218
                                "DeletedIngress",
1✔
219
                                "Deleted Ingress %s",
1✔
220
                                existing.Namespace)
1✔
221
                }
222
                return nil
1✔
223
        }
224

225
        // Create new Ingress
226
        if existing == nil {
2✔
227
                _, err := c.client.NetworkingV1().Ingresses(ingress.Namespace).Create(ctx, ingress, metav1.CreateOptions{})
1✔
228
                if err != nil {
1✔
229
                        return err
×
230
                }
×
231
                c.recorder.Eventf(
1✔
232
                        stack,
1✔
233
                        apiv1.EventTypeNormal,
1✔
234
                        "CreatedIngress",
1✔
235
                        "Created Ingress %s",
1✔
236
                        ingress.Name)
1✔
237
                return nil
1✔
238
        }
239

240
        // Check if we need to update the Ingress
241
        if core.IsResourceUpToDate(stack, existing.ObjectMeta) {
2✔
242
                return nil
1✔
243
        }
1✔
244

245
        updated := existing.DeepCopy()
1✔
246
        syncObjectMeta(updated, ingress)
1✔
247
        updated.Spec = ingress.Spec
1✔
248

1✔
249
        _, err = c.client.NetworkingV1().Ingresses(updated.Namespace).Update(ctx, updated, metav1.UpdateOptions{})
1✔
250
        if err != nil {
1✔
251
                return err
×
252
        }
×
253
        c.recorder.Eventf(
1✔
254
                stack,
1✔
255
                apiv1.EventTypeNormal,
1✔
256
                "UpdatedIngress",
1✔
257
                "Updated Ingress %s",
1✔
258
                ingress.Name)
1✔
259
        return nil
1✔
260
}
261

262
func (c *StackSetController) ReconcileStackRouteGroup(ctx context.Context, stack *zv1.Stack, existing *rgv1.RouteGroup, generateUpdated func() (*rgv1.RouteGroup, error)) error {
1✔
263
        routegroup, err := generateUpdated()
1✔
264
        if err != nil {
1✔
265
                return err
×
266
        }
×
267

268
        // RouteGroup removed
269
        if routegroup == nil {
2✔
270
                if existing != nil {
2✔
271
                        err := c.client.RouteGroupV1().RouteGroups(existing.Namespace).Delete(ctx, existing.Name, metav1.DeleteOptions{})
1✔
272
                        if err != nil {
1✔
273
                                return err
×
274
                        }
×
275
                        c.recorder.Eventf(
1✔
276
                                stack,
1✔
277
                                apiv1.EventTypeNormal,
1✔
278
                                "DeletedRouteGroup",
1✔
279
                                "Deleted RouteGroup %s",
1✔
280
                                existing.Namespace)
1✔
281
                }
282
                return nil
1✔
283
        }
284

285
        // Create new RouteGroup
286
        if existing == nil {
2✔
287
                _, err := c.client.RouteGroupV1().RouteGroups(routegroup.Namespace).Create(ctx, routegroup, metav1.CreateOptions{})
1✔
288
                if err != nil {
1✔
289
                        return err
×
290
                }
×
291
                c.recorder.Eventf(
1✔
292
                        stack,
1✔
293
                        apiv1.EventTypeNormal,
1✔
294
                        "CreatedRouteGroup",
1✔
295
                        "Created RouteGroup %s",
1✔
296
                        routegroup.Name)
1✔
297
                return nil
1✔
298
        }
299

300
        // Check if we need to update the RouteGroup
301
        if core.IsResourceUpToDate(stack, existing.ObjectMeta) {
2✔
302
                return nil
1✔
303
        }
1✔
304

305
        updated := existing.DeepCopy()
1✔
306
        syncObjectMeta(updated, routegroup)
1✔
307
        updated.Spec = routegroup.Spec
1✔
308

1✔
309
        _, err = c.client.RouteGroupV1().RouteGroups(updated.Namespace).Update(ctx, updated, metav1.UpdateOptions{})
1✔
310
        if err != nil {
1✔
311
                return err
×
312
        }
×
313
        c.recorder.Eventf(
1✔
314
                stack,
1✔
315
                apiv1.EventTypeNormal,
1✔
316
                "UpdatedRouteGroup",
1✔
317
                "Updated RouteGroup %s",
1✔
318
                routegroup.Name)
1✔
319
        return nil
1✔
320
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc