From 8aad004c06bb0d0850e218c1b69ea78dc3c06173 Mon Sep 17 00:00:00 2001 From: Paul Abel <128620221+pdabelf5@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:47:33 +0000 Subject: [PATCH] allow k8s events in initial validation (#6814) --- cmd/nginx-ingress/main.go | 22 ++++++++++++++++++++-- internal/k8s/controller.go | 13 ++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/cmd/nginx-ingress/main.go b/cmd/nginx-ingress/main.go index e309fd859e..eb2f9d6d42 100644 --- a/cmd/nginx-ingress/main.go +++ b/cmd/nginx-ingress/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "errors" "fmt" @@ -38,9 +39,11 @@ import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" + core_v1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + "k8s.io/client-go/tools/record" kitlog "github.com/go-kit/log" @@ -90,6 +93,15 @@ func main() { config, kubeClient := mustCreateConfigAndKubeClient(ctx) mustValidateKubernetesVersionInfo(ctx, kubeClient) + eventBroadcaster := record.NewBroadcaster() + eventBroadcaster.StartLogging(func(format string, args ...interface{}) { + nl.Infof(l, format, args...) + }) + eventBroadcaster.StartRecordingToSink(&core_v1.EventSinkImpl{ + Interface: core_v1.New(kubeClient.CoreV1().RESTClient()).Events(""), + }) + eventRecorder := eventBroadcaster.NewRecorder(scheme.Scheme, + api_v1.EventSource{Component: "nginx-ingress-controller"}) mustValidateIngressClass(ctx, kubeClient) checkNamespaces(ctx, kubeClient) @@ -128,7 +140,7 @@ func main() { agentVersion = getAgentVersionInfo(nginxManager) } - go updateSelfWithVersionInfo(ctx, kubeClient, version, appProtectVersion, agentVersion, nginxVersion, 10, time.Second*5) + go updateSelfWithVersionInfo(ctx, eventRecorder, kubeClient, version, appProtectVersion, agentVersion, nginxVersion, 10, time.Second*5) templateExecutor, templateExecutorV2 := createTemplateExecutors(ctx) @@ -230,6 +242,7 @@ func main() { ConfClient: confClient, DynClient: dynClient, RestConfig: config, + Recorder: eventRecorder, ResyncPeriod: 30 * time.Second, LoggerContext: ctx, Namespace: watchNamespaces, @@ -983,7 +996,7 @@ func processMGMTConfigMap(kubeClient *kubernetes.Clientset, mgmtCfgParams *confi return mgmtCfgParams } -func updateSelfWithVersionInfo(ctx context.Context, kubeClient *kubernetes.Clientset, version, appProtectVersion, agentVersion string, nginxVersion nginx.Version, maxRetries int, waitTime time.Duration) { +func updateSelfWithVersionInfo(ctx context.Context, eventLog record.EventRecorder, kubeClient *kubernetes.Clientset, version, appProtectVersion, agentVersion string, nginxVersion nginx.Version, maxRetries int, waitTime time.Duration) { l := nl.LoggerFromContext(ctx) podUpdated := false @@ -1020,6 +1033,11 @@ func updateSelfWithVersionInfo(ctx context.Context, kubeClient *kubernetes.Clien continue } + labelsString := new(bytes.Buffer) + for key, value := range labels { + fmt.Fprintf(labelsString, "%s=\"%s\", ", key, value) + } + eventLog.Eventf(newPod, api_v1.EventTypeNormal, "UpdatePodLabel", "Successfully added version labels, %s", strings.TrimRight(labelsString.String(), ", ")) nl.Infof(l, "Pod label updated: %s", pod.ObjectMeta.Name) podUpdated = true } diff --git a/internal/k8s/controller.go b/internal/k8s/controller.go index 66a2687e78..3225ca4885 100644 --- a/internal/k8s/controller.go +++ b/internal/k8s/controller.go @@ -44,8 +44,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/kubernetes/scheme" - core_v1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/leaderelection" "k8s.io/client-go/tools/record" @@ -189,6 +187,7 @@ type NewLoadBalancerControllerInput struct { ConfClient k8s_nginx.Interface DynClient dynamic.Interface RestConfig *rest.Config + Recorder record.EventRecorder ResyncPeriod time.Duration LoggerContext context.Context Namespace []string @@ -246,6 +245,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc confClient: input.ConfClient, dynClient: input.DynClient, restConfig: input.RestConfig, + recorder: input.Recorder, Logger: nl.LoggerFromContext(input.LoggerContext), configurator: input.NginxConfigurator, specialSecrets: specialSecrets, @@ -273,15 +273,6 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc nginxConfigMapName: input.ConfigMaps, mgmtConfigMapName: input.MGMTConfigMap, } - eventBroadcaster := record.NewBroadcaster() - eventBroadcaster.StartLogging(func(format string, args ...interface{}) { - nl.Infof(lbc.Logger, format, args...) - }) - eventBroadcaster.StartRecordingToSink(&core_v1.EventSinkImpl{ - Interface: core_v1.New(input.KubeClient.CoreV1().RESTClient()).Events(""), - }) - lbc.recorder = eventBroadcaster.NewRecorder(scheme.Scheme, - api_v1.EventSource{Component: "nginx-ingress-controller"}) lbc.syncQueue = newTaskQueue(lbc.Logger, lbc.sync) var err error