Skip to content

Commit c469bb4

Browse files
authored
make filter function variadic, remove rancher annotations in ingress #231 (#234)
1 parent ebc4fdb commit c469bb4

3 files changed

Lines changed: 39 additions & 40 deletions

File tree

internal/controller/typesensecluster_ingress.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"text/template"
1111
"time"
1212

13-
"reflect"
14-
1513
tsv1alpha1 "github.com/akyriako/typesense-operator/api/v1alpha1"
1614
appsv1 "k8s.io/api/apps/v1"
1715
v1 "k8s.io/api/core/v1"
@@ -57,6 +55,8 @@ const (
5755
}`
5856
)
5957

58+
const clusterIssuerAnnotationKey = "cert-manager.io/cluster-issuer"
59+
6060
func (r *TypesenseClusterReconciler) ReconcileIngress(ctx context.Context, ts tsv1alpha1.TypesenseCluster) (err error) {
6161
r.logger.V(debugLevel).Info("reconciling ingress")
6262

@@ -90,8 +90,8 @@ func (r *TypesenseClusterReconciler) ReconcileIngress(ctx context.Context, ts ts
9090
}
9191
} else {
9292
if ts.Spec.Ingress.Host != ig.Spec.Rules[0].Host ||
93-
(ts.Spec.Ingress.ClusterIssuer != nil && *ts.Spec.Ingress.ClusterIssuer != ig.Annotations["cert-manager.io/cluster-issuer"]) ||
94-
!reflect.DeepEqual(ts.Spec.Ingress.Annotations, r.getIngressAnnotations(ig)) ||
93+
(ts.Spec.Ingress.ClusterIssuer != nil && *ts.Spec.Ingress.ClusterIssuer != ig.Annotations[clusterIssuerAnnotationKey]) ||
94+
!apiequality.Semantic.DeepEqual(ts.Spec.Ingress.Annotations, r.getIngressAnnotations(ig)) ||
9595
(ts.Spec.Ingress.TLSSecretName != nil && *ts.Spec.Ingress.TLSSecretName != ig.Spec.TLS[0].SecretName) ||
9696
ts.Spec.Ingress.IngressClassName != *ig.Spec.IngressClassName ||
9797
ts.Spec.Ingress.Path != ig.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Path ||
@@ -173,12 +173,12 @@ func (r *TypesenseClusterReconciler) ReconcileIngress(ctx context.Context, ts ts
173173
}
174174
} else {
175175
desiredResources := ts.Spec.Ingress.GetReverseProxyResources()
176-
deploymentResourcesNeedUpdate := !reflect.DeepEqual(desiredResources, deployment.Spec.Template.Spec.Containers[0].Resources)
176+
deploymentResourcesNeedUpdate := !apiequality.Semantic.DeepEqual(desiredResources, deployment.Spec.Template.Spec.Containers[0].Resources)
177177
if deploymentResourcesNeedUpdate {
178178
deployment.Spec.Template.Spec.Containers[0].Resources = desiredResources
179179
}
180180

181-
deploymentImageNeedUpdate := !reflect.DeepEqual(ts.Spec.Ingress.Image, deployment.Spec.Template.Spec.Containers[0].Image)
181+
deploymentImageNeedUpdate := !apiequality.Semantic.DeepEqual(ts.Spec.Ingress.Image, deployment.Spec.Template.Spec.Containers[0].Image)
182182
if deploymentImageNeedUpdate {
183183
deployment.Spec.Template.Spec.Containers[0].Image = ts.Spec.Ingress.Image
184184
}
@@ -199,7 +199,7 @@ func (r *TypesenseClusterReconciler) ReconcileIngress(ctx context.Context, ts ts
199199
}
200200
}
201201

202-
if !reflect.DeepEqual(securityContext, deployment.Spec.Template.Spec.Containers[0].SecurityContext) {
202+
if !apiequality.Semantic.DeepEqual(securityContext, deployment.Spec.Template.Spec.Containers[0].SecurityContext) {
203203
readOnlyRootFilesystemSpecsNeedUpdate = true
204204
deployment.Spec.Template.Spec.Containers[0].SecurityContext = securityContext
205205
}
@@ -282,7 +282,7 @@ func (r *TypesenseClusterReconciler) createIngress(ctx context.Context, key clie
282282
var tlsSecretName string
283283

284284
if ts.Spec.Ingress.ClusterIssuer != nil {
285-
annotations["cert-manager.io/cluster-issuer"] = *ts.Spec.Ingress.ClusterIssuer
285+
annotations[clusterIssuerAnnotationKey] = *ts.Spec.Ingress.ClusterIssuer
286286
tlsSecretName = fmt.Sprintf("%s-reverse-proxy-%s-certificate-tls", ts.Name, *ts.Spec.Ingress.ClusterIssuer)
287287
}
288288

@@ -358,7 +358,7 @@ func (r *TypesenseClusterReconciler) updateIngress(ctx context.Context, ig netwo
358358
var tlsSecretName string
359359

360360
if ts.Spec.Ingress.ClusterIssuer != nil {
361-
annotations["cert-manager.io/cluster-issuer"] = *ts.Spec.Ingress.ClusterIssuer
361+
annotations[clusterIssuerAnnotationKey] = *ts.Spec.Ingress.ClusterIssuer
362362
tlsSecretName = fmt.Sprintf("%s-reverse-proxy-%s-certificate-tls", ts.Name, *ts.Spec.Ingress.ClusterIssuer)
363363
}
364364

@@ -389,17 +389,8 @@ func (r *TypesenseClusterReconciler) deleteIngress(ctx context.Context, ig *netw
389389
}
390390

391391
func (r *TypesenseClusterReconciler) getIngressAnnotations(ig *networkingv1.Ingress) map[string]string {
392-
annotations := make(map[string]string, len(ig.Annotations))
393-
for k, v := range ig.Annotations {
394-
annotations[k] = v
395-
}
396-
397-
delete(annotations, "cert-manager.io/cluster-issuer")
398-
if len(annotations) == 0 {
399-
annotations = nil
400-
}
401-
402-
return annotations
392+
filtered := filterAnnotations(ig.Annotations, clusterIssuerAnnotationKey, rancherDomainAnnotationKey)
393+
return filtered
403394
}
404395

405396
func (r *TypesenseClusterReconciler) createIngressConfigMap(ctx context.Context, key client.ObjectKey, ts *tsv1alpha1.TypesenseCluster, ig *networkingv1.Ingress) (*v1.ConfigMap, error) {

internal/controller/typesensecluster_statefulset.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
readLagAnnotationKey = "ts.opentelekomcloud.com/read-lag-threshold"
3131
writeLagAnnotationKey = "ts.opentelekomcloud.com/write-lag-threshold"
3232
restartPodsAnnotationKey = "kubectl.kubernetes.io/restartedAt"
33+
rancherDomainAnnotationKey = "cattle.io"
3334
)
3435

3536
func (r *TypesenseClusterReconciler) ReconcileStatefulSet(ctx context.Context, ts *tsv1alpha1.TypesenseCluster) (*appsv1.StatefulSet, error) {
@@ -91,7 +92,7 @@ func (r *TypesenseClusterReconciler) ReconcileStatefulSet(ctx context.Context, t
9192

9293
update, triggers := r.shouldUpdateStatefulSet(sts, desiredSts, ts)
9394
if update {
94-
r.logger.V(debugLevel).Info("updating statefulset", "sts", sts.Name, "trigger", triggers)
95+
r.logger.V(debugLevel).Info("updating statefulset", "sts", sts.Name, "triggers", triggers)
9596

9697
oldImage := strings.Replace(sts.Spec.Template.Spec.Containers[0].Image, "typesense/typesense:", "", -1)
9798
newImage := strings.Replace(desiredSts.Spec.Template.Spec.Containers[0].Image, "typesense/typesense:", "", -1)
@@ -529,22 +530,6 @@ var (
529530
ContainerSecurityContextChanged UpdateStatefulSetTrigger = "ContainerSecurityContextChanged"
530531
)
531532

532-
func filterStatefulSetAnnotations(annotations map[string]string) map[string]string {
533-
if len(annotations) == 0 {
534-
return annotations
535-
}
536-
537-
filtered := make(map[string]string, len(annotations))
538-
for key, value := range annotations {
539-
if strings.HasPrefix(key, "field.cattle.io") {
540-
continue
541-
}
542-
filtered[key] = value
543-
}
544-
545-
return filtered
546-
}
547-
548533
func (r *TypesenseClusterReconciler) shouldUpdateStatefulSet(sts *appsv1.StatefulSet, desired *appsv1.StatefulSet, ts *tsv1alpha1.TypesenseCluster) (update bool, triggers []UpdateStatefulSetTrigger) {
549534
update = false
550535

@@ -570,9 +555,8 @@ func (r *TypesenseClusterReconciler) shouldUpdateStatefulSet(sts *appsv1.Statefu
570555
update = true
571556
}
572557

573-
stsAnnotations := filterStatefulSetAnnotations(sts.ObjectMeta.Annotations)
574-
podAnnotations := filterStatefulSetAnnotations(sts.Spec.Template.Annotations)
575-
delete(podAnnotations, restartPodsAnnotationKey)
558+
stsAnnotations := filterAnnotations(sts.ObjectMeta.Annotations, rancherDomainAnnotationKey)
559+
podAnnotations := filterAnnotations(sts.Spec.Template.Annotations, restartPodsAnnotationKey, rancherDomainAnnotationKey)
576560

577561
// PodAnnotationsChanged
578562
if !apiequality.Semantic.DeepEqual(podAnnotations, desired.Spec.Template.Annotations) {

internal/controller/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"regexp"
88
"sort"
9+
"strings"
910

1011
tsv1alpha1 "github.com/akyriako/typesense-operator/api/v1alpha1"
1112
"golang.org/x/text/cases"
@@ -187,3 +188,26 @@ func hasIP4Prefix(s string) bool {
187188
func toTitle(s string) string {
188189
return cases.Title(language.Und, cases.NoLower).String(s)
189190
}
191+
192+
func filterAnnotations(annotations map[string]string, filters ...string) map[string]string {
193+
if len(annotations) == 0 {
194+
return annotations
195+
}
196+
197+
filtered := make(map[string]string, len(annotations))
198+
for key, value := range annotations {
199+
skip := false
200+
for _, f := range filters {
201+
if strings.Contains(key, f) {
202+
skip = true
203+
break
204+
}
205+
}
206+
if skip {
207+
continue
208+
}
209+
filtered[key] = value
210+
}
211+
212+
return filtered
213+
}

0 commit comments

Comments
 (0)