-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathnon_payload_binary_admission.go
More file actions
122 lines (107 loc) · 3.75 KB
/
non_payload_binary_admission.go
File metadata and controls
122 lines (107 loc) · 3.75 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package extensions
import (
"context"
"strings"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
testextensionv1 "github.com/openshift/origin/pkg/apis/testextension/v1"
)
const (
// ComponentAnnotation is the annotation on ImageStreamTags that advertise a non-payload extension.
// Value format: product-name-component-name.
ComponentAnnotation = "testextension.redhat.io/component"
// BinaryAnnotation is the annotation on ImageStreamTags that identifies the binary path
// and optional args. Value format: <binary-path>.gz [--argument=value]
BinaryAnnotation = "testextension.redhat.io/binary"
)
// PermitPattern represents one entry from TestExtensionAdmission spec.permit.
// Namespace and ImageStream may be "*" to match any.
// ImageStream is the ImageStream resource name only (tag is irrelevant for matching).
type PermitPattern struct {
Namespace string
ImageStream string
}
// ParsePermitPattern parses a permit string "namespace/imagestream" into a PermitPattern.
// Supports "*" for namespace and/or imagestream (e.g. "openshift/*", "*/*", "ns/stream").
func ParsePermitPattern(permit string) (PermitPattern, bool) {
parts := strings.SplitN(permit, "/", 2)
if len(parts) != 2 {
return PermitPattern{}, false
}
ns := strings.TrimSpace(parts[0])
is := strings.TrimSpace(parts[1])
if ns == "" || is == "" {
return PermitPattern{}, false
}
return PermitPattern{Namespace: ns, ImageStream: is}, true
}
// MatchesAnyPermit returns true if (namespace, imagestream) matches any of the patterns.
func MatchesAnyPermit(namespace, imagestream string, patterns []PermitPattern) bool {
for _, p := range patterns {
nsMatch := p.Namespace == "*" || p.Namespace == namespace
isMatch := p.ImageStream == "*" || p.ImageStream == imagestream
if nsMatch && isMatch {
return true
}
}
return false
}
// DiscoverNonPayloadBinaryAdmission checks for the TestExtensionAdmission CRD, lists instances, and builds the permit set
func DiscoverNonPayloadBinaryAdmission(ctx context.Context, config *rest.Config) ([]PermitPattern, error) {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
gv := testextensionv1.SchemeGroupVersion
_, err = discoveryClient.ServerResourcesForGroupVersion(gv.String())
if err != nil {
if isNotFound(err) {
logrus.Infof("TestExtensionAdmission CRD not found; will discover extension ImageStreamTags cluster-wide and report all as unpermitted")
return nil, nil
}
return nil, err
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
gvr := schema.GroupVersionResource{
Group: testextensionv1.GroupName,
Version: testextensionv1.Version,
Resource: "testextensionadmissions",
}
list, err := dynamicClient.Resource(gvr).List(ctx, metav1.ListOptions{})
if err != nil {
if isNotFound(err) {
return nil, nil
}
return nil, err
}
var patterns []PermitPattern
for _, item := range list.Items {
var admission testextensionv1.TestExtensionAdmission
err := runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, &admission)
if err != nil {
logrus.Warnf("Failed to convert TestExtensionAdmission: %v", err)
continue
}
for _, permitStr := range admission.Spec.Permit {
pat, ok := ParsePermitPattern(permitStr)
if !ok {
logrus.Warnf("Invalid permit pattern in TestExtensionAdmission %q: %q", admission.Name, permitStr)
continue
}
patterns = append(patterns, pat)
}
}
return patterns, nil
}
func isNotFound(err error) bool {
return apierrors.IsNotFound(err)
}