forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefaults.go
73 lines (65 loc) · 2.52 KB
/
defaults.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package v1
import (
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/apis/core"
)
func SetDefaults_ImagePolicyConfig(obj *ImagePolicyConfig) {
if obj == nil {
return
}
if len(obj.ResolveImages) == 0 {
obj.ResolveImages = Attempt
}
for i := range obj.ExecutionRules {
if len(obj.ExecutionRules[i].OnResources) == 0 {
obj.ExecutionRules[i].OnResources = []GroupResource{{Resource: "pods", Group: kapi.GroupName}}
}
}
if obj.ResolutionRules == nil {
obj.ResolutionRules = []ImageResolutionPolicyRule{
{TargetResource: GroupResource{Resource: "pods"}, LocalNames: true},
{TargetResource: GroupResource{Group: "build.openshift.io", Resource: "builds"}, LocalNames: true},
{TargetResource: GroupResource{Group: "batch", Resource: "jobs"}, LocalNames: true},
{TargetResource: GroupResource{Group: "extensions", Resource: "replicasets"}, LocalNames: true},
{TargetResource: GroupResource{Resource: "replicationcontrollers"}, LocalNames: true},
{TargetResource: GroupResource{Group: "apps", Resource: "deployments"}, LocalNames: true},
{TargetResource: GroupResource{Group: "extensions", Resource: "deployments"}, LocalNames: true},
{TargetResource: GroupResource{Group: "apps", Resource: "statefulsets"}, LocalNames: true},
{TargetResource: GroupResource{Group: "extensions", Resource: "daemonsets"}, LocalNames: true},
}
// default the resolution policy to the global default
for i := range obj.ResolutionRules {
if len(obj.ResolutionRules[i].Policy) != 0 {
continue
}
obj.ResolutionRules[i].Policy = DoNotAttempt
for _, rule := range obj.ExecutionRules {
if executionRuleCoversResource(rule, obj.ResolutionRules[i].TargetResource) {
obj.ResolutionRules[i].Policy = obj.ResolveImages
break
}
}
}
} else {
// default the resolution policy to the global default
for i := range obj.ResolutionRules {
if len(obj.ResolutionRules[i].Policy) != 0 {
continue
}
obj.ResolutionRules[i].Policy = obj.ResolveImages
}
}
}
func addDefaultingFuncs(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&ImagePolicyConfig{}, func(obj interface{}) { SetDefaults_ImagePolicyConfig(obj.(*ImagePolicyConfig)) })
return nil
}
// executionRuleCoversResource returns true if gr is covered by rule.
func executionRuleCoversResource(rule ImageExecutionPolicyRule, gr GroupResource) bool {
for _, target := range rule.OnResources {
if target.Group == gr.Group && (target.Resource == gr.Resource || target.Resource == "*") {
return true
}
}
return false
}