Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lbc to add metadata struct #6885

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func main() {
ExternalServiceName: *externalService,
IngressLink: *ingressLink,
ControllerNamespace: controllerNamespace,
Pod: pod,
ReportIngressStatus: *reportIngressStatus,
IsLeaderElectionEnabled: *leaderElectionEnabled,
LeaderElectionLockName: *leaderElectionLockName,
Expand Down
14 changes: 11 additions & 3 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const (
typeKeyword = "type"
helmReleaseType = "helm.sh/release.v1"
splitClientAmountWhenWeightChangesDynamicReload = 101
secretDeletedReason = "SecretDeleted"
)

var (
Expand Down Expand Up @@ -106,6 +107,11 @@ type specialSecrets struct {
wildcardTLSSecret string
}

type controllerMetadata struct {
namespace string
pod *api_v1.Pod
}

// LoadBalancerController watches Kubernetes API and
// reconfigures NGINX via NginxController when needed
type LoadBalancerController struct {
Expand Down Expand Up @@ -144,7 +150,7 @@ type LoadBalancerController struct {
resync time.Duration
namespaceList []string
secretNamespaceList []string
controllerNamespace string
metadata controllerMetadata
areCustomResourcesEnabled bool
enableOIDC bool
metricsCollector collectors.ControllerCollector
Expand Down Expand Up @@ -197,6 +203,7 @@ type NewLoadBalancerControllerInput struct {
ExternalServiceName string
IngressLink string
ControllerNamespace string
Pod *api_v1.Pod
ReportIngressStatus bool
IsLeaderElectionEnabled bool
LeaderElectionLockName string
Expand Down Expand Up @@ -253,7 +260,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
resync: input.ResyncPeriod,
namespaceList: input.Namespace,
secretNamespaceList: input.SecretNamespace,
controllerNamespace: input.ControllerNamespace,
metadata: controllerMetadata{namespace: input.ControllerNamespace, pod: input.Pod},
areCustomResourcesEnabled: input.AreCustomResourcesEnabled,
enableOIDC: input.EnableOIDC,
metricsCollector: input.MetricsCollector,
Expand Down Expand Up @@ -1694,7 +1701,8 @@ func (lbc *LoadBalancerController) syncSecret(task task) {
lbc.handleRegularSecretDeletion(resources)
}
if lbc.isSpecialSecret(key) {
nl.Warnf(lbc.Logger, "A special TLS Secret %v was removed. Retaining the Secret.", key)
lbc.recorder.Eventf(lbc.metadata.pod, conf_v1.StateWarning, secretDeletedReason, "A special secret [%s] was deleted. Retaining the secret on this pod but this will affect new pods.", key)
nl.Warnf(lbc.Logger, "A special Secret %v was removed. Retaining the Secret.", key)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/ingress_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (lbc *LoadBalancerController) addIngressLinkHandler(handlers cache.Resource
options.FieldSelector = fields.Set{"metadata.name": name}.String()
}

informer := dynamicinformer.NewFilteredDynamicInformer(lbc.dynClient, ingressLinkGVR, lbc.controllerNamespace, lbc.resync,
informer := dynamicinformer.NewFilteredDynamicInformer(lbc.dynClient, ingressLinkGVR, lbc.metadata.namespace, lbc.resync,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, optionsModifier)

informer.Informer().AddEventHandlerWithResyncPeriod(handlers, lbc.resync) //nolint:errcheck,gosec
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func createLeaderHandler(lbc *LoadBalancerController) leaderelection.LeaderCallb
// addLeaderHandler adds the handler for leader election to the controller
func (lbc *LoadBalancerController) addLeaderHandler(leaderHandler leaderelection.LeaderCallbacks) {
var err error
lbc.leaderElector, err = newLeaderElector(lbc.client, leaderHandler, lbc.controllerNamespace, lbc.leaderElectionLockName)
lbc.leaderElector, err = newLeaderElector(lbc.client, leaderHandler, lbc.metadata.namespace, lbc.leaderElectionLockName)
if err != nil {
nl.Debugf(lbc.Logger, "Error starting LeaderElection: %v", err)
}
Expand Down