generated from telekom/reuse-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): implement reconciliation
- Loading branch information
1 parent
ca28afd
commit 4a2ce71
Showing
6 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2024 Deutsche Telekom IT GmbH | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package reconciliation | ||
|
||
import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
type Functions struct { | ||
AddFunc func(obj *unstructured.Unstructured) | ||
CountFunc func(mapName string) (int, error) | ||
KeysFunc func(mapName string) ([]string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2024 Deutsche Telekom IT GmbH | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package reconciliation | ||
|
||
import ( | ||
"context" | ||
"github.com/rs/zerolog/log" | ||
"github.com/telekom/quasar/internal/config" | ||
"github.com/telekom/quasar/internal/utils" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/dynamic" | ||
) | ||
|
||
type Reconciliation struct { | ||
client dynamic.Interface | ||
resource *config.ResourceConfiguration | ||
} | ||
|
||
func NewReconciliation(client dynamic.Interface, resource *config.ResourceConfiguration) *Reconciliation { | ||
return &Reconciliation{client, resource} | ||
} | ||
|
||
func (r *Reconciliation) Reconcile(reconcileFuncs Functions) { | ||
resources, err := r.client.Resource(r.resource.GetGroupVersionResource()). | ||
Namespace(r.resource.Kubernetes.Namespace). | ||
List(context.Background(), v1.ListOptions{}) | ||
|
||
if err != nil { | ||
log.Error().Err(err).Fields(map[string]any{ | ||
"cache": r.resource.GetCacheName(), | ||
}).Msg("Could not retrieve resources from cluster") | ||
return | ||
} | ||
|
||
resourceCount := len(resources.Items) | ||
storeSize, err := reconcileFuncs.CountFunc(r.resource.GetCacheName()) | ||
if err != nil { | ||
log.Error().Err(err).Fields(map[string]any{ | ||
"cache": r.resource.GetCacheName(), | ||
}).Msg("Could not get size of store") | ||
return | ||
} | ||
|
||
log.Info().Fields(map[string]any{ | ||
"cache": r.resource.GetCacheName(), | ||
"storeSize": storeSize, | ||
"resourceCount": resourceCount, | ||
}).Msg("Checking for store size mismatch...") | ||
|
||
if storeSize < resourceCount { | ||
log.Warn().Fields(map[string]any{ | ||
"cache": r.resource.GetCacheName(), | ||
}).Msg("Store size does not match resource count. Generating diff for reconciliation...") | ||
|
||
storeKeys, err := reconcileFuncs.KeysFunc(r.resource.GetCacheName()) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Could no retrieve store keys") | ||
} | ||
|
||
missingEntries := r.generateDiff(resources.Items, storeKeys) | ||
log.Warn().Msgf("Identified %d missing cache entries. Reprocessing...", len(missingEntries)) | ||
for _, entry := range missingEntries { | ||
reconcileFuncs.AddFunc(&entry) | ||
log.Warn().Fields(utils.CreateFieldsForOp("restore", &entry)).Msg("Restored") | ||
} | ||
} | ||
} | ||
|
||
func (r *Reconciliation) generateDiff(resources []unstructured.Unstructured, storeKeys []string) []unstructured.Unstructured { | ||
var diff = make([]unstructured.Unstructured, 0) | ||
for _, resource := range resources { | ||
found := false | ||
for _, storeKey := range storeKeys { | ||
if resource.GetName() == storeKey { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
diff = append(diff, resource) | ||
} | ||
} | ||
|
||
return diff | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters