8000 gc: Add flag 'mark-only' to mark garbage pods without deleting them. by yifan-gu · Pull Request #2400 · rkt/rkt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 24, 2020. It is now read-only.

gc: Add flag 'mark-only' to mark garbage pods without deleting them. #2400

Merged
merged 1 commit into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Documentation/subcommands/gc.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Garbage collecting pod "f07a4070-79a9-4db0-ae65-a090c9c393a3"
| --- | --- | --- | --- |
| `--expire-prepared` | `24h0m0s` | A time | Duration to wait before expiring prepared pods |
| `--grace-period` | `30m0s` | A time | Duration to wait before discarding inactive pods from garbage |
| `--mark-only` | `false` | If set to true, then the exited/aborted pods will be moved to the garbage directories without actually deleting them, this is useful for marking the exit time of a pod |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the wording, better to reference the phases/passes: https://github.com/coreos/rkt/blob/master/Documentation/devel/pod-lifecycle.md#garbage-collection

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If set to true, only the "mark" phase of the garbage collection process will be formed (i.e., exited/aborted pods will be moved to the garbage, but nothing will be deleted)

or similar


## Global options

Expand Down
6 changes: 6 additions & 0 deletions rkt/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ Use --grace-period=0s to effectively disable the grace-period.`,
}
flagGracePeriod time.Duration
flagPreparedExpiration time.Duration
flagMarkOnly bool
)

func init() {
cmdRkt.AddCommand(cmdGC)
cmdGC.Flags().DurationVar(&flagGracePeriod, "grace-period", defaultGracePeriod, "duration to wait before discarding inactive pods from garbage")
cmdGC.Flags().DurationVar(&flagPreparedExpiration, "expire-prepared", defaultPreparedExpiration, "duration to wait before expiring prepared pods")
cmdGC.Flags().BoolVar(&flagMarkOnly, "mark-only", false, "if set to true, then the exited/aborted pods will be moved to the garbage directories without actually deleting them, this is useful for marking the exit time of a pod")
}

func runGC(cmd *cobra.Command, args []string) (exit int) {
Expand All @@ -67,6 +69,10 @@ func runGC(cmd *cobra.Command, args []string) (exit int) {
return 1
}

if flagMarkOnly {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why skip renameExpired?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonboulle If we don't skip renameExpired, then when doing gc --mark-only, we need to give a very long time duration to --expired-period, otherwise it will remove some just prepared pods.
I would like gc --mark-only to be called without other flags.

return
}

if err := renameExpired(flagPreparedExpiration); err != nil {
stderr.PrintE("failed to rename expired prepared pods", err)
return 1
Expand Down
74 changes: 74 additions & 0 deletions tests/rkt_gc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2015 The rkt Authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2016

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/coreos/rkt/tests/testutils"
)

func TestGC(t *testing.T) {
ctx := testutils.NewRktRunCtx()
defer ctx.Cleanup()

// Finished pods.
patchImportAndRun("inspect-gc-test-run.aci", []string{"--exec=/inspect --print-msg=HELLO_API --exit-code=0"}, t, ctx)

// Prepared pods.
patchImportAndPrepare("inspect-gc-test-prepare.aci", []string{"--exec=/inspect --print-msg=HELLO_API --exit-code=0"}, t, ctx)

// Abort prepare.
imagePath := patchTestACI("inspect-gc-test-abort.aci", []string{"--exec=/inspect --print-msg=HELLO_API --exit-code=0"}...)
defer os.Remove(imagePath)
cmd := fmt.Sprintf("%s --insecure-options=image prepare %s %s", ctx.Cmd(), imagePath, imagePath)
spawnAndWaitOrFail(t, cmd, 1)

gcCmd := fmt.Sprintf("%s gc --mark- --expire-prepared=0 --grace-period=0", ctx.Cmd())
spawnAndWaitOrFail(t, gcCmd, 0)

gcDirs := []string{
filepath.Join(ctx.DataDir(), "pods", "exited-garbage"),
filepath.Join(ctx.DataDir(), "pods", "prepared"),
filepath.Join(ctx.DataDir(), "pods", "garbage"),
}

for _, dir := range gcDirs {
pods, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("cannot read gc directory %q: %v", dir, err)
}
if len(pods) == 0 {
t.Fatalf("pods should still exist in directory %q", dir)
}
}

gcCmd = fmt.Sprintf("%s gc --mark- --expire-prepared=0 --grace-period=0", ctx.Cmd())
spawnAndWaitOrFail(t, gcCmd, 0)

for _, dir := range gcDirs {
pods, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("cannot read gc directory %q: %v", dir, err)
}
if len(pods) != 0 {
t.Fatalf("no pods should exist in directory %q, but found: %v", dir, pods)
}
}
}
8 changes: 8 additions & 0 deletions tests/rkt_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ func patchImportAndRun(image string, patches []string, t *testing.T, ctx *testut
spawnAndWaitOrFail(t, cmd, 0)
}

func patchImportAndPrepare(image string, patches []string, t *testing.T, ctx *testutils.RktRunCtx) {
imagePath := patchTestACI(image, patches...)
defer os.Remove(imagePath)

cmd := fmt.Sprintf("%s --insecure-options=image prepare %s", ctx.Cmd(), imagePath)
spawnAndWaitOrFail(t, cmd, 0)
}

func runGC(t *testing.T, ctx *testutils.RktRunCtx) {
cmd := fmt.Sprintf("%s gc --grace-period=0s", ctx.Cmd())
spawnAndWaitOrFail(t, cmd, 0)
Expand Down
0