Skip to content

Commit d57cd7d

Browse files
aalexandruaalexand
andauthored
Skip cache invalidation for SLTs (#260)
Co-authored-by: aalexand <[email protected]>
1 parent 4f730e6 commit d57cd7d

File tree

7 files changed

+48
-13
lines changed

7 files changed

+48
-13
lines changed

cmd/apiserver/apiserver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ func main() {
129129
log.Errorf("Failed to delete message: %s", err.Error())
130130
return
131131
}
132+
133+
if val, ok := msg.MessageAttributes[sqs.MessageAttributeSkipCacheInvalidation]; ok && *val.StringValue == "true" {
134+
log.Debugf("Skipping cache invalidation")
135+
return
136+
}
137+
132138
log.Debugf("Invalidating clusters cache")
133139
err = cacheManager.Invalidate(context.Background(), store.WithInvalidateTags([]string{"clusters"}))
134140
if err != nil {

local/sqs/sqs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func main() {
9393
DataType: aws.String("String"),
9494
StringValue: aws.String(cluster.Spec.Name),
9595
},
96+
"SkipCacheInvalidation": {
97+
DataType: aws.String("Bool"),
98+
StringValue: aws.String(fmt.Sprintf("%t", false)),
99+
},
96100
},
97101
MessageBody: aws.String(string(data)),
98102
},

pkg/client/controllers/cluster_controller.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type ClusterReconciler struct {
4646
const (
4747
// HashAnnotation ...
4848
HashAnnotation = "registry.ethos.adobe.com/hash"
49+
50+
// SkipCacheInvalidationAnnotation ...
51+
SkipCacheInvalidationAnnotation = "registry.ethos.adobe.com/skip-cache-invalidation"
4952
)
5053

5154
//+kubebuilder:rbac:groups=registry.ethos.adobe.com,resources=clusters,verbs=get;list;watch;create;update;patch;delete
@@ -89,10 +92,18 @@ func (r *ClusterReconciler) ReconcileCreateUpdate(instance *registryv1.Cluster,
8992
if annotations == nil {
9093
annotations = make(map[string]string, 1)
9194
}
95+
9296
annotations[HashAnnotation] = hash
97+
98+
skipCacheInvalidation := false
99+
if _, ok := annotations[SkipCacheInvalidationAnnotation]; ok {
100+
delete(annotations, SkipCacheInvalidationAnnotation)
101+
skipCacheInvalidation = true
102+
}
103+
93104
instance.SetAnnotations(annotations)
94105

95-
err := r.enqueue(instance)
106+
err := r.enqueue(instance, skipCacheInvalidation)
96107
if err != nil {
97108
r.Log.Error(err, "error enqueuing message")
98109
return ctrl.Result{}, err
@@ -132,7 +143,7 @@ func (r *ClusterReconciler) eventFilters() predicate.Predicate {
132143
}
133144
}
134145

135-
func (r *ClusterReconciler) enqueue(instance *registryv1.Cluster) error {
146+
func (r *ClusterReconciler) enqueue(instance *registryv1.Cluster, skipCacheInvalidation bool) error {
136147
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
137148
defer cancel()
138149

@@ -160,6 +171,10 @@ func (r *ClusterReconciler) enqueue(instance *registryv1.Cluster) error {
160171
DataType: aws.String("String"),
161172
StringValue: aws.String(instance.Spec.Name),
162173
},
174+
"SkipCacheInvalidation": {
175+
DataType: aws.String("String"),
176+
StringValue: aws.String(fmt.Sprintf("%t", skipCacheInvalidation)),
177+
},
163178
},
164179
MessageBody: aws.String(string(obj)),
165180
},
@@ -182,12 +197,13 @@ func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
182197
}
183198

184199
// hashCluster returns a SHA256 hash of the Cluster object, after removing the ResourceVersion,
185-
// ManagedFields and hashCluster annotation
200+
// ManagedFields and hash/no-cache annotation
186201
func hashCluster(instance *registryv1.Cluster) string {
187202
clone := instance.DeepCopyObject().(*registryv1.Cluster)
188203

189204
annotations := clone.GetAnnotations()
190205
delete(annotations, HashAnnotation)
206+
delete(annotations, SkipCacheInvalidationAnnotation)
191207
clone.SetAnnotations(annotations)
192208

193209
clone.SetResourceVersion("")

pkg/sqs/event.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919
)
2020

2121
const (
22-
MessageAttributeType = "Type"
23-
MessageAttributeClusterName = "ClusterName"
22+
MessageAttributeType = "Type"
23+
MessageAttributeClusterName = "ClusterName"
24+
MessageAttributeSkipCacheInvalidation = "SkipCacheInvalidation"
2425

2526
// ClusterUpdateEvent refers to an update of the Cluster object that
2627
// is sent by the client controller. This event is sent to the SQS queue and

test/slt/checks/update/update.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"errors"
2424
"fmt"
25+
"github.com/adobe/cluster-registry/pkg/client/controllers"
2526
"time"
2627

2728
h "github.com/adobe/cluster-registry/test/slt/helpers"
@@ -124,6 +125,14 @@ func updateCrd(namespace string) (string, string, error) {
124125
// Remove immutable Kubernetes field
125126
(*cluster).ObjectMeta.ManagedFields = []metav1.ManagedFieldsEntry{}
126127

128+
// Set SkipCacheInvalidationAnnotation to true
129+
annotations := (*cluster).GetAnnotations()
130+
if annotations == nil {
131+
annotations = make(map[string]string, 1)
132+
}
133+
annotations[controllers.SkipCacheInvalidationAnnotation] = ""
134+
(*cluster).SetAnnotations(annotations)
135+
127136
data, err := json.Marshal(*cluster)
128137
if err != nil {
129138
return "", "", fmt.Errorf("could not marshal updated CRD: %s", err.Error())
@@ -153,8 +162,7 @@ func checkAPIforUpdate(url, clusterName, tagSLTValue, jwtToken string) error {
153162
if cluster.Tags == nil {
154163
return errors.New("tags field is empty")
155164
} else if tagSLTValue != cluster.Tags[tagSLT] {
156-
return fmt.Errorf("the 'Tags' field is not what expected. The "+
157-
"value is '%s', expected '%s'.", cluster.Tags[tagSLT], tagSLTValue)
165+
return fmt.Errorf("the 'Tags' field is not what expected. The value is '%s', expected '%s'", cluster.Tags[tagSLT], tagSLTValue)
158166
}
159167

160168
return nil
@@ -174,7 +182,7 @@ func Run(config TestConfig, jwtToken string) (int, error) {
174182
for nrOfTries <= maxNrOfTries {
175183
// Give to the CR client time to push to the SQS queue and for the API to read
176184
// from the queue and update the DB. By local tests it takes around 11s
177-
time.Sleep(11 * time.Second)
185+
time.Sleep(30 * time.Second)
178186

179187
logger.Infof("checking the API for the update (check %d/%d)...",
180188
nrOfTries, maxNrOfTries)

test/slt/helpers/helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ func reqGet(endpoint, bearer string) (*[]byte, int, error) {
127127
func GetCluster(url, clusterName, jwtToken string) (*cr.ClusterSpec, error) {
128128
var cluster cr.ClusterSpec
129129

130-
endpoint := fmt.Sprintf("%s/api/v1/clusters/%s", url, clusterName)
130+
endpoint := fmt.Sprintf("%s/api/v2/clusters/%s", url, clusterName)
131131
bearer := "Bearer " + jwtToken
132132

133133
start := time.Now()
134134
body, respCode, err := reqGet(endpoint, bearer)
135135
timeTook := float64(time.Since(start).Seconds())
136136
metrics.EgressReqDuration.WithLabelValues(
137-
"/api/v1/clusters/[cluster]",
137+
"/api/v2/clusters/[cluster]",
138138
"GET",
139139
strconv.Itoa(respCode)).Observe(timeTook)
140140
if err != nil {
@@ -156,14 +156,14 @@ func GetCluster(url, clusterName, jwtToken string) (*cr.ClusterSpec, error) {
156156
func GetClusters(url, perPageLimit, pageNr, jwtToken string) (*ClusterList, error) {
157157
var clusters ClusterList
158158

159-
endpoint := fmt.Sprintf("%s/api/v1/clusters?offset=%s&limit=%s", url, pageNr, perPageLimit)
159+
endpoint := fmt.Sprintf("%s/api/v2/clusters?offset=%s&limit=%s", url, pageNr, perPageLimit)
160160
bearer := "Bearer " + jwtToken
161161

162162
start := time.Now()
163163
body, respCode, err := reqGet(endpoint, bearer)
164164
timeTook := float64(time.Since(start).Seconds())
165165
metrics.EgressReqDuration.WithLabelValues(
166-
"/api/v1/clusters",
166+
"/api/v2/clusters",
167167
"GET",
168168
strconv.Itoa(respCode)).Observe(timeTook)
169169
if err != nil {

test/slt/release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ IMAGE_SLT="${IMAGE_SLT:-"${default_image_name}"}"
2424
IMAGE_SLT="${IMAGE_SLT}${IMAGE_SUFFIX}"
2525

2626

27-
printf "Realeasing image %s...\n\n" "${IMAGE_SLT}:${TAG}"
27+
printf "Releasing image %s...\n\n" "${IMAGE_SLT}:${TAG}"
2828

2929
make -C "${ROOT_DIR}" --always-make build-slt \
3030
TAG="${TAG}" \

0 commit comments

Comments
 (0)