Skip to content

Commit c69799a

Browse files
authored
Updated th redis for Binding Policy (#910)
1 parent b95f584 commit c69799a

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

backend/redis/redis.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ func StoreBindingPolicy(policy *BindingPolicyCache) error {
296296
return fmt.Errorf("cannot store nil binding policy")
297297
}
298298

299+
// Check if Redis is available
300+
if err := rdb.Ping(ctx).Err(); err != nil {
301+
log.LogWarn("redis not available, skipping cache store", zap.Error(err))
302+
return nil // Don't fail the operation if Redis is down
303+
}
304+
299305
// Marshal the policy to JSON
300306
jsonData, err := json.Marshal(policy)
301307
if err != nil {
@@ -319,6 +325,11 @@ func StoreBindingPolicy(policy *BindingPolicyCache) error {
319325

320326
// GetBindingPolicy retrieves a binding policy from Redis by name
321327
func GetBindingPolicy(name string) (*BindingPolicyCache, error) {
328+
// Check if Redis is available
329+
if err := rdb.Ping(ctx).Err(); err != nil {
330+
return nil, fmt.Errorf("redis not available: %v", err)
331+
}
332+
322333
val, err := rdb.HGet(ctx, BindingPolicyHashKey, name).Result()
323334
if err == redis.Nil {
324335
return nil, nil // Policy not found
@@ -336,11 +347,20 @@ func GetBindingPolicy(name string) (*BindingPolicyCache, error) {
336347

337348
// GetAllBindingPolicies retrieves all binding policies from Redis
338349
func GetAllBindingPolicies() ([]*BindingPolicyCache, error) {
350+
// Check if Redis is available
351+
if err := rdb.Ping(ctx).Err(); err != nil {
352+
return nil, fmt.Errorf("redis not available: %v", err)
353+
}
354+
339355
values, err := rdb.HGetAll(ctx, BindingPolicyHashKey).Result()
340356
if err != nil {
341357
return nil, fmt.Errorf("failed to get all binding policies from Redis: %v", err)
342358
}
343359

360+
if len(values) == 0 {
361+
return nil, nil
362+
}
363+
344364
policies := make([]*BindingPolicyCache, 0, len(values))
345365
for _, val := range values {
346366
var policy BindingPolicyCache
@@ -356,6 +376,12 @@ func GetAllBindingPolicies() ([]*BindingPolicyCache, error) {
356376

357377
// DeleteBindingPolicy removes a binding policy from Redis
358378
func DeleteBindingPolicy(name string) error {
379+
// Check if Redis is available
380+
if err := rdb.Ping(ctx).Err(); err != nil {
381+
log.LogWarn("redis not available, skipping cache delete", zap.Error(err))
382+
return nil // Don't fail the operation if Redis is down
383+
}
384+
359385
err := rdb.HDel(ctx, BindingPolicyHashKey, name).Err()
360386
if err != nil {
361387
return fmt.Errorf("failed to delete binding policy from Redis: %v", err)
@@ -365,6 +391,12 @@ func DeleteBindingPolicy(name string) error {
365391

366392
// DeleteAllBindingPolicies removes all binding policies from Redis
367393
func DeleteAllBindingPolicies() error {
394+
// Check if Redis is available
395+
if err := rdb.Ping(ctx).Err(); err != nil {
396+
log.LogWarn("redis not available, skipping cache delete", zap.Error(err))
397+
return nil // Don't fail the operation if Redis is down
398+
}
399+
368400
err := rdb.Del(ctx, BindingPolicyHashKey).Err()
369401
if err != nil {
370402
return fmt.Errorf("failed to delete all binding policies from Redis: %v", err)

backend/wds/bp/handlers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GetBindingPolicies(namespace string) ([]map[string]interface{}, error) {
5959
cachedPolicies, err := redis.GetAllBindingPolicies()
6060
if err != nil {
6161
log.LogWarn("failed to get binding policies from Redis cache", zap.Error(err))
62-
} else if len(cachedPolicies) > 0 {
62+
} else if cachedPolicies != nil && len(cachedPolicies) > 0 {
6363
// Convert cached policies to response format
6464
responseArray := make([]map[string]interface{}, len(cachedPolicies))
6565
for i, bpolicy := range cachedPolicies {
@@ -603,6 +603,7 @@ func CreateBp(ctx *gin.Context) {
603603
if err != nil {
604604
log.LogError(err.Error())
605605
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
606+
return
606607
}
607608

608609
// After successful creation, store in Redis
@@ -619,6 +620,7 @@ func CreateBp(ctx *gin.Context) {
619620
log.LogWarn("failed to cache new binding policy", zap.Error(err))
620621
}
621622

623+
ctx.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("Created binding policy '%s' successfully", bp.Name)})
622624
}
623625

624626
// DeleteBp deletes a BindingPolicy by name and namespace

backend/wds/bp/utils.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,50 @@ func watchOnBps() {
346346
}
347347
log.LogInfo("BP modified: ", zap.String("name", bp.Name))
348348

349+
// Update the cache with the modified binding policy
350+
status := "inactive"
351+
if bp.ObjectMeta.Generation == bp.Status.ObservedGeneration {
352+
status = "active"
353+
}
354+
355+
cachedPolicy := &redis.BindingPolicyCache{
356+
Name: bp.Name,
357+
Namespace: bp.Namespace,
358+
Status: status,
359+
BindingMode: "Downsync",
360+
Clusters: extractTargetClusters(bp),
361+
Workloads: extractWorkloads(bp),
362+
CreationTimestamp: bp.CreationTimestamp.Format("2006-01-02T15:04:05Z"),
363+
RawYAML: "", // We don't have the raw YAML in watch events
364+
}
365+
366+
if err := redis.StoreBindingPolicy(cachedPolicy); err != nil {
367+
log.LogWarn("failed to update binding policy in cache", zap.Error(err))
368+
}
369+
349370
case "ADDED":
350371
bp, _ := event.Object.(*v1alpha1.BindingPolicy)
351372
log.LogInfo("BP added: ", zap.String("name", bp.Name))
352373

374+
// Add the new binding policy to cache
375+
cachedPolicy := &redis.BindingPolicyCache{
376+
Name: bp.Name,
377+
Namespace: bp.Namespace,
378+
Status: "inactive", // New policies start as inactive
379+
BindingMode: "Downsync",
380+
Clusters: extractTargetClusters(bp),
381+
Workloads: extractWorkloads(bp),
382+
CreationTimestamp: bp.CreationTimestamp.Format("2006-01-02T15:04:05Z"),
383+
RawYAML: "", // We don't have the raw YAML in watch events
384+
}
385+
386+
if err := redis.StoreBindingPolicy(cachedPolicy); err != nil {
387+
log.LogWarn("failed to cache new binding policy from watch", zap.Error(err))
388+
}
389+
353390
case "DELETED":
354391
bp, _ := event.Object.(*v1alpha1.BindingPolicy)
355-
err := redis.DeleteBpcmd(bp.Name)
392+
err := redis.DeleteBindingPolicy(bp.Name)
356393
if err != nil {
357394
log.LogError("Error deleting bp from redis", zap.String("error", err.Error()))
358395
}

0 commit comments

Comments
 (0)