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 and AlexFenlon committed Nov 15, 2024
1 parent 186ea2b commit 8aad004
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"
"errors"
"fmt"
Expand Down Expand Up @@ -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"

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

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

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

0 comments on commit 8aad004

Please sign in to comment.