forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjanitor.go
143 lines (117 loc) · 3.48 KB
/
janitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2017 The Kubernetes Authors.
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 (
"flag"
"fmt"
"os/exec"
"time"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/boskos/client"
"k8s.io/test-infra/boskos/common"
)
var (
bufferSize = 1 // Maximum holding resources
serviceAccount = flag.String("service-account", "", "Path to projects service account")
)
var rTypes common.ResTypes
var poolSize int
func init() {
flag.Var(&rTypes, "resource-type", "comma-separated list of resources need to be cleaned up")
flag.IntVar(&poolSize, "pool-size", 20, "number of concurrent janitor goroutine")
}
func main() {
logrus.SetFormatter(&logrus.JSONFormatter{})
boskos := client.NewClient("Janitor", "http://boskos")
logrus.Info("Initialized boskos client!")
// Activate service account
flag.Parse()
if *serviceAccount == "" {
logrus.Fatal("--service-account cannot be empty!")
}
if len(rTypes) == 0 {
logrus.Fatal("--resource-type must not be empty!")
}
cmd := exec.Command("gcloud", "auth", "activate-service-account", "--key-file="+*serviceAccount)
if b, err := cmd.CombinedOutput(); err != nil {
logrus.WithError(err).Fatalf("fail to activate service account from %s :%s", *serviceAccount, string(b))
}
buffer := setup(boskos, poolSize, bufferSize, janitorClean)
for {
run(boskos, buffer, rTypes)
time.Sleep(time.Minute)
}
}
type clean func(string) error
// Clean by janitor script
func janitorClean(proj string) error {
cmd := exec.Command("/bin/janitor.py", fmt.Sprintf("--project=%s", proj), "--hour=0")
err := cmd.Run()
if err != nil {
logrus.WithError(err).Errorf("failed to clean up project %s", proj)
} else {
logrus.Infof("successfully cleaned up project %s", proj)
}
return err
}
type boskosClient interface {
Acquire(rtype string, state string, dest string) (string, error)
ReleaseOne(name string, dest string) error
}
func setup(c boskosClient, janitorCount int, bufferSize int, cleanFunc clean) chan string {
buffer := make(chan string, 1)
for i := 0; i < janitorCount; i++ {
go janitor(c, buffer, cleanFunc)
}
return buffer
}
func run(c boskosClient, buffer chan string, rtypes []string) int {
totalAcquire := 0
res := make(map[string]int)
for _, s := range rtypes {
res[s] = 0
}
for {
for r := range res {
if proj, err := c.Acquire(r, "dirty", "cleaning"); err != nil {
logrus.WithError(err).Error("boskos acquire failed!")
totalAcquire += res[r]
delete(res, r)
} else if proj == "" {
totalAcquire += res[r]
delete(res, r)
} else {
buffer <- proj // will block until buffer has a free slot
res[r]++
}
}
if len(res) == 0 {
break
}
}
return totalAcquire
}
// async janitor goroutine
func janitor(c boskosClient, buffer chan string, fn clean) {
for {
proj := <-buffer
dest := "free"
if err := fn(proj); err != nil {
logrus.WithError(err).Error("janitor.py failed!")
dest = "dirty"
}
if err := c.ReleaseOne(proj, dest); err != nil {
logrus.WithError(err).Error("boskos release failed!")
}
}
}