Skip to content

Commit

Permalink
add events, and fix master node fetch script
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolik committed Jul 31, 2024
1 parent e597043 commit f792a11
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions internal/controller/valkey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"math/big"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -164,6 +165,9 @@ func (r *ValkeyReconciler) upsertService(ctx context.Context, valkey *hyperv1.Va
} else {
return err
}
} else {
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("Service %s/%s is created", valkey.Namespace, valkey.Name))
}
return nil
}
Expand Down Expand Up @@ -206,13 +210,16 @@ func (r *ValkeyReconciler) upsertConfigMap(ctx context.Context, valkey *hyperv1.
if err := r.Create(ctx, cm); err != nil {
if errors.IsAlreadyExists(err) {
if err := r.Update(ctx, cm); err != nil {
logger.Error(err, "failed to update service", "valkey", valkey.Name, "namespace", valkey.Namespace)
logger.Error(err, "failed to update ConfigMap", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
} else {
logger.Error(err, "failed to create service", "valkey", valkey.Name, "namespace", valkey.Namespace)
logger.Error(err, "failed to create ConfigMap", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
} else {
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("ConfigMap %s/%s is created", valkey.Namespace, valkey.Name))
}
return nil
}
Expand Down Expand Up @@ -260,6 +267,8 @@ func (r *ValkeyReconciler) upsertServiceHeadless(ctx context.Context, valkey *hy
return err
}
}
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("Service %s/%s is created", valkey.Namespace, valkey.Name+"-headless"))
return nil
}

Expand Down Expand Up @@ -291,6 +300,8 @@ func (r *ValkeyReconciler) upsertSecret(ctx context.Context, valkey *hyperv1.Val
logger.Error(err, "failed to update secret", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("Secret %s/%s is created", valkey.Namespace, valkey.Name))
} else if err == nil && !once {
if err := r.Update(ctx, secret); err != nil {
logger.Error(err, "failed to create secret", "valkey", valkey.Name, "namespace", valkey.Namespace)
Expand Down Expand Up @@ -327,10 +338,21 @@ func (r *ValkeyReconciler) upsertServiceAccount(ctx context.Context, valkey *hyp
logger.Error(err, "failed to create service account", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
} else {
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("ServiceAccount %s/%s is created", valkey.Namespace, valkey.Name))
}
return nil
}

func getMasterNodes(valkey *hyperv1.Valkey) string {
var nodes []string
for i := 0; i <= int(valkey.Spec.MasterNodes); i++ {
nodes = append(nodes, valkey.Name+"-"+fmt.Sprint(i)+"."+valkey.Name+"-headless."+valkey.Namespace+".svc")
}
return strings.Join(nodes, " ")
}

func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv1.Valkey) error {
logger := log.FromContext(ctx)

Expand Down Expand Up @@ -431,9 +453,8 @@ fi
},
},
{
Name: "VALKEY_NODES",
// @TODO generate nodes function
Value: valkey.Name + "-0." + valkey.Name + "-headless." + valkey.Namespace + ".svc " + valkey.Name + "-1." + valkey.Name + "-headless." + valkey.Namespace + ".svc " + valkey.Name + "-2." + valkey.Name + "-headless." + valkey.Namespace + ".svc",
Name: "VALKEY_NODES",
Value: getMasterNodes(valkey),
},
{
Name: "REDISCLI_AUTH",
Expand Down Expand Up @@ -595,6 +616,9 @@ fi
logger.Error(err, "failed to create statefulset", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
} else {
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("StatefulSet %s/%s is created", valkey.Namespace, valkey.Name))
}
return nil
}
Expand Down

0 comments on commit f792a11

Please sign in to comment.