/
common.go
168 lines (135 loc) · 3.84 KB
/
common.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package organization
import (
"context"
"sort"
"github.com/giantswarm/microerror"
securityv1alpha1 "github.com/giantswarm/organization-operator/api/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (s *Service) Get(ctx context.Context, options GetOptions) (Resource, error) {
var resource Resource
var err error
if len(options.Name) > 0 {
resource, err = s.getByName(ctx, options.Name)
if err != nil {
return nil, microerror.Mask(err)
}
} else {
resource, err = s.getAll(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
}
return resource, nil
}
func (s *Service) getByName(ctx context.Context, name string) (Resource, error) {
var err error
o := &Organization{}
{
org := &securityv1alpha1.Organization{}
err = s.client.CtrlClient().Get(ctx, client.ObjectKey{
Name: name,
Namespace: metav1.NamespaceNone,
}, org)
if apierrors.IsNotFound(err) {
return nil, microerror.Mask(notFoundError)
} else if err != nil {
return nil, microerror.Mask(err)
}
o.Organization = omitManagedFields(org)
o.Organization.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: securityv1alpha1.SchemeGroupVersion.Group,
Version: securityv1alpha1.SchemeGroupVersion.Version,
Kind: "Organization",
})
}
return o, nil
}
func (s *Service) getAll(ctx context.Context) (Resource, error) {
accessReview, err := s.getSelfSubjectAccessReview(ctx)
if err != nil {
return nil, microerror.Mask(err)
} else if accessReview.Status.Allowed {
return s.getAllCRs(ctx)
} else {
return s.getAllPermittedOrgResources(ctx)
}
}
func (s *Service) getAllCRs(ctx context.Context) (Resource, error) {
var err error
orgCollection := &Collection{}
{
orgs := &securityv1alpha1.OrganizationList{}
{
err = s.client.CtrlClient().List(ctx, orgs)
if err != nil {
return nil, microerror.Mask(err)
} else if len(orgs.Items) == 0 {
return nil, microerror.Mask(noResourcesError)
}
}
for _, org := range orgs.Items {
o := Organization{
Organization: org.DeepCopy(),
}
o.Organization.ManagedFields = nil
orgCollection.Items = append(orgCollection.Items, o)
}
}
return orgCollection, nil
}
func (s *Service) getAllPermittedOrgResources(ctx context.Context) (Resource, error) {
rulesReview, err := s.getSelfSubjectRulesReview(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
contains := func(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}
var orgNames []string
for _, rule := range rulesReview.Status.ResourceRules {
if contains(rule.Verbs, "get") && contains(rule.Resources, "organizations") {
orgNames = append(orgNames, rule.ResourceNames...)
}
}
if len(orgNames) == 0 {
return nil, microerror.Mask(noResourcesError)
}
sort.Strings(orgNames)
return s.getByNames(ctx, orgNames)
}
func (s *Service) getByNames(ctx context.Context, names []string) (*Collection, error) {
orgCollection := &Collection{}
for _, name := range names {
resource, err := s.getByName(ctx, name)
if err != nil {
return nil, microerror.Mask(err)
}
org := resource.(*Organization)
if org == nil {
continue
}
o := Organization{
Organization: org.Organization.DeepCopy(),
}
orgCollection.Items = append(orgCollection.Items, o)
}
if len(orgCollection.Items) == 0 {
return nil, microerror.Mask(noResourcesError)
}
return orgCollection, nil
}
// omitManagedFields removes managed fields to make YAML output easier to read.
// With Kubernetes 1.21 we can use OmitManagedFieldsPrinter and remove this.
func omitManagedFields(org *securityv1alpha1.Organization) *securityv1alpha1.Organization {
org.ManagedFields = nil
return org
}