Skip to content

Commit 95c7b72

Browse files
committed
CachedResource replication reconciler uses hash as CachedObject name
The name of the CachedObject is a hash generated as base36(sha256(GVR+namespace+name)) from its wrapped object to prevent hitting the name length limit. On-behalf-of: @SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent c1b5e45 commit 95c7b72

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

pkg/reconciler/cache/cachedresources/replication/replication_reconcile.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ limitations under the License.
1717
package replication
1818

1919
import (
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+
5268
func (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

Comments
 (0)