Skip to content

Commit

Permalink
Address comments, introduce task interface
Browse files Browse the repository at this point in the history
  • Loading branch information
liamfallon committed Nov 18, 2024
1 parent e9eacdb commit a42e86e
Show file tree
Hide file tree
Showing 20 changed files with 742 additions and 705 deletions.
27 changes: 0 additions & 27 deletions pkg/cache/memory/packagerevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,3 @@ func (c *cachedPackageRevision) GetPackageRevision(ctx context.Context) (*api.Pa

return apiPR, nil
}

func (c *cachedPackageRevision) GetCachedPackageRevision(ctx context.Context) (*api.PackageRevision, error) {
repoPkgRev, err := c.GetPackageRevision(ctx)
if err != nil {
return nil, err
}
var isLatest bool
if val, found := repoPkgRev.Labels[api.LatestPackageRevisionKey]; found && val == api.LatestPackageRevisionValue {
isLatest = true
}
repoPkgRev.Labels = c.GetMeta().Labels
if isLatest {
// copy the labels in case the cached object is being read by another go routine
labels := make(map[string]string, len(repoPkgRev.Labels))
for k, v := range repoPkgRev.Labels {
labels[k] = v
}
labels[api.LatestPackageRevisionKey] = api.LatestPackageRevisionValue
repoPkgRev.Labels = labels
}
repoPkgRev.Annotations = c.GetMeta().Annotations
repoPkgRev.Finalizers = c.GetMeta().Finalizers
repoPkgRev.OwnerReferences = c.GetMeta().OwnerReferences
repoPkgRev.DeletionTimestamp = c.GetMeta().DeletionTimestamp

return repoPkgRev, nil
}
2 changes: 0 additions & 2 deletions pkg/cache/memory/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ func identifyLatestRevisions(result map[repository.PackageRevisionKey]*cachedPac
for _, v := range latest {
v.isLatestRevision = true
}

// If the
}

func toPackageRevisionSlice(cached map[repository.PackageRevisionKey]*cachedPackageRevision, filter repository.ListPackageRevisionFilter) []repository.PackageRevision {
Expand Down
5 changes: 4 additions & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ type CaDEngine interface {
}

func NewCaDEngine(opts ...EngineOption) (CaDEngine, error) {
engine := &cadEngine{}
engine := &cadEngine{
taskHandler: task.GetDefaultTaskHandler(),
}

for _, opt := range opts {
if err := opt.apply(engine); err != nil {
return nil, err
Expand Down
28 changes: 14 additions & 14 deletions pkg/engine/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type EngineOptionFunc func(engine *cadEngine) error
var _ EngineOption = EngineOptionFunc(nil)

func (f EngineOptionFunc) apply(engine *cadEngine) error {
engine.taskHandler.RepoOpener = engine
engine.taskHandler.SetRepoOpener(engine)
return f(engine)
}

Expand All @@ -48,12 +48,12 @@ func WithCache(cache cache.Cache) EngineOption {
func WithBuiltinFunctionRuntime() EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
runtime := newBuiltinRuntime()
if engine.taskHandler.Runtime == nil {
engine.taskHandler.Runtime = runtime
} else if mr, ok := engine.taskHandler.Runtime.(*fn.MultiRuntime); ok {
if engine.taskHandler.GetRuntime() == nil {
engine.taskHandler.SetRuntime(runtime)
} else if mr, ok := engine.taskHandler.GetRuntime().(*fn.MultiRuntime); ok {
mr.Add(runtime)
} else {
engine.taskHandler.Runtime = fn.NewMultiRuntime([]fn.FunctionRuntime{engine.taskHandler.Runtime, runtime})
engine.taskHandler.SetRuntime(fn.NewMultiRuntime([]fn.FunctionRuntime{engine.taskHandler.GetRuntime(), runtime}))
}
return nil
})
Expand All @@ -65,27 +65,27 @@ func WithGRPCFunctionRuntime(address string) EngineOption {
if err != nil {
return fmt.Errorf("failed to create function runtime: %w", err)
}
if engine.taskHandler.Runtime == nil {
engine.taskHandler.Runtime = runtime
} else if mr, ok := engine.taskHandler.Runtime.(*fn.MultiRuntime); ok {
if engine.taskHandler.GetRuntime() == nil {
engine.taskHandler.SetRuntime(runtime)
} else if mr, ok := engine.taskHandler.GetRuntime().(*fn.MultiRuntime); ok {
mr.Add(runtime)
} else {
engine.taskHandler.Runtime = fn.NewMultiRuntime([]fn.FunctionRuntime{engine.taskHandler.Runtime, runtime})
engine.taskHandler.SetRuntime(fn.NewMultiRuntime([]fn.FunctionRuntime{engine.taskHandler.GetRuntime(), runtime}))
}
return nil
})
}

func WithFunctionRuntime(runtime fn.FunctionRuntime) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.taskHandler.Runtime = runtime
engine.taskHandler.SetRuntime(runtime)
return nil
})
}

