@@ -17,12 +17,16 @@ limitations under the License.
1717package replication
1818
1919import (
20+ "bytes"
2021 "context"
22+ "crypto/sha256"
2123 "encoding/json"
2224 "fmt"
2325 "strings"
2426 "time"
2527
28+ "github.com/martinlindhe/base36"
29+
2630 apierrors "k8s.io/apimachinery/pkg/api/errors"
2731 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2832 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -49,6 +53,18 @@ const (
4953 LabelKeyObjectOriginalNamespace = "cache.kcp.io/object-original-namespace"
5054)
5155
56+ func GenCachedObjectName (gvr schema.GroupVersionResource , namespace , name string ) string {
57+ buf := bytes.Buffer {}
58+ buf .WriteString (gvr .String ())
59+ buf .WriteString (namespace )
60+ buf .WriteString (name )
61+
62+ hash := sha256 .Sum256 ([]byte (name ))
63+ base36hash := strings .ToLower (base36 .EncodeBytes (hash [:]))
64+
65+ return base36hash
66+ }
67+
5268func (c * Controller ) reconcile (ctx context.Context , gvrKey string ) error {
5369 if c .deleted {
5470 return nil
@@ -142,7 +158,7 @@ func (c *Controller) reconcile(ctx context.Context, gvrKey string) error {
142158 APIVersion : cachev1alpha1 .SchemeGroupVersion .String (),
143159 },
144160 ObjectMeta : metav1.ObjectMeta {
145- Name : gvr . Version + "." + gvr . Resource + "." + gvr . Group + "." + obj .GetName (), // TODO: handle namespace
161+ Name : GenCachedObjectName ( gvr , obj . GetNamespace (), obj .GetName ()),
146162 Labels : obj .GetLabels (),
147163 Annotations : obj .GetAnnotations (),
148164 CreationTimestamp : metav1 .NewTime (time .Now ()),
@@ -182,7 +198,7 @@ func (c *Controller) reconcile(ctx context.Context, gvrKey string) error {
182198 APIVersion : cachev1alpha1 .SchemeGroupVersion .String (),
183199 },
184200 ObjectMeta : metav1.ObjectMeta {
185- Name : gvr . Version + "." + gvr . Resource + "." + gvr . Group + "." + obj .GetName (),
201+ Name : GenCachedObjectName ( gvr , obj . GetNamespace (), obj .GetName () ),
186202 Labels : obj .GetLabels (),
187203 Annotations : obj .GetAnnotations (),
188204 ResourceVersion : obj .GetResourceVersion (),
@@ -212,8 +228,11 @@ func (c *Controller) reconcile(ctx context.Context, gvrKey string) error {
212228 gvr .Group = "core"
213229 }
214230
215- nameCache := gvr .Version + "." + gvr .Resource + "." + gvr .Group + "." + name // TODO: handle namespace
216- return c .kcpCacheClient .Cluster (cluster .Path ()).CacheV1alpha1 ().CachedObjects ().Delete (ctx , nameCache , metav1.DeleteOptions {})
231+ cachedObjName := GenCachedObjectName (gvr , ns , name )
232+ if ns != "" {
233+ cachedObjName += "." + ns
234+ }
235+ return c .kcpCacheClient .Cluster (cluster .Path ()).CacheV1alpha1 ().CachedObjects ().Delete (ctx , cachedObjName , metav1.DeleteOptions {})
217236 },
218237 }
219238 defer c .callback ()
0 commit comments