Skip to content

Commit

Permalink
Skip cache invalidation for SLTs (#260)
Browse files Browse the repository at this point in the history
Co-authored-by: aalexand <aalexand@adobe.com>
  • Loading branch information
aalexandru and aalexand authored Oct 4, 2024
1 parent 4f730e6 commit d57cd7d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
6 changes: 6 additions & 0 deletions cmd/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ func main() {
log.Errorf("Failed to delete message: %s", err.Error())
return
}

if val, ok := msg.MessageAttributes[sqs.MessageAttributeSkipCacheInvalidation]; ok && *val.StringValue == "true" {
log.Debugf("Skipping cache invalidation")
return
}

log.Debugf("Invalidating clusters cache")
err = cacheManager.Invalidate(context.Background(), store.WithInvalidateTags([]string{"clusters"}))
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions local/sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func main() {
DataType: aws.String("String"),
StringValue: aws.String(cluster.Spec.Name),
},
"SkipCacheInvalidation": {
DataType: aws.String("Bool"),
StringValue: aws.String(fmt.Sprintf("%t", false)),
},
},
MessageBody: aws.String(string(data)),
},
Expand Down
22 changes: 19 additions & 3 deletions pkg/client/controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type ClusterReconciler struct {
const (
// HashAnnotation ...
HashAnnotation = "registry.ethos.adobe.com/hash"

// SkipCacheInvalidationAnnotation ...
SkipCacheInvalidationAnnotation = "registry.ethos.adobe.com/skip-cache-invalidation"
)

//+kubebuilder:rbac:groups=registry.ethos.adobe.com,resources=clusters,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -89,10 +92,18 @@ func (r *ClusterReconciler) ReconcileCreateUpdate(instance *registryv1.Cluster,
if annotations == nil {
annotations = make(map[string]string, 1)
}

annotations[HashAnnotation] = hash

skipCacheInvalidation := false
if _, ok := annotations[SkipCacheInvalidationAnnotation]; ok {
delete(annotations, SkipCacheInvalidationAnnotation)
skipCacheInvalidation = true
}

instance.SetAnnotations(annotations)

err := r.enqueue(instance)
err := r.enqueue(instance, skipCacheInvalidation)
if err != nil {
r.Log.Error(err, "error enqueuing message")
return ctrl.Result{}, err
Expand Down Expand Up @@ -132,7 +143,7 @@ func (r *ClusterReconciler) eventFilters() predicate.Predicate {
}
}

func (r *ClusterReconciler) enqueue(instance *registryv1.Cluster) error {
func (r *ClusterReconciler) enqueue(instance *registryv1.Cluster, skipCacheInvalidation bool) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand Down Expand Up @@ -160,6 +171,10 @@ func (r *ClusterReconciler) enqueue(instance *registryv1.Cluster) error {
DataType: aws.String("String"),
StringValue: aws.String(instance.Spec.Name),
},
"SkipCacheInvalidation": {
DataType: aws.String("String"),
StringValue: aws.String(fmt.Sprintf("%t", skipCacheInvalidation)),
},
},
MessageBody: aws.String(string(obj)),
},
Expand All @@ -182,12 +197,13 @@ func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

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

annotations := clone.GetAnnotations()
delete(annotations, HashAnnotation)
delete(annotations, SkipCacheInvalidationAnnotation)
clone.SetAnnotations(annotations)

clone.SetResourceVersion("")
Expand Down
5 changes: 3 additions & 2 deletions pkg/sqs/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

const (
MessageAttributeType = "Type"
MessageAttributeClusterName = "ClusterName"
MessageAttributeType = "Type"
MessageAttributeClusterName = "ClusterName"
MessageAttributeSkipCacheInvalidation = "SkipCacheInvalidation"

// ClusterUpdateEvent refers to an update of the Cluster object that
// is sent by the client controller. This event is sent to the SQS queue and
Expand Down
14 changes: 11 additions & 3 deletions test/slt/checks/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/adobe/cluster-registry/pkg/client/controllers"
"time"

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

// Set SkipCacheInvalidationAnnotation to true
annotations := (*cluster).GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
annotations[controllers.SkipCacheInvalidationAnnotation] = ""
(*cluster).SetAnnotations(annotations)

data, err := json.Marshal(*cluster)
if err != nil {
return "", "", fmt.Errorf("could not marshal updated CRD: %s", err.Error())
Expand Down Expand Up @@ -153,8 +162,7 @@ func checkAPIforUpdate(url, clusterName, tagSLTValue, jwtToken string) error {
if cluster.Tags == nil {
return errors.New("tags field is empty")
} else if tagSLTValue != cluster.Tags[tagSLT] {
return fmt.Errorf("the 'Tags' field is not what expected. The "+
"value is '%s', expected '%s'.", cluster.Tags[tagSLT], tagSLTValue)
return fmt.Errorf("the 'Tags' field is not what expected. The value is '%s', expected '%s'", cluster.Tags[tagSLT], tagSLTValue)
}

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

logger.Infof("checking the API for the update (check %d/%d)...",
nrOfTries, maxNrOfTries)
Expand Down
8 changes: 4 additions & 4 deletions test/slt/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ func reqGet(endpoint, bearer string) (*[]byte, int, error) {
func GetCluster(url, clusterName, jwtToken string) (*cr.ClusterSpec, error) {
var cluster cr.ClusterSpec

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

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

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

start := time.Now()
body, respCode, err := reqGet(endpoint, bearer)
timeTook := float64(time.Since(start).Seconds())
metrics.EgressReqDuration.WithLabelValues(
"/api/v1/clusters",
"/api/v2/clusters",
"GET",
strconv.Itoa(respCode)).Observe(timeTook)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/slt/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ IMAGE_SLT="${IMAGE_SLT:-"${default_image_name}"}"
IMAGE_SLT="${IMAGE_SLT}${IMAGE_SUFFIX}"


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

make -C "${ROOT_DIR}" --always-make build-slt \
TAG="${TAG}" \
Expand Down

0 comments on commit d57cd7d

Please sign in to comment.