Skip to content

Commit

Permalink
feat: add playground command
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Oct 12, 2023
1 parent bc9ee31 commit 0d83382
Show file tree
Hide file tree
Showing 37 changed files with 1,467 additions and 181 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ build: $(CLI_BIN) ## Build

.PHONY: build-wasm
build-wasm: fmt vet ## Build the wasm binary
@echo Build wasm binary... >&2
@GOOS=js GOARCH=wasm go build -o ./playground/assets/main.wasm -ldflags=$(LD_FLAGS) ./cmd/wasm/main.go

.PHONY: serve
serve: build-wasm ## Serve static files.
python3 -m http.server -d playground/ 8080
@echo Serve playground... >&2
@python3 -m http.server -d playground/ 8080

###########
# CODEGEN #
Expand Down Expand Up @@ -224,7 +226,7 @@ codegen-mkdocs: codegen-docs ## Generate mkdocs website

.PHONY: codegen-schema-openapi
codegen-schema-openapi: $(KIND) $(HELM) ## Generate openapi schemas (v2 and v3)
@echo Generate openapi schema... >&2
@echo Generate openapi schemas... >&2
@rm -rf ./schemas
@mkdir -p ./schemas/openapi/v2
@mkdir -p ./schemas/openapi/v3/apis/json.kyverno.io
Expand All @@ -237,15 +239,21 @@ codegen-schema-openapi: $(KIND) $(HELM) ## Generate openapi schemas (v2 and v3)

.PHONY: codegen-schema-json
codegen-schema-json: codegen-schema-openapi ## Generate json schemas
@echo Generate json schemas... >&2
@$(PIP) install openapi2jsonschema
@rm -rf ./schemas/json
@openapi2jsonschema ./schemas/openapi/v2/schema.json --kubernetes --stand-alone --expanded -o ./schemas/json

.PHONY: codegen-schema-all
codegen-schema-all: codegen-schema-openapi codegen-schema-json ## Generate openapi and json schemas