func WithSimpleFunctionRuntime() EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.taskHandler.Runtime = kpt.NewSimpleFunctionRuntime()
engine.taskHandler.SetRuntime(kpt.NewSimpleFunctionRuntime())
return nil
})
}
Expand All @@ -96,21 +96,21 @@ func WithRunnerOptions(options fnruntime.RunnerOptions) EngineOption {

func WithRunnerOptionsResolver(fn func(namespace string) fnruntime.RunnerOptions) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.taskHandler.RunnerOptionsResolver = fn
engine.taskHandler.SetRunnerOptionsResolver(fn)
return nil
})
}

func WithCredentialResolver(resolver repository.CredentialResolver) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.taskHandler.CredentialResolver = resolver
engine.taskHandler.SetCredentialResolver(resolver)
return nil
})
}

func WithReferenceResolver(resolver repository.ReferenceResolver) EngineOption {
return EngineOptionFunc(func(engine *cadEngine) error {
engine.taskHandler.ReferenceResolver = resolver
engine.taskHandler.SetReferenceResolver(resolver)
return nil
})
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/repository/fake/packagerevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,3 @@ func (f *FakePackageRevision) GetMeta() meta.PackageRevisionMeta {

func (f *FakePackageRevision) SetMeta(meta.PackageRevisionMeta) {
}

func (f *FakePackageRevision) IsLatest() bool {
return false
}

func (f *FakePackageRevision) SetLatest(latest bool) {
}
2 changes: 1 addition & 1 deletion pkg/task/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func newPackageContextGeneratorMutation(packageConfig *builtins.PackageConfig) (

var _ mutation = &builtinEvalMutation{}

func (m *builtinEvalMutation) Apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
func (m *builtinEvalMutation) apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
ff := &runtimeutil.FunctionFilter{
Run: m.runner.Run,
Results: &yaml.RNode{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestPackageContext(t *testing.T) {
t.Fatalf("Failed to get builtin function mutation: %v", err)
}

got, _, err := m.Apply(context.Background(), input)
got, _, err := m.apply(context.Background(), input)
if err != nil {
t.Fatalf("Failed to apply builtin function mutation: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/task/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type clonePackageMutation struct {
packageConfig *builtins.PackageConfig
}

func (m *clonePackageMutation) Apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
func (m *clonePackageMutation) apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
ctx, span := tracer.Start(ctx, "clonePackageMutation::Apply", trace.WithAttributes())
defer span.End()

Expand Down Expand Up @@ -83,7 +83,7 @@ func (m *clonePackageMutation) Apply(ctx context.Context, resources repository.P
if err != nil {
return repository.PackageResources{}, nil, err
}
cloned, _, err = genPkgContextMutation.Apply(ctx, cloned)
cloned, _, err = genPkgContextMutation.apply(ctx, cloned)
if err != nil {
return repository.PackageResources{}, nil, fmt.Errorf("failed to generate deployment context %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/task/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ func TestCloneGitBasicAuth(t *testing.T) {
},
}

_, _, err = cpm.Apply(context.Background(), repository.PackageResources{})
_, _, err = cpm.apply(context.Background(), repository.PackageResources{})
if err == nil {
t.Errorf("Expected error (unauthorized); got none")
}

cpm.credentialResolver = auth

r, _, err := cpm.Apply(context.Background(), repository.PackageResources{})
r, _, err := cpm.apply(context.Background(), repository.PackageResources{})
if err != nil {
t.Errorf("task apply failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type editPackageMutation struct {

var _ mutation = &editPackageMutation{}

func (m *editPackageMutation) Apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
func (m *editPackageMutation) apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
ctx, span := tracer.Start(ctx, "editPackageMutation::Apply", trace.WithAttributes())
defer span.End()

Expand Down
2 changes: 1 addition & 1 deletion pkg/task/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ info:
repoOpener: repoOpener,
}

res, _, err := epm.Apply(context.Background(), repository.PackageResources{})
res, _, err := epm.apply(context.Background(), repository.PackageResources{})
if err != nil {
t.Errorf("task apply failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type evalFunctionMutation struct {
task *api.Task
}

func (m *evalFunctionMutation) Apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
func (m *evalFunctionMutation) apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) {
ctx, span := tracer.Start(ctx, "evalFunctionMutation::Apply", trace.WithAttributes())
defer span.End()

Expand Down
Loading

0 comments on commit a42e86e

Please sign in to comment.