Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Signed-off-by: asamonik <[email protected]>
  • Loading branch information
asamonik committed Aug 13, 2024
1 parent 8aec7ef commit 0db3b71
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
7 changes: 1 addition & 6 deletions keptn-gateway/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package main

import (
"fmt"
"keptn.sh/keptn-gateway/internal/gateway"
)

func main() {
gw := gateway.NewGateway()

err := gw.Server.ListenAndServe()
if err != nil {
panic(fmt.Sprintf("cannot start server: %s", err))
}
gw.Serve()
}
49 changes: 34 additions & 15 deletions keptn-gateway/internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"

_ "github.com/joho/godotenv/autoload"

Check failure on line 13 in keptn-gateway/internal/gateway/gateway.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`godotenv` is not a recognized word. (unrecognized-spelling)

Check failure on line 13 in keptn-gateway/internal/gateway/gateway.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`joho` is not a recognized word. (unrecognized-spelling)
Expand All @@ -19,7 +21,7 @@ type Gateway struct {
port int
logger *zap.Logger
client *kubernetes.Clientset
Server *http.Server
server *http.Server
}

func NewGateway() *Gateway {
Expand All @@ -28,12 +30,6 @@ func NewGateway() *Gateway {
port = 8080
}

NewGateway := &Gateway{
port: port,
logger: zap.Must(zap.NewDevelopment()),
}
defer NewGateway.logger.Sync()

// kubernetes clientset in cluster
/*
config, err := rest.InClusterConfig()
Expand All @@ -50,19 +46,42 @@ func NewGateway() *Gateway {
return nil
}

NewGateway.client, err = kubernetes.NewForConfig(config)
client, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}

// http server
NewGateway.Server = &http.Server{
Addr: fmt.Sprintf(":%d", NewGateway.port),
Handler: NewGateway.RegisterRoutes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
NewGateway := &Gateway{
port: port,
logger: zap.Must(zap.NewDevelopment()),
server: &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: RegisterRoutes(client),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
},
}
defer NewGateway.logger.Sync()

return NewGateway
}

func (gw *Gateway) Serve() error {
go func() {
gw.logger.Info("server starting")
if err := gw.server.ListenAndServe(); err != http.ErrServerClosed {
panic(err)
}
}()

c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

<-c // block until signal received

gw.logger.Info("shutting down")
gw.server.Shutdown(nil)

return nil
}
22 changes: 17 additions & 5 deletions keptn-gateway/internal/gateway/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ import (

"github.com/gin-contrib/cors"

Check failure on line 8 in keptn-gateway/internal/gateway/routes.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`cors` is not a recognized word. (unrecognized-spelling)
"github.com/gin-gonic/gin"

Check failure on line 9 in keptn-gateway/internal/gateway/routes.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`gonic` is not a recognized word. (unrecognized-spelling)
"go.uber.org/zap"

Check failure on line 10 in keptn-gateway/internal/gateway/routes.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`uber` is not a recognized word. (unrecognized-spelling)
"k8s.io/client-go/kubernetes"
)

func (s *Gateway) RegisterRoutes() http.Handler {
type RouteConfig struct {
client *kubernetes.Clientset
logger *zap.Logger
}

func RegisterRoutes(client *kubernetes.Clientset) http.Handler {
rc := RouteConfig{
client: client,
logger: zap.Must(zap.NewDevelopment()),
}

r := gin.Default()

r.Use(cors.New(cors.Config{

Check failure on line 27 in keptn-gateway/internal/gateway/routes.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`cors` is not a recognized word. (unrecognized-spelling)

Check failure on line 27 in keptn-gateway/internal/gateway/routes.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`cors` is not a recognized word. (unrecognized-spelling)
Expand All @@ -21,13 +33,13 @@ func (s *Gateway) RegisterRoutes() http.Handler {
MaxAge: 12 * time.Hour,
}))

r.GET("/namespace/:namespace/keptnapp/:keptnapp/resources", s.ResourcePanelHandler)
r.GET("/namespace/:namespace/keptnapp/:keptnapp/health", s.HealthHandler)
r.GET("/namespace/:namespace/keptnapp/:keptnapp/resources", rc.ResourcePanelHandler)
r.GET("/namespace/:namespace/keptnapp/:keptnapp/health", rc.HealthHandler)

return r
}

func (s *Gateway) ResourcePanelHandler(c *gin.Context) {
func (rc *RouteConfig) ResourcePanelHandler(c *gin.Context) {
namespace := c.Param("namespace")
keptnapp := c.Param("keptnapp")

Expand All @@ -38,7 +50,7 @@ func (s *Gateway) ResourcePanelHandler(c *gin.Context) {
c.JSON(http.StatusOK, resp)
}

func (s *Gateway) HealthHandler(c *gin.Context) {
func (rc *RouteConfig) HealthHandler(c *gin.Context) {
_ = c.Param("namespace")
_ = c.Param("keptnapp")

Expand Down

0 comments on commit 0db3b71

Please sign in to comment.