Skip to content

Commit

Permalink
feat: support kyverno version select
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
fjogeleit committed Dec 14, 2024
1 parent f07a51a commit 8961a1d
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ frontend/node_modules
frontend/.vite
frontend/dist
backend/coverage.out
backend/config.yaml
backend/dist
backend/pkg/server/ui/dist
backend/backend
Expand Down
9 changes: 7 additions & 2 deletions backend/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
version: 2

project_name: kyverno-playground

before:
hooks:
- go mod tidy
Expand All @@ -10,6 +14,7 @@ builds:
- windows
- darwin
binary: kyverno-playground
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath
ldflags:
Expand All @@ -22,7 +27,7 @@ kos:
- '{{ if not .Prerelease }}latest{{ end }}'
bare: true
preserve_import_paths: false
sbom: none
sbom: spdx
platforms:
- all
flags:
Expand Down Expand Up @@ -66,7 +71,7 @@ sboms:
artifacts: source

snapshot:
name_template: '{{ incpatch .Version }}-next'
version_template: '{{ incpatch .Version }}-next'

release:
prerelease: auto
Expand Down
7 changes: 7 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ require (
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/go-logr/logr v1.4.2
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/file v1.1.2
github.com/knadh/koanf/v2 v2.1.2
github.com/kyverno/kyverno v1.12.6
github.com/loopfz/gadgeto v0.11.4
github.com/spf13/cobra v1.8.1
Expand Down Expand Up @@ -155,6 +158,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down Expand Up @@ -205,6 +209,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/kyverno/go-jmespath v0.4.1-0.20231124160150-95e59c162877 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand All @@ -214,9 +219,11 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
15 changes: 14 additions & 1 deletion backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEe
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
Expand Down Expand Up @@ -555,6 +557,14 @@ github.com/klauspost/compress v1.17.5/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6K
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w=
github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/v2 v2.1.2 h1:I2rtLRqXRy1p01m/utEtpZSSA6dcJbgGVuE27kW2PzQ=
github.com/knadh/koanf/v2 v2.1.2/go.mod h1:Gphfaen0q1Fc1HTgJgSTC4oRX9R2R5ErYMZJy8fLJBo=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down Expand Up @@ -596,12 +606,16 @@ github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60
github.com/miekg/pkcs11 v1.0.3-0.20190429190417-a667d056470f/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -660,7 +674,6 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
Expand Down
13 changes: 13 additions & 0 deletions backend/pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"k8s.io/client-go/tools/clientcmd"

"github.com/kyverno/playground/backend/pkg/cluster"
"github.com/kyverno/playground/backend/pkg/config"
"github.com/kyverno/playground/backend/pkg/engine"
"github.com/kyverno/playground/backend/pkg/server"
"github.com/kyverno/playground/backend/pkg/server/api"
apiconfig "github.com/kyverno/playground/backend/pkg/server/api/config"
"github.com/kyverno/playground/backend/pkg/utils"
)

Expand All @@ -25,6 +27,7 @@ type commandFlags struct {
uiFlags uiFlags
engineFlags engineFlags
clusterFlags clusterFlags
configFile string
}

