-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support update klog log level dynamically based on configmap (#561
- Loading branch information
Showing
7 changed files
with
297 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2024 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package logging | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/katanomi/pkg/testing" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"go.uber.org/zap" | ||
corev1 "k8s.io/api/core/v1" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
var _ = Describe("LevelManager_Update", func() { | ||
var lm LevelManager | ||
var cm = &corev1.ConfigMap{} | ||
testing.MustLoadYaml("testdata/config.yaml", cm) | ||
|
||
BeforeEach(func() { | ||
ctx := logging.WithLogger(context.Background(), logger) | ||
lm = NewLevelManager(ctx, "test") | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
lm.Update()(cm) | ||
}) | ||
|
||
Context("test for AtomicLevel", func() { | ||
var infoLevel zap.AtomicLevel | ||
var errorLevel zap.AtomicLevel | ||
BeforeEach(func() { | ||
infoLevel = lm.Get("test_info") | ||
errorLevel = lm.Get("test_error") | ||
}) | ||
|
||
It("should get correct log level", func() { | ||
Expect(infoLevel.Level()).To(Equal(zap.InfoLevel)) | ||
Expect(errorLevel.Level()).To(Equal(zap.ErrorLevel)) | ||
}) | ||
}) | ||
|
||
Context("test for custom observer", func() { | ||
var gotData map[string]string | ||
BeforeEach(func() { | ||
lm.AddCustomObserver(func(d map[string]string) { | ||
gotData = d | ||
}) | ||
}) | ||
|
||
It("should get full data of the configmap", func() { | ||
expectCm := &corev1.ConfigMap{} | ||
testing.MustLoadYaml("testdata/config.yaml", expectCm) | ||
Expect(cmp.Diff(expectCm.Data, gotData)).To(BeEmpty()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2024 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package logging | ||
|
||
import "strconv" | ||
|
||
// KlogLevelKey the key to set klog log level in the configmap | ||
// Example: | ||
// | ||
// data: | ||
// | ||
// klog.level: "9" | ||
const KlogLevelKey = "klog.level" | ||
|
||
// GetKlogLevelFromConfigMapData get klog level from configmap data | ||
func GetKlogLevelFromConfigMapData(data map[string]string) (level string) { | ||
if data == nil { | ||
return "0" | ||
} | ||
|
||
level = data[KlogLevelKey] | ||
if level == "" { | ||
return "0" | ||
} | ||
|
||
_, err := strconv.Atoi(level) | ||
if err != nil { | ||
return "0" | ||
} | ||
return level | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2024 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package logging | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestGetKlogLevelFromConfigMapData(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
tests := []struct { | ||
name string | ||
data map[string]string | ||
wantLevel string | ||
}{ | ||
{ | ||
name: "nil config", | ||
data: nil, | ||
wantLevel: "0", | ||
}, | ||
{ | ||
name: "empty config", | ||
data: map[string]string{}, | ||
wantLevel: "0", | ||
}, | ||
{ | ||
name: "not exist the key klog.level", | ||
data: map[string]string{ | ||
"a": "b", | ||
}, | ||
wantLevel: "0", | ||
}, | ||
{ | ||
name: "exist the key klog.level and the value is correct number", | ||
data: map[string]string{ | ||
KlogLevelKey: "3", | ||
}, | ||
wantLevel: "3", | ||
}, | ||
{ | ||
name: "exist the key klog.level and the value is not a number", | ||
data: map[string]string{ | ||
KlogLevelKey: "a3", | ||
}, | ||
wantLevel: "0", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g.Expect(GetKlogLevelFromConfigMapData(tt.data)).To(Equal(tt.wantLevel)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package logging | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
uberzap "go.uber.org/zap" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
var ( | ||
logger *uberzap.SugaredLogger | ||
) | ||
|
||
func init() { | ||
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) | ||
logger = zap.NewRaw(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)).Sugar() | ||
} | ||
|
||
func TestLogging(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Logging Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
apiVersion: v1 | ||
data: | ||
_example_: 'supported: debug | info | warn | error | dpanic | panic | fatal' | ||
klog.level: "9" | ||
loglevel.test_info: "info" | ||
loglevel.test_warn: "warn" | ||
loglevel.test_error: "error" | ||
zap-logger-config: | | ||
{ | ||
"level": "info", | ||
"development": false, | ||
"outputPaths": ["stdout"], | ||
"errorOutputPaths": ["stderr"], | ||
"encoding": "json", | ||
"encoderConfig": { | ||
"timeKey": "ts", | ||
"levelKey": "level", | ||
"nameKey": "logger", | ||
"callerKey": "caller", | ||
"messageKey": "msg", | ||
"stacktraceKey": "stacktrace", | ||
"lineEnding": "", | ||
"levelEncoder": "", | ||
"timeEncoder": "iso8601", | ||
"durationEncoder": "", | ||
"callerEncoder": "" | ||
} | ||
} | ||
kind: ConfigMap | ||
metadata: | ||
name: katanomi-config-logging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters