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

Commit

Permalink
Allow create of app with no name to do generated name
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <[email protected]>
  • Loading branch information
ibuildthecloud committed Aug 9, 2023
1 parent 392a214 commit a51352d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/server/registry/apigroups/acorn/apps/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewStorage(c kclient.WithWatch, clientFactory *client.Factory, recorder eve
validator := NewValidator(c, clientFactory, strategy, transport)

return stores.NewBuilder(c.Scheme(), &apiv1.App{}).
WithPrepareCreate(validator).
WithCompleteCRUD(strategy).
WithValidateUpdate(validator).
WithValidateCreate(validator).
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/registry/apigroups/acorn/apps/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/acorn-io/baaah/pkg/merr"
"github.com/acorn-io/baaah/pkg/typed"
"github.com/acorn-io/mink/pkg/strategy"
"github.com/acorn-io/mink/pkg/types"
"github.com/acorn-io/namegenerator"
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/appdefinition"
Expand Down Expand Up @@ -38,6 +40,10 @@ import (
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)

var (
nameGenerator = namegenerator.NewNameGenerator(time.Now().UnixNano())
)

type Validator struct {
client kclient.Client
clientFactory *client.Factory
Expand All @@ -55,6 +61,13 @@ func NewValidator(client kclient.Client, clientFactory *client.Factory, deleter
}
}

func (s *Validator) PrepareForCreate(ctx context.Context, obj runtime.Object) {
r := obj.(types.Object)
if r.GetName() == "" && r.GetGenerateName() == "" {
r.SetName(nameGenerator.Generate())
}
}

func (s *Validator) ValidateName(_ context.Context, obj runtime.Object) (result field.ErrorList) {
name := obj.(kclient.Object).GetName()
if errs := validation.IsDNS1035Label(name); len(errs) > 0 {
Expand Down

0 comments on commit a51352d

Please sign in to comment.