1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/cmd/cli/kubectl-kyverno/store/store.go
Charles-Edouard Brétéché 584f841c1e
refactor: make CLI store non static (#9200)
* refactor: make CLI store non static

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* registry access

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* apply

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-12-19 22:45:53 +08:00

99 lines
2 KiB
Go

package store
import (
"github.com/kyverno/kyverno/pkg/registryclient"
)
type Context struct {
Policies []Policy `json:"policies"`
}
type Policy struct {
Name string `json:"name"`
Rules []Rule `json:"rules"`
}
type Rule struct {
Name string `json:"name"`
Values map[string]interface{} `json:"values"`
ForEachValues map[string][]interface{} `json:"foreachValues"`
}
type Store struct {
local bool
registryClient registryclient.Client
allowApiCalls bool
policies []Policy
foreachElement int
}
// SetLocal sets local (clusterless) execution for the CLI
func (s *Store) SetLocal(m bool) {
s.local = m
}
// IsLocal returns 'true' if the CLI is in local (clusterless) execution
func (s *Store) IsLocal() bool {
return s.local
}
func (s *Store) SetForEachElement(element int) {
s.foreachElement = element
}
func (s *Store) GetForeachElement() int {
return s.foreachElement
}
func (s *Store) SetRegistryAccess(access bool) {
if access {
s.registryClient = registryclient.NewOrDie(registryclient.WithLocalKeychain())
}
}
func (s *Store) GetRegistryAccess() bool {
return s.registryClient != nil
}
func (s *Store) GetRegistryClient() registryclient.Client {
return s.registryClient
}
func (s *Store) SetPolicies(p ...Policy) {
s.policies = p
}
func (s *Store) HasPolicies() bool {
return len(s.policies) != 0
}
func (s *Store) GetPolicy(policyName string) *Policy {
for _, policy := range s.policies {
if policy.Name == policyName {
return &policy
}
}
return nil
}
func (s *Store) GetPolicyRule(policyName string, ruleName string) *Rule {
for _, policy := range s.policies {
if policy.Name == policyName {
for _, rule := range policy.Rules {
switch ruleName {
case rule.Name, "autogen-" + rule.Name, "autogen-cronjob-" + rule.Name:
return &rule
}
}
}
}
return nil
}
func (s *Store) AllowApiCall(allow bool) {
s.allowApiCalls = allow
}
func (s *Store) IsApiCallAllowed() bool {
return s.allowApiCalls
}