Skip to content

Commit

Permalink
Fixes gin-mode issue, removes panic(err) from public-facing code paths (
Browse files Browse the repository at this point in the history
  • Loading branch information
amorey authored Sep 24, 2024
1 parent 57e9f08 commit a873f66
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ k8s_resource(
'kubetail-server',
port_forwards='4000:4000',
objects=[
'kubetail-server:serviceaccount',
'kubetail-server:clusterrole',
'kubetail-server:clusterrolebinding',
'kubetail-server:role',
'kubetail-server:rolebinding',
'kubetail-server:serviceaccount',
],
resource_deps=['kubetail-shared'],
)
Expand Down
2 changes: 1 addition & 1 deletion backend/agent/internal/services/logmetadata/logmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (s *LogMetadataService) newK8SClientset(ctx context.Context) kubernetes.Int

clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

return clientset
Expand Down
2 changes: 1 addition & 1 deletion backend/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Config struct {
Server struct {
Addr string `validate:"omitempty,hostname_port"`
BasePath string `mapstructure:"base-path"`
GinMode string `validate:"omitempty,oneof=debug release"`
GinMode string `mapstructure:"gin-mode" validate:"omitempty,oneof=debug release"`

// session options
Session struct {
Expand Down
7 changes: 4 additions & 3 deletions backend/common/k8shelpers/k8shelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"regexp"

zlog "github.com/rs/zerolog/log"
authv1 "k8s.io/api/authentication/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -39,7 +40,7 @@ type Options struct {
func MustConfigure(cfg *config.Config) *rest.Config {
k8sCfg, err := Configure(cfg)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}
return k8sCfg
}
Expand All @@ -58,7 +59,7 @@ func Configure(cfg *config.Config) (*rest.Config, error) {
}
return configureLocal(cfg.KubeConfig)
default:
panic(errors.New("not implemented"))
panic("not implemented")
}
}

Expand Down Expand Up @@ -125,7 +126,7 @@ func (s *HelperService) HasAccess(token string) (string, error) {

return getUsername(result.Status.User.Username), nil
default:
panic(errors.New("not implemented"))
panic("not implemented")
}
}

Expand Down
9 changes: 8 additions & 1 deletion backend/server/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package main

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
zlog "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -21,7 +23,7 @@ import (
type CLI struct {
Addr string `validate:"omitempty,hostname_port"`
Config string `validate:"omitempty,file"`
GinMode string `validate:"omitempty,oneof=debug release"`
GinMode string `validate:"omitempty,oneof=release debug"`
}

func main() {
Expand Down Expand Up @@ -61,6 +63,11 @@ func main() {
zlog.Fatal().Caller().Err(err).Send()
}

// set gin mode
fmt.Println(v.GetString("server.gin-mode"))
fmt.Println(cfg.Server.GinMode)
gin.SetMode(cfg.Server.GinMode)

// configure logger
config.ConfigureLogger(config.LoggerOptions{
Enabled: cfg.Server.Logging.Enabled,
Expand Down
8 changes: 4 additions & 4 deletions backend/server/graph/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"sort"
Expand All @@ -29,6 +28,7 @@ import (
"time"

"github.com/99designs/gqlgen/graphql/handler/transport"
zlog "github.com/rs/zerolog/log"
"github.com/sosodev/duration"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -613,12 +613,12 @@ func newLogRecordFromLogLine(logLine string) model.LogRecord {

parts := strings.SplitN(logLine, " ", 2)
if len(parts) != 2 {
panic(errors.New("log line timestamp not found"))
panic("log line timestamp not found")
}

ts, err := time.Parse(time.RFC3339Nano, parts[0])
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

return model.LogRecord{
Expand All @@ -645,7 +645,7 @@ func decodeTailCursor(input string) (*TailCursor, error) {
}
cursor := &TailCursor{}
if err = json.Unmarshal(decodedData, cursor); err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}
return cursor, nil
}
Expand Down
3 changes: 2 additions & 1 deletion backend/server/graph/model/models_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/99designs/gqlgen/graphql"
zlog "github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/timestamppb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -51,7 +52,7 @@ func MarshalStringMap(val map[string]string) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
err := json.NewEncoder(w).Encode(val)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}
})
}
Expand Down
8 changes: 5 additions & 3 deletions backend/server/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (
"context"
"slices"

grpcdispatcher "github.com/kubetail-org/grpc-dispatcher-go"
zlog "github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
dynamicFake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"

grpcdispatcher "github.com/kubetail-org/grpc-dispatcher-go"
)

