Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

fix: the merge cluster topologies function and add panic handler in the HTTP server #8

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions kontrol-service/engine/flow/merger.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package flow

import (
"encoding/json"
"github.com/samber/lo"
v1 "k8s.io/api/networking/v1"
"kardinal.kontrol-service/types/cluster_topology/resolved"
)

func MergeClusterTopologies(baseTopology resolved.ClusterTopology, clusterTopologies []resolved.ClusterTopology) *resolved.ClusterTopology {
mergedTopology := &resolved.ClusterTopology{
func MergeClusterTopologies(baseTopology resolved.ClusterTopology, clusterTopologies []resolved.ClusterTopology) (mergedTopology *resolved.ClusterTopology) {
mergedTopology = &resolved.ClusterTopology{
FlowID: "all",
Services: deepCopySlice(baseTopology.Services),
ServiceDependencies: deepCopySlice(baseTopology.ServiceDependencies),
Expand All @@ -19,11 +20,13 @@ func MergeClusterTopologies(baseTopology resolved.ClusterTopology, clusterTopolo
mergedTopology.Ingresses = append(mergedTopology.Ingresses, topology.Ingresses...)
}

mergedTopology.Services = lo.Uniq(mergedTopology.Services)
mergedTopology.ServiceDependencies = lo.Uniq(mergedTopology.ServiceDependencies)
//TODO improve the filtering method, we could implement the `Service.Equal` method to compare and filter the services
//TODO and inside this method we could use the k8s service marshall method (https://pkg.go.dev/k8s.io/api/core/v1#Service.Marsha) and also the same for other k8s fields
//TODO it should be faster
mergedTopology.Services = lo.UniqBy(mergedTopology.Services, MustGetMarshalledKey[*resolved.Service])
mergedTopology.ServiceDependencies = lo.UniqBy(mergedTopology.ServiceDependencies, MustGetMarshalledKey[resolved.ServiceDependency])
mergedTopology.Ingresses = foldAllIngress(mergedTopology.Ingresses)

// fmt.Printf("topology: %s\n", SPrintJSONClusterTopology(mergedTopology))
return mergedTopology
}

Expand All @@ -39,3 +42,11 @@ func foldAllIngress(ingresses []*resolved.Ingress) []*resolved.Ingress {
return &merged
})
}

func MustGetMarshalledKey[T any](resource T) string {
bytes, err := json.Marshal(resource)
lostbean marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic("Failed to marshal resource")
}
return string(bytes)
}
30 changes: 30 additions & 0 deletions kontrol-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"flag"
"net/http"
"os"
"strconv"

cli_api "github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api/api/golang/server"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -95,6 +97,34 @@ func startServer(isDevMode bool) {
},
}))

// Panic handler
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
defer func() {
if r := recover(); r != nil {

internalServerErrorResponse := cli_api.ErrorJSONResponse{
Error: "internal server error",
}

// Handle the panic and return a 500 error response
c.JSON(http.StatusInternalServerError, internalServerErrorResponse)
var debugMsg string
switch recoverErr := r.(type) {
case string:
debugMsg = recoverErr
case error:
debugMsg = recoverErr.Error()
default:
debugMsg = "recover didn't get error msg"
}
logrus.Errorf("HTTP server handle this internal panic: %s", debugMsg)
}
}()
return next(c)
}
})

server.RegisterExternalAndInternalApi(e)

// And we serve HTTP until the world ends.
Expand Down
Loading