tao12345666333 commented on code in PR #1238:
URL:
https://github.com/apache/apisix-ingress-controller/pull/1238#discussion_r956857671
##########
test/e2e/suite-ingress/suite-ingress-features/namespace.go:
##########
@@ -189,3 +269,130 @@ spec:
})
})
})
+
+var _ = ginkgo.Describe("suite-ingress-features: namespacing ub-label", func()
{
Review Comment:
```suggestion
var _ = ginkgo.Describe("suite-ingress-features: namespacing un-label",
func() {
```
##########
test/e2e/suite-ingress/suite-ingress-features/namespace.go:
##########
@@ -189,3 +269,130 @@ spec:
})
})
})
+
+var _ = ginkgo.Describe("suite-ingress-features: namespacing ub-label", func()
{
+ labelName, labelValue := fmt.Sprintf("namespace-selector-%d",
time.Now().Nanosecond()), "watch"
+ s := scaffold.NewScaffold(&scaffold.Options{
+ Name: "un-label",
+ IngressAPISIXReplicas: 1,
+ ApisixResourceVersion: scaffold.ApisixResourceVersion().Default,
+ NamespaceSelectorLabel: map[string]string{
+ labelName: labelValue,
+ },
+ DisableNamespaceLabel: true,
+ })
+ namespace1 := fmt.Sprintf("un-label-%d", time.Now().Nanosecond())
+
+ ginkgo.It("un-label", func() {
+ ns := fmt.Sprintf(`
+apiVersion: v1
+kind: Namespace
+metadata:
+ name: %s
+ labels:
+ %s: %s
+`, namespace1, labelName, labelValue)
+ assert.Nil(ginkgo.GinkgoT(),
s.CreateResourceFromStringWithNamespace(ns, namespace1), "creating namespace")
+ //defer s.DeleteResourceFromStringWithNamespace(ns, namespace1)
+ _, err := s.NewHTTPBINWithNamespace(namespace1)
+ assert.Nil(ginkgo.GinkgoT(), err, "create httpbin service in",
namespace1)
+
+ backendSvc, backendSvcPort := s.DefaultHTTPBackend()
+ route1 := fmt.Sprintf(`
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+ name: httpbin-route
+spec:
+ ingressClassName: apisix
+ rules:
+ - host: httpbin.com
+ http:
+ paths:
+ - path: /ip
+ pathType: Exact
+ backend:
+ service:
+ name: %s
+ port:
+ number: %d
+`, backendSvc, backendSvcPort[0])
+ assert.Nil(ginkgo.GinkgoT(),
s.CreateResourceFromStringWithNamespace(route1, namespace1), "creating ingress")
+ assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1))
+ time.Sleep(time.Second * 6)
+ _ = s.NewAPISIXClient().GET("/ip").WithHeader("Host",
"httpbin.com").Expect().Status(http.StatusOK).Body().Raw()
+
+ assert.Nil(ginkgo.GinkgoT(),
s.DeleteResourceFromStringWithNamespace(route1, namespace1), "deleting ingress")
+ ns = fmt.Sprintf(`
+apiVersion: v1
+kind: Namespace
+metadata:
+ name: %s
+`, namespace1)
+ assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ns),
"creating namespace")
+ assert.Nil(ginkgo.GinkgoT(), err, "create httpbin service in",
namespace1)
+ time.Sleep(6 * time.Second)
+ routes, err := s.ListApisixRoutes()
+ assert.Nil(ginkgo.GinkgoT(), err)
+ assert.Len(ginkgo.GinkgoT(), routes, 0)
Review Comment:
In order to be more in line with the actual situation, I suggest modifying
the logic here to directly clean up the namespace labels
##########
test/e2e/suite-ingress/suite-ingress-features/namespace.go:
##########
@@ -37,12 +38,37 @@ type headers struct {
}
var _ = ginkgo.Describe("suite-ingress-features: namespacing filtering
enable", func() {
- s := scaffold.NewDefaultScaffold()
+ s := scaffold.NewScaffold(&scaffold.Options{
+ Name: "enable-namespace-selector",
+ IngressAPISIXReplicas: 1,
+ ApisixResourceVersion: scaffold.ApisixResourceVersion().Default,
+ NamespaceSelectorLabel: map[string]string{
+ fmt.Sprintf("namespace-selector-%d",
time.Now().Nanosecond()): "watch",
+ },
+ DisableNamespaceLabel: true,
+ })
ginkgo.Context("with namespace_selector", func() {
+ namespace1 := fmt.Sprintf("namespace-selector-1-%d",
time.Now().Nanosecond())
+ namespace2 := fmt.Sprintf("namespace-selector-2-%d",
time.Now().Nanosecond())
+
+ createNamespaceLabel := func(namespace string) {
+ k8s.CreateNamespaceWithMetadata(ginkgo.GinkgoT(),
&k8s.KubectlOptions{ConfigPath: scaffold.GetKubeconfig()},
metav1.ObjectMeta{Name: namespace, Labels: s.NamespaceSelectorLabel()})
+ _, err := s.NewHTTPBINWithNamespace(namespace)
+ time.Sleep(6 * time.Second)
+ assert.Nil(ginkgo.GinkgoT(), err, "create second
httpbin service")
Review Comment:
```suggestion
assert.Nil(ginkgo.GinkgoT(), err, "create new httpbin
service")
```
##########
pkg/providers/k8s/namespace/namespace_provider.go:
##########
@@ -48,44 +46,25 @@ type WatchingNamespaceProvider interface {
}
func NewWatchingNamespaceProvider(ctx context.Context, kube *kube.KubeClient,
cfg *config.Config) (WatchingNamespaceProvider, error) {
- var (
- watchingNamespaces = new(sync.Map)
- watchingLabels = make(map[string]string)
- )
- if len(cfg.Kubernetes.AppNamespaces) > 1 ||
cfg.Kubernetes.AppNamespaces[0] != v1.NamespaceAll {
- for _, ns := range cfg.Kubernetes.AppNamespaces {
- watchingNamespaces.Store(ns, struct{}{})
- }
- }
- // support namespace label-selector
- for _, selector := range cfg.Kubernetes.NamespaceSelector {
- labelSlice := strings.Split(selector, "=")
- watchingLabels[labelSlice[0]] = labelSlice[1]
- }
-
- // watchingNamespaces and watchingLabels are empty means to monitor all
namespaces.
- if !validation.HasValueInSyncMap(watchingNamespaces) &&
len(watchingLabels) == 0 {
- opts := metav1.ListOptions{}
- // list all namespaces
- nsList, err := kube.Client.CoreV1().Namespaces().List(ctx, opts)
- if err != nil {
- log.Error(err.Error())
- ctx.Done()
- } else {
- wns := new(sync.Map)
- for _, v := range nsList.Items {
- wns.Store(v.Name, struct{}{})
- }
- watchingNamespaces = wns
- }
- }
-
c := &watchingProvider{
kube: kube,
cfg: cfg,
- watchingNamespaces: watchingNamespaces,
- watchingLabels: watchingLabels,
+ watchingNamespaces: new(sync.Map),
+ watchingLabels: make(map[string]string),
+
+ enableLabelsWatching: false,
+ }
+
+ if len(cfg.Kubernetes.NamespaceSelector) == 0 {
+ return c, nil
+ }
+
+ // support namespace label-selector
+ c.enableLabelsWatching = true
+ for _, selector := range cfg.Kubernetes.NamespaceSelector {
+ labelSlice := strings.Split(selector, "=")
+ c.watchingLabels[labelSlice[0]] = labelSlice[1]
Review Comment:
I think a judgment should be added here to avoid mistakes. (Avoid indexing
over
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]