type serverFlags struct {
Expand Down Expand Up @@ -58,6 +61,7 @@ func NewRootCommand() *cobra.Command {
res := &cobra.Command{
RunE: command.Run,
}
res.Flags().StringVar(&command.configFile, "config", "", "path to an optional config file")
// server flags
res.Flags().StringVar(&command.serverFlags.host, "server-host", "0.0.0.0", "server host")
res.Flags().IntVar(&command.serverFlags.port, "server-port", 8080, "server port")
Expand All @@ -78,6 +82,12 @@ func NewRootCommand() *cobra.Command {
}

func (c *commandFlags) Run(_ *cobra.Command, _ []string) error {
cfg := &config.Config{}
err := config.Load(cfg, c.configFile)
if err != nil {
return err
}

// initialise gin framework
gin.SetMode(c.ginFlags.mode)
tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
Expand Down Expand Up @@ -111,6 +121,9 @@ func (c *commandFlags) Run(_ *cobra.Command, _ []string) error {
BuiltInCrds: c.engineFlags.builtInCrds,
LocalCrds: c.engineFlags.localCrds,
},
Versions: utils.Map(cfg.Versions, func(version config.Version) apiconfig.Version {
return apiconfig.Version(version)
}),
}
// register API routes (with/without cluster support)
if c.clusterFlags.cluster {
Expand Down
10 changes: 10 additions & 0 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

type Version struct {
Name string `koanf:"name"`
URL string `koanf:"url"`
}

type Config struct {
Versions []Version `koanf:"versions"`
}
34 changes: 34 additions & 0 deletions backend/pkg/config/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package config

import (
"errors"
"fmt"
"os"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
)

func Load(c *Config, cfgFile string) error {
if cfgFile == "" {
return nil
}

k := koanf.New("!")
if _, err := os.Stat(cfgFile); errors.Is(err, os.ErrNotExist) {
fmt.Printf("[INFO] No config file found")
return nil
}

if err := k.Load(file.Provider(cfgFile), yaml.Parser()); err != nil {
fmt.Printf("[ERROR] failed to load config file: %v\n", err)
}

err := k.Unmarshal("", c)
if err != nil {
return err
}

return err
}
17 changes: 12 additions & 5 deletions backend/pkg/server/api/config/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ import (
"github.com/kyverno/playground/backend/pkg/cluster"
)

type Version struct {
Name string `json:"name"`
URL string `json:"url"`
}

type ConfigResponse struct {
Cluster bool `json:"cluster"`
Sponsor string `json:"sponsor"`
Cluster bool `json:"cluster"`
Sponsor string `json:"sponsor"`
Versions []Version `json:"versions"`
}

func AddRoutes(group *gin.RouterGroup, cluster cluster.Cluster, sponsor string) error {
func AddRoutes(group *gin.RouterGroup, cluster cluster.Cluster, sponsor string, versions []Version) error {
group.GET("/config", func(c *gin.Context) {
c.JSON(http.StatusOK, ConfigResponse{
Cluster: cluster != nil && !cluster.IsFake(),
Sponsor: sponsor,
Cluster: cluster != nil && !cluster.IsFake(),
Sponsor: sponsor,
Versions: versions,
})
})
return nil
Expand Down
5 changes: 3 additions & 2 deletions backend/pkg/server/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ type EngineConfiguration = apiengine.APIConfiguration

type APIConfiguration struct {
EngineConfiguration
Sponsor string
Versions []apiconfig.Version
Sponsor string
}

func AddRoutes(group *gin.RouterGroup, cluster cluster.Cluster, config APIConfiguration) error {
if err := apiconfig.AddRoutes(group, cluster, config.Sponsor); err != nil {
if err := apiconfig.AddRoutes(group, cluster, config.Sponsor, config.Versions); err != nil {
return err
}
if err := apiengine.AddRoutes(group, cluster, config.EngineConfiguration); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions backend/pkg/utils/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

func Map[T any, R any](source []T, cb func(T) R) []R {
list := make([]R, 0, len(source))
for _, i := range source {
list = append(list, cb(i))
}

return list
}
1 change: 1 addition & 0 deletions charts/kyverno-playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ helm install kyverno-playground --namespace kyverno --create-namespace kyverno-p
| config.ui.sponsor | string | `""` | Sponsor name |
| config.engine.builtinCrds | list | `[]` | Builtin CRDs enabled (`argocd`, `cert-manager`, `prometheus-operator`, `tekton-pipelines`) |
| config.engine.localCrds | list | `[]` | Paths to folders containing yaml definitions for CRDs |
| config.version | list | `[]` | list of additional Kyverno Playground versions |

## Source Code

Expand Down
4 changes: 4 additions & 0 deletions charts/kyverno-playground/config/config.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{- with .Values.config.versions }}
versions:
{{- toYaml . | nindent 4 }}
{{- end }}
8 changes: 8 additions & 0 deletions charts/kyverno-playground/templates/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "kyverno-playground.fullname" . }}-config
labels:
{{- include "kyverno-playground.labels" . | nindent 4 }}
data:
config.yaml: {{ tpl (.Files.Get "config/config.tmpl") . }}
8 changes: 8 additions & 0 deletions charts/kyverno-playground/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ spec:
containers:
- name: {{ .Chart.Name }}
args:
- --config=/config.yaml
- --gin-mode={{ .Values.config.gin.mode }}
- --gin-cors={{ .Values.config.gin.cors }}
- --gin-log={{ .Values.config.gin.logger }}
Expand Down Expand Up @@ -69,11 +70,18 @@ spec:
volumeMounts:
- mountPath: {{ .Values.tufRootMountPath }}
name: sigstore
- mountPath: /config.yaml
name: config-file
readOnly: true
subPath: config.yaml
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumes:
- name: sigstore
emptyDir: {}
- name: config-file
configMap:
name: {{ include "kyverno-playground.fullname" . }}-config
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand Down
6 changes: 6 additions & 0 deletions charts/kyverno-playground/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,9 @@ config:
builtinCrds: []
# -- Paths to folders containing yaml definitions for CRDs
localCrds: []
# -- list of additional Kyverno Playground versions
version: []
# - name: Kyverno v1.13
# url: https://playground.kyverno.io/
# - name: Kyverno v1.12
# url: https://playground.kyverno.io/v1.12/
Loading

0 comments on commit 8961a1d

Please sign in to comment.