Skip to content

Commit 197c8df

Browse files
authored
[ACTP] Expose PrivateActionRunner app for reuse (#45971)
## What does this PR do? This PR extract and expose an app from the `privateactionrunner` component. ## Motivation In an attempt to re-use the private action runner in the cluster agent, we need to expose this app since the cluster agent doesn't use fx injection to a good extent. ## Describe how you validated your changes I ran the PAR in local with self enrolment and actions execution still works ## Additional Notes Co-authored-by: merchrist.kiki <merchrist.kiki@datadoghq.com>
1 parent fdfba17 commit 197c8df

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

comp/privateactionrunner/impl/privateactionrunner.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Provides struct {
4848
Comp privateactionrunner.Component
4949
}
5050

51-
type privateactionrunnerImpl struct {
51+
type PrivateActionRunner struct {
5252
workflowRunner *runners.WorkflowRunner
5353
commonRunner *runners.CommonRunner
5454
drain func()
@@ -61,60 +61,73 @@ func NewComponent(reqs Requires) (Provides, error) {
6161
reqs.Log.Info("private-action-runner is not enabled. Set privateactionrunner.enabled: true in your datadog.yaml file or set the environment variable DD_PRIVATEACTIONRUNNER_ENABLED=true.")
6262
return Provides{}, privateactionrunner.ErrNotEnabled
6363
}
64-
persistedIdentity, err := enrollment.GetIdentityFromPreviousEnrollment(reqs.Config)
64+
65+
runner, err := NewPrivateActionRunner(ctx, reqs.Config, reqs.Hostname, reqs.RcClient, reqs.Log)
66+
if err != nil {
67+
return Provides{}, err
68+
}
69+
reqs.Lifecycle.Append(compdef.Hook{
70+
OnStart: runner.Start,
71+
OnStop: runner.Stop,
72+
})
73+
return Provides{Comp: runner}, nil
74+
}
75+
76+
func NewPrivateActionRunner(
77+
ctx context.Context,
78+
coreConfig model.ReaderWriter,
79+
hostnameGetter hostnameinterface.Component,
80+
rcClient rcclient.Component,
81+
logger log.Component,
82+
) (*PrivateActionRunner, error) {
83+
persistedIdentity, err := enrollment.GetIdentityFromPreviousEnrollment(coreConfig)
6584
if err != nil {
66-
return Provides{}, fmt.Errorf("self-enrollment failed: %w", err)
85+
return nil, fmt.Errorf("self-enrollment failed: %w", err)
6786
}
6887
if persistedIdentity != nil {
69-
reqs.Config.Set("privateactionrunner.private_key", persistedIdentity.PrivateKey, model.SourceAgentRuntime)
70-
reqs.Config.Set("privateactionrunner.urn", persistedIdentity.URN, model.SourceAgentRuntime)
88+
coreConfig.Set("privateactionrunner.private_key", persistedIdentity.PrivateKey, model.SourceAgentRuntime)
89+
coreConfig.Set("privateactionrunner.urn", persistedIdentity.URN, model.SourceAgentRuntime)
7190
}
7291

73-
cfg, err := parconfig.FromDDConfig(reqs.Config)
92+
cfg, err := parconfig.FromDDConfig(coreConfig)
7493
if err != nil {
75-
return Provides{}, err
94+
return nil, err
7695
}
7796

78-
canSelfEnroll := reqs.Config.GetBool("privateactionrunner.self_enroll")
97+
canSelfEnroll := coreConfig.GetBool("privateactionrunner.self_enroll")
7998
if cfg.IdentityIsIncomplete() && canSelfEnroll {
80-
reqs.Log.Info("Identity not found and self-enrollment enabled. Self-enrolling private action runner")
81-
updatedCfg, err := performSelfEnrollment(ctx, reqs.Log, reqs.Config, reqs.Hostname, cfg)
99+
logger.Info("Identity not found and self-enrollment enabled. Self-enrolling private action runner")
100+
updatedCfg, err := performSelfEnrollment(ctx, logger, coreConfig, hostnameGetter, cfg)
82101
if err != nil {
83-
return Provides{}, fmt.Errorf("self-enrollment failed: %w", err)
102+
return nil, fmt.Errorf("self-enrollment failed: %w", err)
84103
}
85-
reqs.Config.Set("privateactionrunner.private_key", updatedCfg.PrivateKey, model.SourceAgentRuntime)
86-
reqs.Config.Set("privateactionrunner.urn", updatedCfg.Urn, model.SourceAgentRuntime)
104+
coreConfig.Set("privateactionrunner.private_key", updatedCfg.PrivateKey, model.SourceAgentRuntime)
105+
coreConfig.Set("privateactionrunner.urn", updatedCfg.Urn, model.SourceAgentRuntime)
87106
cfg = updatedCfg
88107
} else if cfg.IdentityIsIncomplete() {
89-
return Provides{}, errors.New("identity not found and self-enrollment disabled. Please provide a valid URN and private key")
108+
return nil, errors.New("identity not found and self-enrollment disabled. Please provide a valid URN and private key")
90109
}
91-
reqs.Log.Info("Private action runner starting")
92-
reqs.Log.Info("==> Version : " + parversion.RunnerVersion)
93-
reqs.Log.Info("==> Site : " + cfg.DatadogSite)
94-
reqs.Log.Info("==> URN : " + cfg.Urn)
110+
logger.Info("Private action runner starting")
111+
logger.Info("==> Version : " + parversion.RunnerVersion)
112+
logger.Info("==> Site : " + cfg.DatadogSite)
113+
logger.Info("==> URN : " + cfg.Urn)
95114

96-
keysManager := taskverifier.NewKeyManager(reqs.RcClient)
115+
keysManager := taskverifier.NewKeyManager(rcClient)
97116
taskVerifier := taskverifier.NewTaskVerifier(keysManager, cfg)
98117
opmsClient := opms.NewClient(cfg)
99118

100119
r, err := runners.NewWorkflowRunner(cfg, keysManager, taskVerifier, opmsClient)
101120
if err != nil {
102-
return Provides{}, err
121+
return nil, err
103122
}
104-
runner := &privateactionrunnerImpl{
123+
runner := &PrivateActionRunner{
105124
workflowRunner: r,
106125
commonRunner: runners.NewCommonRunner(cfg),
107126
}
108-
reqs.Lifecycle.Append(compdef.Hook{
109-
OnStart: runner.Start,
110-
OnStop: runner.Stop,
111-
})
112-
return Provides{
113-
Comp: runner,
114-
}, nil
127+
return runner, nil
115128
}
116129

117-
func (p *privateactionrunnerImpl) Start(_ context.Context) error {
130+
func (p *PrivateActionRunner) Start(_ context.Context) error {
118131
// Use background context to avoid inheriting any deadlines from component lifecycle which stop the PAR loop
119132
ctx, cancel := context.WithCancel(context.Background())
120133
p.drain = cancel
@@ -125,7 +138,7 @@ func (p *privateactionrunnerImpl) Start(_ context.Context) error {
125138
return p.workflowRunner.Start(ctx)
126139
}
127140

128-
func (p *privateactionrunnerImpl) Stop(ctx context.Context) error {
141+
func (p *PrivateActionRunner) Stop(ctx context.Context) error {
129142
err := p.workflowRunner.Stop(ctx)
130143
if err != nil {
131144
return err

0 commit comments

Comments
 (0)