Skip to content

Commit

Permalink
CRT op inactivity timeout refactoring and extending imagebuild to sup…
Browse files Browse the repository at this point in the history
…port the simple build engine

Signed-off-by: Kyle Quest <kcq.public@gmail.com>
  • Loading branch information
kcq committed Oct 14, 2024
1 parent 90f6024 commit 9dd7eb3
Show file tree
Hide file tree
Showing 18 changed files with 393 additions and 182 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ Global options:
- `--host` - Docker host address or socket (prefix with `tcp://` or `unix://`)
- `--crt-connection` - Container runtime connection (for non-Docker runtimes / for Docker user --host)
- `--crt-context` - Container runtime context name if supported (for Docker similar to setting '--context' or DOCKER_CONTEXT)
- `--crt-io-inactivity-timeout` - CRT I/O general inactivity timeout.
- `--crt-save-inactivity-timeout` - CRT save image operation inactivity timeout (overrides the general I/O timeout). If not set, defaults to 30 seconds.
- `--crt-copy-inactivity-timeout` - CRT copy from container operation inactivity timeout (overrides the general I/O timeout). If not set, defaults to 30 seconds.
- `--tls` - use TLS connecting to Docker
- `--tls-verify` - do TLS verification
- `--tls-cert-path` - path to TLS cert files
Expand Down
11 changes: 10 additions & 1 deletion pkg/app/master/command/build/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,14 @@ func OnCommand(
}

xc.Out.Info("image.data.inspection.save.image.start")
err = dockerutil.SaveImage(client, imageID, iaPath, false, false)
var saveInactivityTimeout int
if gparams.CRTSaveInactivityTimeout > 0 {
saveInactivityTimeout = gparams.CRTSaveInactivityTimeout
} else if gparams.CRTIOInactivityTimeout > 0 {
saveInactivityTimeout = gparams.CRTIOInactivityTimeout
}

err = dockerutil.SaveImage(client, imageID, iaPath, false, false, saveInactivityTimeout)
errutil.FailOn(err)

