Skip to content

Commit

Permalink
[Enhancement]- Fix log pollution (#585)
Browse files Browse the repository at this point in the history
* Add param to set operator log verbosity

Signed-off-by: drivebyer <[email protected]>

* Level down some logger

Signed-off-by: drivebyer <[email protected]>

* Remove unnecessary flag, `opts.BindFlags(flag.CommandLine)` already add flag `zap-log-level`

Signed-off-by: drivebyer <[email protected]>

* Level up every info, not logger

Signed-off-by: drivebyer <[email protected]>

---------

Signed-off-by: drivebyer <[email protected]>
Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer authored Aug 27, 2023
1 parent d2b483e commit caf446e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 54 deletions.
26 changes: 13 additions & 13 deletions k8sutils/cluster-scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ func ReshardRedisCluster(cr *redisv1beta1.RedisCluster) {

cmd = append(cmd, "--cluster-yes")

logger.Info("Redis cluster reshard command is", "Command", cmd)
logger.V(1).Info("Redis cluster reshard command is", "Command", cmd)

if slot == "0" {
log.Info("Skipped the execution of", "Cmd", cmd)
logger.V(1).Info("Skipped the execution of", "Cmd", cmd)
return
}
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-0")
Expand Down Expand Up @@ -106,7 +106,7 @@ func getRedisClusterSlots(cr *redisv1beta1.RedisCluster, nodeID string) string {
}
}

logger.Info("Total cluster slots to be transfered from", "node", nodeID, "is", totalSlots)
logger.V(1).Info("Total cluster slots to be transfered from", "node", nodeID, "is", totalSlots)

return strconv.Itoa(totalSlots)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func getRedisNodeID(cr *redisv1beta1.RedisCluster, pod RedisDetails) string {
logger.Error(err, "Redis command failed with this error")
return ""
}
logger.Info("Redis node ID ", "is", output)
logger.V(1).Info("Redis node ID ", "is", output)
return output
}

Expand Down Expand Up @@ -170,7 +170,7 @@ func RebalanceRedisClusterEmptyMasters(cr *redisv1beta1.RedisCluster) {

cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, cr.ObjectMeta.Name+"-leader-0")...)

logger.Info("Redis cluster rebalance command is", "Command", cmd)
logger.V(1).Info("Redis cluster rebalance command is", "Command", cmd)
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-1")
}

Expand All @@ -187,7 +187,7 @@ func CheckIfEmptyMasters(cr *redisv1beta1.RedisCluster) {
podSlots := getRedisClusterSlots(cr, podNodeID)

if podSlots == "0" || podSlots == "" {
logger.Info("Found Empty Redis Leader Node", "pod", pod)
logger.V(1).Info("Found Empty Redis Leader Node", "pod", pod)
RebalanceRedisClusterEmptyMasters(cr)
break
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func RebalanceRedisCluster(cr *redisv1beta1.RedisCluster) {

cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, cr.ObjectMeta.Name+"-leader-0")...)

logger.Info("Redis cluster rebalance command is", "Command", cmd)
logger.V(1).Info("Redis cluster rebalance command is", "Command", cmd)
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-1")
}

Expand Down Expand Up @@ -262,7 +262,7 @@ func AddRedisNodeToCluster(cr *redisv1beta1.RedisCluster) {

cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, cr.ObjectMeta.Name+"-leader-0")...)

logger.Info("Redis cluster add-node command is", "Command", cmd)
logger.V(1).Info("Redis cluster add-node command is", "Command", cmd)
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-0")
}

Expand Down Expand Up @@ -292,7 +292,7 @@ func getAttachedFollowerNodeIDs(cr *redisv1beta1.RedisCluster, masterNodeID stri

}

logger.Info("Slaves Nodes attached to", "node", masterNodeID, "are", slaveIDs)
logger.V(1).Info("Slaves Nodes attached to", "node", masterNodeID, "are", slaveIDs)
return slaveIDs
}

Expand Down Expand Up @@ -336,7 +336,7 @@ func RemoveRedisFollowerNodesFromCluster(cr *redisv1beta1.RedisCluster) {
for _, followerNodeID := range followerNodeIDs {

cmd = append(cmd, followerNodeID)
logger.Info("Redis cluster follower remove command is", "Command", cmd)
logger.V(1).Info("Redis cluster follower remove command is", "Command", cmd)
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-0")
cmd = cmd[:len(cmd)-1]
}
Expand Down Expand Up @@ -379,9 +379,9 @@ func RemoveRedisNodeFromCluster(cr *redisv1beta1.RedisCluster) {

cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, cr.ObjectMeta.Name+"-leader-0")...)

logger.Info("Redis cluster leader remove command is", "Command", cmd)
logger.V(1).Info("Redis cluster leader remove command is", "Command", cmd)
if getRedisClusterSlots(cr, removePodNodeID) != "0" {
logger.Info("Skipping execution remove leader not empty", "cmd", cmd)
logger.V(1).Info("Skipping execution remove leader not empty", "cmd", cmd)
}
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-0")
}
Expand Down Expand Up @@ -440,6 +440,6 @@ func ClusterFailover(cr *redisv1beta1.RedisCluster) {

cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, slavePodName)...)

logger.Info("Redis cluster failover command is", "Command", cmd)
logger.V(1).Info("Redis cluster failover command is", "Command", cmd)
executeCommand(cr, cmd, slavePodName)
}
12 changes: 6 additions & 6 deletions k8sutils/poddisruption.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ReconcileRedisPodDisruptionBudget(cr *redisv1beta1.RedisCluster, role strin
if err == nil {
return deletePodDisruptionBudget(cr.Namespace, pdbName)
} else if err != nil && errors.IsNotFound(err) {
logger.Info("Reconciliation Successful, no PodDisruptionBudget Found.")
logger.V(1).Info("Reconciliation Successful, no PodDisruptionBudget Found.")
// Its ok if its not found, as we're deleting anyway
return nil
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func patchPodDisruptionBudget(storedPdb *policyv1.PodDisruptionBudget, newPdb *p
return err
}
if !patchResult.IsEmpty() {
logger.Info("Changes in PodDisruptionBudget Detected, Updating...",
logger.V(1).Info("Changes in PodDisruptionBudget Detected, Updating...",
"patch", string(patchResult.Patch),
"Current", string(patchResult.Current),
"Original", string(patchResult.Original),
Expand Down Expand Up @@ -132,7 +132,7 @@ func createPodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudg
logger.Error(err, "Redis PodDisruptionBudget creation failed")
return err
}
logger.Info("Redis PodDisruptionBudget creation was successful")
logger.V(1).Info("Redis PodDisruptionBudget creation was successful")
return nil
}

Expand All @@ -144,7 +144,7 @@ func updatePodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudg
logger.Error(err, "Redis PodDisruptionBudget update failed")
return err
}
logger.Info("Redis PodDisruptionBudget update was successful", "PDB.Spec", pdb.Spec)
logger.V(1).Info("Redis PodDisruptionBudget update was successful", "PDB.Spec", pdb.Spec)
return nil
}

Expand All @@ -156,7 +156,7 @@ func deletePodDisruptionBudget(namespace string, pdbName string) error {
logger.Error(err, "Redis PodDisruption deletion failed")
return err
}
logger.Info("Redis PodDisruption delete was successful")
logger.V(1).Info("Redis PodDisruption delete was successful")
return nil
}

Expand All @@ -171,7 +171,7 @@ func GetPodDisruptionBudget(namespace string, pdb string) (*policyv1.PodDisrupti
logger.Info("Redis PodDisruptionBudget get action failed")
return nil, err
}
logger.Info("Redis PodDisruptionBudget get action was successful")
logger.V(1).Info("Redis PodDisruptionBudget get action was successful")
return pdbInfo, err
}

