Repository: traefik/traefik (language: go)

Issue:
Kubernetes provider incorrectly warns about failing to list ingress classes
### Welcome!

- [X] Yes, I've searched similar issues on [GitHub](https://github.com/traefik/traefik/issues) and didn't find any.
- [X] Yes, I've searched similar issues on the [Traefik community forum](https://community.traefik.io) and didn't find any.

### What did you do?

Running the latest Traefik with flags `--providers.kubernetescrd.disableClusterScopeResources=true` and `--providers.kubernetesingress.disableClusterScopeResources=true` and without any assigned ClusterRole, a runtime warning `WRN Failed to list ingress classes error="cluster factory not loaded" providerName=kubernetes` is raised.

Note that the issue seems to be limited to a warning; behavior seems to be fine.

### What did you see instead?

I expected to see no warning appear, as per #10946 this flag should supersede the `disableIngressClassLookup` flag and thus suppress this error correctly (and more importantly, also suppress the corresponding lookup).

### What version of Traefik are you using?

Version:      3.1.2
Codename:     comte
Go version:   go1.22.5
Built:        2024-08-06T13:37:51Z
OS/Arch:      linux/amd64

### What is your environment & configuration?

Helm chart (30.1.0) with values
```yaml
rbac:
    namespaced: true
```

Add more configuration information here.


### If applicable, please paste the log output in DEBUG level

`--log.level=INFO`


Discussion / hints:
I think changing the following lines 
https://github.com/traefik/traefik/blob/58bbc0cf0f599b49702f4bb587c5f2d899dbe2d0/pkg/provider/kubernetes/ingress/kubernetes.go#L222-L233

to also check for `!p.DisableClusterScopeResources` should be sufficient to fix this issue, but I'm not sure. I'm willing to provide a MR with this changes if it is correct.
/cc @mloiseleur as I encountered this warning with the current chart version
I can confirm the warning. 
Following the logic of https://github.com/traefik/traefik/pull/10946 , maybe `disableIngressClassLookup` could also be deprecated in favor of `disableClusterScopeResources`.
Hey @jnoordsij,

Thanks for reaching out.
If any community member can help us to move forward on it, we would love the help.


Fix patch:
diff --git a/pkg/provider/kubernetes/ingress/kubernetes.go b/pkg/provider/kubernetes/ingress/kubernetes.go
index 982cc09e87..c6b38e5eac 100644
--- a/pkg/provider/kubernetes/ingress/kubernetes.go
+++ b/pkg/provider/kubernetes/ingress/kubernetes.go
@@ -219,7 +219,7 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
 
 	var ingressClasses []*netv1.IngressClass
 
-	if !p.DisableIngressClassLookup {
+	if !p.DisableIngressClassLookup && !p.DisableClusterScopeResources {
 		ics, err := client.GetIngressClasses()
 		if err != nil {
 			log.Ctx(ctx).Warn().Err(err).Msg("Failed to list ingress classes")


Test patch:
diff --git a/pkg/provider/kubernetes/ingress/kubernetes_test.go b/pkg/provider/kubernetes/ingress/kubernetes_test.go
index 9b736a454f..2af574c352 100644
--- a/pkg/provider/kubernetes/ingress/kubernetes_test.go
+++ b/pkg/provider/kubernetes/ingress/kubernetes_test.go
@@ -26,11 +26,12 @@ func Bool(v bool) *bool { return &v }
 
 func TestLoadConfigurationFromIngresses(t *testing.T) {
 	testCases := []struct {
-		desc                      string
-		ingressClass              string
-		expected                  *dynamic.Configuration
-		allowEmptyServices        bool
-		disableIngressClassLookup bool
+		desc                         string
+		ingressClass                 string
+		expected                     *dynamic.Configuration
+		allowEmptyServices           bool
+		disableIngressClassLookup    bool
+		disableClusterScopeResources bool
 	}{
 		{
 			desc: "Empty ingresses",
@@ -1335,6 +1336,38 @@ func TestLoadConfigurationFromIngresses(t *testing.T) {
 				},
 			},
 		},
+		{
+			// Duplicate test case with the same fixture as the one above, but with the disableClusterScopeResources option to true.
+			// Showing that disabling the ingressClass discovery still allow the discovery of ingresses with ingress annotation.
+			desc:                         "Ingress with ingress annotation",
+			disableClusterScopeResources: true,
+			expected: &dynamic.Configuration{
+				HTTP: &dynamic.HTTPConfiguration{
+					Middlewares: map[string]*dynamic.Middleware{},
+					Routers: map[string]*dynamic.Router{
+						"testing-bar": {
+							Rule:    "PathPrefix(`/bar`)",
+							Service: "testing-service1-80",
+						},
+					},
+					Services: map[string]*dynamic.Service{
+						"testing-service1-80": {
+							LoadBalancer: &dynamic.ServersLoadBalancer{
+								PassHostHeader: Bool(true),
+								ResponseForwarding: &dynamic.ResponseForwarding{
+									FlushInterval: ptypes.Duration(100 * time.Millisecond),
+								},
+								Servers: []dynamic.Server{
+									{
+										URL: "http://10.10.0.1:8080",
+									},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
 		{
 			desc: "Ingress with ingressClass",
 			expected: &dynamic.Configuration{
@@ -1377,6 +1410,19 @@ func TestLoadConfigurationFromIngresses(t *testing.T) {
 				},
 			},
 		},
+		{
+			// Duplicate test case with the same fixture as the one above, but with the disableClusterScopeResources option to true.
+			// Showing that disabling the ingressClass discovery avoid discovering Ingresses with an IngressClass.
+			desc:                         "Ingress with ingressClass",
+			disableClusterScopeResources: true,
+			expected: &dynamic.Configuration{
+				HTTP: &dynamic.HTTPConfiguration{
+					Middlewares: map[string]*dynamic.Middleware{},
+					Routers:     map[string]*dynamic.Router{},
+					Services:    map[string]*dynamic.Service{},
+				},
+			},
+		},
 		{
 			desc: "Ingress with named port",
 			expected: &dynamic.Configuration{
@@ -1455,9 +1501,10 @@ func TestLoadConfigurationFromIngresses(t *testing.T) {
 
 			clientMock := newClientMock(generateTestFilename(test.desc))
 			p := Provider{
-				IngressClass:              test.ingressClass,
-				AllowEmptyServices:        test.allowEmptyServices,
-				DisableIngressClassLookup: test.disableIngressClassLookup,
+				IngressClass:                 test.ingressClass,
+				AllowEmptyServices:           test.allowEmptyServices,
+				DisableIngressClassLookup:    test.disableIngressClassLookup,
+				DisableClusterScopeResources: test.disableClusterScopeResources,
 			}
 			conf := p.loadConfigurationFromIngresses(context.Background(), clientMock)