-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
242 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package artifact | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"k8s.io/kubernetes/pkg/volume/util/fs" | ||
|
||
"github.com/caicloud/cyclone/pkg/server/common" | ||
"github.com/caicloud/nirvana/log" | ||
) | ||
|
||
// Manager manages artifacts | ||
type Manager struct { | ||
cleanPeriod time.Duration | ||
artifactHomeDir string | ||
} | ||
|
||
// NewManager Initiates a artifacts manager. | ||
// If artifactHomeDir not passed, the default '/var/lib/cyclone' will be used. | ||
func NewManager(artifactHomeDir ...string) *Manager { | ||
var home = common.CycloneHome | ||
if artifactHomeDir != nil && artifactHomeDir[0] != "" { | ||
home = artifactHomeDir[0] | ||
} | ||
m := &Manager{ | ||
cleanPeriod: time.Duration(time.Hour), | ||
artifactHomeDir: home, | ||
} | ||
|
||
return m | ||
} | ||
|
||
// CleanPeriodically will clean up artifacts which exceeded retention time periodically. | ||
// This func will run forever unless panics, you'd better invoke it by a go-routine. | ||
func (m *Manager) CleanPeriodically(retention time.Duration) { | ||
t := time.NewTicker(time.Duration(time.Hour)) | ||
defer t.Stop() | ||
|
||
for ; true; <-t.C { | ||
log.Info("Start to scan and clean artifacts") | ||
if err := m.scanAndClean(selectArtifact, retention); err != nil { | ||
log.Warningf("Clean artifacts error: ", err) | ||
} | ||
} | ||
} | ||
|
||
type artifactSelector func(path string) bool | ||
|
||
func selectArtifact(path string) bool { | ||
path = strings.TrimPrefix(path, "/") | ||
// artifact path must be in format of {tenant}/{project}/{workflow}/{workflowrun}/artifacts/{stage}/xxx | ||
splitPaths := strings.SplitN(path, "/", 7) | ||
|
||
if len(splitPaths) < 7 { | ||
return false | ||
} | ||
|
||
if splitPaths[4] != "artifacts" { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// scanAndClean scans the artifacts folders and finds out exceeded retention time artifacts, and | ||
// then delete them. | ||
func (m *Manager) scanAndClean(selectArtifact artifactSelector, retention time.Duration) error { | ||
return filepath.Walk(m.artifactHomeDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return fmt.Errorf("walk in %s error: %v", path, err) | ||
} | ||
|
||
if !selectArtifact(strings.TrimPrefix(path, m.artifactHomeDir)) { | ||
return nil | ||
} | ||
|
||
if time.Now().Before(info.ModTime().Add(retention)) { | ||
return nil | ||
} | ||
|
||
log.Infof("Start to remove artifact: %s", path) | ||
return os.RemoveAll(path) | ||
}) | ||
} | ||
|
||
// GetDiskAvailablePercentage returns available space in percentage format of the artifact home folder. | ||
func (m *Manager) GetDiskAvailablePercentage() (float64, error) { | ||
s := time.Now() | ||
|
||
available, capacity, _, _, _, _, err := fs.FsInfo(m.artifactHomeDir) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
e := time.Now() | ||
log.Infof("fsInfo cost time: %s, available: %d, capacity: %d, percentage: %v", | ||
e.Sub(s), available, capacity, float64(available)/float64(capacity)) | ||
|
||
return float64(available) / float64(capacity), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package artifact | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestScanAndClean(t *testing.T) { | ||
manager := NewManager("testdata") | ||
err := manager.scanAndClean(selectArtifact, time.Duration(0)) | ||
assert.Nil(t, err) | ||
|
||
_, err = os.Stat("testdata/tenant1/project1/wf1/wfr1/artifacts/stage1/artifacts.tar") | ||
assert.NotNil(t, err) | ||
assert.True(t, os.IsNotExist(err)) | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.