1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-28 18:38:40 +00:00

refactor: cli packages structure (#8257)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-05 10:55:01 +02:00 committed by GitHub
parent 0ec76e7165
commit 0b33ae2b06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 87 additions and 133 deletions

View file

@ -12,9 +12,9 @@ import (
"github.com/go-git/go-billy/v5/memfs"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
cobrautils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/cobra"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
reportutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/report"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"

View file

@ -1,9 +1,9 @@
package apply
import (
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/table"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy/annotations"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/output/table"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
)

View file

@ -9,8 +9,8 @@ import (
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/oci"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/version"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/experimental"
cobrautils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/cobra"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/experimental"
"github.com/spf13/cobra"
)

View file

@ -6,7 +6,7 @@ import (
"text/template"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/create/templates"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
cobrautils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/cobra"
"github.com/spf13/cobra"
)

View file

@ -1,86 +0,0 @@
package api
import (
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Test struct {
Name string `json:"name"`
Policies []string `json:"policies"`
Resources []string `json:"resources"`
Variables string `json:"variables,omitempty"`
UserInfo string `json:"userinfo,omitempty"`
Results []TestResults `json:"results"`
Values *Values `json:"values,omitempty"`
}
type TestResults struct {
// Policy mentions the name of the policy.
Policy string `json:"policy"`
// Rule mentions the name of the rule in the policy.
// It's required in case policy is a kyverno policy.
// +optional
Rule string `json:"rule,omitempty"`
// IsValidatingAdmissionPolicy indicates if the policy is a validating admission policy.
// It's required in case policy is a validating admission policy.
// +optional
IsValidatingAdmissionPolicy bool `json:"isValidatingAdmissionPolicy,omitempty"`
// Result mentions the result that the user is expecting.
// Possible values are pass, fail and skip.
Result policyreportv1alpha2.PolicyResult `json:"result"`
// Status mentions the status that the user is expecting.
// Possible values are pass, fail and skip.
Status policyreportv1alpha2.PolicyResult `json:"status,omitempty"`
// Resource mentions the name of the resource on which the policy is to be applied.
Resource string `json:"resource,omitempty"`
// Resources gives us the list of resources on which the policy is going to be applied.
Resources []string `json:"resources"`
// Kind mentions the kind of the resource on which the policy is to be applied.
Kind string `json:"kind"`
// Namespace mentions the namespace of the policy which has namespace scope.
Namespace string `json:"namespace,omitempty"`
// PatchedResource takes a resource configuration file in yaml format from
// the user to compare it against the Kyverno mutated resource configuration.
PatchedResource string `json:"patchedResource,omitempty"`
// GeneratedResource takes a resource configuration file in yaml format from
// the user to compare it against the Kyverno generated resource configuration.
GeneratedResource string `json:"generatedResource,omitempty"`
// CloneSourceResource takes the resource configuration file in yaml format
// from the user which is meant to be cloned by the generate rule.
CloneSourceResource string `json:"cloneSourceResource,omitempty"`
}
type Policy struct {
Name string `json:"name"`
Resources []Resource `json:"resources"`
Rules []Rule `json:"rules"`
}
type Rule struct {
Name string `json:"name"`
Values map[string]interface{} `json:"values"`
ForeachValues map[string][]interface{} `json:"foreachValues"`
}
type Values struct {
Policies []Policy `json:"policies"`
GlobalValues map[string]string `json:"globalValues"`
NamespaceSelectors []NamespaceSelector `json:"namespaceSelector"`
Subresources []Subresource `json:"subresources"`
}
type Resource struct {
Name string `json:"name"`
Values map[string]interface{} `json:"values"`
}
type Subresource struct {
APIResource metav1.APIResource `json:"subresource"`
ParentResource metav1.APIResource `json:"parentResource"`
}
type NamespaceSelector struct {
Name string `json:"name"`
Labels map[string]string `json:"labels"`
}

View file

@ -7,11 +7,11 @@ import (
"github.com/go-git/go-billy/v5"
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/table"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/filter"
cobrautils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/cobra"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/color"
filterutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/filter"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/output/table"
reportutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/report"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
@ -80,7 +80,7 @@ func testCommandExecute(
return rc, sanitizederror.NewWithError("a directory is required", err)
}
// parse filter
filter, errors := filterutils.ParseFilter(testCase)
filter, errors := filter.ParseFilter(testCase)
if len(errors) > 0 {
fmt.Println()
fmt.Println("Filter errors:")

View file

@ -6,7 +6,7 @@ import (
"testing"
"github.com/go-git/go-billy/v5/memfs"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
"gotest.tools/assert"
"sigs.k8s.io/yaml"

View file

@ -12,7 +12,7 @@ import (
func getAndCompareResource(actualResource unstructured.Unstructured, fs billy.Filesystem, path string) (bool, error) {
expectedResource, err := resource.GetResourceFromPath(fs, path)
if err != nil {
return false, fmt.Errorf("Error: failed to load resources (%s)", err)
return false, fmt.Errorf("Error: failed to load resource (%s)", err)
}
unstructuredutils.FixupGenerateLabels(actualResource)
unstructuredutils.FixupGenerateLabels(*expectedResource)

View file

@ -7,9 +7,9 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/filter"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common"
filterutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/filter"
pathutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/path"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
@ -32,7 +32,7 @@ func applyPoliciesFromPath(
policyResourcePath string,
rc *resultCounts,
openApiManager openapi.Manager,
filter filterutils.Filter,
filter filter.Filter,
auditWarn bool,
) ([]api.TestResults, []engineapi.EngineResponse, error) {
engineResponses := make([]engineapi.EngineResponse, 0)

View file

@ -3,10 +3,14 @@ package table
import (
"os"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/color"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/color"
"github.com/lensesio/tableprinter"
)
func rowsLength(length int) bool {
return length > 10
}
func NewTablePrinter() *tableprinter.Printer {
printer := tableprinter.New(os.Stdout)
printer.BorderTop, printer.BorderBottom, printer.BorderLeft, printer.BorderRight = true, true, true, true
@ -16,8 +20,6 @@ func NewTablePrinter() *tableprinter.Printer {
printer.RowCharLimit = 300
printer.HeaderBgColor = color.HeaderBgColor
printer.HeaderFgColor = color.HeaderFgColor
printer.RowLengthTitle = func(rowsLength int) bool {
return rowsLength > 10
}
printer.RowLengthTitle = rowsLength
return printer
}

View file

@ -0,0 +1,42 @@
package table
import (
"testing"
)
func TestNewTablePrinter(t *testing.T) {
if got := NewTablePrinter(); got == nil {
t.Errorf("NewTablePrinter() return nill")
}
}
func Test_rowsLength(t *testing.T) {
tests := []struct {
name string
length int
want bool
}{{
name: "0",
length: 0,
want: false,
}, {
name: "10",
length: 10,
want: false,
}, {
name: "11",
length: 11,
want: true,
}, {
name: "20",
length: 20,
want: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := rowsLength(tt.length); got != tt.want {
t.Errorf("rowsLength() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/pkg/utils/wildcard"
)

View file

@ -5,7 +5,7 @@ import (
"reflect"
"testing"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
)
func Test_policy_Apply(t *testing.T) {
@ -313,9 +313,7 @@ func TestParseFilter(t *testing.T) {
errors.New("Invalid test-case-selector (foo=bar). Parameter can only be policy, rule or resource."),
errors.New("Invalid test-case-selector argument (policy). Parameter must be in the form `<key>=<value>`."),
},
},
// TODO: Add test cases.
}
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := ParseFilter(tt.in)

View file

@ -6,7 +6,7 @@ import (
"path/filepath"
"github.com/go-git/go-billy/v5"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"k8s.io/apimachinery/pkg/util/yaml"
)

View file

@ -6,7 +6,7 @@ import (
"testing"
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
)
func TestTestCases_Errors(t *testing.T) {
@ -196,9 +196,7 @@ func TestLoadTests(t *testing.T) {
},
}},
wantErr: false,
},
// TODO: Add test cases.
}
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := LoadTests(tt.dirPath, tt.fileName)

View file

@ -14,9 +14,9 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy/annotations"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/source"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"

View file

@ -4,8 +4,8 @@ import (
"testing"
"github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
"gotest.tools/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

View file

@ -9,8 +9,8 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"

View file

@ -2,7 +2,7 @@ package common
import (
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

View file

@ -1,7 +1,7 @@
package common
import (
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/pkg/clients/dclient"
"k8s.io/api/admissionregistration/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

View file

@ -8,10 +8,10 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/values"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/values"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/engine/variables/regex"
datautils "github.com/kyverno/kyverno/pkg/utils/data"

View file

@ -5,7 +5,7 @@ import (
"os"
"github.com/go-git/go-billy/v5"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"k8s.io/apimachinery/pkg/util/yaml"
)

View file

@ -6,7 +6,7 @@ import (
"testing"
"github.com/go-git/go-billy/v5"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
)
func Test_readFile(t *testing.T) {
@ -31,18 +31,18 @@ func Test_readFile(t *testing.T) {
wantErr: true,
}, {
name: "does not exist",
filepath: "../../testdata/values/doesnotexist",
filepath: "../testdata/values/doesnotexist",
want: nil,
wantErr: true,
}, {
name: "bad format",
filepath: "../../testdata/values/bad-format.yaml",
want: mustReadFile("../../testdata/values/bad-format.yaml"),
filepath: "../testdata/values/bad-format.yaml",
want: mustReadFile("../testdata/values/bad-format.yaml"),
wantErr: false,
}, {
name: "valid",
filepath: "../../testdata/values/valid.yaml",
want: mustReadFile("../../testdata/values/valid.yaml"),
filepath: "../testdata/values/valid.yaml",
want: mustReadFile("../testdata/values/valid.yaml"),
wantErr: false,
}}
for _, tt := range tests {
@ -73,17 +73,17 @@ func TestLoad(t *testing.T) {
wantErr: true,
}, {
name: "does not exist",
filepath: "../../testdata/values/doesnotexist",
filepath: "../testdata/values/doesnotexist",
want: nil,
wantErr: true,
}, {
name: "bad format",
filepath: "../../testdata/values/bad-format.yaml",
filepath: "../testdata/values/bad-format.yaml",
want: nil,
wantErr: true,
}, {
name: "valid",
filepath: "../../testdata/values/valid.yaml",
filepath: "../testdata/values/valid.yaml",
want: &api.Values{
NamespaceSelectors: []api.NamespaceSelector{{
Name: "test1",