err = fsutil.Touch(iaPathReady)
Expand Down Expand Up @@ -1113,6 +1120,8 @@ func OnCommand(
gparams.LogLevel,
gparams.LogFormat,
gparams.InContainer,
gparams.CRTIOInactivityTimeout,
gparams.CRTCopyInactivityTimeout,
rtaSourcePT,
doObfuscateMetadata,
obfuscateAppPackageNames,
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/master/command/build/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func buildOutputImage(
xc.FailOn(err)

opts := imagebuilder.SimpleBuildOptions{
ImageConfig: imagebuilder.ImageConfig{
ImageConfig: &imagebuilder.ImageConfig{
Architecture: imageBuildArch,
Config: imagebuilder.RunConfig{
ExposedPorts: map[string]struct{}{},
Expand Down
65 changes: 45 additions & 20 deletions pkg/app/master/command/cliflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ import (

// Global flag names
const (
FlagCommandReport = "report"
FlagCheckVersion = "check-version"
FlagDebug = "debug"
FlagVerbose = "verbose"
FlagQuietCLIMode = "quiet"
FlagLogLevel = "log-level"
FlagLog = "log"
FlagLogFormat = "log-format"
FlagAPIVersion = "crt-api-version"
FlagUseTLS = "tls"
FlagVerifyTLS = "tls-verify"
FlagTLSCertPath = "tls-cert-path"
FlagHost = "host"
FlagCRTConnection = "crt-connection"
FlagCRTContext = "crt-context"
FlagStatePath = "state-path"
FlagInContainer = "in-container"
FlagArchiveState = "archive-state"
FlagNoColor = "no-color"
FlagOutputFormat = "output-format"
FlagCommandReport = "report"
FlagCheckVersion = "check-version"
FlagDebug = "debug"
FlagVerbose = "verbose"
FlagQuietCLIMode = "quiet"
FlagLogLevel = "log-level"
FlagLog = "log"
FlagLogFormat = "log-format"
FlagAPIVersion = "crt-api-version"
FlagUseTLS = "tls"
FlagVerifyTLS = "tls-verify"
FlagTLSCertPath = "tls-cert-path"
FlagHost = "host"
FlagCRTConnection = "crt-connection"
FlagCRTContext = "crt-context"
FlagStatePath = "state-path"
FlagInContainer = "in-container"
FlagArchiveState = "archive-state"
FlagNoColor = "no-color"
FlagOutputFormat = "output-format"
FlagCRTIOInactivityTimeout = "crt-io-inactivity-timeout"
FlagCRTSaveInactivityTimeout = "crt-save-inactivity-timeout"
FlagCRTCopyInactivityTimeout = "crt-copy-inactivity-timeout"
)

const (
Expand Down Expand Up @@ -64,6 +67,10 @@ const (
FlagInContainerUsage = "app is running in a container"
FlagArchiveStateUsage = "archive app state to the selected Docker volume (default volume - mint-state). By default, enabled when app is running in a container (disabled otherwise). Set it to 'off' to disable explicitly."
FlagNoColorUsage = "disable color output"

FlagCRTIOInactivityTimeoutUsage = "CRT I/O general inactivity timeout"
FlagCRTSaveInactivityTimeoutUsage = "CRT save image operation inactivity timeout (overrides the general I/O timeout)"
FlagCRTCopyInactivityTimeoutUsage = "CRT copy from container operation inactivity timeout (overrides the general I/O timeout)"
)

// Shared command flag names
Expand Down Expand Up @@ -402,6 +409,24 @@ func GlobalFlags() []cli.Flag {
Usage: FlagCRTContextUsage,
EnvVars: []string{"DSLIM_CRT_CTX"},
},
&cli.IntFlag{
Name: FlagCRTIOInactivityTimeout,
Value: 0,
Usage: FlagCRTIOInactivityTimeoutUsage,
EnvVars: []string{"DSLIM_CRT_IO_INACTIVITY_TIMEOUT"},
},
&cli.IntFlag{
Name: FlagCRTSaveInactivityTimeout,
Value: 0,
Usage: FlagCRTSaveInactivityTimeoutUsage,
EnvVars: []string{"DSLIM_CRT_SAVE_INACTIVITY_TIMEOUT"},
},
&cli.IntFlag{
Name: FlagCRTCopyInactivityTimeout,
Value: 0,
Usage: FlagCRTCopyInactivityTimeoutUsage,
EnvVars: []string{"DSLIM_CRT_COPY_INACTIVITY_TIMEOUT"},
},
&cli.StringFlag{
Name: FlagStatePath,
Value: "",
Expand Down
41 changes: 28 additions & 13 deletions pkg/app/master/command/clifvgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,18 @@ func UpdateGlobalFlagValues(appOpts *config.AppOptions, values *GenericParams) *
values.CRTContext = *appOpts.Global.CRTContext
}

if appOpts.Global.CRTIOInactivityTimeout != nil {
values.CRTIOInactivityTimeout = *appOpts.Global.CRTIOInactivityTimeout
}

if appOpts.Global.CRTSaveInactivityTimeout != nil {
values.CRTSaveInactivityTimeout = *appOpts.Global.CRTSaveInactivityTimeout
}

if appOpts.Global.CRTCopyInactivityTimeout != nil {
values.CRTCopyInactivityTimeout = *appOpts.Global.CRTCopyInactivityTimeout
}

if appOpts.Global.UseTLS != nil {
values.ClientConfig.UseTLS = *appOpts.Global.UseTLS
}
Expand All @@ -421,19 +433,22 @@ func UpdateGlobalFlagValues(appOpts *config.AppOptions, values *GenericParams) *

func GlobalFlagValues(ctx *cli.Context) *GenericParams {
values := GenericParams{
NoColor: ctx.Bool(FlagNoColor),
CheckVersion: ctx.Bool(FlagCheckVersion),
Debug: ctx.Bool(FlagDebug),
Verbose: ctx.Bool(FlagVerbose),
QuietCLIMode: ctx.Bool(FlagQuietCLIMode),
LogLevel: ctx.String(FlagLogLevel),
LogFormat: ctx.String(FlagLogFormat),
OutputFormat: ctx.String(FlagOutputFormat),
Log: ctx.String(FlagLog),
StatePath: ctx.String(FlagStatePath),
ReportLocation: ctx.String(FlagCommandReport),
CRTConnection: ctx.String(FlagCRTConnection),
CRTContext: ctx.String(FlagCRTContext),
NoColor: ctx.Bool(FlagNoColor),
CheckVersion: ctx.Bool(FlagCheckVersion),
Debug: ctx.Bool(FlagDebug),
Verbose: ctx.Bool(FlagVerbose),
QuietCLIMode: ctx.Bool(FlagQuietCLIMode),
LogLevel: ctx.String(FlagLogLevel),
LogFormat: ctx.String(FlagLogFormat),
OutputFormat: ctx.String(FlagOutputFormat),
Log: ctx.String(FlagLog),
StatePath: ctx.String(FlagStatePath),
ReportLocation: ctx.String(FlagCommandReport),
CRTConnection: ctx.String(FlagCRTConnection),
CRTContext: ctx.String(FlagCRTContext),
CRTIOInactivityTimeout: ctx.Int(FlagCRTIOInactivityTimeout),
CRTSaveInactivityTimeout: ctx.Int(FlagCRTSaveInactivityTimeout),
CRTCopyInactivityTimeout: ctx.Int(FlagCRTCopyInactivityTimeout),
}

if values.ReportLocation == "off" {
Expand Down
37 changes: 20 additions & 17 deletions pkg/app/master/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,26 @@ func CLIContextGet(ctx context.Context, key CLIContextKey) interface{} {
/////////////////////////////////////////////////////////

type GenericParams struct {
NoColor bool
CheckVersion bool
Debug bool
Verbose bool
QuietCLIMode bool
LogLevel string
LogFormat string
OutputFormat string
Log string
StatePath string
ReportLocation string
InContainer bool
IsDSImage bool
ArchiveState string
CRTConnection string
CRTContext string
ClientConfig *config.DockerClient
NoColor bool
CheckVersion bool
Debug bool
Verbose bool
QuietCLIMode bool
LogLevel string
LogFormat string
OutputFormat string
Log string
StatePath string
ReportLocation string
InContainer bool
IsDSImage bool
ArchiveState string
CRTConnection string
CRTContext string
ClientConfig *config.DockerClient
CRTIOInactivityTimeout int
CRTSaveInactivityTimeout int
CRTCopyInactivityTimeout int
}

// TODO: spread these code types across all command definition, so it's not all defined here
Expand Down
1 change: 0 additions & 1 deletion pkg/app/master/command/merge/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ func OnCommand(

ensureImage("output", outputTags[0], cmdReport)
xc.Out.State("output.image.generate.done")
//////////////////////////////////////////////////

xc.Out.State(cmd.StateCompleted)
cmdReport.State = cmd.StateCompleted
Expand Down
2 changes: 2 additions & 0 deletions pkg/app/master/command/profile/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ func OnCommand(
logLevel,
logFormat,
gparams.InContainer,
gparams.CRTIOInactivityTimeout,
gparams.CRTCopyInactivityTimeout,
true, //rtaSourcePT
false, //doObfuscateMetadata
"", //doObfuscateAPN
Expand Down
8 changes: 7 additions & 1 deletion pkg/app/master/command/xray/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,13 @@ func OnCommand(
}

xc.Out.Info("image.data.inspection.save.image.start")
err = crtClient.SaveImage(imageID, iaPath, false, false)
var saveInactivityTimeout int
if gparams.CRTSaveInactivityTimeout > 0 {
saveInactivityTimeout = gparams.CRTSaveInactivityTimeout
} else if gparams.CRTIOInactivityTimeout > 0 {
saveInactivityTimeout = gparams.CRTIOInactivityTimeout
}
err = crtClient.SaveImage(imageID, iaPath, false, false, saveInactivityTimeout)
errutil.FailOn(err)

err = fsutil.Touch(iaPathReady)
Expand Down
41 changes: 22 additions & 19 deletions pkg/app/master/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ type AppOptions struct {

// GlobalAppOptions provides a set of global application parameters
type GlobalAppOptions struct {
NoColor *bool `json:"no_color,omitempty"`
CheckVersion *bool `json:"check_version,omitempty"`
Debug *bool `json:"debug,omitempty"`
Verbose *bool `json:"verbose,omitempty"`
Quiet *bool `json:"quiet,omitempty"`
OutputFormat *string `json:"output_format,omitempty"`
LogLevel *string `json:"log_level,omitempty"`
Log *string `json:"log,omitempty"`
LogFormat *string `json:"log_format,omitempty"`
UseTLS *bool `json:"tls,omitempty"`
VerifyTLS *bool `json:"tls_verify,omitempty"`
TLSCertPath *string `json:"tls_cert_path,omitempty"`
APIVersion *string `json:"api_version,omitempty"`
Host *string `json:"host,omitempty"`
StatePath *string `json:"state_path,omitempty"`
ReportLocation *string `json:"report_location,omitempty"`
CRTConnection *string `json:"crt_connection,omitempty"`
CRTContext *string `json:"crt_context,omitempty"`
ArchiveState *string `json:"archive_state,omitempty"`
NoColor *bool `json:"no_color,omitempty"`
CheckVersion *bool `json:"check_version,omitempty"`
Debug *bool `json:"debug,omitempty"`
Verbose *bool `json:"verbose,omitempty"`
Quiet *bool `json:"quiet,omitempty"`
OutputFormat *string `json:"output_format,omitempty"`
LogLevel *string `json:"log_level,omitempty"`
Log *string `json:"log,omitempty"`
LogFormat *string `json:"log_format,omitempty"`
UseTLS *bool `json:"tls,omitempty"`
VerifyTLS *bool `json:"tls_verify,omitempty"`
TLSCertPath *string `json:"tls_cert_path,omitempty"`
APIVersion *string `json:"api_version,omitempty"`
Host *string `json:"host,omitempty"`
StatePath *string `json:"state_path,omitempty"`
ReportLocation *string `json:"report_location,omitempty"`
CRTConnection *string `json:"crt_connection,omitempty"`
CRTContext *string `json:"crt_context,omitempty"`
ArchiveState *string `json:"archive_state,omitempty"`
CRTIOInactivityTimeout *int `json:"crt_io_inactivity_timeout,omitempty"`
CRTSaveInactivityTimeout *int `json:"crt_save_inactivity_timeout,omitempty"`
CRTCopyInactivityTimeout *int `json:"crt_copy_inactivity_timeout,omitempty"`
}

func NewAppOptionsFromFile(dir string) (*AppOptions, error) {
Expand Down
19 changes: 16 additions & 3 deletions pkg/app/master/inspectors/container/container_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ type Inspector struct {
LogFormat string
PrintState bool
InContainer bool
CRTIOInactivityTimeout int
CRTCopyInactivityTimeout int
RTASourcePT bool
DoObfuscateMetadata bool
ObfuscateAppPackageNames string
Expand Down Expand Up @@ -221,6 +223,8 @@ func NewInspector(
logLevel string,
logFormat string,
inContainer bool,
crtIOInactivityTimeout int,
crtCopyInactivityTimeout int,
rtaSourcePT bool,
doObfuscateMetadata bool,
obfuscateAppPackageNames string,
Expand Down Expand Up @@ -282,6 +286,8 @@ func NewInspector(
LogFormat: logFormat,
PrintState: printState,
InContainer: inContainer,
CRTIOInactivityTimeout: crtIOInactivityTimeout,
CRTCopyInactivityTimeout: crtCopyInactivityTimeout,
RTASourcePT: rtaSourcePT,
DoObfuscateMetadata: doObfuscateMetadata,
ObfuscateAppPackageNames: obfuscateAppPackageNames,
Expand Down Expand Up @@ -1247,10 +1253,17 @@ func (i *Inspector) ShutdownContainer(terminateOnly bool) error {
deleteOrig = false
}

var copyInactivityTimeout int
if i.CRTCopyInactivityTimeout > 0 {
copyInactivityTimeout = i.CRTCopyInactivityTimeout
} else if i.CRTIOInactivityTimeout > 0 {
copyInactivityTimeout = i.CRTIOInactivityTimeout
}

//copy the container report
reportLocalPath := filepath.Join(i.LocalVolumePath, ArtifactsDir, ReportArtifactTar)
reportRemotePath := filepath.Join(app.DefaultArtifactsDirPath, report.DefaultContainerReportFileName)
err := dockerutil.CopyFromContainer(i.APIClient, i.ContainerID, reportRemotePath, reportLocalPath, true, deleteOrig)
err := dockerutil.CopyFromContainer(i.APIClient, i.ContainerID, reportRemotePath, reportLocalPath, true, deleteOrig, copyInactivityTimeout)
if err != nil {
logger.WithError(err).WithField("container", i.ContainerID).Error("dockerutil.CopyFromContainer")
//can't call errutil.FailOn() because we won't cleanup the target container
Expand All @@ -1261,7 +1274,7 @@ func (i *Inspector) ShutdownContainer(terminateOnly bool) error {
//copy the monitor data event log (if available)
mondelLocalPath := filepath.Join(i.LocalVolumePath, ArtifactsDir, MondelArtifactTar)
mondelRemotePath := filepath.Join(app.DefaultArtifactsDirPath, report.DefaultMonDelFileName)
err = dockerutil.CopyFromContainer(i.APIClient, i.ContainerID, mondelRemotePath, mondelLocalPath, true, deleteOrig)
err = dockerutil.CopyFromContainer(i.APIClient, i.ContainerID, mondelRemotePath, mondelLocalPath, true, deleteOrig, copyInactivityTimeout)
if err != nil {
//not a failure because the log might not be there (just log it)
logger.WithFields(log.Fields{
Expand Down Expand Up @@ -1290,7 +1303,7 @@ func (i *Inspector) ShutdownContainer(terminateOnly bool) error {

filesOutLocalPath := filepath.Join(i.LocalVolumePath, ArtifactsDir, FileArtifactsOutTar)
filesRemotePath := filepath.Join(app.DefaultArtifactsDirPath, app.ArtifactFilesDirName)
err = dockerutil.CopyFromContainer(i.APIClient, i.ContainerID, filesRemotePath, filesOutLocalPath, false, false)
err = dockerutil.CopyFromContainer(i.APIClient, i.ContainerID, filesRemotePath, filesOutLocalPath, false, false, copyInactivityTimeout)
if err != nil {
logger.WithError(err).WithField("container", i.ContainerID).Error("dockerutil.CopyFromContainer")
//can't call errutil.FailOn() because we won't cleanup the target container
Expand Down
2 changes: 1 addition & 1 deletion pkg/crt/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type InspectorAPIClient interface {
}

type ImageSaverAPIClient interface {
SaveImage(imageRef, localPath string, extract, removeOrig bool) error
SaveImage(imageRef, localPath string, extract, removeOrig bool, inactivityTimeout int) error
}

type ImageLoaderAPIClient interface {
Expand Down
4 changes: 2 additions & 2 deletions pkg/crt/docker/dockercrtclient/dockercrtclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ func (ref *Instance) GetRegistryAuthConfig(account, secret, configPath, registry
return cred, nil
}

func (ref *Instance) SaveImage(imageRef, localPath string, extract, removeOrig bool) error {
err := dockerutil.SaveImage(ref.pclient, imageRef, localPath, extract, removeOrig)
func (ref *Instance) SaveImage(imageRef, localPath string, extract, removeOrig bool, inactivityTimeout int) error {
err := dockerutil.SaveImage(ref.pclient, imageRef, localPath, extract, removeOrig, inactivityTimeout)
if err != nil {
if err == dockerutil.ErrBadParam {
err = crt.ErrBadParam
Expand Down
Loading

0 comments on commit 9dd7eb3

Please sign in to comment.