// This file will not be regenerated automatically.
Expand Down Expand Up @@ -58,7 +60,7 @@ func (r *Resolver) K8SClientset(ctx context.Context) kubernetes.Interface {

clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

return clientset
Expand All @@ -81,7 +83,7 @@ func (r *Resolver) K8SDynamicClient(ctx context.Context) dynamic.Interface {

dynamicClient, err := dynamic.NewForConfig(cfg)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

return dynamicClient
Expand Down
3 changes: 1 addition & 2 deletions backend/server/internal/ginapp/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package ginapp

import (
"errors"
"net/http"
"time"

Expand Down Expand Up @@ -108,7 +107,7 @@ func (app *AuthHandlers) SessionGET(c *gin.Context) {
response["user"] = nil
}
default:
panic(errors.New("not implemented"))
panic("not implemented")
}

c.JSON(http.StatusOK, response)
Expand Down
3 changes: 2 additions & 1 deletion backend/server/internal/ginapp/ginapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/gorilla/csrf"
adapter "github.com/gwatts/gin-adapter"
grpcdispatcher "github.com/kubetail-org/grpc-dispatcher-go"
zlog "github.com/rs/zerolog/log"
"k8s.io/client-go/rest"

"github.com/kubetail-org/kubetail/backend/common/config"
Expand Down Expand Up @@ -101,7 +102,7 @@ func NewGinApp(cfg *config.Config) (*GinApp, error) {
// set basepath to cwd
basepathTmp, err := os.Getwd()
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}
basepath = basepathTmp
}
Expand Down
3 changes: 2 additions & 1 deletion backend/server/internal/ginapp/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/gin-gonic/gin"
zlog "github.com/rs/zerolog/log"
"k8s.io/client-go/rest"

grpcdispatcher "github.com/kubetail-org/grpc-dispatcher-go"
Expand All @@ -41,7 +42,7 @@ func (app *GraphQLHandlers) EndpointHandler(cfg *rest.Config, grpcDispatcher *gr
// init resolver
r, err := graph.NewResolver(cfg, grpcDispatcher, allowedNamespaces)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

csrfTestServer := http.NewServeMux()
Expand Down
2 changes: 1 addition & 1 deletion backend/server/internal/ginapp/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func mustLoadTemplatesWithFuncs(glob string) *template.Template {
// parse templates from a specified directory or pattern
parsedTemplates, err := tmpl.ParseGlob(glob)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

return parsedTemplates
Expand Down
5 changes: 3 additions & 2 deletions backend/server/internal/ginapp/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"

"github.com/gin-gonic/gin"
zlog "github.com/rs/zerolog/log"

"github.com/kubetail-org/kubetail/backend/common/config"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func (app *WebsiteHandlers) EndpointHandler(cfg *config.Config) gin.HandlerFunc
decoder := json.NewDecoder(manifestFile)
err = decoder.Decode(&manifest)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}

// define runtime config for react app
Expand All @@ -64,7 +65,7 @@ func (app *WebsiteHandlers) EndpointHandler(cfg *config.Config) gin.HandlerFunc

runtimeConfigBytes, err := json.Marshal(runtimeConfig)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}
runtimeConfigJS := template.JS(string(runtimeConfigBytes))

Expand Down
7 changes: 4 additions & 3 deletions backend/server/internal/k8shelpers/k8shelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"regexp"

zlog "github.com/rs/zerolog/log"
authv1 "k8s.io/api/authentication/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -45,7 +46,7 @@ type Options struct {
func MustConfigure(opts Options) *rest.Config {
cfg, err := configure(opts)
if err != nil {
panic(err)
zlog.Fatal().Err(err).Send()
}
return cfg
}
Expand All @@ -64,7 +65,7 @@ func configure(opts Options) (*rest.Config, error) {
}
return configureLocal(opts.KubeConfig)
default:
panic(errors.New("not implemented"))
panic("not implemented")
}
}

Expand Down Expand Up @@ -131,7 +132,7 @@ func (s *K8sHelperService) HasAccess(token string) (string, error) {

return getUsername(result.Status.User.Username), nil
default:
panic(errors.New("not implemented"))
panic("not implemented")
}
}

Expand Down
18 changes: 17 additions & 1 deletion hack/tilt/kubetail.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ data:
allowed-namespaces: []
server:
addr: :4000
gin-mode: debug
base-path: /
gin-mode: debug
session:
secret: REPLACEME
cookie:
Expand Down Expand Up @@ -183,6 +183,22 @@ spec:
configMap:
name: kubetail
---
kind: Service
apiVersion: v1
metadata:
name: kubetail-server
namespace: default
spec:
selector:
app.kubernetes.io/name: kubetail
app.kubernetes.io/component: server
ports:
- name: http
protocol: TCP
port: 4000
targetPort: http
appProtocol: http
---
kind: ServiceAccount
apiVersion: v1
metadata:
Expand Down

0 comments on commit a873f66

Please sign in to comment.