Skip to content

Commit e0e4846

Browse files
hafeciarams87
andauthored
Watch subset of namespaces for secrets (#3170)
* Watch subset of namespaces for secrets Add new command line option "watch-secret-namespace" that can be used to configure namespaces watched for secrets. Closes #3154 * Apply suggestions from code review Co-authored-by: Ciara Stacke <[email protected]> * Update cmd/nginx-ingress/flags.go Co-authored-by: Ciara Stacke <[email protected]>
1 parent 91f9da8 commit e0e4846

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

cmd/nginx-ingress/flags.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ var (
3131

3232
watchNamespaces []string
3333

34+
watchSecretNamespace = flag.String("watch-secret-namespace", "",
35+
`Comma separated list of namespaces the Ingress Controller should watch for secrets. If this arg is not configured, the Ingress Controller watches the same namespaces for all resources. See "watch-namespace". `)
36+
37+
watchSecretNamespaces []string
38+
3439
nginxConfigMaps = flag.String("nginx-configmaps", "",
3540
`A ConfigMap resource for customizing NGINX configuration. If a ConfigMap is set,
3641
but the Ingress Controller is not able to fetch it from Kubernetes API, the Ingress Controller will fail to start.
@@ -188,6 +193,16 @@ func parseFlags() {
188193
initialChecks()
189194

190195
watchNamespaces = strings.Split(*watchNamespace, ",")
196+
glog.Infof("Namespaces watched: %v", watchNamespaces)
197+
198+
if len(*watchSecretNamespace) > 0 {
199+
watchSecretNamespaces = strings.Split(*watchSecretNamespace, ",")
200+
} else {
201+
// empty => default to watched namespaces
202+
watchSecretNamespaces = watchNamespaces
203+
}
204+
205+
glog.Infof("Namespaces watched for secrets: %v", watchSecretNamespaces)
191206

192207
validationChecks()
193208

@@ -297,6 +312,11 @@ func validationChecks() {
297312
glog.Fatalf("Invalid values for namespaces: %v", namespacesNameValidationError)
298313
}
299314

315+
namespacesNameValidationError = validateNamespaceNames(watchSecretNamespaces)
316+
if namespacesNameValidationError != nil {
317+
glog.Fatalf("Invalid values for secret namespaces: %v", namespacesNameValidationError)
318+
}
319+
300320
statusPortValidationError := validatePort(*nginxStatusPort)
301321
if statusPortValidationError != nil {
302322
glog.Fatalf("Invalid value for nginx-status-port: %v", statusPortValidationError)

cmd/nginx-ingress/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ func main() {
5353

5454
validateIngressClass(kubeClient)
5555

56-
checkNamespaceExists(kubeClient)
56+
checkNamespaceExists(kubeClient, watchNamespaces)
57+
58+
checkNamespaceExists(kubeClient, watchSecretNamespaces)
5759

5860
dynClient, confClient := createCustomClients(config)
5961

@@ -127,6 +129,7 @@ func main() {
127129
RestConfig: config,
128130
ResyncPeriod: 30 * time.Second,
129131
Namespace: watchNamespaces,
132+
SecretNamespace: watchSecretNamespaces,
130133
NginxConfigurator: cnf,
131134
DefaultServerSecret: *defaultServerSecret,
132135
AppProtectEnabled: *appProtect,
@@ -240,8 +243,8 @@ func validateIngressClass(kubeClient kubernetes.Interface) {
240243
}
241244
}
242245

243-
func checkNamespaceExists(kubeClient kubernetes.Interface) {
244-
for _, ns := range watchNamespaces {
246+
func checkNamespaceExists(kubeClient kubernetes.Interface, namespaces []string) {
247+
for _, ns := range namespaces {
245248
if ns != "" {
246249
_, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), ns, meta_v1.GetOptions{})
247250
if err != nil {

deployments/helm-chart/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Parameter | Description | Default
187187
`controller.ingressClass` | A class of the Ingress Controller. An IngressClass resource with the name equal to the class must be deployed. Otherwise, the Ingress Controller will fail to start. The Ingress Controller only processes resources that belong to its class - i.e. have the "ingressClassName" field resource equal to the class. The Ingress Controller processes all the VirtualServer/VirtualServerRoute/TransportServer resources that do not have the "ingressClassName" field for all versions of kubernetes. | nginx
188188
`controller.setAsDefaultIngress` | New Ingresses without an `"ingressClassName"` field specified will be assigned the class specified in `controller.ingressClass`. | false
189189
`controller.watchNamespace` | Comma separated list of namespaces the Ingress Controller should watch for resources. By default the Ingress Controller watches all namespaces. Please note that if configuring multiple namespaces using the Helm cli `--set` option, the string needs to wrapped in double quotes and the commas escaped using a backslash - e.g. `--set controller.watchNamespace="default\,nginx-ingress"`. | ""
190+
`controller.watchSecretNamespace` | Comma separated list of namespaces the Ingress Controller should watch for resources of type Secret. If this arg is not configured, the Ingress Controller watches the same namespaces for all resources. See `watch-namespace`. Please note that if configuring multiple namespaces using the Helm cli `--set` option, the string needs to wrapped in double quotes and the commas escaped using a backslash - e.g. `--set controller.watchSecretNamespace="default\,nginx-ingress"`. | ""
190191
`controller.enableCustomResources` | Enable the custom resources. | true
191192
`controller.enablePreviewPolicies` | Enable preview policies. This parameter is deprecated. To enable OIDC Policies please use `controller.enableOIDC` instead. | false
192193
`controller.enableOIDC` | Enable OIDC policies. | false

deployments/helm-chart/templates/controller-daemonset.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ spec:
163163
- -ingress-class={{ .Values.controller.ingressClass }}
164164
{{- if .Values.controller.watchNamespace }}
165165
- -watch-namespace={{ .Values.controller.watchNamespace }}
166+
{{- end }}
167+
{{- if .Values.controller.watchSecretNamespace }}
168+
- -watch-secret-namespace={{ .Values.controller.watchSecretNamespace }}
166169
{{- end }}
167170
- -health-status={{ .Values.controller.healthStatus }}
168171
- -health-status-uri={{ .Values.controller.healthStatusURI }}

deployments/helm-chart/templates/controller-deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ spec:
166166
- -ingress-class={{ .Values.controller.ingressClass }}
167167
{{- if .Values.controller.watchNamespace }}
168168
- -watch-namespace={{ .Values.controller.watchNamespace }}
169+
{{- end }}
170+
{{- if .Values.controller.watchSecretNamespace }}
171+
- -watch-secret-namespace={{ .Values.controller.watchSecretNamespace }}
169172
{{- end }}
170173
- -health-status={{ .Values.controller.healthStatus }}
171174
- -health-status-uri={{ .Values.controller.healthStatusURI }}

deployments/helm-chart/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ controller:
186186
## Comma separated list of namespaces to watch for Ingress resources. By default the Ingress Controller watches all namespaces.
187187
watchNamespace: ""
188188

189+
## Comma separated list of namespaces to watch for Secret resources. By default the Ingress Controller watches all namespaces.
190+
watchSecretNamespace: ""
191+
189192
## Enable the custom resources.
190193
enableCustomResources: true
191194

docs/content/installation/installation-with-helm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ The following tables lists the configurable parameters of the NGINX Ingress Cont
185185
|``controller.ingressClass`` | A class of the Ingress Controller. An IngressClass resource with the name equal to the class must be deployed. Otherwise, the Ingress Controller will fail to start. The Ingress Controller only processes resources that belong to its class - i.e. have the "ingressClassName" field resource equal to the class. The Ingress Controller processes all the VirtualServer/VirtualServerRoute/TransportServer resources that do not have the "ingressClassName" field for all versions of kubernetes. | nginx |
186186
|``controller.setAsDefaultIngress`` | New Ingresses without an ingressClassName field specified will be assigned the class specified in `controller.ingressClass`. | false |
187187
|``controller.watchNamespace`` | Comma separated list of namespaces the Ingress Controller should watch for resources. By default the Ingress Controller watches all namespaces. Please note that if configuring multiple namespaces using the Helm cli `--set` option, the string needs to wrapped in double quotes and the commas escaped using a backslash - e.g. ``--set controller.watchNamespace="default\,nginx-ingress"``. | "" |
188+
|``controller.watchSecretNamespace`` | Comma separated list of namespaces the Ingress Controller should watch for resources of type Secret. If this arg is not configured, the Ingress Controller watches the same namespaces for all resources. See `watch-namespace`. Please note that if configuring multiple namespaces using the Helm cli `--set` option, the string needs to wrapped in double quotes and the commas escaped using a backslash - e.g. ``--set controller.watchSecretNamespace="default\,nginx-ingress"``. | "" |
188189
|``controller.enableCustomResources`` | Enable the custom resources. | true |
189190
|``controller.enablePreviewPolicies`` | Enable preview policies. This parameter is deprecated. To enable OIDC Policies please use ``controller.enableOIDC`` instead. | false |
190191
|``controller.enableOIDC`` | Enable OIDC policies. | false |

internal/k8s/controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ type LoadBalancerController struct {
150150
leaderElectionLockName string
151151
resync time.Duration
152152
namespaceList []string
153+
secretNamespaceList []string
153154
controllerNamespace string
154155
wildcardTLSSecret string
155156
areCustomResourcesEnabled bool
@@ -184,6 +185,7 @@ type NewLoadBalancerControllerInput struct {
184185
RestConfig *rest.Config
185186
ResyncPeriod time.Duration
186187
Namespace []string
188+
SecretNamespace []string
187189
NginxConfigurator *configs.Configurator
188190
DefaultServerSecret string
189191
AppProtectEnabled bool
@@ -234,6 +236,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
234236
leaderElectionLockName: input.LeaderElectionLockName,
235237
resync: input.ResyncPeriod,
236238
namespaceList: input.Namespace,
239+
secretNamespaceList: input.SecretNamespace,
237240
controllerNamespace: input.ControllerNamespace,
238241
wildcardTLSSecret: input.WildcardTLSSecret,
239242
areCustomResourcesEnabled: input.AreCustomResourcesEnabled,
@@ -297,7 +300,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
297300
}
298301

299302
// Creating a separate informer for secrets.
300-
for _, ns := range lbc.namespaceList {
303+
for _, ns := range lbc.secretNamespaceList {
301304
lbc.secretInformerFactory = append(lbc.secretInformerFactory, informers.NewSharedInformerFactoryWithOptions(lbc.client, input.ResyncPeriod, informers.WithNamespace(ns), informers.WithTweakListOptions(secretsTweakListOptionsFunc)))
302305
}
303306

0 commit comments

Comments
 (0)