Replace fmt/panic logging with klog in cmd/main.go#1
Open
Conversation
…ed logging Co-authored-by: Iceber <13391143+Iceber@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize log printing in cmd/main.go
Replace fmt/panic logging with klog in cmd/main.go
Feb 12, 2026
Iceber
reviewed
Feb 12, 2026
| flag.StringVar(&gate.namespace, "gate-namespace", "", "namespace of gate resource") | ||
| flag.StringVar(&gate.name, "gate-name", "", "name of gate resource") | ||
| flag.StringVar(&expression, "gate-expression", "", "name of gate resource") | ||
| klog.InitFlags(nil) |
Author
There was a problem hiding this comment.
当参数为 nil 时,klog 会将其日志相关的 flags(如 -v、-logtostderr 等)注册到 Go 标准库的 flag.CommandLine 上。如果传入一个自定义的 *flag.FlagSet,则 klog flags 会注册到那个自定义的 FlagSet 上,而不是全局的 flag.CommandLine。这里传 nil 是最常见的用法,使得 klog flags 和应用自身的 flags 统一通过 flag.Parse() 解析。
Iceber
requested changes
Feb 12, 2026
cmd/main.go
Outdated
| UpdateFunc: func(_, obj interface{}) { gateChecker(obj.(*unstructured.Unstructured)) }, | ||
| DeleteFunc: func(_ interface{}) { /*log*/ }, | ||
| DeleteFunc: func(obj interface{}) { | ||
| o := obj.(*unstructured.Unstructured) |
Co-authored-by: Iceber <13391143+Iceber@users.noreply.github.com>
Iceber
reviewed
Feb 12, 2026
cmd/main.go
Outdated
| UpdateFunc: func(_, obj interface{}) { gateChecker(obj.(*unstructured.Unstructured)) }, | ||
| DeleteFunc: func(_ interface{}) { /*log*/ }, | ||
| DeleteFunc: func(obj interface{}) { | ||
| if d, ok := obj.(cache.DeletedFinalStateUnknown); ok { |
Owner
There was a problem hiding this comment.
@copilot 可以使用 cache 包中的函数来获取 obj 的 key,然后解析 key 即可得到 namespace 和 name 了,不需要在这里判断 obj 的类型
Author
There was a problem hiding this comment.
已改为使用 cache.DeletionHandlingMetaNamespaceKeyFunc 获取 key,再通过 cache.SplitMetaNamespaceKey 解析 namespace 和 name,无需手动判断类型。见 commit 2a0bdd4。
Co-authored-by: Iceber <13391143+Iceber@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fmt.Printlnandpanicwithklogstructured loggingos.Exit(1)calls (useklog.Fatal/klog.Fatalf)klog.InitFlags(nil)beforeflag.Parse()fmtimportcache.DeletedFinalStateUnknownin DeleteFunc usingcache.DeletionHandlingMetaNamespaceKeyFunc+cache.SplitMetaNamespaceKey💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.