@@ -23,7 +23,8 @@ import (
23
23
"strings"
24
24
"time"
25
25
26
- "github.com/sirupsen/logrus"
26
+ "github.com/operator-framework/operator-sdk/pkg/sdk/log"
27
+
27
28
corev1 "k8s.io/api/core/v1"
28
29
apierrors "k8s.io/apimachinery/pkg/api/errors"
29
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -50,12 +51,12 @@ const PodNameEnv = "POD_NAME"
50
51
// leader. Upon termination of that pod, the garbage collector will delete the
51
52
// ConfigMap, enabling a different pod to become the leader.
52
53
func Become (ctx context.Context , lockName string ) error {
53
- logrus .Info ("trying to become the leader" )
54
+ log .Info ("Trying to become the leader. " )
54
55
55
56
ns , err := myNS ()
56
57
if err != nil {
57
58
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. " )
59
60
return nil
60
61
}
61
62
return err
@@ -90,17 +91,17 @@ func Become(ctx context.Context, lockName string) error {
90
91
case err == nil :
91
92
for _ , existingOwner := range existing .GetOwnerReferences () {
92
93
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." )
95
96
return nil
96
97
} else {
97
- logrus .Infof ("Found existing lock from %s " , existingOwner .Name )
98
+ log .Infof ("Found existing lock" , "LockOwner " , existingOwner .Name )
98
99
}
99
100
}
100
101
case apierrors .IsNotFound (err ):
101
- logrus .Info ("No pre-existing lock was found." )
102
+ log .Info ("No pre-existing lock was found." )
102
103
default :
103
- logrus .Error ("unknown error trying to get ConfigMap" )
104
+ log .Error (err , "unknown error trying to get ConfigMap" )
104
105
return err
105
106
}
106
107
@@ -122,10 +123,10 @@ func Become(ctx context.Context, lockName string) error {
122
123
err := client .Create (ctx , cm )
123
124
switch {
124
125
case err == nil :
125
- logrus .Info ("Became the leader." )
126
+ log .Info ("Became the leader." )
126
127
return nil
127
128
case apierrors .IsAlreadyExists (err ):
128
- logrus .Info ("Not the leader. Waiting." )
129
+ log .Info ("Not the leader. Waiting." )
129
130
select {
130
131
case <- time .After (wait .Jitter (backoff , .2 )):
131
132
if backoff < maxBackoffInterval {
@@ -136,7 +137,7 @@ func Become(ctx context.Context, lockName string) error {
136
137
return ctx .Err ()
137
138
}
138
139
default :
139
- logrus . Errorf ( "unknown error creating configmap: %v" , err )
140
+ log . Error ( err , "unknown error creating configmap" )
140
141
return err
141
142
}
142
143
}
@@ -147,13 +148,13 @@ func myNS() (string, error) {
147
148
nsBytes , err := ioutil .ReadFile ("/var/run/secrets/kubernetes.io/serviceaccount/namespace" )
148
149
if err != nil {
149
150
if os .IsNotExist (err ) {
150
- logrus . Debug ("current namespace not found" )
151
+ log . V ( log . VDebug ). Info ("current namespace not found" )
151
152
return "" , errNoNS
152
153
}
153
154
return "" , err
154
155
}
155
156
ns := strings .TrimSpace (string (nsBytes ))
156
- logrus . Debugf ( "found namespace: %s " , ns )
157
+ log . V ( log . VDebug ). Info ( "found namespace" , "Namespace " , ns )
157
158
return ns , nil
158
159
}
159
160
@@ -166,7 +167,7 @@ func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1
166
167
return nil , fmt .Errorf ("required env %s not set, please configure downward API" , PodNameEnv )
167
168
}
168
169
169
- logrus . Debugf ( "found podname: %s " , podName )
170
+ log . V ( log . VDebug ). Info ( "found podname" , "PodName " , podName )
170
171
171
172
myPod := & corev1.Pod {
172
173
TypeMeta : metav1.TypeMeta {
@@ -178,7 +179,7 @@ func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1
178
179
key := crclient.ObjectKey {Namespace : ns , Name : podName }
179
180
err := client .Get (ctx , key , myPod )
180
181
if err != nil {
181
- logrus .Errorf ("failed to get pod: %v" , err )
182
+ log .Errorf (err , "failed to get pod" )
182
183
return nil , err
183
184
}
184
185
0 commit comments