From 2fb3986386315dd604ece066a6dcb01b8addc795 Mon Sep 17 00:00:00 2001 From: Tristan Keen Date: Tue, 18 Jan 2022 16:23:58 +0000 Subject: [PATCH] fix: Allow all resources to add external links (#7923) Signed-off-by: Tristan Keen --- controller/cache/info.go | 5 -- controller/cache/info_test.go | 87 +++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/controller/cache/info.go b/controller/cache/info.go index ab1357ee0e89b..a6e5e419a3942 100644 --- a/controller/cache/info.go +++ b/controller/cache/info.go @@ -30,25 +30,20 @@ func populateNodeInfo(un *unstructured.Unstructured, res *ResourceInfo) { switch gvk.Kind { case kube.PodKind: populatePodInfo(un, res) - return case kube.ServiceKind: populateServiceInfo(un, res) - return case "Node": populateHostNodeInfo(un, res) - return } case "extensions", "networking.k8s.io": switch gvk.Kind { case kube.IngressKind: populateIngressInfo(un, res) - return } case "networking.istio.io": switch gvk.Kind { case "VirtualService": populateIstioVirtualServiceInfo(un, res) - return } } diff --git a/controller/cache/info_test.go b/controller/cache/info_test.go index dbbfee2a77579..a4b6f31477de0 100644 --- a/controller/cache/info_test.go +++ b/controller/cache/info_test.go @@ -43,6 +43,25 @@ var ( ingress: - hostname: localhost`) + testLinkAnnotatedService = strToUnstructured(` + apiVersion: v1 + kind: Service + metadata: + name: helm-guestbook + namespace: default + resourceVersion: "123" + uid: "4" + annotations: + link.argocd.argoproj.io/external-link: http://my-grafana.com/pre-generated-link + spec: + selector: + app: guestbook + type: LoadBalancer + status: + loadBalancer: + ingress: + - hostname: localhost`) + testIngress = strToUnstructured(` apiVersion: extensions/v1beta1 kind: Ingress @@ -74,6 +93,39 @@ var ( ingress: - ip: 107.178.210.11`) + testLinkAnnotatedIngress = strToUnstructured(` + apiVersion: extensions/v1beta1 + kind: Ingress + metadata: + name: helm-guestbook + namespace: default + uid: "4" + annotations: + link.argocd.argoproj.io/external-link: http://my-grafana.com/ingress-link + spec: + backend: + serviceName: not-found-service + servicePort: 443 + rules: + - host: helm-guestbook.com + http: + paths: + - backend: + serviceName: helm-guestbook + servicePort: 443 + path: / + - backend: + serviceName: helm-guestbook + servicePort: https + path: / + tls: + - host: helm-guestbook.com + secretName: my-tls-secret + status: + loadBalancer: + ingress: + - ip: 107.178.210.11`) + testIngressWildCardPath = strToUnstructured(` apiVersion: extensions/v1beta1 kind: Ingress @@ -268,6 +320,17 @@ func TestGetServiceInfo(t *testing.T) { }, info.NetworkingInfo) } +func TestGetLinkAnnotatedServiceInfo(t *testing.T) { + info := &ResourceInfo{} + populateNodeInfo(testLinkAnnotatedService, info) + assert.Equal(t, 0, len(info.Info)) + assert.Equal(t, &v1alpha1.ResourceNetworkingInfo{ + TargetLabels: map[string]string{"app": "guestbook"}, + Ingress: []v1.LoadBalancerIngress{{Hostname: "localhost"}}, + ExternalURLs: []string{"http://my-grafana.com/pre-generated-link"}, + }, info.NetworkingInfo) +} + func TestGetIstioVirtualServiceInfo(t *testing.T) { info := &ResourceInfo{} populateNodeInfo(testIstioVirtualService, info) @@ -323,6 +386,30 @@ func TestGetIngressInfo(t *testing.T) { } } +func TestGetLinkAnnotatedIngressInfo(t *testing.T) { + info := &ResourceInfo{} + populateNodeInfo(testLinkAnnotatedIngress, info) + assert.Equal(t, 0, len(info.Info)) + sort.Slice(info.NetworkingInfo.TargetRefs, func(i, j int) bool { + return strings.Compare(info.NetworkingInfo.TargetRefs[j].Name, info.NetworkingInfo.TargetRefs[i].Name) < 0 + }) + assert.Equal(t, &v1alpha1.ResourceNetworkingInfo{ + Ingress: []v1.LoadBalancerIngress{{IP: "107.178.210.11"}}, + TargetRefs: []v1alpha1.ResourceRef{{ + Namespace: "default", + Group: "", + Kind: kube.ServiceKind, + Name: "not-found-service", + }, { + Namespace: "default", + Group: "", + Kind: kube.ServiceKind, + Name: "helm-guestbook", + }}, + ExternalURLs: []string{"https://helm-guestbook.com/", "http://my-grafana.com/ingress-link"}, + }, info.NetworkingInfo) +} + func TestGetIngressInfoWildCardPath(t *testing.T) { info := &ResourceInfo{} populateNodeInfo(testIngressWildCardPath, info)