Skip to content

Commit

Permalink
Merge pull request #190 from TaichiHo/feature/janitor-resource-config
Browse files Browse the repository at this point in the history
Allow janitor to reload resource types from a config file
  • Loading branch information
k8s-ci-robot authored Apr 16, 2024
2 parents 786da16 + 32afae1 commit 59dbd6c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
15 changes: 12 additions & 3 deletions cmd/janitor/janitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
var (
bufferSize = 1 // Maximum holding resources
rTypes common.CommaSeparatedStrings
rTypesConfig string
poolSize int
updateFrequency time.Duration
janitorPath = flag.String("janitor-path", "/bin/gcp_janitor.py", "Path to janitor binary path")
Expand All @@ -44,6 +45,8 @@ var (

func init() {
flag.Var(&rTypes, "resource-type", "comma-separated list of resources need to be cleaned up")
flag.StringVar(&rTypesConfig, "resource-type-from-config", "", "extract a list of resources need to be cleaned up from boskos' config file; "+
"if both resource-type and resource-type-from-config are specified, resource-type will take priority")
flag.IntVar(&poolSize, "pool-size", 20, "number of concurrent janitor goroutine")
flag.DurationVar(&updateFrequency, "update-frequency", 5*time.Minute, "How often to heartbeat owning resources.")
}
Expand All @@ -66,10 +69,16 @@ func main() {
}
logrus.Info("Initialized boskos client!")

if len(rTypes) == 0 {
logrus.Fatal("--resource-type must not be empty!")
if len(rTypes) == 0 && rTypesConfig == "" {
logrus.Fatal("--resource-type or --resource-type-from-config must be set")
}

var rt common.ResourceTypes
if rt, err = common.NewResourceTypes(rTypes, rTypesConfig); err != nil {
logrus.WithError(err).Fatal("new resource types")
}
logrus.Infof("initialized resource types: %+v", rt.Types())

go func(boskos boskosClient) {
for range time.Tick(updateFrequency) {
if err := boskos.SyncAll(); err != nil {
Expand All @@ -81,7 +90,7 @@ func main() {
buffer := setup(boskos, poolSize, bufferSize, janitorClean, extraJanitorFlags)

for {
run(boskos, buffer, rTypes)
run(boskos, buffer, rt.Types())
time.Sleep(time.Minute)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/reaper/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func main() {
logrus.Fatal("--target-state must not be empty!")
}

var rt ResourceTypes
if rt, err = NewResourceTypes(rTypes, rTypesConfig); err != nil {
var rt common.ResourceTypes
if rt, err = common.NewResourceTypes(rTypes, rTypesConfig); err != nil {
logrus.WithError(err).Fatal("new resource types")
}
logrus.Info("initialized resource types")
Expand Down
7 changes: 3 additions & 4 deletions cmd/reaper/resource_types.go → common/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package common

import (
"fmt"
Expand All @@ -24,7 +24,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/boskos/common"
)

type ResourceTypes interface {
Expand Down Expand Up @@ -105,11 +104,11 @@ func (rtw *resourceTypesWatcher) Types() []string {

func parseResourceTypesFromConfig(config string) ([]string, error) {
types := make([]string, 0)
cfg, err := common.ParseConfig(config)
cfg, err := ParseConfig(config)
if err != nil {
return types, fmt.Errorf("parse config %q: %w", config, err)
}
if err := common.ValidateConfig(cfg); err != nil {
if err := ValidateConfig(cfg); err != nil {
return types, fmt.Errorf("validate config %q: %w", config, err)
}
for _, r := range cfg.Resources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package common

import (
"io/ioutil"
Expand Down

0 comments on commit 59dbd6c

Please sign in to comment.