diff --git a/cmd/nginx-ingress/main.go b/cmd/nginx-ingress/main.go index 2563638809..881e82985f 100644 --- a/cmd/nginx-ingress/main.go +++ b/cmd/nginx-ingress/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "fmt" "io" @@ -37,9 +38,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" @@ -88,6 +91,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) @@ -126,7 +138,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) @@ -218,6 +230,7 @@ func main() { ConfClient: confClient, DynClient: dynClient, RestConfig: config, + Recorder: eventRecorder, ResyncPeriod: 30 * time.Second, LoggerContext: ctx, Namespace: watchNamespaces, @@ -877,7 +890,7 @@ func processConfigMaps(kubeClient *kubernetes.Clientset, cfgParams *configs.Conf return cfgParams } -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 @@ -914,6 +927,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 1e2a053712..50ff55987c 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" @@ -183,6 +181,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 @@ -239,6 +238,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, @@ -264,15 +264,6 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc isIPV6Disabled: input.IsIPV6Disabled, weightChangesDynamicReload: input.DynamicWeightChangesReload, } - 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