Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Make the project namespace check configurable
Browse files Browse the repository at this point in the history
In certain situations, we may not be able to do the namespace check
associated with create projects. This change adds a parameter to make
this configurable by the caller.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Jul 7, 2023
1 parent 0184d27 commit 5520276
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pkg/server/registry/apigroups/acorn/apigroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func Stores(c kclient.WithWatch, cfg, localCfg *clientgo.Config) (map[string]res
"images/push": images.NewImagePush(c, transport),
"images/pull": images.NewImagePull(c, clientFactory, transport),
"images/details": images.NewImageDetails(c, transport),
"projects": projects.NewStorage(c),
"projects": projects.NewStorage(c, true),
"volumes": volumesStorage,
"volumeclasses": class.NewClassStorage(c),
"containerreplicas": containersStorage,
Expand Down
55 changes: 29 additions & 26 deletions pkg/server/registry/apigroups/acorn/projects/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,56 @@ import (
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func NewStorage(c kclient.WithWatch) rest.Storage {
func NewStorage(c kclient.WithWatch, namespaceCheck bool) rest.Storage {
remoteResource := translation.NewSimpleTranslationStrategy(&translator{}, remote.NewRemote(&v1.ProjectInstance{}, c))
validator := &Validator{Client: c}
return stores.NewBuilder(c.Scheme(), &apiv1.Project{}).
WithCompleteCRUD(remoteResource).
WithCreate(&projectCreater{creater: remoteResource, client: c}).
WithCreate(&projectCreater{creater: remoteResource, client: c, namespaceCheck: namespaceCheck}).
WithValidateCreate(validator).
WithValidateUpdate(validator).
WithTableConverter(tables.ProjectConverter).
Build()
}

type projectCreater struct {
creater strategy.Creater
client kclient.Client
namespaceCheck bool
creater strategy.Creater
client kclient.Client
}

func (pr *projectCreater) New() types.Object {
return pr.creater.New()
}

func (pr *projectCreater) Create(ctx context.Context, object types.Object) (types.Object, error) {
ns := &corev1.Namespace{}
err := pr.client.Get(ctx, router.Key("", object.GetName()), ns)
if err == nil {
// Project corresponds to a labeled namespace
if ns.Labels[labels.AcornProject] != "true" {
qualifiedResource := schema.GroupResource{
Resource: "namespaces",
}
return nil, &apierrors.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusConflict,
Reason: metav1.StatusReasonAlreadyExists,
Details: &metav1.StatusDetails{
Group: qualifiedResource.Group,
Kind: qualifiedResource.Resource,
Name: object.GetName(),
if pr.namespaceCheck {
ns := &corev1.Namespace{}
err := pr.client.Get(ctx, router.Key("", object.GetName()), ns)
if err == nil {
// Project corresponds to a labeled namespace
if ns.Labels[labels.AcornProject] != "true" {
qualifiedResource := schema.GroupResource{
Resource: "namespaces",
}
return nil, &apierrors.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusConflict,
Reason: metav1.StatusReasonAlreadyExists,
Details: &metav1.StatusDetails{
Group: qualifiedResource.Group,
Kind: qualifiedResource.Resource,
Name: object.GetName(),
},
Message: fmt.Sprintf("%s %q already exists but does not contain the %s=true label",
qualifiedResource.String(), object.GetName(), labels.AcornProject),
},
Message: fmt.Sprintf("%s %q already exists but does not contain the %s=true label",
qualifiedResource.String(), object.GetName(), labels.AcornProject),
},
}
}
} else if !apierrors.IsNotFound(err) {
return nil, err
}
} else if !apierrors.IsNotFound(err) {
return nil, err
}

return pr.creater.Create(ctx, object)
Expand Down

0 comments on commit 5520276

Please sign in to comment.