@@ -2,15 +2,18 @@ package organization
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
7
8
orgv1 "github.com/appuio/control-api/apis/organization/v1"
8
9
9
10
corev1 "k8s.io/api/core/v1"
11
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
10
12
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
11
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
14
"k8s.io/apimachinery/pkg/labels"
13
15
"k8s.io/apimachinery/pkg/runtime"
16
+ "k8s.io/apimachinery/pkg/runtime/schema"
14
17
"k8s.io/apimachinery/pkg/selection"
15
18
"k8s.io/apimachinery/pkg/watch"
16
19
genericregistry "k8s.io/apiserver/pkg/registry/generic"
@@ -30,14 +33,6 @@ func New() restbuilder.ResourceHandlerProvider {
30
33
type organizationStorage struct {
31
34
namepaces namespaceProvider
32
35
}
33
- type namespaceProvider interface {
34
- getNamespace (ctx context.Context , name string , options * metav1.GetOptions ) (* corev1.Namespace , error )
35
- deleteNamespace (ctx context.Context , name string , options * metav1.DeleteOptions ) (* corev1.Namespace , error )
36
- createNamespace (ctx context.Context , ns * corev1.Namespace , options * metav1.CreateOptions ) error
37
- updateNamespace (ctx context.Context , ns * corev1.Namespace , options * metav1.UpdateOptions ) error
38
- listNamespaces (ctx context.Context , options * metainternalversion.ListOptions ) (* corev1.NamespaceList , error )
39
- watchNamespaces (ctx context.Context , options * metainternalversion.ListOptions ) (watch.Interface , error )
40
- }
41
36
42
37
func (s organizationStorage ) New () runtime.Object {
43
38
return & orgv1.Organization {}
@@ -52,13 +47,17 @@ func (s *organizationStorage) NamespaceScoped() bool {
52
47
var _ rest.Getter = & organizationStorage {}
53
48
54
49
func (s * organizationStorage ) Get (ctx context.Context , name string , options * metav1.GetOptions ) (runtime.Object , error ) {
55
- ns , err := s .namepaces .getNamespace (ctx , name , options )
50
+ org := & orgv1.Organization {}
51
+ ns , err := s .namepaces .GetNamespace (ctx , name , options )
56
52
if err != nil {
57
- return nil , err
53
+ return nil , convertNamespaceError ( err )
58
54
}
59
-
60
- // TODO(glrf) Check that this is actually an organization and not a random namespace
61
- return orgv1 .NewOrganizationFromNS (ns ), nil
55
+ org = orgv1 .NewOrganizationFromNS (ns )
56
+ if org == nil {
57
+ // This namespace is not an organization
58
+ return nil , apierrors .NewNotFound (org .GetGroupVersionResource ().GroupResource (), name )
59
+ }
60
+ return org , nil
62
61
}
63
62
64
63
var _ rest.Creater = & organizationStorage {}
@@ -74,8 +73,8 @@ func (s *organizationStorage) Create(ctx context.Context, obj runtime.Object, cr
74
73
return nil , err
75
74
}
76
75
77
- if err := s .namepaces .createNamespace (ctx , org .ToNamespace (), options ); err != nil {
78
- return nil , err
76
+ if err := s .namepaces .CreateNamespace (ctx , org .ToNamespace (), options ); err != nil {
77
+ return nil , convertNamespaceError ( err )
79
78
}
80
79
return org , nil
81
80
}
@@ -92,7 +91,7 @@ func (s *organizationStorage) List(ctx context.Context, options *metainternalver
92
91
return nil , err
93
92
}
94
93
options .LabelSelector = options .LabelSelector .Add (* orgNamspace )
95
- namespaces , err := s .namepaces .listNamespaces (ctx , options )
94
+ namespaces , err := s .namepaces .ListNamespaces (ctx , options )
96
95
if err != nil {
97
96
return nil , err
98
97
}
@@ -141,7 +140,7 @@ func (s *organizationStorage) Update(ctx context.Context, name string, objInfo r
141
140
}
142
141
}
143
142
144
- return newOrg , false , s .namepaces .updateNamespace (ctx , newOrg .ToNamespace (), options )
143
+ return newOrg , false , s .namepaces .UpdateNamespace (ctx , newOrg .ToNamespace (), options )
145
144
}
146
145
147
146
var _ rest.GracefulDeleter = & organizationStorage {}
@@ -159,7 +158,7 @@ func (s *organizationStorage) Delete(ctx context.Context, name string, deleteVal
159
158
}
160
159
}
161
160
162
- ns , err := s .namepaces .deleteNamespace (ctx , name , options )
161
+ ns , err := s .namepaces .DeleteNamespace (ctx , name , options )
163
162
return orgv1 .NewOrganizationFromNS (ns ), false , err
164
163
}
165
164
@@ -172,7 +171,7 @@ func (s *organizationStorage) Watch(ctx context.Context, options *metainternalve
172
171
}
173
172
options .LabelSelector = options .LabelSelector .Add (* orgNamspace )
174
173
175
- nsWatcher , err := s .namepaces .watchNamespaces (ctx , options )
174
+ nsWatcher , err := s .namepaces .WatchNamespaces (ctx , options )
176
175
if err != nil {
177
176
return nil , err
178
177
}
@@ -194,3 +193,21 @@ func (s *organizationStorage) Watch(ctx context.Context, options *metainternalve
194
193
return in , true
195
194
}), nil
196
195
}
196
+
197
+ func convertNamespaceError (err error ) error {
198
+ groupResource := schema.GroupResource {
199
+ Group : orgv1 .GroupVersion .Group ,
200
+ Resource : "organizations" ,
201
+ }
202
+ statusErr := & apierrors.StatusError {}
203
+
204
+ if errors .As (err , & statusErr ) {
205
+ switch {
206
+ case apierrors .IsNotFound (err ):
207
+ return apierrors .NewNotFound (groupResource , statusErr .ErrStatus .Details .Name )
208
+ case apierrors .IsAlreadyExists (err ):
209
+ return apierrors .NewAlreadyExists (groupResource , statusErr .ErrStatus .Details .Name )
210
+ }
211
+ }
212
+ return err
213
+ }
0 commit comments