Skip to content

Commit 1dffbd7

Browse files
committed
Refactors logging to use the zapr implementation of go-logr/logr
1 parent 9a7a8f5 commit 1dffbd7

31 files changed

+114
-2048
lines changed

Gopkg.lock

Lines changed: 4 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/cluster-ingress-operator/main.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77

88
"github.com/openshift/cluster-ingress-operator/pkg/dns"
99
awsdns "github.com/openshift/cluster-ingress-operator/pkg/dns/aws"
10+
logf "github.com/openshift/cluster-ingress-operator/pkg/log"
1011
"github.com/openshift/cluster-ingress-operator/pkg/operator"
1112
operatorconfig "github.com/openshift/cluster-ingress-operator/pkg/operator/config"
1213

1314
configv1 "github.com/openshift/api/config/v1"
1415

15-
"github.com/sirupsen/logrus"
16-
1716
corev1 "k8s.io/api/core/v1"
1817

1918
"k8s.io/apimachinery/pkg/types"
@@ -32,54 +31,64 @@ const (
3231
cloudCredentialsSecretName = "cloud-credentials"
3332
)
3433

34+
var log = logf.Logger.WithName("entrypoint")
35+
3536
func main() {
3637
// Get a kube client.
3738
kubeConfig, err := config.GetConfig()
3839
if err != nil {
39-
logrus.Fatalf("failed to get kube config: %v", err)
40+
log.Error(err, "failed to get kube config")
41+
os.Exit(1)
4042
}
4143
kubeClient, err := operator.Client(kubeConfig)
4244
if err != nil {
43-
logrus.Fatalf("failed to create kube client: %v", err)
45+
log.Error(err, "failed to create kube client")
46+
os.Exit(1)
4447
}
4548

4649
// Collect operator configuration.
4750
operatorNamespace, ok := os.LookupEnv("WATCH_NAMESPACE")
4851
if !ok {
49-
logrus.Fatalf("WATCH_NAMESPACE environment variable is required")
52+
log.Error(fmt.Errorf("missing environment variable"), "'WATCH_NAMESPACE' environment variable must be set")
53+
os.Exit(1)
5054
}
5155
routerImage := os.Getenv("IMAGE")
5256
if len(routerImage) == 0 {
53-
logrus.Fatalf("IMAGE environment variable is required")
57+
log.Error(fmt.Errorf("missing environment variable"), "'IMAGE' environment variable must be set")
58+
os.Exit(1)
5459
}
5560

5661
// Retrieve the cluster infrastructure config.
5762
infraConfig := &configv1.Infrastructure{}
5863
err = kubeClient.Get(context.TODO(), types.NamespacedName{Name: "cluster"}, infraConfig)
5964
if err != nil {
60-
logrus.Fatalf("failed to get infrastructure 'config': %v", err)
65+
log.Error(err, "failed to get infrastructure 'config'")
66+
os.Exit(1)
6167
}
6268

6369
// Retrieve the cluster ingress config.
6470
ingressConfig := &configv1.Ingress{}
6571
err = kubeClient.Get(context.TODO(), types.NamespacedName{Name: "cluster"}, ingressConfig)
6672
if err != nil {
67-
logrus.Fatalf("failed to get ingress 'cluster': %v", err)
73+
log.Error(err, "failed to get ingress 'cluster'")
74+
os.Exit(1)
6875
}
6976
if len(ingressConfig.Spec.Domain) == 0 {
70-
logrus.Warnln("cluster ingress configuration has an empty domain; default ClusterIngress will have empty ingressDomain")
77+
log.Info("cluster ingress configuration has an empty domain; default ClusterIngress will have empty ingressDomain")
7178
}
7279

7380
dnsConfig := &configv1.DNS{}
7481
err = kubeClient.Get(context.TODO(), types.NamespacedName{Name: "cluster"}, dnsConfig)
7582
if err != nil {
76-
logrus.Fatalf("failed to get dns 'cluster': %v", err)
83+
log.Error(err, "failed to get dns 'cluster'")
84+
os.Exit(1)
7785
}
7886

7987
// Set up the DNS manager.
8088
dnsManager, err := createDNSManager(kubeClient, operatorNamespace, infraConfig, dnsConfig)
8189
if err != nil {
82-
logrus.Fatalf("failed to create DNS manager: %v", err)
90+
log.Error(err, "failed to create DNS manager")
91+
os.Exit(1)
8392
}
8493

8594
// Set up and start the operator.
@@ -91,10 +100,12 @@ func main() {
91100
}
92101
op, err := operator.New(operatorConfig, dnsManager, kubeConfig)
93102
if err != nil {
94-
logrus.Fatalf("failed to create operator: %v", err)
103+
log.Error(err, "failed to create operator")
104+
os.Exit(1)
95105
}
96106
if err := op.Start(signals.SetupSignalHandler()); err != nil {
97-
logrus.Fatal(err)
107+
log.Error(err, "failed to start operator")
108+
os.Exit(1)
98109
}
99110
}
100111

pkg/dns/aws/dns.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"strings"
77
"sync"
88

