Skip to content

Commit

Permalink
refactor lbc to add metadata struct (#6885)
Browse files Browse the repository at this point in the history
refactor lbc namespace

Co-authored-by: Paul Abel <128620221+pdabelf5@users.noreply.github.com>
  • Loading branch information
j1m-ryan and pdabelf5 authored Nov 29, 2024
1 parent 579a790 commit 726fe44
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
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

0 comments on commit 726fe44

Please sign in to comment.