.PHONY: codegen-playground
codegen-playground: build-wasm ## Generate playground
@echo Generate playground... >&2
cp -r ./playground/* ./pkg/server/ui/dist

.PHONY: codegen
codegen: codegen-crds codegen-deepcopy codegen-register codegen-client codegen-docs codegen-mkdocs codegen-schema-all ## Rebuild all generated code and docs
codegen: codegen-crds codegen-deepcopy codegen-register codegen-client codegen-docs codegen-mkdocs codegen-schema-all codegen-playground ## Rebuild all generated code and docs

.PHONY: verify-codegen
verify-codegen: codegen ## Verify all generated code and docs are up to date
Expand Down
35 changes: 7 additions & 28 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright 2023 Undistro Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build js && wasm

package main
Expand All @@ -20,35 +6,28 @@ import (
"context"
"os/signal"
"syscall"
"time"

server "github.com/kyverno/kyverno-json/pkg/server/wasm"
"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/server"
"github.com/kyverno/kyverno-json/pkg/server/playground"
)

func main() {
// initialise gin framework
// gin.SetMode(c.ginFlags.mode)
// tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
gin.SetMode(gin.DebugMode)
// create server
server, err := server.New(true, true)
router, err := server.New(true, true)
if err != nil {
panic(err)
}
// register playground routes
if err := server.AddPlaygroundRoutes(); err != nil {
if err := playground.AddRoutes(router.Group(server.PlaygroundPrefix)); err != nil {
panic(err)
}
// run server
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
shutdown := server.Run(ctx)
server.RunWasm(ctx, router)
<-ctx.Done()
stop()
if shutdown != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := shutdown(ctx); err != nil {
panic(err)
}
}
}
27 changes: 27 additions & 0 deletions pkg/commands/playground/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package playground

import (
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
)

func Command(parents ...string) *cobra.Command {
var command options
cmd := &cobra.Command{
Use: "playground",
Short: "playground",
Long: "Serve playground",
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: command.Run,
}
// server flags
cmd.Flags().StringVar(&command.serverFlags.host, "server-host", "0.0.0.0", "server host")
cmd.Flags().IntVar(&command.serverFlags.port, "server-port", 8080, "server port")
// gin flags
cmd.Flags().StringVar(&command.ginFlags.mode, "gin-mode", gin.ReleaseMode, "gin run mode")
cmd.Flags().BoolVar(&command.ginFlags.log, "gin-log", true, "enable gin logger")
cmd.Flags().BoolVar(&command.ginFlags.cors, "gin-cors", true, "enable gin cors")
cmd.Flags().IntVar(&command.ginFlags.maxBodySize, "gin-max-body-size", 2*1024*1024, "gin max body size")
return cmd
}
60 changes: 60 additions & 0 deletions pkg/commands/playground/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package playground

import (
"context"
"os/signal"
"syscall"
"time"

"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/server"
"github.com/kyverno/kyverno-json/pkg/server/ui"
"github.com/loopfz/gadgeto/tonic"
"github.com/spf13/cobra"
)

type options struct {
serverFlags serverFlags
ginFlags ginFlags
}

type serverFlags struct {
host string
port int
}

type ginFlags struct {
mode string
log bool
cors bool
maxBodySize int
}

func (c *options) Run(_ *cobra.Command, _ []string) error {
// initialise gin framework
gin.SetMode(c.ginFlags.mode)
tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
// create router
router, err := server.New(c.ginFlags.log, c.ginFlags.cors)
if err != nil {
return err
}
// register api routes
if err := ui.AddRoutes(router); err != nil {
return err
}
// run server
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
shutdown := server.Run(ctx, router, c.serverFlags.host, c.serverFlags.port)
<-ctx.Done()
stop()
if shutdown != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := shutdown(ctx); err != nil {
return err
}
}
return nil
}
2 changes: 2 additions & 0 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/kyverno/kyverno-json/pkg/command"
"github.com/kyverno/kyverno-json/pkg/commands/docs"
"github.com/kyverno/kyverno-json/pkg/commands/jp"
"github.com/kyverno/kyverno-json/pkg/commands/playground"
"github.com/kyverno/kyverno-json/pkg/commands/scan"
"github.com/kyverno/kyverno-json/pkg/commands/serve"
"github.com/kyverno/kyverno-json/pkg/commands/version"
Expand All @@ -28,6 +29,7 @@ func RootCommand() *cobra.Command {
cmd.AddCommand(
docs.Command("kyverno-json"),
jp.Command("kyverno-json"),
playground.Command(),
scan.Command(),
serve.Command("kyverno-json"),
version.Command("kyverno-json"),
Expand Down
16 changes: 7 additions & 9 deletions pkg/commands/serve/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/client/clientset/versioned"
"github.com/kyverno/kyverno-json/pkg/server"
"github.com/kyverno/kyverno-json/pkg/server/api"
"github.com/kyverno/kyverno-json/pkg/server/scan"
restutils "github.com/kyverno/kyverno-json/pkg/utils/rest"
"github.com/loopfz/gadgeto/tonic"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,8 +42,8 @@ func (c *options) Run(_ *cobra.Command, _ []string) error {
// initialise gin framework
gin.SetMode(c.ginFlags.mode)
tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
// create server
server, err := server.New(c.ginFlags.log, c.ginFlags.cors)
// create router
router, err := server.New(c.ginFlags.log, c.ginFlags.cors)
if err != nil {
return err
}
Expand All @@ -55,19 +55,17 @@ func (c *options) Run(_ *cobra.Command, _ []string) error {
if err != nil {
return err
}
config := api.Configuration{
PolicyProvider: &provider{
client: client,
},
provider := &provider{
client: client,
}
// register api routes
if err := server.AddApiRoutes(config); err != nil {
if err := scan.AddRoutes(router.Group(server.ApiPrefix), provider); err != nil {
return err
}
// run server
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
shutdown := server.Run(ctx, c.serverFlags.host, c.serverFlags.port)
shutdown := server.Run(ctx, router, c.serverFlags.host, c.serverFlags.port)
<-ctx.Done()
stop()
if shutdown != nil {
Expand Down
9 changes: 0 additions & 9 deletions pkg/server/api/config.go

This file was deleted.

13 changes: 0 additions & 13 deletions pkg/server/api/routes.go

This file was deleted.

25 changes: 25 additions & 0 deletions pkg/server/linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build !js && !wasm

package server

import (
"context"
"fmt"
"net/http"
"time"
)

func Run(_ context.Context, s Server, host string, port int) Shutdown {
address := fmt.Sprintf("%v:%v", host, port)
srv := &http.Server{
Addr: address,
Handler: s.Handler(),
ReadHeaderTimeout: 3 * time.Second,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(err)
}
}()
return srv.Shutdown
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scan
package playground

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scan
package playground

type Request struct {
Payload string `json:"payload"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scan
package playground

import (
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
Expand Down
5 changes: 3 additions & 2 deletions pkg/server/playground/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package playground

import (
"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/server/playground/scan"
)

func AddRoutes(group *gin.RouterGroup) error {
if err := scan.AddRoutes(group); err != nil {
handler, err := newHandler()
if err != nil {
return err
}
group.POST("/scan", handler)
return nil
}
14 changes: 0 additions & 14 deletions pkg/server/playground/scan/routes.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0d83382

Please sign in to comment.