Skip to content

Commit 8572bca

Browse files
committed
feat(cli): exposes max concurrency and timeout as config options
Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
1 parent cdbac88 commit 8572bca

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

cmd/c2pcli/cli/subcommands/config.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import (
2424
"github.com/oscal-compass/compliance-to-policy-go/v2/framework/actions"
2525
)
2626

27-
// Plugin running times might be highly variable this is maximum timeout value.
28-
var pluginTimeout = 5 * time.Minute
29-
3027
// Config returns a populated C2PConfig for the CLI to use.
3128
func Config(option *Options) (*framework.C2PConfig, error) {
3229
c2pConfig := framework.DefaultConfig()
@@ -40,7 +37,7 @@ func Config(option *Options) (*framework.C2PConfig, error) {
4037
return c2pConfig, nil
4138
}
4239

43-
func Context(ap *oscalTypes.AssessmentPlan) (*actions.InputContext, error) {
40+
func Context(option *Options, ap *oscalTypes.AssessmentPlan) (*actions.InputContext, error) {
4441
if ap.LocalDefinitions == nil || ap.LocalDefinitions.Activities == nil || ap.AssessmentAssets.Components == nil {
4542
return nil, errors.New("error converting component definition to assessment plan")
4643
}
@@ -59,6 +56,12 @@ func Context(ap *oscalTypes.AssessmentPlan) (*actions.InputContext, error) {
5956
apSettings := settings.NewAssessmentActivitiesSettings(*ap.LocalDefinitions.Activities)
6057
inputCtx.Settings = apSettings
6158

59+
// Set the max concurrency if set by the user
60+
if option.AdvancedOptions.MaxConcurrency != 0 {
61+
option.logger.Debug("Setting max concurrency", "max", option.AdvancedOptions.MaxPluginTimeout)
62+
inputCtx.MaxConcurrency = option.AdvancedOptions.MaxConcurrency
63+
}
64+
6265
return inputCtx, nil
6366
}
6467

@@ -114,3 +117,14 @@ func loadPlan(path string) (*oscalTypes.AssessmentPlan, error) {
114117
}
115118
return plan, nil
116119
}
120+
121+
func maxTimeout(options *Options) time.Duration {
122+
// Plugin running times might be highly variable.
123+
// This is default maximum timeout value.
124+
pluginTimeout := 5
125+
if options.AdvancedOptions.MaxPluginTimeout != 0 {
126+
pluginTimeout = options.AdvancedOptions.MaxPluginTimeout
127+
}
128+
options.logger.Debug("Setting plugin timeout:", "minutes", pluginTimeout)
129+
return time.Duration(pluginTimeout) * time.Minute
130+
}

cmd/c2pcli/cli/subcommands/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ type Options struct {
5353
AssessmentResults string `yaml:"assessment-results" mapstructure:"assessment-results"`
5454
Plugins map[string]map[string]string `yaml:"plugins" mapstructure:"plugins"`
5555
Output string `yaml:"out" mapstructure:"out"`
56+
AdvancedOptions AdvancedOptions `yaml:"advanced" mapstructure:"advanced"`
5657
logger hclog.Logger
5758
}
5859

60+
type AdvancedOptions struct {
61+
// MaxConcurrency defines the maximum number of goroutines for plugin operations
62+
MaxConcurrency int `yaml:"max-concurrency" mapstructure:"max-concurrency"`
63+
// MaxPluginTimeout is maximum time a plugin has to complete operations in minutes.
64+
MaxPluginTimeout int `yaml:"max-plugin-timeout" mapstructure:"max-plugin-timeout"`
65+
}
66+
5967
// NewOptions returns an initialized Options struct.
6068
func NewOptions() *Options {
6169
return &Options{

cmd/c2pcli/cli/subcommands/oscal2policy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func runOSCAL2Policy(ctx context.Context, option *Options) error {
5959
return err
6060
}
6161

62-
inputContext, err := Context(plan)
62+
inputContext, err := Context(option, plan)
6363
if err != nil {
6464
return err
6565
}
@@ -83,7 +83,7 @@ func runOSCAL2Policy(ctx context.Context, option *Options) error {
8383
return err
8484
}
8585

86-
pluginCtx, cancel := context.WithTimeout(ctx, pluginTimeout)
86+
pluginCtx, cancel := context.WithTimeout(ctx, maxTimeout(option))
8787
defer cancel()
8888

8989
err = actions.GeneratePolicy(pluginCtx, inputContext, launchedPlugins)

cmd/c2pcli/cli/subcommands/result2oscal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func runResult2Policy(ctx context.Context, option *Options) error {
6666
if err != nil {
6767
return err
6868
}
69-
inputContext, err := Context(plan)
69+
inputContext, err := Context(option, plan)
7070
if err != nil {
7171
return err
7272
}
@@ -90,7 +90,7 @@ func runResult2Policy(ctx context.Context, option *Options) error {
9090
return err
9191
}
9292

93-
pluginCtx, cancel := context.WithTimeout(ctx, pluginTimeout)
93+
pluginCtx, cancel := context.WithTimeout(ctx, maxTimeout(option))
9494
defer cancel()
9595

9696
results, err := actions.AggregateResults(pluginCtx, inputContext, launchedPlugins)

docs/c2p-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ plugins:
1313
policy-results-dir: ./internal/testdata/kyverno/policy-reports
1414
temp-dir: /tmp/kyverno
1515
output-dir: /tmp/outputs
16+
advanced:
17+
max-plugin-timeout: 3

0 commit comments

Comments
 (0)