diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index f36610d..c95819c 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -26,7 +26,7 @@ func init() { flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") flag.StringVar(&configFile, "config.file", "", "Event exporter configuration file path") - flag.BoolVar(&util.NewEventType, "newEventType", false, "if true, exporter will use new event type") + flag.StringVar(&util.NewEventType, "newEventType", util.EventVersionOld, "event version, eventsv1 or corev1") } func main() { diff --git a/pkg/exporter/kube_events_exporter.go b/pkg/exporter/kube_events_exporter.go index 6a27cac..d60168a 100644 --- a/pkg/exporter/kube_events_exporter.go +++ b/pkg/exporter/kube_events_exporter.go @@ -4,11 +4,12 @@ import ( "context" "errors" "fmt" + "sync" + "github.com/kubesphere/kube-events/pkg/util" - v1 "k8s.io/api/events/v1" + eventsv1 "k8s.io/api/events/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sync" "github.com/kubesphere/kube-events/pkg/config" "github.com/kubesphere/kube-events/pkg/exporter/sinks" @@ -204,8 +205,8 @@ func NewKubeEventSource(client *kubernetes.Clientset) *K8sEventSource { } var eventType runtime.Object var lw *cache.ListWatch - if util.NewEventType { - eventType = &v1.Event{} + if util.NewEventType == util.EventVersionNew { + eventType = &eventsv1.Event{} lw = cache.NewListWatchFromClient(client.EventsV1().RESTClient(), "events", metav1.NamespaceAll, fields.Everything()) } else { diff --git a/pkg/util/annoutil.go b/pkg/util/annoutil.go index d74bed2..4190408 100644 --- a/pkg/util/annoutil.go +++ b/pkg/util/annoutil.go @@ -9,8 +9,13 @@ import ( "k8s.io/klog/v2" ) +const ( + EventVersionOld = "corev1" + EventVersionNew = "eventsv1" +) + var cluster string -var NewEventType bool +var NewEventType string func SetClusterName(client *kubernetes.Clientset) { setCluster(client) @@ -30,6 +35,7 @@ func setCluster(client *kubernetes.Clientset) { ns, err := client.CoreV1().Namespaces().Get(context.Background(), "kubesphere-system", metav1.GetOptions{}) if err != nil { klog.Errorf("get namespace kubesphere-system error: %s", err) + return } if ns.Annotations != nil {