Expand Down
2 changes: 1 addition & 1 deletion k8sutils/redis-sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func getRedisReplicationMasterIP(cr *redisv1beta1.RedisSentinel) string {
logger.Error(err, "Failed to Execute Get Request", "replication name", replicationName, "namespace", replicationNamespace)
return ""
} else {
logger.Info("Successfully Execute the Get Request", "replication name", replicationName, "namespace", replicationNamespace)
logger.V(1).Info("Successfully Execute the Get Request", "replication name", replicationName, "namespace", replicationNamespace)
}

// Marshal CustomObject to JSON
Expand Down
40 changes: 20 additions & 20 deletions k8sutils/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func getRedisServerIP(redisInfo RedisDetails) string {
// If we're NOT IPv4, assume were IPv6..
if redisIP != "" {
if net.ParseIP(redisIP).To4() == nil {
logger.Info("Redis is IPv6", "ip", redisIP, "ipv6", net.ParseIP(redisIP).To16())
logger.V(1).Info("Redis is IPv6", "ip", redisIP, "ipv6", net.ParseIP(redisIP).To16())
redisIP = fmt.Sprintf("[%s]", redisIP)
}
}

logger.Info("Successfully got the ip for redis", "ip", redisIP)
logger.V(1).Info("Successfully got the ip for redis", "ip", redisIP)
return redisIP
}

Expand All @@ -60,7 +60,7 @@ func CreateSingleLeaderRedisCommand(cr *redisv1beta1.RedisCluster) []string {
cmd = append(cmd, strconv.Itoa(i))
}

logger.Info("Redis Add Slots command for single node cluster is", "Command", cmd)
logger.V(1).Info("Redis Add Slots command for single node cluster is", "Command", cmd)
return cmd
}

Expand All @@ -83,7 +83,7 @@ func CreateMultipleLeaderRedisCommand(cr *redisv1beta1.RedisCluster) []string {
}
cmd = append(cmd, "--cluster-yes")

logger.Info("Redis Add Slots command for single node cluster is", "Command", cmd)
logger.V(1).Info("Redis Add Slots command for single node cluster is", "Command", cmd)
return cmd
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func ExecuteRedisClusterCommand(cr *redisv1beta1.RedisCluster) {
cmd = append(cmd, pass)
}
cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, cr.ObjectMeta.Name+"-leader-0")...)
logger.Info("Redis cluster creation command is", "Command", cmd)
logger.V(1).Info("Redis cluster creation command is", "Command", cmd)
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-0")
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func ExecuteRedisReplicationCommand(cr *redisv1beta1.RedisCluster) {
}
podIP = getRedisServerIP(followerPod)
if !checkRedisNodePresence(cr, nodes, podIP) {
logger.Info("Adding node to cluster.", "Node.IP", podIP, "Follower.Pod", followerPod)
logger.V(1).Info("Adding node to cluster.", "Node.IP", podIP, "Follower.Pod", followerPod)
cmd := createRedisReplicationCommand(cr, leaderPod, followerPod)
redisClient := configureRedisClient(cr, followerPod.PodName)
pong, err := redisClient.Ping().Result()
Expand All @@ -187,10 +187,10 @@ func ExecuteRedisReplicationCommand(cr *redisv1beta1.RedisCluster) {
if pong == "PONG" {
executeCommand(cr, cmd, cr.ObjectMeta.Name+"-leader-0")
} else {
logger.Info("Skipping execution of command due to failed Redis ping", "Follower.Pod", followerPod)
logger.V(1).Info("Skipping execution of command due to failed Redis ping", "Follower.Pod", followerPod)
}
} else {
logger.Info("Skipping Adding node to cluster, already present.", "Follower.Pod", followerPod)
logger.V(1).Info("Skipping Adding node to cluster, already present.", "Follower.Pod", followerPod)
}

followerIdx++
Expand All @@ -214,7 +214,7 @@ func checkRedisCluster(cr *redisv1beta1.RedisCluster) [][]string {
if err != nil {
logger.Error(err, "Redis command failed with this error")
}
logger.Info("Redis cluster nodes are listed", "Output", output)
logger.V(1).Info("Redis cluster nodes are listed", "Output", output)

csvOutput := csv.NewReader(strings.NewReader(output))
csvOutput.Comma = ' '
Expand Down Expand Up @@ -248,7 +248,7 @@ func executeFailoverCommand(cr *redisv1beta1.RedisCluster, role string) error {
replicas := cr.Spec.GetReplicaCounts(role)
podName := fmt.Sprintf("%s-%s-", cr.ObjectMeta.Name, role)
for podCount := 0; podCount <= int(replicas)-1; podCount++ {
logger.Info("Executing redis failover operations", "Redis Node", podName+strconv.Itoa(podCount))
logger.V(1).Info("Executing redis failover operations", "Redis Node", podName+strconv.Itoa(podCount))
client := configureRedisClient(cr, podName+strconv.Itoa(podCount))
defer client.Close()
cmd := redis.NewStringCmd("cluster", "reset")
Expand All @@ -272,7 +272,7 @@ func executeFailoverCommand(cr *redisv1beta1.RedisCluster, role string) error {
logger.Error(err, "Redis command failed with this error")
return err
}
logger.Info("Redis cluster failover executed", "Output", output)
logger.V(1).Info("Redis cluster failover executed", "Output", output)
}
return nil
}
Expand All @@ -299,9 +299,9 @@ func CheckRedisNodeCount(cr *redisv1beta1.RedisCluster, nodeType string) int32 {
count++
}
}
logger.Info("Number of redis nodes are", "Nodes", strconv.Itoa(count), "Type", nodeType)
logger.V(1).Info("Number of redis nodes are", "Nodes", strconv.Itoa(count), "Type", nodeType)
} else {
logger.Info("Total number of redis nodes are", "Nodes", strconv.Itoa(count))
logger.V(1).Info("Total number of redis nodes are", "Nodes", strconv.Itoa(count))
}
return int32(count)
}
Expand All @@ -317,7 +317,7 @@ func CheckRedisClusterState(cr *redisv1beta1.RedisCluster) int {
count++
}
}
logger.Info("Number of failed nodes in cluster", "Failed Node Count", count)
logger.V(1).Info("Number of failed nodes in cluster", "Failed Node Count", count)
return count
}

