Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task/container restartability #194

Merged
merged 1 commit into from
May 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions launcher/container_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ func (r *ContainerRunner) measureContainerClaims(ctx context.Context) error {
if err := r.attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.ImageDigestType, EventContent: []byte(image.Target().Digest)}); err != nil {
return err
}
r.attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.RestartPolicyType, EventContent: []byte(r.launchSpec.RestartPolicy)})
if err := r.attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.RestartPolicyType, EventContent: []byte(r.launchSpec.RestartPolicy)}); err != nil {
return err
}
if imageConfig, err := image.Config(ctx); err == nil { // if NO error
if err := r.attestAgent.MeasureEvent(cel.CosTlv{EventType: cel.ImageIDType, EventContent: []byte(imageConfig.Digest)}); err != nil {
return err
Expand Down Expand Up @@ -286,7 +288,6 @@ func (r *ContainerRunner) fetchAndWriteToken(ctx context.Context) error {
}

// Run the container
// Doesn't support container restart yet
// Container output will always be redirected to stdio for now
func (r *ContainerRunner) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
Expand All @@ -300,30 +301,36 @@ func (r *ContainerRunner) Run(ctx context.Context) error {
return fmt.Errorf("failed to fetch and write OIDC token: %v", err)
}

task, err := r.container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
return err
}
defer task.Delete(ctx)

exitStatus, err := task.Wait(ctx)
if err != nil {
return err
}
log.Println("task started")

if err := task.Start(ctx); err != nil {
return err
}
status := <-exitStatus
for {
task, err := r.container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
return err
}
exitStatus, err := task.Wait(ctx)
if err != nil {
return err
}
log.Println("task started")

code, _, err := status.Result()
if err != nil {
return err
}
if err := task.Start(ctx); err != nil {
return err
}
status := <-exitStatus

if code != 0 {
return fmt.Errorf("task ended with non-zero return code %d", code)
code, _, err := status.Result()
if err != nil {
return err
}
task.Delete(ctx)

log.Printf("task ended with return code %d \n", code)
if r.launchSpec.RestartPolicy == spec.Always {
log.Println("restarting task")
} else if r.launchSpec.RestartPolicy == spec.OnFailure && code != 0 {
log.Println("restarting task on failure")
} else {
break
}
}

return nil
Expand Down