Skip to content

Replace fmt/panic logging with klog in cmd/main.go#1

Open
Copilot wants to merge 4 commits intomainfrom
copilot/optimize-logging-in-cmd-main
Open

Replace fmt/panic logging with klog in cmd/main.go#1
Copilot wants to merge 4 commits intomainfrom
copilot/optimize-logging-in-cmd-main

Conversation

Copy link

Copilot AI commented Feb 12, 2026

  • Replace fmt.Println and panic with klog structured logging
  • Add error messages before silent os.Exit(1) calls (use klog.Fatal/klog.Fatalf)
  • Add proper logging for the delete event handler
  • Initialize klog flags with klog.InitFlags(nil) before flag.Parse()
  • Remove unused fmt import
  • Handle cache.DeletedFinalStateUnknown in DeleteFunc using cache.DeletionHandlingMetaNamespaceKeyFunc + cache.SplitMetaNamespaceKey
  • Verify build succeeds

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…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
Copilot AI requested a review from Iceber February 12, 2026 02:18
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

klog.InitFlags 的参数是什么作用

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当参数为 nil 时,klog 会将其日志相关的 flags(如 -v-logtostderr 等)注册到 Go 标准库的 flag.CommandLine 上。如果传入一个自定义的 *flag.FlagSet,则 klog flags 会注册到那个自定义的 FlagSet 上,而不是全局的 flag.CommandLine。这里传 nil 是最常见的用法,使得 klog flags 和应用自身的 flags 统一通过 flag.Parse() 解析。

cmd/main.go Outdated
UpdateFunc: func(_, obj interface{}) { gateChecker(obj.(*unstructured.Unstructured)) },
DeleteFunc: func(_ interface{}) { /*log*/ },
DeleteFunc: func(obj interface{}) {
o := obj.(*unstructured.Unstructured)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot obj 可能是 cache. DeletedFinalStateUnknown 类型,请使用 kubernetes controller 的常用范式修复这个问题

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修复,在 DeleteFunc 中先检查 cache.DeletedFinalStateUnknown 类型并提取内部对象,再进行安全的类型断言。见 commit 95c004c

@Iceber Iceber marked this pull request as ready for review February 12, 2026 02:55
Co-authored-by: Iceber <13391143+Iceber@users.noreply.github.com>
Copilot AI requested a review from Iceber February 12, 2026 02:58
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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot 可以使用 cache 包中的函数来获取 obj 的 key,然后解析 key 即可得到 namespace 和 name 了,不需要在这里判断 obj 的类型

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为使用 cache.DeletionHandlingMetaNamespaceKeyFunc 获取 key,再通过 cache.SplitMetaNamespaceKey 解析 namespace 和 name,无需手动判断类型。见 commit 2a0bdd4

Co-authored-by: Iceber <13391143+Iceber@users.noreply.github.com>
Copilot AI requested a review from Iceber February 12, 2026 03:03
Copy link
Owner

@Iceber Iceber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants