Skip to content

Commit

Permalink
chore: take template vars as dynamic parameters of action (#7990)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Aug 22, 2024
1 parent 46bed94 commit d550d0c
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions pkg/controller/component/lifecycle/kbagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ func (a *kbagent) callAction(ctx context.Context, cli client.Reader, spec *appsv
}

func (a *kbagent) buildActionRequest(ctx context.Context, cli client.Reader, la lifecycleAction, opts *Options) (*proto.ActionRequest, error) {
parameters, err := la.parameters(ctx, cli)
parameters, err := a.parameters(ctx, cli, la)
if err != nil {
return nil, err
}

req := &proto.ActionRequest{
Action: la.name(),
Parameters: parameters,
Expand All @@ -138,6 +137,47 @@ func (a *kbagent) buildActionRequest(ctx context.Context, cli client.Reader, la
return req, nil
}

func (a *kbagent) parameters(ctx context.Context, cli client.Reader, la lifecycleAction) (map[string]string, error) {
m, err := a.templateVarsParameters(ctx, cli)
if err != nil {
return nil, err
}
sys, err := la.parameters(ctx, cli)
if err != nil {
return nil, err
}

for k, v := range sys {
// template vars take precedence
if _, ok := m[k]; !ok {
m[k] = v
}
}
return m, nil
}

func (a *kbagent) templateVarsParameters(ctx context.Context, cli client.Reader) (map[string]string, error) {
templateVars := a.synthesizedComp.TemplateVars
if templateVars == nil {
// TODO: vars from SynthesizedComponent
var err error
compDef := &appsv1alpha1.ComponentDefinition{}
if err = cli.Get(ctx, client.ObjectKey{Name: a.synthesizedComp.CompDefName}, compDef); err != nil {
return nil, err
}
// TODO: handle the case where delete a component which is in provisioning
templateVars, _, err = component.ResolveTemplateNEnvVars(ctx, cli, a.synthesizedComp, compDef.Spec.Vars)
if err != nil {
return nil, err
}
}
m := map[string]string{}
for k, v := range templateVars {
m[k] = v.(string)
}
return m, nil
}

func (a *kbagent) callActionWithSelector(ctx context.Context, spec *appsv1alpha1.Action, la lifecycleAction, req *proto.ActionRequest) error {
pods, err := a.selectTargetPods(spec)
if err != nil {
Expand Down

0 comments on commit d550d0c

Please sign in to comment.