Skip to content

Commit 19edf93

Browse files
committed
pkg/leader/leader.go: use pkg/sdk/log instead of std log/logrus
1 parent 470f673 commit 19edf93

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

pkg/leader/leader.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"strings"
2424
"time"
2525

26-
"github.com/sirupsen/logrus"
26+
"github.com/operator-framework/operator-sdk/pkg/sdk/log"
27+
2728
corev1 "k8s.io/api/core/v1"
2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -50,12 +51,12 @@ const PodNameEnv = "POD_NAME"
5051
// leader. Upon termination of that pod, the garbage collector will delete the
5152
// ConfigMap, enabling a different pod to become the leader.
5253
func Become(ctx context.Context, lockName string) error {
53-
logrus.Info("trying to become the leader")
54+
log.Info("Trying to become the leader.")
5455

5556
ns, err := myNS()
5657
if err != nil {
5758
if err == errNoNS {
58-
logrus.Info("Skipping leader election; not running in a cluster")
59+
log.Info("Skipping leader election; not running in a cluster.")
5960
return nil
6061
}
6162
return err
@@ -90,17 +91,17 @@ func Become(ctx context.Context, lockName string) error {
9091
case err == nil:
9192
for _, existingOwner := range existing.GetOwnerReferences() {
9293
if existingOwner.Name == owner.Name {
93-
logrus.Info("Found existing lock with my name. I was likely restarted.")
94-
logrus.Info("Continuing as the leader.")
94+
log.Info("Found existing lock with my name. I was likely restarted.")
95+
log.Info("Continuing as the leader.")
9596
return nil
9697
} else {
97-
logrus.Infof("Found existing lock from %s", existingOwner.Name)
98+
log.Infof("Found existing lock", "LockOwner", existingOwner.Name)
9899
}
99100
}
100101
case apierrors.IsNotFound(err):
101-
logrus.Info("No pre-existing lock was found.")
102+
log.Info("No pre-existing lock was found.")
102103
default:
103-
logrus.Error("unknown error trying to get ConfigMap")
104+
log.Error(err, "unknown error trying to get ConfigMap")
104105
return err
105106
}
106107

@@ -122,10 +123,10 @@ func Become(ctx context.Context, lockName string) error {
122123
err := client.Create(ctx, cm)
123124
switch {
124125
case err == nil:
125-
logrus.Info("Became the leader.")
126+
log.Info("Became the leader.")
126127
return nil
127128
case apierrors.IsAlreadyExists(err):
128-
logrus.Info("Not the leader. Waiting.")
129+
log.Info("Not the leader. Waiting.")
129130
select {
130131
case <-time.After(wait.Jitter(backoff, .2)):
131132
if backoff < maxBackoffInterval {
@@ -136,7 +137,7 @@ func Become(ctx context.Context, lockName string) error {
136137
return ctx.Err()
137138
}
138139
default:
139-
logrus.Errorf("unknown error creating configmap: %v", err)
140+
log.Error(err, "unknown error creating configmap")
140141
return err
141142
}
142143
}
@@ -147,13 +148,13 @@ func myNS() (string, error) {
147148
nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
148149
if err != nil {
149150
if os.IsNotExist(err) {
150-
logrus.Debug("current namespace not found")
151+
log.V(log.VDebug).Info("current namespace not found")
151152
return "", errNoNS
152153
}
153154
return "", err
154155
}
155156
ns := strings.TrimSpace(string(nsBytes))
156-
logrus.Debugf("found namespace: %s", ns)
157+
log.V(log.VDebug).Info("found namespace", "Namespace", ns)
157158
return ns, nil
158159
}
159160

@@ -166,7 +167,7 @@ func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1
166167
return nil, fmt.Errorf("required env %s not set, please configure downward API", PodNameEnv)
167168
}
168169

169-
logrus.Debugf("found podname: %s", podName)
170+
log.V(log.VDebug).Info("found podname", "PodName", podName)
170171

171172
myPod := &corev1.Pod{
172173
TypeMeta: metav1.TypeMeta{
@@ -178,7 +179,7 @@ func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1
178179
key := crclient.ObjectKey{Namespace: ns, Name: podName}
179180
err := client.Get(ctx, key, myPod)
180181
if err != nil {
181-
logrus.Errorf("failed to get pod: %v", err)
182+
log.Errorf(err, "failed to get pod")
182183
return nil, err
183184
}
184185

0 commit comments

Comments
 (0)