1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-07 00:17:13 +00:00
kyverno/pkg/webhooks/utils.go

90 lines
2.1 KiB
Go
Raw Normal View History

2019-06-18 11:47:45 -07:00
package webhooks
import (
2019-07-15 16:07:56 -07:00
"fmt"
2019-06-18 11:47:45 -07:00
"strings"
2019-06-19 14:05:23 -07:00
2019-07-19 12:47:20 -07:00
"github.com/golang/glog"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
2019-07-15 16:07:56 -07:00
"github.com/nirmata/kyverno/pkg/info"
2019-06-18 11:47:45 -07:00
)
2019-07-15 16:07:56 -07:00
const policyKind = "Policy"
func isAdmSuccesful(policyInfos []info.PolicyInfo) (bool, string) {
2019-07-15 16:07:56 -07:00
var admSuccess = true
var errMsgs []string
for _, pi := range policyInfos {
if !pi.IsSuccessful() {
admSuccess = false
errMsgs = append(errMsgs, fmt.Sprintf("\nPolicy %s failed with following rules", pi.Name))
// Get the error rules
errorRules := pi.ErrorRules()
errMsgs = append(errMsgs, errorRules)
}
}
return admSuccess, strings.Join(errMsgs, ";")
}
2019-07-23 00:55:45 -04:00
//ArrayFlags to store filterkinds
2019-06-18 11:47:45 -07: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 00:55:45 -04:00
//Set setter for array flags
2019-06-18 11:47:45 -07:00
func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
2019-06-19 14:05:23 -07:00
// extract the kinds that the policy rules apply to
func getApplicableKindsForPolicy(p *kyverno.Policy) []string {
2019-06-19 14:05:23 -07:00
kindsMap := map[string]interface{}{}
kinds := []string{}
// iterate over the rules an identify all kinds
2019-07-23 23:34:03 -04:00
// Matching
2019-06-19 14:05:23 -07:00
for _, rule := range p.Spec.Rules {
2019-07-23 23:34:03 -04:00
for _, k := range rule.MatchResources.Kinds {
2019-06-19 14:05:23 -07:00
kindsMap[k] = nil
}
2019-07-23 23:34:03 -04:00
// remove excluded ones
for _, k := range rule.ExcludeResources.Kinds {
if _, ok := kindsMap[k]; ok {
// delete kind
delete(kindsMap, k)
}
}
2019-06-19 14:05:23 -07:00
}
// get the kinds
for k := range kindsMap {
kinds = append(kinds, k)
}
return kinds
}
// Policy Reporting Modes
const (
2019-07-23 18:29:44 -04:00
BlockChanges = "enforce"
ReportViolation = "audit"
)
2019-07-16 15:53:14 -07:00
2019-07-18 10:22:20 -07:00
// 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 toBlock(pis []info.PolicyInfo) bool {
2019-07-16 15:53:14 -07:00
for _, pi := range pis {
if pi.ValidationFailureAction != ReportViolation {
glog.V(3).Infoln("ValidationFailureAction set to enforce, blocking resource ceation")
2019-07-16 15:53:14 -07:00
return true
}
}
glog.V(3).Infoln("ValidationFailureAction set to audit, allowing resource creation, reporting with violation")
2019-07-16 15:53:14 -07:00
return false
}