Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching revision in IstioOperator istio-gateway cache fix #42529

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions operator/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func RemoveObject(name, objHash string) {
objectCache.Mu.Unlock()
}
}

// RemoveCache removes the object Cache with the give name.
func RemoveCache(name string) {
objectCachesMu.Lock()
defer objectCachesMu.Unlock()

delete(objectCaches, name)
}
57 changes: 56 additions & 1 deletion operator/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestRemoveObject(t *testing.T) {
tests := []struct {
desc string
in map[string]*ObjectCache
// key for map of caces
// key for map of caches
objCacheRemovalKey string
// key for map of K8sObjects
removalKey string
Expand Down Expand Up @@ -150,3 +150,58 @@ func TestRemoveObject(t *testing.T) {
})
}
}

func TestRemoveCache(t *testing.T) {
tests := []struct {
desc string
in map[string]*ObjectCache
// key for map of caches
objCacheRemovalKey string
expected map[string]*ObjectCache
}{
{
desc: "remove-cache",
in: map[string]*ObjectCache{
"cache-foo-key": {
Cache: map[string]*object.K8sObject{
"obj-foo-key": object.NewK8sObject(&unstructured.Unstructured{
Object: make(map[string]any),
}, nil, nil),
},
Mu: &sync.RWMutex{},
},
"dont-touch-me-key": {
Cache: map[string]*object.K8sObject{
"obj-foo-key": object.NewK8sObject(&unstructured.Unstructured{
Object: make(map[string]any),
}, nil, nil),
},
Mu: &sync.RWMutex{},
},
},
objCacheRemovalKey: "cache-foo-key",
expected: map[string]*ObjectCache{
"dont-touch-me-key": {
Cache: map[string]*object.K8sObject{
"obj-foo-key": object.NewK8sObject(&unstructured.Unstructured{
Object: make(map[string]any),
}, nil, nil),
},
Mu: &sync.RWMutex{},
},
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
for key, value := range tt.in {
objectCaches[key] = value
}
defer FlushObjectCaches()
RemoveCache(tt.objCacheRemovalKey)
if !reflect.DeepEqual(objectCaches, tt.expected) {
t.Errorf("%s: expected object cache %v, got %v\n", tt.desc, tt.expected, objectCaches)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ var (
return false
}

// If revision is updated in the IstioOperator resource, we must remove entries
// from the cache. If the IstioOperator resource is reverted back to match this operator's
// revision, a clean cache would ensure that the operator Reconcile the IstioOperator,
// and not skip it.
if oldIOP.Spec.Revision != newIOP.Spec.Revision {
var host string
if restConfig != nil {
host = restConfig.Host
}
for _, component := range name.AllComponentNames {
crHash := strings.Join([]string{newIOP.Name, newIOP.Namespace, string(component), host}, "-")
cache.RemoveCache(crHash)
}
}

if oldIOP.GetDeletionTimestamp() != newIOP.GetDeletionTimestamp() {
metrics.IncrementReconcileRequest("update_deletion_timestamp")
return true
Expand Down