This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
resource.go
101 lines (81 loc) · 2.55 KB
/
resource.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
package clusterrolebinding
import (
"reflect"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
apiv1 "k8s.io/api/rbac/v1"
"k8s.io/client-go/kubernetes"
)
const (
// Name is the identifier of the resource.
Name = "clusterrolebinding"
)
// Config represents the configuration used to create a new config map resource.
type Config struct {
K8sClient kubernetes.Interface
Logger micrologger.Logger
ClusterRoleGeneral string
ClusterRolePSP string
}
// Resource implements the config map resource.
type Resource struct {
k8sClient kubernetes.Interface
logger micrologger.Logger
clusterRoleGeneral string
clusterRolePSP string
}
// New creates a new configured config map resource.
func New(config Config) (*Resource, error) {
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.ClusterRoleGeneral == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.ClusterRoleGeneral must not be empty", config)
}
if config.ClusterRolePSP == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.ClusterRolePSP must not be empty", config)
}
newService := &Resource{
k8sClient: config.K8sClient,
logger: config.Logger,
clusterRoleGeneral: config.ClusterRoleGeneral,
clusterRolePSP: config.ClusterRolePSP,
}
return newService, nil
}
func (r *Resource) Name() string {
return Name
}
func containsClusterRoleBinding(list []*apiv1.ClusterRoleBinding, item *apiv1.ClusterRoleBinding) bool {
_, err := getClusterRoleBindingByName(list, item.Name)
if IsNotFound(err) {
return false
} else if err != nil {
return false
}
return true
}
func getClusterRoleBindingByName(list []*apiv1.ClusterRoleBinding, name string) (*apiv1.ClusterRoleBinding, error) {
for _, l := range list {
if l.Name == name {
return l, nil
}
}
return nil, microerror.Maskf(notFoundError, "cluster role binding '%s' not found", name)
}
func isClusterRoleBindingModified(a, b *apiv1.ClusterRoleBinding) bool {
return !reflect.DeepEqual(a.Subjects, b.Subjects) || !reflect.DeepEqual(a.RoleRef, b.RoleRef)
}
func toClusterRoleBindings(v interface{}) ([]*apiv1.ClusterRoleBinding, error) {
if v == nil {
return nil, nil
}
clusterRoleBindings, ok := v.([]*apiv1.ClusterRoleBinding)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", []*apiv1.ClusterRoleBinding{}, v)
}
return clusterRoleBindings, nil
}