Skip to content

Commit

Permalink
Pass through options to namespace client
Browse files Browse the repository at this point in the history
  • Loading branch information
glrf committed Dec 22, 2021
1 parent f0aa158 commit 4e8becc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
32 changes: 22 additions & 10 deletions apiserver/organization/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"

corev1 "k8s.io/api/core/v1"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/apiserver-runtime/pkg/util/loopback"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -25,7 +27,7 @@ func (p *loopbackNamespaceProvider) init() error {
return err
}

func (p *loopbackNamespaceProvider) getNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
func (p *loopbackNamespaceProvider) getNamespace(ctx context.Context, name string, options *metav1.GetOptions) (*corev1.Namespace, error) {
err := p.init()
if err != nil {
return nil, err
Expand All @@ -35,41 +37,51 @@ func (p *loopbackNamespaceProvider) getNamespace(ctx context.Context, name strin
return &ns, err
}

func (p *loopbackNamespaceProvider) deleteNamespace(ctx context.Context, name string) (*corev1.Namespace, error) {
func (p *loopbackNamespaceProvider) deleteNamespace(ctx context.Context, name string, options *metav1.DeleteOptions) (*corev1.Namespace, error) {
err := p.init()
if err != nil {
return nil, err
}
ns := corev1.Namespace{}
ns.Name = name
err = p.client.Delete(ctx, &ns)
err = p.client.Delete(ctx, &ns, &client.DeleteOptions{
Raw: options,
})
return &ns, err
}

func (p *loopbackNamespaceProvider) createNamespace(ctx context.Context, ns *corev1.Namespace) error {
func (p *loopbackNamespaceProvider) createNamespace(ctx context.Context, ns *corev1.Namespace, options *metav1.CreateOptions) error {
err := p.init()
if err != nil {
return err
}
return p.client.Create(ctx, ns)
return p.client.Create(ctx, ns, &client.CreateOptions{
Raw: options,
})
}

func (p *loopbackNamespaceProvider) updateNamespace(ctx context.Context, ns *corev1.Namespace) error {
func (p *loopbackNamespaceProvider) updateNamespace(ctx context.Context, ns *corev1.Namespace, options *metav1.UpdateOptions) error {
err := p.init()
if err != nil {
return err
}
return p.client.Update(ctx, ns)
return p.client.Update(ctx, ns, &client.UpdateOptions{
Raw: options,
})
}

func (p *loopbackNamespaceProvider) listNamespaces(ctx context.Context) (*corev1.NamespaceList, error) {
func (p *loopbackNamespaceProvider) listNamespaces(ctx context.Context, options *metainternalversion.ListOptions) (*corev1.NamespaceList, error) {
err := p.init()
if err != nil {
return nil, err
}

nl := corev1.NamespaceList{}
err = p.client.List(ctx, &nl, client.MatchingLabels{typeKey: "organization"})
err = p.client.List(ctx, &nl, &client.ListOptions{
LabelSelector: options.LabelSelector,
FieldSelector: options.FieldSelector,
Limit: options.Limit,
Continue: options.Continue,
})
if err != nil {
return nil, err
}
Expand Down
38 changes: 19 additions & 19 deletions apiserver/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/apiserver/pkg/endpoints/filters"
Expand All @@ -37,11 +39,11 @@ type organizationStorage struct {
namespaceAuthorizer authorizer.Authorizer
}
type namespaceProvider interface {
getNamespace(ctx context.Context, name string) (*corev1.Namespace, error)
deleteNamespace(ctx context.Context, name string) (*corev1.Namespace, error)
createNamespace(ctx context.Context, ns *corev1.Namespace) error
updateNamespace(ctx context.Context, ns *corev1.Namespace) error
listNamespaces(ctx context.Context) (*corev1.NamespaceList, error)
getNamespace(ctx context.Context, name string, options *metav1.GetOptions) (*corev1.Namespace, error)
deleteNamespace(ctx context.Context, name string, options *metav1.DeleteOptions) (*corev1.Namespace, error)
createNamespace(ctx context.Context, ns *corev1.Namespace, options *metav1.CreateOptions) error
updateNamespace(ctx context.Context, ns *corev1.Namespace, options *metav1.UpdateOptions) error
listNamespaces(ctx context.Context, options *metainternalversion.ListOptions) (*corev1.NamespaceList, error)
}

func (s organizationStorage) New() runtime.Object {
Expand Down Expand Up @@ -70,7 +72,7 @@ func (s *organizationStorage) Get(ctx context.Context, name string, options *met
}

nsName := orgNameToNamespaceName(name)
ns, err := s.namepaces.getNamespace(ctx, nsName)
ns, err := s.namepaces.getNamespace(ctx, nsName, options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -105,7 +107,7 @@ func (s *organizationStorage) Create(ctx context.Context, obj runtime.Object, cr
}

ns := orgToNamespace(org)
if err := s.namepaces.createNamespace(ctx, ns); err != nil {
if err := s.namepaces.createNamespace(ctx, ns, options); err != nil {
return nil, err
}
return org, nil
Expand All @@ -118,9 +120,14 @@ func (s organizationStorage) NewList() runtime.Object {
}

func (s *organizationStorage) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
//TODO(glrf) Don't ignore list options

namespaces, err := s.namepaces.listNamespaces(ctx)
orgNamspace, err := labels.NewRequirement(typeKey, selection.Equals, []string{"organization"})
if err != nil {
return nil, err
}
options.LabelSelector = options.LabelSelector.Add(*orgNamspace)
log.Println(options.LabelSelector.Requirements())
namespaces, err := s.namepaces.listNamespaces(ctx, options)
if err != nil {
return nil, err
}
Expand All @@ -144,9 +151,6 @@ func (s *organizationStorage) Update(ctx context.Context, name string, objInfo r
createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc,
forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {

//TODO(glrf) Don't ignore update options
log.Println("Update")

newOrg := &orgv1.Organization{}

a, err := filters.GetAuthorizerAttributes(ctx)
Expand All @@ -161,17 +165,14 @@ func (s *organizationStorage) Update(ctx context.Context, name string, objInfo r
return nil, false, kerrors.NewNotFound(newOrg.GetGroupVersionResource().GroupResource(), name)
}

log.Println("Update authorized")
oldOrg, err := s.Get(ctx, name, nil)
if err != nil {

log.Printf("unable to get organization: %s\n", err)
return nil, false, fmt.Errorf("unable to get organization: %w", err)
}

newObj, err := objInfo.UpdatedObject(ctx, oldOrg)
if err != nil {
log.Printf("unable to update org: %s\n", err)
return nil, false, err
}

Expand All @@ -186,9 +187,8 @@ func (s *organizationStorage) Update(ctx context.Context, name string, objInfo r
return nil, false, err
}
}
log.Printf("Validated and updating")

return newOrg, false, s.namepaces.updateNamespace(ctx, orgToNamespace(newOrg))
return newOrg, false, s.namepaces.updateNamespace(ctx, orgToNamespace(newOrg), options)
}

var _ rest.GracefulDeleter = &organizationStorage{}
Expand All @@ -206,8 +206,8 @@ func (s *organizationStorage) Delete(ctx context.Context, name string, deleteVal
}
}

ns, err := s.namepaces.deleteNamespace(ctx, orgNameToNamespaceName(name))
return namespaceToOrg(ns), true, err
ns, err := s.namepaces.deleteNamespace(ctx, orgNameToNamespaceName(name), options)
return namespaceToOrg(ns), false, err
}

func (s *organizationStorage) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
Expand Down

0 comments on commit 4e8becc

Please sign in to comment.