diff --git a/pkg/client/listers/kyverno/v1alpha1/expansion_generated.go b/pkg/client/listers/kyverno/v1alpha1/expansion_generated.go new file mode 100644 index 0000000000..f37f2e4f76 --- /dev/null +++ b/pkg/client/listers/kyverno/v1alpha1/expansion_generated.go @@ -0,0 +1,101 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by lister-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "fmt" + + kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1" + v1alpha1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" +) + +// PolicyListerExpansion allows custom methods to be added to +// PolicyLister. +type PolicyListerExpansion interface { + GetPolicyForPolicyViolation(pv *kyverno.PolicyViolation) ([]*kyverno.Policy, error) + ListResources(selector labels.Selector) (ret []*v1alpha1.Policy, err error) +} + +// PolicyViolationListerExpansion allows custom methods to be added to +// PolicyViolationLister. +type PolicyViolationListerExpansion interface { + // List lists all PolicyViolations in the indexer with GVK. + // List lists all PolicyViolations in the indexer with GVK. + ListResources(selector labels.Selector) (ret []*v1alpha1.PolicyViolation, err error) +} + +//ListResources is a wrapper to List and adds the resource kind information +// as the lister is specific to a gvk we can harcode the values here +func (pvl *policyViolationLister) ListResources(selector labels.Selector) (ret []*v1alpha1.PolicyViolation, err error) { + policyviolations, err := pvl.List(selector) + for index := range policyviolations { + policyviolations[index].SetGroupVersionKind(kyverno.SchemeGroupVersion.WithKind("PolicyViolation")) + } + return policyviolations, nil +} + +//ListResources is a wrapper to List and adds the resource kind information +// as the lister is specific to a gvk we can harcode the values here +func (pl *policyLister) ListResources(selector labels.Selector) (ret []*v1alpha1.Policy, err error) { + policies, err := pl.List(selector) + for index := range policies { + policies[index].SetGroupVersionKind(kyverno.SchemeGroupVersion.WithKind("Policy")) + } + return policies, err +} + +func (pl *policyLister) GetPolicyForPolicyViolation(pv *kyverno.PolicyViolation) ([]*kyverno.Policy, error) { + if len(pv.Labels) == 0 { + return nil, fmt.Errorf("no Policy found for PolicyViolation %v because it has no labels", pv.Name) + } + + pList, err := pl.List(labels.Everything()) + if err != nil { + return nil, err + } + + var policies []*kyverno.Policy + for _, p := range pList { + policyLabelmap := map[string]string{"policy": p.Name} + + ls := &metav1.LabelSelector{} + err = metav1.Convert_Map_string_To_string_To_v1_LabelSelector(&policyLabelmap, ls, nil) + if err != nil { + return nil, fmt.Errorf("failed to generate label sector of Policy name %s: %v", p.Name, err) + } + selector, err := metav1.LabelSelectorAsSelector(ls) + if err != nil { + return nil, fmt.Errorf("invalid label selector: %v", err) + } + // If a policy with a nil or empty selector creeps in, it should match nothing, not everything. + if selector.Empty() || !selector.Matches(labels.Set(pv.Labels)) { + continue + } + policies = append(policies, p) + } + + if len(policies) == 0 { + return nil, fmt.Errorf("could not find Policy set for PolicyViolation %s with labels: %v", pv.Name, pv.Labels) + } + + return policies, nil + +} diff --git a/pkg/client/listers/kyverno/v1alpha1/policy.go b/pkg/client/listers/kyverno/v1alpha1/policy.go new file mode 100644 index 0000000000..c981855c33 --- /dev/null +++ b/pkg/client/listers/kyverno/v1alpha1/policy.go @@ -0,0 +1,65 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by lister-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + v1alpha1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/tools/cache" +) + +// PolicyLister helps list Policies. +type PolicyLister interface { + // List lists all Policies in the indexer. + List(selector labels.Selector) (ret []*v1alpha1.Policy, err error) + // Get retrieves the Policy from the index for a given name. + Get(name string) (*v1alpha1.Policy, error) + PolicyListerExpansion +} + +// policyLister implements the PolicyLister interface. +type policyLister struct { + indexer cache.Indexer +} + +// NewPolicyLister returns a new PolicyLister. +func NewPolicyLister(indexer cache.Indexer) PolicyLister { + return &policyLister{indexer: indexer} +} + +// List lists all Policies in the indexer. +func (s *policyLister) List(selector labels.Selector) (ret []*v1alpha1.Policy, err error) { + err = cache.ListAll(s.indexer, selector, func(m interface{}) { + ret = append(ret, m.(*v1alpha1.Policy)) + }) + return ret, err +} + +// Get retrieves the Policy from the index for a given name. +func (s *policyLister) Get(name string) (*v1alpha1.Policy, error) { + obj, exists, err := s.indexer.GetByKey(name) + if err != nil { + return nil, err + } + if !exists { + return nil, errors.NewNotFound(v1alpha1.Resource("policy"), name) + } + return obj.(*v1alpha1.Policy), nil +} diff --git a/pkg/client/listers/kyverno/v1alpha1/policyviolation.go b/pkg/client/listers/kyverno/v1alpha1/policyviolation.go new file mode 100644 index 0000000000..e93ec95228 --- /dev/null +++ b/pkg/client/listers/kyverno/v1alpha1/policyviolation.go @@ -0,0 +1,65 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by lister-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + v1alpha1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/tools/cache" +) + +// PolicyViolationLister helps list PolicyViolations. +type PolicyViolationLister interface { + // List lists all PolicyViolations in the indexer. + List(selector labels.Selector) (ret []*v1alpha1.PolicyViolation, err error) + // Get retrieves the PolicyViolation from the index for a given name. + Get(name string) (*v1alpha1.PolicyViolation, error) + PolicyViolationListerExpansion +} + +// policyViolationLister implements the PolicyViolationLister interface. +type policyViolationLister struct { + indexer cache.Indexer +} + +// NewPolicyViolationLister returns a new PolicyViolationLister. +func NewPolicyViolationLister(indexer cache.Indexer) PolicyViolationLister { + return &policyViolationLister{indexer: indexer} +} + +// List lists all PolicyViolations in the indexer. +func (s *policyViolationLister) List(selector labels.Selector) (ret []*v1alpha1.PolicyViolation, err error) { + err = cache.ListAll(s.indexer, selector, func(m interface{}) { + ret = append(ret, m.(*v1alpha1.PolicyViolation)) + }) + return ret, err +} + +// Get retrieves the PolicyViolation from the index for a given name. +func (s *policyViolationLister) Get(name string) (*v1alpha1.PolicyViolation, error) { + obj, exists, err := s.indexer.GetByKey(name) + if err != nil { + return nil, err + } + if !exists { + return nil, errors.NewNotFound(v1alpha1.Resource("policyviolation"), name) + } + return obj.(*v1alpha1.PolicyViolation), nil +}