From a175667a11261bf4ec47facad4613b444cc726ab Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 15:17:29 +0300 Subject: [PATCH 01/15] Added Destination Finder for Jaeger + Infra to extend it to more Destinations. --- .../destination_recognition/jaeger.go | 38 +++++++++ .../destination_recognition/utils.go | 83 +++++++++++++++++++ frontend/endpoints/destinations.go | 22 +++++ 3 files changed, 143 insertions(+) create mode 100644 frontend/endpoints/destination_recognition/jaeger.go create mode 100644 frontend/endpoints/destination_recognition/utils.go diff --git a/frontend/endpoints/destination_recognition/jaeger.go b/frontend/endpoints/destination_recognition/jaeger.go new file mode 100644 index 000000000..3b553cf3b --- /dev/null +++ b/frontend/endpoints/destination_recognition/jaeger.go @@ -0,0 +1,38 @@ +package destination_recognition + +import ( + "fmt" + "github.com/odigos-io/odigos/common" + k8s "k8s.io/api/core/v1" + "strings" +) + +type JaegerDestinationFinder struct{} + +const JaegerGrpcOtlpPort int32 = 4317 + +func (j *JaegerDestinationFinder) findPotentialServices(services []k8s.Service) []k8s.Service { + var potentialServices []k8s.Service + for _, service := range services { + for _, port := range service.Spec.Ports { + if port.Port == JaegerGrpcOtlpPort && strings.Contains(service.Name, string(common.JaegerDestinationType)) { + potentialServices = append(potentialServices, service) + } + } + } + + return potentialServices +} + +func (j *JaegerDestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { + var destinationDetails []DestinationDetails + for _, service := range services { + urlString := fmt.Sprintf("url: %s.%s:%d", service.Name, service.Namespace, JaegerGrpcOtlpPort) + destinationDetails = append(destinationDetails, DestinationDetails{ + Name: string(common.JaegerDestinationType), + UrlString: urlString, + }) + } + + return destinationDetails +} diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go new file mode 100644 index 000000000..b326af274 --- /dev/null +++ b/frontend/endpoints/destination_recognition/utils.go @@ -0,0 +1,83 @@ +package destination_recognition + +import ( + "github.com/gin-gonic/gin" + "github.com/odigos-io/odigos/frontend/kube" + k8s "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type DestinationType string + +const ( + JaegerDestinationType DestinationType = "jaeger" +) + +var SupportedDestinationType = []DestinationType{JaegerDestinationType} + +type DestinationDetails struct { + Name string `json:"name"` + UrlString string `json:"urlString"` +} + +type IDestinationFinder interface { + findPotentialServices([]k8s.Service) []k8s.Service + fetchDestinationDetails([]k8s.Service) []DestinationDetails +} + +type DestinationFinder struct { + destinationFinder IDestinationFinder +} + +func (d *DestinationFinder) findPotentialServices(services []k8s.Service) []k8s.Service { + return d.destinationFinder.findPotentialServices(services) +} + +func (d *DestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { + return d.destinationFinder.fetchDestinationDetails(services) +} + +func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { + helmManagedServices := findHelmManagedServices(ctx, namespaces) + + var destinationFinder DestinationFinder + for _, destinationType := range SupportedDestinationType { + switch destinationType { + case JaegerDestinationType: + destinationFinder = DestinationFinder{ + destinationFinder: &JaegerDestinationFinder{}, + } + } + } + + potentialServices := destinationFinder.findPotentialServices(helmManagedServices) + destinationDetails := destinationFinder.fetchDestinationDetails(potentialServices) + + return destinationDetails, nil +} + +func findHelmManagedServices(ctx *gin.Context, namespaces []k8s.Namespace) []k8s.Service { + var helmManagedServices []k8s.Service + for _, ns := range namespaces { + services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, metav1.ListOptions{}) + + for _, service := range services.Items { + if isHelmManagedPod(service) { + helmManagedServices = append(helmManagedServices, service) + } + } + } + + return helmManagedServices +} + +// isHelmManagedPod checks if a Pod was created by Helm +func isHelmManagedPod(service k8s.Service) bool { + annotations := service.GetAnnotations() + labels := service.GetLabels() + + _, hasHelmReleaseName := annotations["meta.helm.sh/release-name"] + managedByHelm := labels["app.kubernetes.io/managed-by"] == "Helm" + + return hasHelmReleaseName && managedByHelm +} diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index 8e19da46c..e1de8c8d1 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "fmt" + "github.com/odigos-io/odigos/frontend/endpoints/destination_recognition" + "github.com/odigos-io/odigos/k8sutils/pkg/env" "net/http" "github.com/gin-gonic/gin" @@ -91,6 +93,12 @@ func GetDestinationTypes(c *gin.Context) { }) } + potentialDestinations, err := findPotentialDestinations(c) + if err != nil { + return + } + fmt.Printf("Potential destinations: %v\n", potentialDestinations) + c.JSON(200, resp) } @@ -634,3 +642,17 @@ func addDestinationOwnerReferenceToSecret(ctx context.Context, odigosns string, } return nil } + +func findPotentialDestinations(ctx *gin.Context) ([]destination_recognition.DestinationDetails, error) { + relevantNamespaces, err := getRelevantNameSpaces(ctx, env.GetCurrentNamespace()) + if err != nil { + return nil, err + } + + destinationDetails, err := destination_recognition.GetAllPotentialDestinationDetails(ctx, relevantNamespaces) + if err != nil { + return nil, err + } + + return destinationDetails, nil +} From dc10dc759a9e5606a2f76de7ea5abc1f4070e64e Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 15:46:05 +0300 Subject: [PATCH 02/15] Added Destination Finder for Jaeger + Infra to extend it to more Destinations. --- .../destination_finder.go | 55 +++++++++++++++++ .../destination_recognition/jaeger.go | 7 ++- .../destination_recognition/utils.go | 61 ++----------------- 3 files changed, 67 insertions(+), 56 deletions(-) create mode 100644 frontend/endpoints/destination_recognition/destination_finder.go diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go new file mode 100644 index 000000000..c85763a2f --- /dev/null +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -0,0 +1,55 @@ +package destination_recognition + +import ( + "github.com/gin-gonic/gin" + k8s "k8s.io/api/core/v1" +) + +type DestinationType string + +const ( + JaegerDestinationType DestinationType = "jaeger" +) + +var SupportedDestinationType = []DestinationType{JaegerDestinationType} + +type DestinationDetails struct { + Name string `json:"name"` + UrlString string `json:"urlString"` +} + +type IDestinationFinder interface { + findPotentialServices([]k8s.Service) []k8s.Service + fetchDestinationDetails([]k8s.Service) []DestinationDetails +} + +type DestinationFinder struct { + destinationFinder IDestinationFinder +} + +func (d *DestinationFinder) findPotentialServices(services []k8s.Service) []k8s.Service { + return d.destinationFinder.findPotentialServices(services) +} + +func (d *DestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { + return d.destinationFinder.fetchDestinationDetails(services) +} + +func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { + helmManagedServices := findHelmManagedServices(ctx, namespaces) + + var destinationFinder DestinationFinder + for _, destinationType := range SupportedDestinationType { + switch destinationType { + case JaegerDestinationType: + destinationFinder = DestinationFinder{ + destinationFinder: &JaegerDestinationFinder{}, + } + } + } + + potentialServices := destinationFinder.findPotentialServices(helmManagedServices) + destinationDetails := destinationFinder.fetchDestinationDetails(potentialServices) + + return destinationDetails, nil +} diff --git a/frontend/endpoints/destination_recognition/jaeger.go b/frontend/endpoints/destination_recognition/jaeger.go index 3b553cf3b..1a925ee9c 100644 --- a/frontend/endpoints/destination_recognition/jaeger.go +++ b/frontend/endpoints/destination_recognition/jaeger.go @@ -15,8 +15,9 @@ func (j *JaegerDestinationFinder) findPotentialServices(services []k8s.Service) var potentialServices []k8s.Service for _, service := range services { for _, port := range service.Spec.Ports { - if port.Port == JaegerGrpcOtlpPort && strings.Contains(service.Name, string(common.JaegerDestinationType)) { + if isJaegerService(port.Port, service.Name) { potentialServices = append(potentialServices, service) + break } } } @@ -24,6 +25,10 @@ func (j *JaegerDestinationFinder) findPotentialServices(services []k8s.Service) return potentialServices } +func isJaegerService(portNumber int32, name string) bool { + return portNumber == JaegerGrpcOtlpPort && strings.Contains(name, string(common.JaegerDestinationType)) +} + func (j *JaegerDestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { var destinationDetails []DestinationDetails for _, service := range services { diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go index b326af274..1d72854ae 100644 --- a/frontend/endpoints/destination_recognition/utils.go +++ b/frontend/endpoints/destination_recognition/utils.go @@ -3,63 +3,14 @@ package destination_recognition import ( "github.com/gin-gonic/gin" "github.com/odigos-io/odigos/frontend/kube" - k8s "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/api/core/v1" + v12 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type DestinationType string - -const ( - JaegerDestinationType DestinationType = "jaeger" -) - -var SupportedDestinationType = []DestinationType{JaegerDestinationType} - -type DestinationDetails struct { - Name string `json:"name"` - UrlString string `json:"urlString"` -} - -type IDestinationFinder interface { - findPotentialServices([]k8s.Service) []k8s.Service - fetchDestinationDetails([]k8s.Service) []DestinationDetails -} - -type DestinationFinder struct { - destinationFinder IDestinationFinder -} - -func (d *DestinationFinder) findPotentialServices(services []k8s.Service) []k8s.Service { - return d.destinationFinder.findPotentialServices(services) -} - -func (d *DestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { - return d.destinationFinder.fetchDestinationDetails(services) -} - -func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { - helmManagedServices := findHelmManagedServices(ctx, namespaces) - - var destinationFinder DestinationFinder - for _, destinationType := range SupportedDestinationType { - switch destinationType { - case JaegerDestinationType: - destinationFinder = DestinationFinder{ - destinationFinder: &JaegerDestinationFinder{}, - } - } - } - - potentialServices := destinationFinder.findPotentialServices(helmManagedServices) - destinationDetails := destinationFinder.fetchDestinationDetails(potentialServices) - - return destinationDetails, nil -} - -func findHelmManagedServices(ctx *gin.Context, namespaces []k8s.Namespace) []k8s.Service { - var helmManagedServices []k8s.Service +func findHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) []v1.Service { + var helmManagedServices []v1.Service for _, ns := range namespaces { - services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, metav1.ListOptions{}) + services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, v12.ListOptions{}) for _, service := range services.Items { if isHelmManagedPod(service) { @@ -72,7 +23,7 @@ func findHelmManagedServices(ctx *gin.Context, namespaces []k8s.Namespace) []k8s } // isHelmManagedPod checks if a Pod was created by Helm -func isHelmManagedPod(service k8s.Service) bool { +func isHelmManagedPod(service v1.Service) bool { annotations := service.GetAnnotations() labels := service.GetLabels() From 2490ae6c6792f9162e59c006fe17df2cce2db6bb Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 15:47:15 +0300 Subject: [PATCH 03/15] Added Destination Finder for Jaeger + Infra to extend it to more Destinations. --- frontend/endpoints/destinations.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index e1de8c8d1..aea5c4375 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,12 +93,6 @@ func GetDestinationTypes(c *gin.Context) { }) } - potentialDestinations, err := findPotentialDestinations(c) - if err != nil { - return - } - fmt.Printf("Potential destinations: %v\n", potentialDestinations) - c.JSON(200, resp) } From 498e987a63d58a5c51ec28e1cc1cacaee685f2d7 Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 15:50:33 +0300 Subject: [PATCH 04/15] Added Destination Finder for Jaeger + Infra to extend it to more Destinations. --- .../destination_recognition/destination_finder.go | 2 +- frontend/endpoints/destination_recognition/utils.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index c85763a2f..07f9456cb 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -36,7 +36,7 @@ func (d *DestinationFinder) fetchDestinationDetails(services []k8s.Service) []De } func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { - helmManagedServices := findHelmManagedServices(ctx, namespaces) + helmManagedServices := getAllHelmManagedServices(ctx, namespaces) var destinationFinder DestinationFinder for _, destinationType := range SupportedDestinationType { diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go index 1d72854ae..30fd3daea 100644 --- a/frontend/endpoints/destination_recognition/utils.go +++ b/frontend/endpoints/destination_recognition/utils.go @@ -7,13 +7,13 @@ import ( v12 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func findHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) []v1.Service { +func getAllHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) []v1.Service { var helmManagedServices []v1.Service for _, ns := range namespaces { services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, v12.ListOptions{}) for _, service := range services.Items { - if isHelmManagedPod(service) { + if isHelmManagedService(service) { helmManagedServices = append(helmManagedServices, service) } } @@ -22,8 +22,8 @@ func findHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) []v1.S return helmManagedServices } -// isHelmManagedPod checks if a Pod was created by Helm -func isHelmManagedPod(service v1.Service) bool { +// isHelmManagedService checks if a Pod was created by Helm +func isHelmManagedService(service v1.Service) bool { annotations := service.GetAnnotations() labels := service.GetLabels() From 3f5cf35dbab691e04efcb78b9311c211c96c8e32 Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 17:48:27 +0300 Subject: [PATCH 05/15] Changed a bit the infra --- .../destination_finder.go | 33 +++++++++++-------- .../destination_recognition/jaeger.go | 30 +++++++---------- frontend/endpoints/destinations.go | 6 ++++ 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index 07f9456cb..d94532aa1 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -19,37 +19,42 @@ type DestinationDetails struct { } type IDestinationFinder interface { - findPotentialServices([]k8s.Service) []k8s.Service - fetchDestinationDetails([]k8s.Service) []DestinationDetails + isPotentialService(k8s.Service) bool + fetchDestinationDetails(service k8s.Service) DestinationDetails } type DestinationFinder struct { destinationFinder IDestinationFinder } -func (d *DestinationFinder) findPotentialServices(services []k8s.Service) []k8s.Service { - return d.destinationFinder.findPotentialServices(services) +func (d *DestinationFinder) isPotentialService(service k8s.Service) bool { + return d.destinationFinder.isPotentialService(service) } -func (d *DestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { - return d.destinationFinder.fetchDestinationDetails(services) +func (d *DestinationFinder) fetchDestinationDetails(service k8s.Service) DestinationDetails { + return d.destinationFinder.fetchDestinationDetails(service) } func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { helmManagedServices := getAllHelmManagedServices(ctx, namespaces) var destinationFinder DestinationFinder - for _, destinationType := range SupportedDestinationType { - switch destinationType { - case JaegerDestinationType: - destinationFinder = DestinationFinder{ - destinationFinder: &JaegerDestinationFinder{}, + var destinationDetails []DestinationDetails + for _, service := range helmManagedServices { + for _, destinationType := range SupportedDestinationType { + switch destinationType { + case JaegerDestinationType: + destinationFinder = DestinationFinder{ + destinationFinder: &JaegerDestinationFinder{}, + } + } + + if destinationFinder.isPotentialService(service) { + destinationDetails = append(destinationDetails, destinationFinder.fetchDestinationDetails(service)) + break } } } - potentialServices := destinationFinder.findPotentialServices(helmManagedServices) - destinationDetails := destinationFinder.fetchDestinationDetails(potentialServices) - return destinationDetails, nil } diff --git a/frontend/endpoints/destination_recognition/jaeger.go b/frontend/endpoints/destination_recognition/jaeger.go index 1a925ee9c..c6f6c77ce 100644 --- a/frontend/endpoints/destination_recognition/jaeger.go +++ b/frontend/endpoints/destination_recognition/jaeger.go @@ -10,34 +10,26 @@ import ( type JaegerDestinationFinder struct{} const JaegerGrpcOtlpPort int32 = 4317 +const JaegerGrpcUrlFormat = "%s.%s:%d" -func (j *JaegerDestinationFinder) findPotentialServices(services []k8s.Service) []k8s.Service { - var potentialServices []k8s.Service - for _, service := range services { - for _, port := range service.Spec.Ports { - if isJaegerService(port.Port, service.Name) { - potentialServices = append(potentialServices, service) - break - } +func (j *JaegerDestinationFinder) isPotentialService(service k8s.Service) bool { + for _, port := range service.Spec.Ports { + if isJaegerService(port.Port, service.Name) { + return true } } - return potentialServices + return false } func isJaegerService(portNumber int32, name string) bool { return portNumber == JaegerGrpcOtlpPort && strings.Contains(name, string(common.JaegerDestinationType)) } -func (j *JaegerDestinationFinder) fetchDestinationDetails(services []k8s.Service) []DestinationDetails { - var destinationDetails []DestinationDetails - for _, service := range services { - urlString := fmt.Sprintf("url: %s.%s:%d", service.Name, service.Namespace, JaegerGrpcOtlpPort) - destinationDetails = append(destinationDetails, DestinationDetails{ - Name: string(common.JaegerDestinationType), - UrlString: urlString, - }) +func (j *JaegerDestinationFinder) fetchDestinationDetails(service k8s.Service) DestinationDetails { + urlString := fmt.Sprintf(JaegerGrpcUrlFormat, service.Name, service.Namespace, JaegerGrpcOtlpPort) + return DestinationDetails{ + Name: string(common.JaegerDestinationType), + UrlString: urlString, } - - return destinationDetails } diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index aea5c4375..e1de8c8d1 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,6 +93,12 @@ func GetDestinationTypes(c *gin.Context) { }) } + potentialDestinations, err := findPotentialDestinations(c) + if err != nil { + return + } + fmt.Printf("Potential destinations: %v\n", potentialDestinations) + c.JSON(200, resp) } From 881eee07f145642128922f69afa5d34fc7bafd0b Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 17:51:30 +0300 Subject: [PATCH 06/15] Changed a bit the infra --- .../endpoints/destination_recognition/destination_finder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index d94532aa1..6552ee9f9 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -20,7 +20,7 @@ type DestinationDetails struct { type IDestinationFinder interface { isPotentialService(k8s.Service) bool - fetchDestinationDetails(service k8s.Service) DestinationDetails + fetchDestinationDetails(k8s.Service) DestinationDetails } type DestinationFinder struct { From 05792f9e26a00528363338558963589f8ecffc36 Mon Sep 17 00:00:00 2001 From: yodigos Date: Wed, 31 Jul 2024 18:03:21 +0300 Subject: [PATCH 07/15] Changed a bit the infra --- .../destination_finder.go | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index 6552ee9f9..4e7f13ac0 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -38,17 +38,11 @@ func (d *DestinationFinder) fetchDestinationDetails(service k8s.Service) Destina func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { helmManagedServices := getAllHelmManagedServices(ctx, namespaces) - var destinationFinder DestinationFinder + var destinationFinder *DestinationFinder var destinationDetails []DestinationDetails for _, service := range helmManagedServices { for _, destinationType := range SupportedDestinationType { - switch destinationType { - case JaegerDestinationType: - destinationFinder = DestinationFinder{ - destinationFinder: &JaegerDestinationFinder{}, - } - } - + destinationFinder = getDestinationFinder(destinationType) if destinationFinder.isPotentialService(service) { destinationDetails = append(destinationDetails, destinationFinder.fetchDestinationDetails(service)) break @@ -58,3 +52,14 @@ func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namesp return destinationDetails, nil } + +func getDestinationFinder(destinationType DestinationType) *DestinationFinder { + switch destinationType { + case JaegerDestinationType: + return &DestinationFinder{ + destinationFinder: &JaegerDestinationFinder{}, + } + } + + return nil +} From 864e24fcc2ac572ebdc0ab27048f553e433a059a Mon Sep 17 00:00:00 2001 From: yodigos Date: Thu, 1 Aug 2024 09:38:13 +0300 Subject: [PATCH 08/15] Changed a bit the infra --- frontend/endpoints/destination_recognition/utils.go | 4 ++-- frontend/endpoints/destinations.go | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go index 30fd3daea..12b227dc5 100644 --- a/frontend/endpoints/destination_recognition/utils.go +++ b/frontend/endpoints/destination_recognition/utils.go @@ -4,13 +4,13 @@ import ( "github.com/gin-gonic/gin" "github.com/odigos-io/odigos/frontend/kube" "k8s.io/api/core/v1" - v12 "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func getAllHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) []v1.Service { var helmManagedServices []v1.Service for _, ns := range namespaces { - services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, v12.ListOptions{}) + services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, metav1.ListOptions{}) for _, service := range services.Items { if isHelmManagedService(service) { diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index e1de8c8d1..aea5c4375 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,12 +93,6 @@ func GetDestinationTypes(c *gin.Context) { }) } - potentialDestinations, err := findPotentialDestinations(c) - if err != nil { - return - } - fmt.Printf("Potential destinations: %v\n", potentialDestinations) - c.JSON(200, resp) } From 41ca82de74d017cdeb5201a6c63c822693b8fe2c Mon Sep 17 00:00:00 2001 From: yodigos Date: Thu, 1 Aug 2024 10:01:12 +0300 Subject: [PATCH 09/15] Memory optimization when querying all Services --- .../destination_finder.go | 5 +++- .../destination_recognition/utils.go | 24 ++++++++++++------- frontend/endpoints/destinations.go | 7 ++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index 4e7f13ac0..607c53549 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -36,7 +36,10 @@ func (d *DestinationFinder) fetchDestinationDetails(service k8s.Service) Destina } func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { - helmManagedServices := getAllHelmManagedServices(ctx, namespaces) + helmManagedServices, err := getAllHelmManagedServices(ctx, namespaces) + if err != nil { + return nil, err + } var destinationFinder *DestinationFinder var destinationDetails []DestinationDetails diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go index 12b227dc5..b0618b04d 100644 --- a/frontend/endpoints/destination_recognition/utils.go +++ b/frontend/endpoints/destination_recognition/utils.go @@ -3,23 +3,31 @@ package destination_recognition import ( "github.com/gin-gonic/gin" "github.com/odigos-io/odigos/frontend/kube" + "github.com/odigos-io/odigos/k8sutils/pkg/client" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func getAllHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) []v1.Service { +func getAllHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) ([]v1.Service, error) { var helmManagedServices []v1.Service + var err error for _, ns := range namespaces { - services, _ := kube.DefaultClient.CoreV1().Services(ns.Name).List(ctx, metav1.ListOptions{}) + err = client.ListWithPages(client.DefaultPageSize, kube.DefaultClient.CoreV1().Services(ns.Name).List, + ctx, metav1.ListOptions{}, func(services *v1.ServiceList) error { + for _, service := range services.Items { + if isHelmManagedService(service) { + helmManagedServices = append(helmManagedServices, service) + } + } + return nil + }) + } - for _, service := range services.Items { - if isHelmManagedService(service) { - helmManagedServices = append(helmManagedServices, service) - } - } + if err != nil { + return nil, err } - return helmManagedServices + return helmManagedServices, nil } // isHelmManagedService checks if a Pod was created by Helm diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index aea5c4375..f6f161439 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,6 +93,13 @@ func GetDestinationTypes(c *gin.Context) { }) } + //TODO - remove before merge + //potentialDestinations, err := findPotentialDestinations(c) + //if err != nil { + // return + //} + //fmt.Printf("Potential destinations: %v\n", potentialDestinations) + c.JSON(200, resp) } From 0228b0fd3afb2237d5980cd6b7b10cdc9ea416bb Mon Sep 17 00:00:00 2001 From: yodigos Date: Thu, 1 Aug 2024 10:11:20 +0300 Subject: [PATCH 10/15] Memory optimization when querying all Services --- frontend/endpoints/destinations.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index f6f161439..aea5c4375 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,13 +93,6 @@ func GetDestinationTypes(c *gin.Context) { }) } - //TODO - remove before merge - //potentialDestinations, err := findPotentialDestinations(c) - //if err != nil { - // return - //} - //fmt.Printf("Potential destinations: %v\n", potentialDestinations) - c.JSON(200, resp) } From 8e0113890edd18a323fe1678d9300aa95bb1a5dc Mon Sep 17 00:00:00 2001 From: yodigos Date: Thu, 1 Aug 2024 10:14:05 +0300 Subject: [PATCH 11/15] Memory optimization when querying all Services --- frontend/endpoints/destination_recognition/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go index b0618b04d..11484d386 100644 --- a/frontend/endpoints/destination_recognition/utils.go +++ b/frontend/endpoints/destination_recognition/utils.go @@ -30,7 +30,7 @@ func getAllHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) ([]v return helmManagedServices, nil } -// isHelmManagedService checks if a Pod was created by Helm +// isHelmManagedService checks if a Service was created by Helm func isHelmManagedService(service v1.Service) bool { annotations := service.GetAnnotations() labels := service.GetLabels() From ffe637bf74573b97d79766af44a04bf0fc1378ce Mon Sep 17 00:00:00 2001 From: yodigos Date: Thu, 1 Aug 2024 16:53:16 +0300 Subject: [PATCH 12/15] Elasticsearch Destination Finder --- .../destination_finder.go | 9 +++-- .../destination_recognition/elasticsearch.go | 35 +++++++++++++++++++ frontend/endpoints/destinations.go | 6 ++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 frontend/endpoints/destination_recognition/elasticsearch.go diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index 607c53549..6ca45ed6d 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -8,10 +8,11 @@ import ( type DestinationType string const ( - JaegerDestinationType DestinationType = "jaeger" + JaegerDestinationType DestinationType = "jaeger" + ElasticSearchDestinationType DestinationType = "elasticsearch" ) -var SupportedDestinationType = []DestinationType{JaegerDestinationType} +var SupportedDestinationType = []DestinationType{JaegerDestinationType, ElasticSearchDestinationType} type DestinationDetails struct { Name string `json:"name"` @@ -62,6 +63,10 @@ func getDestinationFinder(destinationType DestinationType) *DestinationFinder { return &DestinationFinder{ destinationFinder: &JaegerDestinationFinder{}, } + case ElasticSearchDestinationType: + return &DestinationFinder{ + destinationFinder: &ElasticSearchDestinationFinder{}, + } } return nil diff --git a/frontend/endpoints/destination_recognition/elasticsearch.go b/frontend/endpoints/destination_recognition/elasticsearch.go new file mode 100644 index 000000000..baf9505a0 --- /dev/null +++ b/frontend/endpoints/destination_recognition/elasticsearch.go @@ -0,0 +1,35 @@ +package destination_recognition + +import ( + "fmt" + "github.com/odigos-io/odigos/common" + k8s "k8s.io/api/core/v1" + "strings" +) + +type ElasticSearchDestinationFinder struct{} + +const ElasticSearchHttpPort int32 = 9200 +const ElasticSearchHttpUrlFormat = "https://%s.%s:%d" + +func (j *ElasticSearchDestinationFinder) isPotentialService(service k8s.Service) bool { + for _, port := range service.Spec.Ports { + if isElasticSearchService(port.Port, service.Name) { + return true + } + } + + return false +} + +func isElasticSearchService(portNumber int32, name string) bool { + return portNumber == ElasticSearchHttpPort && strings.Contains(name, string(common.ElasticsearchDestinationType)) +} + +func (j *ElasticSearchDestinationFinder) fetchDestinationDetails(service k8s.Service) DestinationDetails { + urlString := fmt.Sprintf(ElasticSearchHttpUrlFormat, service.Name, service.Namespace, ElasticSearchHttpPort) + return DestinationDetails{ + Name: string(common.ElasticsearchDestinationType), + UrlString: urlString, + } +} diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index aea5c4375..e1de8c8d1 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,6 +93,12 @@ func GetDestinationTypes(c *gin.Context) { }) } + potentialDestinations, err := findPotentialDestinations(c) + if err != nil { + return + } + fmt.Printf("Potential destinations: %v\n", potentialDestinations) + c.JSON(200, resp) } From 5be37cd42d0d4612fc08d4b4f8075300c391182b Mon Sep 17 00:00:00 2001 From: yodigos Date: Thu, 1 Aug 2024 17:07:34 +0300 Subject: [PATCH 13/15] Elasticsearch Destination Finder --- frontend/endpoints/destinations.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/frontend/endpoints/destinations.go b/frontend/endpoints/destinations.go index e1de8c8d1..aea5c4375 100644 --- a/frontend/endpoints/destinations.go +++ b/frontend/endpoints/destinations.go @@ -93,12 +93,6 @@ func GetDestinationTypes(c *gin.Context) { }) } - potentialDestinations, err := findPotentialDestinations(c) - if err != nil { - return - } - fmt.Printf("Potential destinations: %v\n", potentialDestinations) - c.JSON(200, resp) } From 982a85f43b4ec9bf23c5d4c05aef450a3e184cb4 Mon Sep 17 00:00:00 2001 From: yodigos Date: Sun, 4 Aug 2024 10:33:58 +0300 Subject: [PATCH 14/15] Removed Helm --- .../destination_finder.go | 36 ++++++++++------ .../destination_recognition/utils.go | 42 ------------------- 2 files changed, 23 insertions(+), 55 deletions(-) delete mode 100644 frontend/endpoints/destination_recognition/utils.go diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index 6ca45ed6d..1a5604caf 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -2,7 +2,10 @@ package destination_recognition import ( "github.com/gin-gonic/gin" + "github.com/odigos-io/odigos/frontend/kube" + "github.com/odigos-io/odigos/k8sutils/pkg/client" k8s "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type DestinationType string @@ -37,21 +40,28 @@ func (d *DestinationFinder) fetchDestinationDetails(service k8s.Service) Destina } func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namespace) ([]DestinationDetails, error) { - helmManagedServices, err := getAllHelmManagedServices(ctx, namespaces) - if err != nil { - return nil, err - } - var destinationFinder *DestinationFinder var destinationDetails []DestinationDetails - for _, service := range helmManagedServices { - for _, destinationType := range SupportedDestinationType { - destinationFinder = getDestinationFinder(destinationType) - if destinationFinder.isPotentialService(service) { - destinationDetails = append(destinationDetails, destinationFinder.fetchDestinationDetails(service)) - break - } - } + var err error + + for _, ns := range namespaces { + err = client.ListWithPages(client.DefaultPageSize, kube.DefaultClient.CoreV1().Services(ns.Name).List, + ctx, metav1.ListOptions{}, func(services *k8s.ServiceList) error { + for _, service := range services.Items { + for _, destinationType := range SupportedDestinationType { + destinationFinder = getDestinationFinder(destinationType) + if destinationFinder.isPotentialService(service) { + destinationDetails = append(destinationDetails, destinationFinder.fetchDestinationDetails(service)) + break + } + } + } + return nil + }) + } + + if err != nil { + return nil, err } return destinationDetails, nil diff --git a/frontend/endpoints/destination_recognition/utils.go b/frontend/endpoints/destination_recognition/utils.go deleted file mode 100644 index 11484d386..000000000 --- a/frontend/endpoints/destination_recognition/utils.go +++ /dev/null @@ -1,42 +0,0 @@ -package destination_recognition - -import ( - "github.com/gin-gonic/gin" - "github.com/odigos-io/odigos/frontend/kube" - "github.com/odigos-io/odigos/k8sutils/pkg/client" - "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func getAllHelmManagedServices(ctx *gin.Context, namespaces []v1.Namespace) ([]v1.Service, error) { - var helmManagedServices []v1.Service - var err error - for _, ns := range namespaces { - err = client.ListWithPages(client.DefaultPageSize, kube.DefaultClient.CoreV1().Services(ns.Name).List, - ctx, metav1.ListOptions{}, func(services *v1.ServiceList) error { - for _, service := range services.Items { - if isHelmManagedService(service) { - helmManagedServices = append(helmManagedServices, service) - } - } - return nil - }) - } - - if err != nil { - return nil, err - } - - return helmManagedServices, nil -} - -// isHelmManagedService checks if a Service was created by Helm -func isHelmManagedService(service v1.Service) bool { - annotations := service.GetAnnotations() - labels := service.GetLabels() - - _, hasHelmReleaseName := annotations["meta.helm.sh/release-name"] - managedByHelm := labels["app.kubernetes.io/managed-by"] == "Helm" - - return hasHelmReleaseName && managedByHelm -} From 857a6ecd368b56bf17fa60137e4e9bad5f8aec19 Mon Sep 17 00:00:00 2001 From: yodigos Date: Sun, 4 Aug 2024 10:59:17 +0300 Subject: [PATCH 15/15] Removed Dest Type --- .../destination_finder.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/frontend/endpoints/destination_recognition/destination_finder.go b/frontend/endpoints/destination_recognition/destination_finder.go index 1a5604caf..4470d66e2 100644 --- a/frontend/endpoints/destination_recognition/destination_finder.go +++ b/frontend/endpoints/destination_recognition/destination_finder.go @@ -2,20 +2,14 @@ package destination_recognition import ( "github.com/gin-gonic/gin" + "github.com/odigos-io/odigos/common" "github.com/odigos-io/odigos/frontend/kube" "github.com/odigos-io/odigos/k8sutils/pkg/client" k8s "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type DestinationType string - -const ( - JaegerDestinationType DestinationType = "jaeger" - ElasticSearchDestinationType DestinationType = "elasticsearch" -) - -var SupportedDestinationType = []DestinationType{JaegerDestinationType, ElasticSearchDestinationType} +var SupportedDestinationType = []common.DestinationType{common.JaegerDestinationType, common.ElasticsearchDestinationType} type DestinationDetails struct { Name string `json:"name"` @@ -67,13 +61,13 @@ func GetAllPotentialDestinationDetails(ctx *gin.Context, namespaces []k8s.Namesp return destinationDetails, nil } -func getDestinationFinder(destinationType DestinationType) *DestinationFinder { +func getDestinationFinder(destinationType common.DestinationType) *DestinationFinder { switch destinationType { - case JaegerDestinationType: + case common.JaegerDestinationType: return &DestinationFinder{ destinationFinder: &JaegerDestinationFinder{}, } - case ElasticSearchDestinationType: + case common.ElasticsearchDestinationType: return &DestinationFinder{ destinationFinder: &ElasticSearchDestinationFinder{}, }