use vi as ingress's target apiVersion (#5019)

Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>

fix test

Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>

fix test

Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>

revert test

Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>

add tests

Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>

Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>
This commit is contained in:
wyike
2022-11-08 12:03:09 +08:00
committed by GitHub
parent e79e12fbe6
commit 5d0b64773e
2 changed files with 44 additions and 2 deletions

View File

@@ -24,6 +24,7 @@ import (
"time"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/networking/v1"
networkv1beta1 "k8s.io/api/networking/v1beta1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -108,7 +109,7 @@ func getServiceEndpoints(ctx context.Context, cli client.Client, gvk schema.Grou
switch gvk.Kind {
case "Ingress":
if gvk.Group == networkv1beta1.GroupName && (gvk.Version == "v1beta1" || gvk.Version == "v1") {
var ingress networkv1beta1.Ingress
var ingress v1.Ingress
ingress.SetGroupVersionKind(gvk)
if err := findResource(ctx, cli, &ingress, name, namespace, cluster); err != nil {
klog.Error(err, fmt.Sprintf("find v1 Ingress %s/%s from cluster %s failure", name, namespace, cluster))
@@ -232,7 +233,7 @@ func generatorFromService(service corev1.Service, selectorNodeIP func() string,
return serviceEndpoints
}
func generatorFromIngress(ingress networkv1beta1.Ingress, cluster, component string) (serviceEndpoints []querytypes.ServiceEndpoint) {
func generatorFromIngress(ingress v1.Ingress, cluster, component string) (serviceEndpoints []querytypes.ServiceEndpoint) {
getAppProtocol := func(host string) string {
if len(ingress.Spec.TLS) > 0 {
for _, tls := range ingress.Spec.TLS {

View File

@@ -18,15 +18,19 @@ package query
import (
"context"
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
monitorContext "github.com/kubevela/pkg/monitor/context"
"github.com/kubevela/workflow/pkg/cue/model/value"
@@ -315,3 +319,40 @@ var _ = Describe("Test query endpoints", func() {
})
})
})
var _ = Describe("Test get ingress endpoint", func() {
It("Test get ingress endpoint with different apiVersion", func() {
ingress1 := v1.Ingress{}
Expect(yaml.Unmarshal([]byte(ingressYaml1), &ingress1)).Should(BeNil())
err := k8sClient.Create(ctx, &ingress1)
Expect(err).Should(BeNil())
gvk := schema.GroupVersionKind{Group: "networking.k8s.io", Version: "v1", Kind: "Ingress"}
Eventually(func() error {
eps := getServiceEndpoints(ctx, k8sClient, gvk, ingress1.Name, ingress1.Namespace, "", "", nil)
if len(eps) != 1 {
return fmt.Errorf("result length missmatch")
}
return nil
}, 2*time.Second, 500*time.Millisecond).Should(BeNil())
})
})
var ingressYaml1 = `
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-1
namespace: default
spec:
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80
`