9-
"github.com/sirupsen/logrus"
10-
119
"github.com/openshift/cluster-ingress-operator/pkg/dns"
10+
logf "github.com/openshift/cluster-ingress-operator/pkg/log"
1211

1312
"github.com/aws/aws-sdk-go/aws"
1413
"github.com/aws/aws-sdk-go/aws/arn"
@@ -25,7 +24,10 @@ import (
2524
configv1 "github.com/openshift/api/config/v1"
2625
)
2726

28-
var _ dns.Manager = &Manager{}
27+
var (
28+
_ dns.Manager = &Manager{}
29+
log = logf.Logger.WithName("aws-dns-manager")
30+
)
2931

3032
// Manager provides AWS DNS record management. In this implementation, calling
3133
// Ensure will create records in any zone specified in the DNS configuration.
@@ -79,15 +81,15 @@ func NewManager(config Config) (*Manager, error) {
7981

8082
region := aws.StringValue(sess.Config.Region)
8183
if len(region) > 0 {
82-
logrus.Infof("using region %q from shared config", region)
84+
log.Info("using region from shared config", "region name", region)
8385
} else {
8486
metadata := ec2metadata.New(sess)
8587
discovered, err := metadata.Region()
8688
if err != nil {
8789
return nil, fmt.Errorf("couldn't get region from metadata: %v", err)
8890
}
8991
region = discovered
90-
logrus.Infof("discovered region %q from metadata", region)
92+
log.Info("discovered region from metadata", "region name", region)
9193
}
9294

9395
return &Manager{
@@ -167,7 +169,7 @@ func (m *Manager) getZoneID(zoneConfig configv1.DNSZone) (string, error) {
167169

168170
// Update the cache
169171
m.idsToTags[id] = zoneConfig.Tags
170-
logrus.Infof("found zone id %q for tags %v", id, zoneConfig.Tags)
172+
log.Info("found hosted zone using tags", "zone id", id, "tags", zoneConfig.Tags)
171173

172174
return id, nil
173175
}
@@ -218,7 +220,7 @@ func (m *Manager) change(record *dns.Record, action action) error {
218220
return fmt.Errorf("failed to describe load balancers: %v", err)
219221
}
220222
for _, lb := range loadBalancers.LoadBalancerDescriptions {
221-
logrus.Debugf("found load balancer: %s (DNS name: %s, zone: %s)", aws.StringValue(lb.LoadBalancerName), aws.StringValue(lb.DNSName), aws.StringValue(lb.CanonicalHostedZoneNameID))
223+
log.Info("found load balancer", "name", aws.StringValue(lb.LoadBalancerName), "dns name", aws.StringValue(lb.DNSName), "hosted zone ID", aws.StringValue(lb.CanonicalHostedZoneNameID))
222224
if aws.StringValue(lb.CanonicalHostedZoneName) == target {
223225
targetHostedZoneID = aws.StringValue(lb.CanonicalHostedZoneNameID)
224226
break
@@ -235,7 +237,7 @@ func (m *Manager) change(record *dns.Record, action action) error {
235237
key := zoneID + domain + target
236238
// Only process updates once for now because we're not diffing.
237239
if m.updatedRecords.Has(key) && action == upsertAction {
238-
logrus.Infof("skipping update for record %v", record)
240+
log.Info("skipping DNS record update", "record", record)
239241
return nil
240242
}
241243
err = m.updateAlias(domain, zoneID, target, targetHostedZoneID, string(action))
@@ -245,10 +247,10 @@ func (m *Manager) change(record *dns.Record, action action) error {
245247
switch action {
246248
case upsertAction:
247249
m.updatedRecords.Insert(key)
248-
logrus.Infof("updated DNS record: %v", record)
250+
log.Info("upserted DNS record", "record", record)
249251
case deleteAction:
250252
m.updatedRecords.Delete(key)
251-
logrus.Infof("deleted DNS record: %v", record)
253+
log.Info("deleted DNS record", "record", record)
252254
}
253255
return nil
254256
}
@@ -278,6 +280,6 @@ func (m *Manager) updateAlias(domain, zoneID, target, targetHostedZoneID, action
278280
if err != nil {
279281
return fmt.Errorf("couldn't update DNS record in zone %s: %v", zoneID, err)
280282
}
281-
logrus.Infof("updated DNS record in zone %s, %s -> %s: %v", zoneID, domain, target, resp)
283+
log.Info("updated DNS record", "zone id", zoneID, "domain", domain, "target", target, "response", resp)
282284
return nil
283285
}

pkg/log/log.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package log
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-logr/logr"
7+
"github.com/go-logr/zapr"
8+
"go.uber.org/zap"
9+
)
10+
11+
// Logger is a simple logging interface for Go.
12+
var Logger logr.Logger
13+
14+
func init() {
15+
// Build a zap development logger.
16+
zapLog, err := zap.NewDevelopment()
17+
if err != nil {
18+
panic(fmt.Sprintf("error building logger: %v", err))
19+
}
20+
// zapr defines an implementation of the Logger
21+
// interface built on top of Zap (go.uber.org/zap).
22+
Logger = zapr.NewLogger(zapLog)
23+
Logger.Info("started development logger")
24+
}

0 commit comments

Comments
 (0)