Add config option for import timeout (#421)

Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
Dan Luhring 2021-05-25 15:01:02 -04:00 committed by GitHub
parent 17bbf840cf
commit 80e23bdb50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 2 deletions

View File

@ -137,6 +137,11 @@ func setPackageFlags(flags *pflag.FlagSet) {
"overwrite-existing-image", false, "overwrite-existing-image", false,
"overwrite an existing image during the upload to Anchore Enterprise", "overwrite an existing image during the upload to Anchore Enterprise",
) )
flags.Uint(
"import-timeout", 30,
"set a timeout duration (in seconds) for the upload to Anchore Enterprise",
)
} }
func bindPackagesConfigOptions(flags *pflag.FlagSet) error { func bindPackagesConfigOptions(flags *pflag.FlagSet) error {
@ -172,6 +177,10 @@ func bindPackagesConfigOptions(flags *pflag.FlagSet) error {
return err return err
} }
if err := viper.BindPFlag("anchore.import-timeout", flags.Lookup("import-timeout")); err != nil {
return err
}
return nil return nil
} }
@ -262,6 +271,7 @@ func runPackageSbomUpload(src source.Source, s source.Metadata, catalog *pkg.Cat
Dockerfile: dockerfileContents, Dockerfile: dockerfileContents,
OverwriteExistingUpload: appConfig.Anchore.OverwriteExistingImage, OverwriteExistingUpload: appConfig.Anchore.OverwriteExistingImage,
Scope: scope, Scope: scope,
Timeout: appConfig.Anchore.ImportTimeout,
} }
if err := c.Import(context.Background(), importCfg); err != nil { if err := c.Import(context.Background(), importCfg); err != nil {

View File

@ -27,6 +27,7 @@ type ImportConfig struct {
Dockerfile []byte Dockerfile []byte
OverwriteExistingUpload bool OverwriteExistingUpload bool
Scope source.Scope Scope source.Scope
Timeout uint
} }
func importProgress(source string) (*progress.Stage, *progress.Manual) { func importProgress(source string) (*progress.Stage, *progress.Manual) {
@ -54,7 +55,8 @@ func importProgress(source string) (*progress.Stage, *progress.Manual) {
func (c *Client) Import(ctx context.Context, cfg ImportConfig) error { func (c *Client) Import(ctx context.Context, cfg ImportConfig) error {
stage, prog := importProgress(c.config.BaseURL) stage, prog := importProgress(c.config.BaseURL)
ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*30) timeout := time.Duration(cfg.Timeout) * time.Second
ctxWithTimeout, cancel := context.WithTimeout(ctx, timeout)
defer cancel() defer cancel()
authedCtx := c.newRequestContext(ctxWithTimeout) authedCtx := c.newRequestContext(ctxWithTimeout)

View File

@ -12,6 +12,8 @@ type anchore struct {
Password string `yaml:"-" json:"-" mapstructure:"password"` // -p , password to authenticate upload Password string `yaml:"-" json:"-" mapstructure:"password"` // -p , password to authenticate upload
Dockerfile string `yaml:"dockerfile" json:"dockerfile" mapstructure:"dockerfile"` // -d , dockerfile to attach for upload Dockerfile string `yaml:"dockerfile" json:"dockerfile" mapstructure:"dockerfile"` // -d , dockerfile to attach for upload
OverwriteExistingImage bool `yaml:"overwrite-existing-image" json:"overwrite-existing-image" mapstructure:"overwrite-existing-image"` // --overwrite-existing-image , if any of the SBOM components have already been uploaded this flag will ensure they are overwritten with the current upload OverwriteExistingImage bool `yaml:"overwrite-existing-image" json:"overwrite-existing-image" mapstructure:"overwrite-existing-image"` // --overwrite-existing-image , if any of the SBOM components have already been uploaded this flag will ensure they are overwritten with the current upload
ImportTimeout uint `yaml:"import-timeout" json:"import-timeout" mapstructure:"import-timeout"` // --import-timeout
// , customize the number of seconds within which the SBOM import must be completed or canceled
} }
func (cfg anchore) loadDefaultValues(v *viper.Viper) { func (cfg anchore) loadDefaultValues(v *viper.Viper) {

View File

@ -174,7 +174,8 @@
"host": "", "host": "",
"path": "", "path": "",
"dockerfile": "", "dockerfile": "",
"overwrite-existing-image": false "overwrite-existing-image": false,
"import-timeout": 0
}, },
"dev": { "dev": {
"profile-cpu": false, "profile-cpu": false,