1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-29 10:55:05 +00:00

refactor: introduce source package in cli (#8226)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-03 23:54:53 +02:00 committed by GitHub
parent f8f9f4e628
commit 3e4ada64cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 133 additions and 50 deletions

View file

@ -17,6 +17,7 @@ import (
"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"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/source"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
@ -381,7 +382,7 @@ func (c *ApplyCommandConfig) loadPolicies(skipInvalidPolicies SkippedInvalidPoli
for _, policy := range c.PolicyPaths {
policyPaths := []string{policy}
isGit := common.IsGitSourcePath(policyPaths)
isGit := source.IsGit(policy)
if isGit {
gitSourceURL, err := url.Parse(policyPaths[0])

View file

@ -17,6 +17,7 @@ import (
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
annotationsutils "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/annotations"
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"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/background/generate"
@ -78,7 +79,7 @@ func GetPolicies(paths []string) (policies []kyvernov1.PolicyInterface, validati
err error
)
isHTTPPath := IsHTTPRegex.MatchString(path)
isHTTPPath := source.IsHttp(path)
// path clean and retrieving file info can be possible if it's not an HTTP URL
if !isHTTPPath {
@ -169,12 +170,6 @@ func GetPolicies(paths []string) (policies []kyvernov1.PolicyInterface, validati
return policies, validatingAdmissionPolicies, errors
}
// IsInputFromPipe - check if input is passed using pipe
func IsInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}
// RemoveDuplicateAndObjectVariables - remove duplicate variables
func RemoveDuplicateAndObjectVariables(matches [][]string) string {
var variableStr string
@ -251,7 +246,7 @@ func GetPoliciesFromPaths(fs billy.Filesystem, dirPath []string, isGit bool, pol
}
} else {
if len(dirPath) > 0 && dirPath[0] == "-" {
if IsInputFromPipe() {
if source.IsStdin() {
policyStr := ""
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
@ -294,7 +289,7 @@ func GetResourceAccordingToResourcePath(fs billy.Filesystem, resourcePaths []str
}
} else {
if len(resourcePaths) > 0 && resourcePaths[0] == "-" {
if IsInputFromPipe() {
if source.IsStdin() {
resourceStr := ""
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
@ -661,10 +656,6 @@ func GetUserInfoFromPath(fs billy.Filesystem, path string, isGit bool, policyRes
return *userInfo, nil
}
func IsGitSourcePath(policyPaths []string) bool {
return strings.Contains(policyPaths[0], "https://")
}
func GetGitBranchOrPolicyPaths(gitBranch, repoURL string, policyPaths []string) (string, string) {
var gitPathToYamls string
if gitBranch == "" {

View file

@ -118,38 +118,6 @@ func Test_NamespaceSelector(t *testing.T) {
}
}
func Test_IsGitSourcePath(t *testing.T) {
type TestCase struct {
path []string
actual bool
desired bool
}
testcases := []TestCase{
{
path: []string{"https://github.com/kyverno/policies/openshift/team-validate-ns-name/"},
desired: true,
},
{
path: []string{"/kyverno/policies/openshift/team-validate-ns-name/"},
desired: false,
},
{
path: []string{"https://bitbucket.org/kyverno/policies/openshift/team-validate-ns-name"},
desired: true,
},
{
path: []string{"https://anydomain.com/kyverno/policies/openshift/team-validate-ns-name"},
desired: true,
},
}
for _, tc := range testcases {
tc.actual = IsGitSourcePath(tc.path)
if tc.actual != tc.desired {
t.Errorf("%s is not a git URL", tc.path)
}
}
}
func Test_GetGitBranchOrPolicyPaths(t *testing.T) {
type TestCase struct {
gitBranch string

View file

@ -13,6 +13,7 @@ import (
"github.com/go-git/go-billy/v5"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/source"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
@ -246,7 +247,7 @@ func getFileBytes(path string) ([]byte, error) {
err error
)
if IsHTTPRegex.MatchString(path) {
if source.IsHttp(path) {
// We accept here that a random URL might be called based on user provided input.
req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, path, nil)
if err != nil {

View file

@ -6,6 +6,3 @@ import (
// RegexVariables represents regex for '{{}}'
var RegexVariables = regexp.MustCompile(`\{\{[^{}]*\}\}`)
// IsHTTPRegex represents regex for starts with http:// or https://
var IsHTTPRegex = regexp.MustCompile("^(http|https)://")

View file

@ -0,0 +1,5 @@
package source
func IsGit(in string) bool {
return IsHttp(in)
}

View file

@ -0,0 +1,38 @@
package source
import "testing"
func TestIsGit(t *testing.T) {
tests := []struct {
name string
in string
want bool
}{{
name: "empty",
in: "",
want: false,
}, {
name: "http",
in: "http://github.com/kyverno/policies",
want: true,
}, {
name: "https",
in: "https://github.com/kyverno/policies",
want: true,
}, {
name: "local path",
in: "/github.com/kyverno/policies",
want: false,
}, {
name: "local path",
in: "/https/kyverno/policies",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsGit(tt.in); got != tt.want {
t.Errorf("IsGit() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -0,0 +1,11 @@
package source
import (
"regexp"
)
var isHTTPRegex = regexp.MustCompile("^(http|https)://")
func IsHttp(in string) bool {
return isHTTPRegex.MatchString(in)
}

View file

@ -0,0 +1,38 @@
package source
import "testing"
func TestIsHttp(t *testing.T) {
tests := []struct {
name string
in string
want bool
}{{
name: "empty",
in: "",
want: false,
}, {
name: "http",
in: "http://github.com/kyverno/policies",
want: true,
}, {
name: "https",
in: "https://github.com/kyverno/policies",
want: true,
}, {
name: "local path",
in: "/github.com/kyverno/policies",
want: false,
}, {
name: "local path",
in: "/https/kyverno/policies",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsHttp(tt.in); got != tt.want {
t.Errorf("IsHttp() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -0,0 +1,13 @@
package source
import (
"os"
)
func IsStdin() bool {
fileInfo, err := os.Stdin.Stat()
if err != nil {
return false
}
return fileInfo.Mode()&os.ModeCharDevice == 0
}

View file

@ -0,0 +1,20 @@
package source
import "testing"
func TestIsStdin(t *testing.T) {
tests := []struct {
name string
want bool
}{{
name: "default",
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsStdin(); got != tt.want {
t.Errorf("IsInputFromPipe() = %v, want %v", got, tt.want)
}
})
}
}