Expand Down Expand Up @@ -392,7 +392,7 @@ func executeCommand(cr *redisv1beta1.RedisCluster, cmd []string, podName string)
logger.Error(err, "Could not execute command", "Command", cmd, "Output", execOut.String(), "Error", execErr.String())
return
}
logger.Info("Successfully executed the command", "Command", cmd, "Output", execOut.String())
logger.V(1).Info("Successfully executed the command", "Command", cmd, "Output", execOut.String())
}

// getContainerID will return the id of container from pod
Expand All @@ -405,7 +405,7 @@ func getContainerID(cr *redisv1beta1.RedisCluster, podName string) (int, *corev1

targetContainer := -1
for containerID, tr := range pod.Spec.Containers {
logger.Info("Pod Counted successfully", "Count", containerID, "Container Name", tr.Name)
logger.V(1).Info("Pod Counted successfully", "Count", containerID, "Container Name", tr.Name)
if tr.Name == cr.ObjectMeta.Name+"-leader" {
targetContainer = containerID
break
Expand All @@ -417,7 +417,7 @@ func getContainerID(cr *redisv1beta1.RedisCluster, podName string) (int, *corev1
// checkRedisNodePresence will check if the redis node exist in cluster or not
func checkRedisNodePresence(cr *redisv1beta1.RedisCluster, nodeList [][]string, nodeName string) bool {
logger := generateRedisManagerLogger(cr.Namespace, cr.ObjectMeta.Name)
logger.Info("Checking if Node is in cluster", "Node", nodeName)
logger.V(1).Info("Checking if Node is in cluster", "Node", nodeName)
for _, node := range nodeList {
s := strings.Split(node[1], ":")
if s[0] == nodeName {
Expand Down Expand Up @@ -552,10 +552,10 @@ func CreateMasterSlaveReplication(cr *redisv1beta1.RedisReplication, masterPods

if len(slavePods) < 1 {
realMasterPod = masterPods[0]
logger.Info("No Master Node Found with attached slave promoting the following pod to master", "pod", masterPods[0])
logger.V(1).Info("No Master Node Found with attached slave promoting the following pod to master", "pod", masterPods[0])
}

logger.Info("Redis Master Node is set to", "pod", realMasterPod)
logger.V(1).Info("Redis Master Node is set to", "pod", realMasterPod)
realMasterInfo := RedisDetails{
PodName: realMasterPod,
Namespace: cr.Namespace,
Expand All @@ -568,7 +568,7 @@ func CreateMasterSlaveReplication(cr *redisv1beta1.RedisReplication, masterPods

redisClient := configureRedisReplicationClient(cr, masterPods[i])
defer redisClient.Close()
log.Info("Setting the", "pod", masterPods[i], "to slave of", realMasterPod)
logger.V(1).Info("Setting the", "pod", masterPods[i], "to slave of", realMasterPod)
err := redisClient.SlaveOf(realMasterPodIP, "6379").Err()
if err != nil {
logger.Error(err, "Failed to set", "pod", masterPods[i], "to slave of", realMasterPod)
Expand Down
4 changes: 2 additions & 2 deletions k8sutils/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func getRedisTLSConfig(cr *redisv1beta1.RedisCluster, redisInfo RedisDetails) *t
tlsCaCertificates = x509.NewCertPool()
ok := tlsCaCertificates.AppendCertsFromPEM(tlsCaCertificate)
if !ok {
reqLogger.Info("Failed to load CA Certificates from Secret")
reqLogger.V(1).Info("Failed to load CA Certificates from Secret")
}

return &tls.Config{
Expand Down Expand Up @@ -118,7 +118,7 @@ func getRedisReplicationTLSConfig(cr *redisv1beta1.RedisReplication, redisInfo R
tlsCaCertificates = x509.NewCertPool()
ok := tlsCaCertificates.AppendCertsFromPEM(tlsCaCertificate)
if !ok {
reqLogger.Info("Failed to load CA Certificates from Secret")
reqLogger.V(1).Info("Failed to load CA Certificates from Secret")
}

return &tls.Config{
Expand Down
12 changes: 6 additions & 6 deletions k8sutils/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func createService(namespace string, service *corev1.Service) error {
logger.Error(err, "Redis service creation is failed")
return err
}
logger.Info("Redis service creation is successful")
logger.V(1).Info("Redis service creation is successful")
return nil
}

Expand All @@ -106,7 +106,7 @@ func updateService(namespace string, service *corev1.Service) error {
logger.Error(err, "Redis service update failed")
return err
}
logger.Info("Redis service updated successfully")
logger.V(1).Info("Redis service updated successfully")
return nil
}

Expand All @@ -121,7 +121,7 @@ func getService(namespace string, service string) (*corev1.Service, error) {
logger.Info("Redis service get action is failed")
return nil, err
}
logger.Info("Redis service get action is successful")
logger.V(1).Info("Redis service get action is successful")
return serviceInfo, nil
}

Expand Down Expand Up @@ -169,7 +169,7 @@ func patchService(storedService *corev1.Service, newService *corev1.Service, nam
return err
}
if !patchResult.IsEmpty() {
logger.Info("Changes in service Detected, Updating...", "patch", string(patchResult.Patch))
logger.V(1).Info("Changes in service Detected, Updating...", "patch", string(patchResult.Patch))

for key, value := range storedService.Annotations {
if _, present := newService.Annotations[key]; !present {
Expand All @@ -180,9 +180,9 @@ func patchService(storedService *corev1.Service, newService *corev1.Service, nam
logger.Error(err, "Unable to patch redis service with comparison object")
return err
}
logger.Info("Syncing Redis service with defined properties")
logger.V(1).Info("Syncing Redis service with defined properties")
return updateService(namespace, newService)
}
logger.Info("Redis service is already in-sync")
logger.V(1).Info("Redis service is already in-sync")
return nil
}
Loading

0 comments on commit caf446e

Please sign in to comment.