Skip to content

Commit

Permalink
reduce complexity in runfunction
Browse files Browse the repository at this point in the history
Signed-off-by: Tim <[email protected]>
  • Loading branch information
Avarei committed Jul 1, 2024
1 parent de6399d commit 71d076a
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions internal/function/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
Ctx: ctx,
}))
}

if err != nil {
response.Fatal(rsp, errors.Wrap(err, "could not create Pkl Evaluator"))
return rsp, nil
Expand All @@ -104,34 +103,43 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
return rsp, nil
}

helperResponse := &helper.CompositionResponse{}
err = yaml.Unmarshal([]byte(renderedManifest), helperResponse)
rsp, err = toResponse(renderedManifest, req.GetMeta().GetTag())
if err != nil {
response.Fatal(rsp, errors.Wrap(err, "error while converting Pkl file"))
return rsp, nil
}

return rsp, nil
}

func toResponse(renderedManifest, tag string) (*fnv1beta1.RunFunctionResponse, error) {
rspHelper := &helper.CompositionResponse{}
err := yaml.Unmarshal([]byte(renderedManifest), rspHelper)
if err != nil {
return nil, errors.Wrapf(err, "rendered Pkl file was not in expected format. did you amend @crossplane/CompositionResponse.pkl?")
}

responseMeta := &fnv1beta1.ResponseMeta{
Tag: req.GetMeta().GetTag(),
Tag: tag,
Ttl: durationpb.New(response.DefaultTTL),
}
if ttl := helperResponse.GetMeta().GetTtl(); ttl != nil {
if ttl := rspHelper.GetMeta().GetTtl(); ttl != nil {
responseMeta.Ttl = ttl
}

// Note: consider not overwriting rsp and whether it makes a difference.
rsp = &fnv1beta1.RunFunctionResponse{
rsp := &fnv1beta1.RunFunctionResponse{
Meta: responseMeta,
Desired: helperResponse.Desired,
Results: helperResponse.Results,
Context: helperResponse.Context,
Desired: rspHelper.Desired,
Results: rspHelper.Results,
Context: rspHelper.Context,
}

if helperResponse.Requirements != nil && helperResponse.Requirements.ExtraResources != nil {
if rspHelper.Requirements != nil && rspHelper.Requirements.ExtraResources != nil {
rsp.Requirements = &fnv1beta1.Requirements{
ExtraResources: convertExtraResources(helperResponse.Requirements.ExtraResources),
ExtraResources: convertExtraResources(rspHelper.Requirements.ExtraResources),
}
}

return rsp, nil
}

Expand Down

0 comments on commit 71d076a

Please sign in to comment.