1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/webhooks/utils.go

97 lines
2.4 KiB
Go
Raw Normal View History

2019-06-18 18:47:45 +00:00
package webhooks
import (
2019-07-15 23:07:56 +00:00
"fmt"
2019-06-18 18:47:45 +00:00
"strings"
2019-06-19 21:05:23 +00:00
2019-07-19 19:47:20 +00:00
"github.com/golang/glog"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
2019-08-24 01:34:23 +00:00
"github.com/nirmata/kyverno/pkg/engine"
2019-06-18 18:47:45 +00:00
)
2019-08-24 01:34:23 +00:00
func isResponseSuccesful(engineReponses []engine.EngineResponseNew) bool {
for _, er := range engineReponses {
if !er.IsSuccesful() {
return false
}
}
return true
}
// returns true -> if there is even one policy that blocks resource requst
// returns false -> if all the policies are meant to report only, we dont block resource request
func toBlockResource(engineReponses []engine.EngineResponseNew) bool {
for _, er := range engineReponses {
if er.PolicyResponse.ValidationFailureAction != ReportViolation {
glog.V(4).Infof("ValidationFailureAction set to enforce for policy %s , blocking resource ceation", er.PolicyResponse.Policy)
return true
}
}
glog.V(4).Infoln("ValidationFailureAction set to audit, allowing resource creation, reporting with violation")
return false
}
func getErrorMsg(engineReponses []engine.EngineResponseNew) string {
var str []string
for _, er := range engineReponses {
if !er.IsSuccesful() {
2019-08-30 08:08:54 +00:00
str = append(str, fmt.Sprintf("failed policy %s", er.PolicyResponse.Policy))
2019-08-24 01:34:23 +00:00
for _, rule := range er.PolicyResponse.Rules {
if !rule.Success {
str = append(str, rule.ToString())
}
}
}
}
return strings.Join(str, "\n")
}
2019-07-23 04:55:45 +00:00
//ArrayFlags to store filterkinds
2019-06-18 18:47:45 +00:00
type ArrayFlags []string
func (i *ArrayFlags) String() string {
var sb strings.Builder
for _, str := range *i {
sb.WriteString(str)
}
return sb.String()
}
2019-07-23 04:55:45 +00:00
//Set setter for array flags
2019-06-18 18:47:45 +00:00
func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
2019-06-19 21:05:23 +00:00
// extract the kinds that the policy rules apply to
func getApplicableKindsForPolicy(p *kyverno.ClusterPolicy) []string {
2019-06-19 21:05:23 +00:00
kinds := []string{}
// iterate over the rules an identify all kinds
2019-07-24 03:34:03 +00:00
// Matching
2019-06-19 21:05:23 +00:00
for _, rule := range p.Spec.Rules {
2019-07-24 03:34:03 +00:00
for _, k := range rule.MatchResources.Kinds {
2019-09-04 17:40:49 +00:00
kinds = append(kinds, k)
2019-07-24 03:34:03 +00:00
}
2019-06-19 21:05:23 +00:00
}
return kinds
}
// Policy Reporting Modes
const (
2019-07-23 22:29:44 +00:00
BlockChanges = "enforce"
ReportViolation = "audit"
)
2019-07-16 22:53:14 +00:00
2019-08-24 01:34:23 +00:00
func processResourceWithPatches(patch []byte, resource []byte) []byte {
if patch == nil {
return nil
}
2019-08-26 23:10:19 +00:00
glog.Info(string(resource))
2019-08-24 01:34:23 +00:00
resource, err := engine.ApplyPatchNew(resource, patch)
if err != nil {
2019-08-26 23:10:19 +00:00
glog.Errorf("failed to patch resource: %v", err)
2019-08-24 01:34:23 +00:00
return nil
}
return resource
}