From 9e65499c103413ff68e0f9284bba89345cb79dae Mon Sep 17 00:00:00 2001 From: Yan Date: Mon, 16 Jul 2018 18:08:40 +0800 Subject: [PATCH] Add garbage collection job implemention, this job could (#5268) be triggered by manual and schedule. It calls registrtctl to do the GC job, and log the output. --- src/common/job/const.go | 2 + src/jobservice/job/impl/gc/job.go | 70 +++++++++++++++++++++++++++++ src/jobservice/runtime/bootstrap.go | 2 + 3 files changed, 74 insertions(+) create mode 100644 src/jobservice/job/impl/gc/job.go diff --git a/src/common/job/const.go b/src/common/job/const.go index 13ef4169c..b495c81ac 100644 --- a/src/common/job/const.go +++ b/src/common/job/const.go @@ -9,6 +9,8 @@ const ( ImageDelete = "IMAGE_DELETE" // ImageReplicate : the name of image replicate job in job service ImageReplicate = "IMAGE_REPLICATE" + // ImageGC the name of image garbage collection job in job service + ImageGC = "IMAGE_GC" //JobKindGeneric : Kind of generic job JobKindGeneric = "Generic" diff --git a/src/jobservice/job/impl/gc/job.go b/src/jobservice/job/impl/gc/job.go new file mode 100644 index 000000000..4a55f3b29 --- /dev/null +++ b/src/jobservice/job/impl/gc/job.go @@ -0,0 +1,70 @@ +// Copyright (c) 2017 VMware, Inc. All Rights Reserved. +// +// 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 gc + +import ( + "github.com/vmware/harbor/src/common/registryctl" + "github.com/vmware/harbor/src/jobservice/env" + "github.com/vmware/harbor/src/jobservice/logger" + "github.com/vmware/harbor/src/registryctl/client" +) + +// GarbageCollector is the struct to run registry's garbage collection +type GarbageCollector struct { + registryCtlClient client.Client + logger logger.Interface +} + +// MaxFails implements the interface in job/Interface +func (gc *GarbageCollector) MaxFails() uint { + return 1 +} + +// ShouldRetry implements the interface in job/Interface +func (gc *GarbageCollector) ShouldRetry() bool { + return false +} + +// Validate implements the interface in job/Interface +func (gc *GarbageCollector) Validate(params map[string]interface{}) error { + return nil +} + +// Run implements the interface in job/Interface +func (gc *GarbageCollector) Run(ctx env.JobContext, params map[string]interface{}) error { + if err := gc.init(ctx); err != nil { + return err + } + if err := gc.registryCtlClient.Health(); err != nil { + gc.logger.Errorf("failed to start gc as regsitry controller is unreachable: %v", err) + return err + } + gc.logger.Infof("start to run gc in job.") + gcr, err := gc.registryCtlClient.StartGC() + if err != nil { + gc.logger.Errorf("failed to get gc result: %v", err) + return err + } + gc.logger.Infof("GC results: status: %t, message: %s, start: %s, end: %s.", gcr.Status, gcr.Msg, gcr.StartTime, gcr.EndTime) + gc.logger.Infof("success to run gc in job.") + return nil +} + +func (gc *GarbageCollector) init(ctx env.JobContext) error { + registryctl.Init() + gc.registryCtlClient = registryctl.RegistryCtlClient + gc.logger = ctx.GetLogger() + return nil +} diff --git a/src/jobservice/runtime/bootstrap.go b/src/jobservice/runtime/bootstrap.go index c4f9ccc02..ca6cac41a 100644 --- a/src/jobservice/runtime/bootstrap.go +++ b/src/jobservice/runtime/bootstrap.go @@ -17,6 +17,7 @@ import ( "github.com/vmware/harbor/src/jobservice/core" "github.com/vmware/harbor/src/jobservice/env" "github.com/vmware/harbor/src/jobservice/job/impl" + "github.com/vmware/harbor/src/jobservice/job/impl/gc" "github.com/vmware/harbor/src/jobservice/job/impl/replication" "github.com/vmware/harbor/src/jobservice/job/impl/scan" "github.com/vmware/harbor/src/jobservice/logger" @@ -186,6 +187,7 @@ func (bs *Bootstrap) loadAndRunRedisWorkerPool(ctx *env.Context, cfg *config.Con job.ImageTransfer: (*replication.Transfer)(nil), job.ImageDelete: (*replication.Deleter)(nil), job.ImageReplicate: (*replication.Replicator)(nil), + job.ImageGC: (*gc.GarbageCollector)(nil), }); err != nil { //exit return nil, err