Skip to content

Commit

Permalink
refactor: reuse code moved in kyverno repo (#498)
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 authored Sep 21, 2023
1 parent c323703 commit da1586c
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 492 deletions.
3 changes: 2 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/go-logr/logr v1.2.4
github.com/kyverno/kyverno v1.11.0-beta.1
github.com/kyverno/kyverno v1.5.0-rc1.0.20230921213230-3c76cf511867
github.com/loopfz/gadgeto v0.11.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -137,6 +137,7 @@ require (
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-ldap/ldap/v3 v3.4.6 // indirect
Expand Down
10 changes: 6 additions & 4 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxm
github.com/go-critic/go-critic v0.5.6/go.mod h1:cVjj0DfqewQVIlIAGexPCaGaZDAqGE29PYDDADIVNEo=
github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -1039,8 +1041,8 @@ github.com/kyverno/go-jmespath v0.4.1-0.20230705123211-d067dc3d6613 h1:M0uOLuCAZ
github.com/kyverno/go-jmespath v0.4.1-0.20230705123211-d067dc3d6613/go.mod h1:yzDHaKovQy16rjN4kFnjF+IdNoN4p1ndw+va6+B8zUU=
github.com/kyverno/go-jmespath/internal/testify v1.5.2-0.20230630133209-945021c749d9 h1:lL311dF3a2aeNibJj8v+uhFU3XkvRHZmCtAdSPOrQYY=
github.com/kyverno/go-jmespath/internal/testify v1.5.2-0.20230630133209-945021c749d9/go.mod h1:XRxUGHIiCy1WYma1CdfdO1WOhIe8dLPTENaZr5D1ex4=
github.com/kyverno/kyverno v1.11.0-beta.1 h1:pICon1vz0CwIm3YVQL185q9kW0QM09Xu2JY/vh9wKyY=
github.com/kyverno/kyverno v1.11.0-beta.1/go.mod h1:xQfQwdPstbC//8ZSLz5ttZOLFMdsuGVgI7Y45DlYzBg=
github.com/kyverno/kyverno v1.5.0-rc1.0.20230921213230-3c76cf511867 h1:ew74ekVA0WR2Ndv34CgXwYglWzQ12if/fxgxvwVMxQY=
github.com/kyverno/kyverno v1.5.0-rc1.0.20230921213230-3c76cf511867/go.mod h1:xQfQwdPstbC//8ZSLz5ttZOLFMdsuGVgI7Y45DlYzBg=
github.com/ldez/gomoddirectives v0.2.1/go.mod h1:sGicqkRgBOg//JfpXwkB9Hj0X5RyJ7mlACM5B9f6Me4=
github.com/ldez/tagliatelle v0.2.0/go.mod h1:8s6WJQwEYHbKZDsp/LjArytKOG8qaMrKQQ3mFukHs88=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
Expand Down Expand Up @@ -1394,8 +1396,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
3 changes: 1 addition & 2 deletions backend/pkg/cluster/cm_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"errors"

"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/convert"
"github.com/kyverno/kyverno/pkg/clients/dclient"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
corev1 "k8s.io/api/core/v1"

"github.com/kyverno/playground/backend/pkg/resource/convert"
)

type clientBasedResolver struct {
Expand Down
39 changes: 39 additions & 0 deletions backend/pkg/exception/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package exception

import (
"fmt"

kyvernov2alpha1 "github.com/kyverno/kyverno/api/kyverno/v2alpha1"
kyvernov2beta1 "github.com/kyverno/kyverno/api/kyverno/v2beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/convert"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/loader"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var (
exceptionV1 = schema.GroupVersion(kyvernov2alpha1.GroupVersion).WithKind("PolicyException")
exceptionV2 = schema.GroupVersion(kyvernov2beta1.GroupVersion).WithKind("PolicyException")
)

func Load(l loader.Loader, content []byte) ([]*kyvernov2alpha1.PolicyException, error) {
untyped, err := resource.LoadResources(l, content)
if err != nil {
return nil, err
}
var exceptions []*kyvernov2alpha1.PolicyException
for _, object := range untyped {
gvk := object.GroupVersionKind()
switch gvk {
case exceptionV1, exceptionV2:
exception, err := convert.To[kyvernov2alpha1.PolicyException](object)
if err != nil {
return nil, err
}
exceptions = append(exceptions, exception)
default:
return nil, fmt.Errorf("policy exception type not supported %s", gvk)
}
}
return exceptions, nil
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package utils_test
package exception

import (
"os"
"testing"

"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/loader"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"

"github.com/kyverno/playground/backend/data"
"github.com/kyverno/playground/backend/pkg/resource/loader"
"github.com/kyverno/playground/backend/pkg/utils"
)

func Test_LoadPolicyExceptions(t *testing.T) {
func Test_Load(t *testing.T) {
tests := []struct {
name string
policies string
Expand Down Expand Up @@ -41,10 +40,10 @@ func Test_LoadPolicyExceptions(t *testing.T) {
),
)
require.NoError(t, err)
if res, err := utils.LoadPolicyExceptions(loader, bytes); (err != nil) != tt.wantErr {
t.Errorf("loader.LoadPolicyExceptions() error = %v, wantErr %v", err, tt.wantErr)
if res, err := Load(loader, bytes); (err != nil) != tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
} else if len(res) != tt.wantLoaded {
t.Errorf("loader.LoadPolicyExceptions() loaded amount = %v, wantLoaded %v", len(res), tt.wantLoaded)
t.Errorf("Load() loaded amount = %v, wantLoaded %v", len(res), tt.wantLoaded)
}
})
}
Expand Down
34 changes: 17 additions & 17 deletions backend/pkg/utils/policy.go → backend/pkg/policy/load.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package utils
package policy

import (
"fmt"

kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
kyvernov2beta1 "github.com/kyverno/kyverno/api/kyverno/v2beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/convert"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/loader"
"k8s.io/api/admissionregistration/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/kyverno/playground/backend/pkg/resource/loader"
)

var (
Expand All @@ -20,34 +20,34 @@ var (
vapV1alpha1 = v1alpha1.SchemeGroupVersion.WithKind("ValidatingAdmissionPolicy")
)

func LoadPolicies(l loader.Loader, content []byte) ([]kyvernov1.PolicyInterface, []v1alpha1.ValidatingAdmissionPolicy, error) {
untyped, err := loader.LoadResources(l, content)
func Load(l loader.Loader, content []byte) ([]kyvernov1.PolicyInterface, []v1alpha1.ValidatingAdmissionPolicy, error) {
untyped, err := resource.LoadResources(l, content)
if err != nil {
return nil, nil, err
}
var policies []kyvernov1.PolicyInterface
var vaps []v1alpha1.ValidatingAdmissionPolicy
for _, policy := range untyped {
gvk := policy.GroupVersionKind()
for _, object := range untyped {
gvk := object.GroupVersionKind()
switch gvk {
case policyV1, policyV2:
var typed kyvernov1.Policy
if err := runtime.DefaultUnstructuredConverter.FromUnstructuredWithValidation(policy.UnstructuredContent(), &typed, true); err != nil {
typed, err := convert.To[kyvernov1.Policy](object)
if err != nil {
return nil, nil, err
}
policies = append(policies, &typed)
policies = append(policies, typed)
case clusterPolicyV1, clusterPolicyV2:
var typed kyvernov1.ClusterPolicy
if err := runtime.DefaultUnstructuredConverter.FromUnstructuredWithValidation(policy.UnstructuredContent(), &typed, true); err != nil {
typed, err := convert.To[kyvernov1.ClusterPolicy](object)
if err != nil {
return nil, nil, err
}
policies = append(policies, &typed)
policies = append(policies, typed)
case vapV1alpha1:
var typed v1alpha1.ValidatingAdmissionPolicy
if err := runtime.DefaultUnstructuredConverter.FromUnstructuredWithValidation(policy.UnstructuredContent(), &typed, true); err != nil {
typed, err := convert.To[v1alpha1.ValidatingAdmissionPolicy](object)
if err != nil {
return nil, nil, err
}
vaps = append(vaps, typed)
vaps = append(vaps, *typed)
default:
return nil, nil, fmt.Errorf("policy type not supported %s", gvk)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package utils_test
package policy

import (
"os"
"testing"

"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/loader"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"

"github.com/kyverno/playground/backend/data"
"github.com/kyverno/playground/backend/pkg/resource/loader"
"github.com/kyverno/playground/backend/pkg/utils"
)

const (
Expand All @@ -21,7 +20,7 @@ const (
policyAndVap string = "../../testdata/policy-and-vap.yaml"
)

func Test_LoadPolicies(t *testing.T) {
func Test_Load(t *testing.T) {
tests := []struct {
name string
policies string
Expand Down Expand Up @@ -76,12 +75,12 @@ func Test_LoadPolicies(t *testing.T) {
),
)
require.NoError(t, err)
if policies, vaps, err := utils.LoadPolicies(loader, bytes); (err != nil) != tt.wantErr {
t.Errorf("loader.LoadPolicies() error = %v, wantErr %v", err, tt.wantErr)
if policies, vaps, err := Load(loader, bytes); (err != nil) != tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
} else if len(policies) != tt.wantPolicies {
t.Errorf("loader.LoadPolicies() loaded amount = %v, wantLoaded %v", len(policies), tt.wantPolicies)
t.Errorf("Load() loaded amount = %v, wantLoaded %v", len(policies), tt.wantPolicies)
} else if len(vaps) != tt.wantVaps {
t.Errorf("loader.LoadPolicies() loaded amount = %v, wantLoaded %v", len(vaps), tt.wantVaps)
t.Errorf("Load() loaded amount = %v, wantLoaded %v", len(vaps), tt.wantVaps)
}
})
}
Expand Down
18 changes: 0 additions & 18 deletions backend/pkg/resource/convert/convert.go

This file was deleted.

42 changes: 0 additions & 42 deletions backend/pkg/resource/convert/convert_test.go

This file was deleted.

36 changes: 0 additions & 36 deletions backend/pkg/resource/loader/helpers.go

This file was deleted.

Loading

0 comments on commit da1586c

Please sign in to comment.