forked from app-sre/deployment-validation-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
121 lines (99 loc) · 2.27 KB
/
helpers.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
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
package integration
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
internaltesting "github.com/app-sre/deployment-validation-operator/internal/testing"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func nameGenerator(pfx string) func() string {
i := 0
return func() string {
name := fmt.Sprintf("%s-%d", pfx, i)
i++
return name
}
}
func getClusterRBAC(group string) ([]client.Object, error) {
root, err := projectRoot()
if err != nil {
return nil, err
}
path := filepath.Join(root, "deploy", "openshift", "cluster-role.yaml")
role, err := internaltesting.LoadUnstructuredFromFile(path)
if err != nil {
return nil, fmt.Errorf("loading role: %w", err)
}
return []client.Object{
role,
&rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: role.GetName(),
},
Subjects: []rbacv1.Subject{
{
Kind: "Group",
Name: group,
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: role.GetName(),
},
},
}, nil
}
func getRBAC(namespace, group string) ([]client.Object, error) {
root, err := projectRoot()
if err != nil {
return nil, err
}
role, err := internaltesting.LoadUnstructuredFromFile(filepath.Join(root, "deploy", "openshift", "role.yaml"))
if err != nil {
return nil, fmt.Errorf("loading role: %w", err)
}
role.SetNamespace(namespace)
return []client.Object{
role,
&rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: role.GetName(),
Namespace: namespace,
},
Subjects: []rbacv1.Subject{
{
Kind: "Group",
Name: group,
},
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: role.GetName(),
},
},
}, nil
}
func projectRoot() (string, error) {
var buf bytes.Buffer
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Stdout = &buf
cmd.Stderr = io.Discard
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("determining top level directory from git: %w", errSetup)
}
return strings.TrimSpace(buf.String()), nil
}
var errSetup = errors.New("test setup failed")
func remove(path string) error {
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
return nil
}
return os.Remove(path)
}