Skip to content

Commit

Permalink
allow k8s events in initial validation (#6814)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 authored Nov 15, 2024
1 parent 5d0b230 commit 8703f0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
22 changes: 20 additions & 2 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -218,6 +230,7 @@ func main() {
ConfClient: confClient,
DynClient: dynClient,
RestConfig: config,
Recorder: eventRecorder,
ResyncPeriod: 30 * time.Second,
LoggerContext: ctx,
Namespace: watchNamespaces,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 2 additions & 11 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 8703f0e

Please sign in to comment.