1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-12 02:46:56 +00:00
kyverno/policycontroller/processPolicy.go

123 lines
3.3 KiB
Go
Raw Normal View History

2019-05-10 12:36:55 -07:00
package policycontroller
2019-05-09 22:26:22 -07:00
import (
"encoding/json"
"fmt"
types "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
2019-05-10 12:36:55 -07:00
event "github.com/nirmata/kube-policy/pkg/event"
2019-05-09 22:26:22 -07:00
"github.com/nirmata/kube-policy/pkg/policyengine/mutation"
2019-05-10 12:36:55 -07:00
policyviolation "github.com/nirmata/kube-policy/pkg/policyviolation"
2019-05-09 22:26:22 -07:00
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)
2019-05-10 12:36:55 -07:00
func (pc *PolicyController) runForPolicy(key string) {
policy, err := pc.getPolicyByKey(key)
2019-05-09 22:26:22 -07:00
if err != nil {
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s, err: %v", key, err))
return
}
if policy == nil {
2019-05-10 12:36:55 -07:00
pc.logger.Printf("Counld not find policy by key %s", key)
2019-05-09 22:26:22 -07:00
return
}
2019-05-10 12:36:55 -07:00
violations, events, err := pc.processPolicy(*policy)
2019-05-09 22:26:22 -07:00
if err != nil {
// add Error processing policy event
}
2019-05-10 12:36:55 -07:00
pc.logger.Printf("%v, %v", violations, events)
2019-05-09 22:26:22 -07:00
// TODO:
// create violations
2019-05-10 12:36:55 -07:00
// pc.violationBuilder.Add()
2019-05-09 22:26:22 -07:00
// create events
2019-05-10 12:36:55 -07:00
// pc.eventBuilder.Add()
2019-05-09 22:26:22 -07:00
}
// processPolicy process the policy to all the matched resources
2019-05-10 12:36:55 -07:00
func (pc *PolicyController) processPolicy(policy types.Policy) (
violations []policyviolation.Info, events []event.Info, err error) {
2019-05-09 22:26:22 -07:00
for _, rule := range policy.Spec.Rules {
2019-05-10 12:36:55 -07:00
resources, err := pc.filterResourceByRule(rule)
2019-05-09 22:26:22 -07:00
if err != nil {
2019-05-10 12:36:55 -07:00
pc.logger.Printf("Failed to filter resources by rule %s, err: %v\n", rule.Name, err)
2019-05-09 22:26:22 -07:00
}
for _, resource := range resources {
rawResource, err := json.Marshal(resource)
if err != nil {
2019-05-10 12:36:55 -07:00
pc.logger.Printf("Failed to marshal resources map to rule %s, err: %v\n", rule.Name, err)
2019-05-09 22:26:22 -07:00
continue
}
2019-05-10 12:36:55 -07:00
violation, eventInfos, err := pc.policyEngine.ProcessExisting(policy, rawResource)
2019-05-09 22:26:22 -07:00
if err != nil {
2019-05-10 12:36:55 -07:00
pc.logger.Printf("Failed to process rule %s, err: %v\n", rule.Name, err)
2019-05-09 22:26:22 -07:00
continue
}
violations = append(violations, violation...)
events = append(events, eventInfos...)
}
}
return violations, events, nil
}
2019-05-10 12:36:55 -07:00
func (pc *PolicyController) filterResourceByRule(rule types.PolicyRule) ([]runtime.Object, error) {
2019-05-09 22:26:22 -07:00
var targetResources []runtime.Object
// TODO: make this namespace all
var namespace = "default"
if err := rule.Validate(); err != nil {
return nil, fmt.Errorf("invalid rule detected: %s, err: %v", rule.Name, err)
}
// Get the resource list from kind
2019-05-10 12:36:55 -07:00
resources, err := pc.kubeClient.ListResource(rule.Resource.Kind, namespace)
2019-05-09 22:26:22 -07:00
if err != nil {
return nil, err
}
for _, resource := range resources {
// TODO:
rawResource, err := json.Marshal(resource)
// objKind := resource.GetObjectKind()
// codecFactory := serializer.NewCodecFactory(runtime.NewScheme())
// codecFactory.EncoderForVersion()
if err != nil {
2019-05-10 12:36:55 -07:00
pc.logger.Printf("failed to marshal object %v", resource)
2019-05-09 22:26:22 -07:00
continue
}
// filter the resource by name and label
if ok, _ := mutation.IsRuleApplicableToResource(rawResource, rule.Resource); ok {
targetResources = append(targetResources, resource)
}
}
return targetResources, nil
}
2019-05-10 12:36:55 -07:00
func (pc *PolicyController) getPolicyByKey(key string) (*types.Policy, error) {
2019-05-09 22:26:22 -07:00
// Create nil Selector to grab all the policies
selector := labels.NewSelector()
2019-05-10 12:36:55 -07:00
cachedPolicies, err := pc.policyLister.List(selector)
2019-05-09 22:26:22 -07:00
if err != nil {
return nil, err
}
for _, elem := range cachedPolicies {
if elem.Name == key {
return elem, nil
}
}
return nil, nil
}