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

Allow create of app with no name to do generated name #2040

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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