Skip to content

Commit

Permalink
fix(kuma-cp): pass future meta to Validate when creating a resource (#…
Browse files Browse the repository at this point in the history
…10927)

At the moment the MeshService validator fails when Create is called because the meta is nil and the future name of the resource is missing.

Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont authored Jul 18, 2024
1 parent c32eb95 commit d8eb3be
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/core/resources/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ func (r *resourcesManager) List(ctx context.Context, list model.ResourceList, fs
}

func (r *resourcesManager) Create(ctx context.Context, resource model.Resource, fs ...store.CreateOptionsFunc) error {
opts := store.NewCreateOptions(fs...)

// Create temporary meta so validation can see the meta that the store will set
existingMeta := resource.GetMeta()
if existingMeta == nil {
resource.SetMeta(metaFromCreateOpts(resource.Descriptor(), *opts))
}
if err := model.Validate(resource); err != nil {
return err
}
opts := store.NewCreateOptions(fs...)
resource.SetMeta(existingMeta)

var owner model.Resource
if resource.Descriptor().Scope == model.ScopeMesh {
Expand Down
63 changes: 63 additions & 0 deletions pkg/core/resources/manager/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package manager

import (
"time"

"github.com/kumahq/kuma/pkg/core/resources/model"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
"github.com/kumahq/kuma/pkg/core/resources/store"
)

type resourceMetaObject struct {
Name string
Version string
Mesh string
CreationTime time.Time
ModificationTime time.Time
Labels map[string]string
}

var _ core_model.ResourceMeta = &resourceMetaObject{}

func (r *resourceMetaObject) GetName() string {
return r.Name
}

func (r *resourceMetaObject) GetNameExtensions() core_model.ResourceNameExtensions {
return core_model.ResourceNameExtensionsUnsupported
}

func (r *resourceMetaObject) GetVersion() string {
return r.Version
}

func (r *resourceMetaObject) GetMesh() string {
return r.Mesh
}

func (r *resourceMetaObject) GetCreationTime() time.Time {
return r.CreationTime
}

func (r *resourceMetaObject) GetModificationTime() time.Time {
return r.ModificationTime
}

func (r *resourceMetaObject) GetLabels() map[string]string {
return r.Labels
}

func metaFromCreateOpts(descriptor model.ResourceTypeDescriptor, fs store.CreateOptions) core_model.ResourceMeta {
if fs.Name == "" {
return nil
}
if fs.Mesh == "" && descriptor.Scope == model.ScopeMesh {
return nil
}
return &resourceMetaObject{
Name: fs.Name,
Mesh: fs.Mesh,
CreationTime: fs.CreationTime,
Labels: fs.Labels,
}
}

0 comments on commit d8eb3be

Please sign in to comment.