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

Watch subset of namespaces for secrets #3170

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions cmd/nginx-ingress/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var (

watchNamespaces []string

watchSecretNamespace = flag.String("watch-secret-namespace", "",
`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`. `)
ciarams87 marked this conversation as resolved.
Show resolved Hide resolved

watchSecretNamespaces []string

nginxConfigMaps = flag.String("nginx-configmaps", "",
`A ConfigMap resource for customizing NGINX configuration. If a ConfigMap is set,
but the Ingress Controller is not able to fetch it from Kubernetes API, the Ingress Controller will fail to start.
Expand Down Expand Up @@ -200,6 +205,16 @@ func parseFlags(versionInfo string, binaryInfo string) {
glog.Info(binaryInfo)

watchNamespaces = strings.Split(*watchNamespace, ",")
glog.Infof("Namespaces watched: %v", watchNamespaces)

if len(*watchSecretNamespace) > 0 {
watchSecretNamespaces = strings.Split(*watchSecretNamespace, ",")
} else {
// empty => default to watched namespaces
watchSecretNamespaces = watchNamespaces
}

glog.Infof("Namespaces watched for secrets: %v", watchSecretNamespaces)

validationChecks()

Expand Down Expand Up @@ -314,6 +329,11 @@ func validationChecks() {
glog.Fatalf("Invalid values for namespaces: %v", namespacesNameValidationError)
}

namespacesNameValidationError = validateNamespaceNames(watchSecretNamespaces)
if namespacesNameValidationError != nil {
glog.Fatalf("Invalid values for secret namespaces: %v", namespacesNameValidationError)
}

statusPortValidationError := validatePort(*nginxStatusPort)
if statusPortValidationError != nil {
glog.Fatalf("Invalid value for nginx-status-port: %v", statusPortValidationError)
Expand Down
9 changes: 6 additions & 3 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func main() {

validateIngressClass(kubeClient)

checkNamespaceExists(kubeClient)
checkNamespaceExists(kubeClient, watchNamespaces)

checkNamespaceExists(kubeClient, watchSecretNamespaces)

dynClient, confClient := createCustomClients(config)

Expand Down Expand Up @@ -121,6 +123,7 @@ func main() {
RestConfig: config,
ResyncPeriod: 30 * time.Second,
Namespace: watchNamespaces,
SecretNamespace: watchSecretNamespaces,
NginxConfigurator: cnf,
DefaultServerSecret: *defaultServerSecret,
AppProtectEnabled: *appProtect,
Expand Down Expand Up @@ -234,8 +237,8 @@ func validateIngressClass(kubeClient kubernetes.Interface) {
}
}

func checkNamespaceExists(kubeClient kubernetes.Interface) {
for _, ns := range watchNamespaces {
func checkNamespaceExists(kubeClient kubernetes.Interface, namespaces []string) {
for _, ns := range namespaces {
if ns != "" {
_, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), ns, meta_v1.GetOptions{})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions deployments/helm-chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Parameter | Description | Default
`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
`controller.setAsDefaultIngress` | New Ingresses without an `"ingressClassName"` field specified will be assigned the class specified in `controller.ingressClass`. | false
`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"`. | ""
`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"`. | ""
`controller.enableCustomResources` | Enable the custom resources. | true
`controller.enablePreviewPolicies` | Enable preview policies. This parameter is deprecated. To enable OIDC Policies please use `controller.enableOIDC` instead. | false
`controller.enableOIDC` | Enable OIDC policies. | false
Expand Down
3 changes: 3 additions & 0 deletions deployments/helm-chart/templates/controller-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ spec:
- -ingress-class={{ .Values.controller.ingressClass }}
{{- if .Values.controller.watchNamespace }}
- -watch-namespace={{ .Values.controller.watchNamespace }}
{{- end }}
{{- if .Values.controller.watchSecretNamespace }}
- -watch-secret-namespace={{ .Values.controller.watchSecretNamespace }}
{{- end }}
- -health-status={{ .Values.controller.healthStatus }}
- -health-status-uri={{ .Values.controller.healthStatusURI }}
Expand Down
3 changes: 3 additions & 0 deletions deployments/helm-chart/templates/controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ spec:
- -ingress-class={{ .Values.controller.ingressClass }}
{{- if .Values.controller.watchNamespace }}
- -watch-namespace={{ .Values.controller.watchNamespace }}
{{- end }}
{{- if .Values.controller.watchSecretNamespace }}
- -watch-secret-namespace={{ .Values.controller.watchSecretNamespace }}
{{- end }}
- -health-status={{ .Values.controller.healthStatus }}
- -health-status-uri={{ .Values.controller.healthStatusURI }}
Expand Down
3 changes: 3 additions & 0 deletions deployments/helm-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ controller:
## Comma separated list of namespaces to watch for Ingress resources. By default the Ingress Controller watches all namespaces.
watchNamespace: ""

## Comma separated list of namespaces to watch for Secret resources. By default the Ingress Controller watches all namespaces.
watchSecretNamespace: ""

## Enable the custom resources.
enableCustomResources: true

Expand Down
1 change: 1 addition & 0 deletions docs/content/installation/installation-with-helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ The following tables lists the configurable parameters of the NGINX Ingress Cont
|``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 |
|``controller.setAsDefaultIngress`` | New Ingresses without an ingressClassName field specified will be assigned the class specified in `controller.ingressClass`. | false |
|``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"``. | "" |
|``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"``. | "" |
|``controller.enableCustomResources`` | Enable the custom resources. | true |
|``controller.enablePreviewPolicies`` | Enable preview policies. This parameter is deprecated. To enable OIDC Policies please use ``controller.enableOIDC`` instead. | false |
|``controller.enableOIDC`` | Enable OIDC policies. | false |
Expand Down
5 changes: 4 additions & 1 deletion internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type LoadBalancerController struct {
leaderElectionLockName string
resync time.Duration
namespaceList []string
secretNamespaceList []string
controllerNamespace string
wildcardTLSSecret string
areCustomResourcesEnabled bool
Expand Down Expand Up @@ -184,6 +185,7 @@ type NewLoadBalancerControllerInput struct {
RestConfig *rest.Config
ResyncPeriod time.Duration
Namespace []string
SecretNamespace []string
NginxConfigurator *configs.Configurator
DefaultServerSecret string
AppProtectEnabled bool
Expand Down Expand Up @@ -234,6 +236,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
leaderElectionLockName: input.LeaderElectionLockName,
resync: input.ResyncPeriod,
namespaceList: input.Namespace,
secretNamespaceList: input.SecretNamespace,
controllerNamespace: input.ControllerNamespace,
wildcardTLSSecret: input.WildcardTLSSecret,
areCustomResourcesEnabled: input.AreCustomResourcesEnabled,
Expand Down Expand Up @@ -297,7 +300,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
}

// Creating a separate informer for secrets.
for _, ns := range lbc.namespaceList {
for _, ns := range lbc.secretNamespaceList {
lbc.secretInformerFactory = append(lbc.secretInformerFactory, informers.NewSharedInformerFactoryWithOptions(lbc.client, input.ResyncPeriod, informers.WithNamespace(ns), informers.WithTweakListOptions(secretsTweakListOptionsFunc)))
}

Expand Down