Skip to content

Commit 29b922b

Browse files
authored
add log level env variable and update helm (#534)
* add log level env variable and update helm * update LOG_LEVEL env var in environment.md
1 parent c884d4b commit 29b922b

File tree

9 files changed

+39
-18
lines changed

9 files changed

+39
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ help: ## Display this help.
4949

5050
.PHONY: run
5151
run: ## Run in development mode
52-
go run cmd/aws-application-networking-k8s/main.go --debug
52+
DEV_MODE=1 go run cmd/aws-application-networking-k8s/main.go
5353

5454

5555
.PHONY: presubmit

cmd/aws-application-networking-k8s/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222

2323
"github.com/go-logr/zapr"
24+
"go.uber.org/zap/zapcore"
2425

2526
"github.com/aws/aws-application-networking-k8s/pkg/aws"
2627
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
@@ -91,17 +92,16 @@ func main() {
9192
var metricsAddr string
9293
var enableLeaderElection bool
9394
var probeAddr string
94-
var debug bool
9595

9696
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
9797
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
98-
flag.BoolVar(&debug, "debug", false, "enable debug mode")
9998
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
10099
"Enable leader election for controller manager. "+
101100
"Enabling this will ensure there is only one active controller manager.")
102101
flag.Parse()
103102

104-
log := gwlog.NewLogger(debug)
103+
logLevel := logLevel()
104+
log := gwlog.NewLogger(logLevel)
105105
ctrl.SetLogger(zapr.NewLogger(log.Desugar()).WithName("runtime"))
106106

107107
setupLog := log.Named("setup")
@@ -220,3 +220,17 @@ func main() {
220220
}
221221

222222
}
223+
224+
func logLevel() zapcore.Level {
225+
level := os.Getenv("LOG_LEVEL")
226+
switch level {
227+
case "debug":
228+
return zapcore.DebugLevel
229+
case "error":
230+
return zapcore.ErrorLevel
231+
case "panic":
232+
return zapcore.PanicLevel
233+
default:
234+
return zapcore.InfoLevel
235+
}
236+
}

docs/guides/environment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ When running AWS Gateway API Controller outside the Kubernetes Cluster, this spe
4444

4545
---
4646

47-
#### `GATEWAY_API_CONTROLLER_LOGLEVEL`
47+
#### `LOG_LEVEL`
4848

4949
Type: string
5050

@@ -74,4 +74,4 @@ Default: ""
7474

7575
When set as "true", the controller will run in "single service network" mode that will override all gateways
7676
to point to default service network, instead of searching for service network with the same name.
77-
Can be used for small setups and conformance tests.
77+
Can be used for small setups and conformance tests.

helm/templates/configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ data:
99
clusterName: {{ .Values.clusterName | quote }}
1010
latticeEndpoint: {{ .Values.latticeEndpoint | quote }}
1111
defaultServiceNetwork: {{ .Values.defaultServiceNetwork | quote }}
12-
12+
logLevel: {{ .Values.log.level | quote }}

helm/templates/deployment.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ spec:
9797
configMapKeyRef:
9898
name: env-config
9999
key: defaultServiceNetwork
100+
- name: LOG_LEVEL
101+
valueFrom:
102+
configMapKeyRef:
103+
name: env-config
104+
key: logLevel
100105

101106
terminationGracePeriodSeconds: 10
102107
nodeSelector: {{ toYaml .Values.deployment.nodeSelector | nindent 8 }}

helm/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ clusterVpcId:
7777
clusterName:
7878
defaultServiceNetwork:
7979
latticeEndpoint:
80+
logLevel:

pkg/utils/gwlog/gwlog.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ package gwlog
22

33
import (
44
"log"
5+
"os"
56

67
"go.uber.org/zap"
78
"go.uber.org/zap/zapcore"
89
)
910

1011
type Logger = *zap.SugaredLogger
1112

12-
func NewLogger(debug bool) Logger {
13+
func NewLogger(level zapcore.Level) Logger {
1314
var zc zap.Config
14-
if debug {
15+
16+
dev := os.Getenv("DEV_MODE")
17+
if dev != "" {
1518
zc = zap.NewDevelopmentConfig()
16-
zc.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
1719
} else {
1820
zc = zap.NewProductionConfig()
1921
zc.DisableStacktrace = true
2022
zc.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
2123
}
24+
25+
zc.Level = zap.NewAtomicLevelAt(level)
26+
2227
z, err := zc.Build()
2328
if err != nil {
2429
log.Fatal("cannot initialize zapr logger", err)
2530
}
2631
return z.Sugar()
2732
}
2833

29-
var FallbackLogger = NewLogger(true)
34+
var FallbackLogger = NewLogger(zap.DebugLevel)

scripts/load_env_variables.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ if [ -z "$KUBEBUILDER_ASSETS" ]; then
1515
fi
1616
echo "KUBEBUILDER_ASSETS=$KUBEBUILDER_ASSETS" >> envFile
1717

18-
1918
# Set CLUSTER_NAME if not set
2019
if [ -z "$CLUSTER_NAME" ]; then
2120
CLUSTER_NAME=$(kubectl config view --minify -o jsonpath='{.clusters[].name}' | rev | cut -d"/" -f1 | rev | cut -d"." -f1)
2221
fi
2322
echo "CLUSTER_NAME=$CLUSTER_NAME" >> envFile
2423

25-
2624
# Set CLUSTER_VPC_ID if not set
2725
if [ -z "$CLUSTER_VPC_ID" ]; then
2826
CLUSTER_VPC_ID=$(aws eks describe-cluster --name ${CLUSTER_NAME} | jq -r ".cluster.resourcesVpcConfig.vpcId")
@@ -40,7 +38,4 @@ if [ -z "$REGION" ]; then
4038
fi
4139
echo "REGION=$REGION" >> envFile
4240

43-
44-
GATEWAY_API_CONTROLLER_LOGLEVEL=debug
45-
echo "GATEWAY_API_CONTROLLER_LOGLEVEL=$GATEWAY_API_CONTROLLER_LOGLEVEL" >> envFile
46-
41+
echo "LOG_LEVEL=debug" >> envFile

test/suites/integration/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/aws/aws-sdk-go/service/vpclattice"
88
. "github.com/onsi/ginkgo/v2"
99
. "github.com/onsi/gomega"
10+
"go.uber.org/zap"
1011
apierrors "k8s.io/apimachinery/pkg/api/errors"
1112
"sigs.k8s.io/controller-runtime/pkg/client"
1213

@@ -63,7 +64,7 @@ var _ = SynchronizedBeforeSuite(func() {
6364

6465
func TestIntegration(t *testing.T) {
6566
ctx = test.NewContext(t)
66-
logger := gwlog.NewLogger(true)
67+
logger := gwlog.NewLogger(zap.DebugLevel)
6768
testFramework = test.NewFramework(ctx, logger, k8snamespace)
6869
RegisterFailHandler(Fail)
6970
RunSpecs(t, "Integration")

0 commit comments

Comments
 (0)