diff --git a/builder/clean/clean.go b/builder/clean/clean.go index 46119e247b..a0cd917978 100644 --- a/builder/clean/clean.go +++ b/builder/clean/clean.go @@ -19,21 +19,27 @@ package clean import ( + "bytes" "context" + "errors" + "github.com/containerd/containerd/errdefs" + dockercli "github.com/docker/docker/client" "github.com/goodrain/rainbond/builder" + "github.com/goodrain/rainbond/builder/sources" "github.com/goodrain/rainbond/builder/sources/registry" + "github.com/goodrain/rainbond/db" + "github.com/goodrain/rainbond/util" + "github.com/sirupsen/logrus" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/remotecommand" "os" "strings" "time" - - "github.com/sirupsen/logrus" - - "github.com/goodrain/rainbond/db" - "github.com/goodrain/rainbond/util" - - "github.com/goodrain/rainbond/builder/sources" ) // Manager CleanManager @@ -95,21 +101,15 @@ func (t *Manager) Start(errchan chan error) error { continue } } - // registry garbage-collect - cmd := []string{"registry", "garbage-collect", "/etc/docker/registry/config.yml"} - out, b, err := reg.PodExecCmd(t.config, t.clientset, "rbd-hub", cmd) - if err != nil { - logrus.Error("rbd-hub exec cmd fail: ", out.String(), b.String(), err.Error()) - continue - } else { - logrus.Info("rbd-hub exec cmd success.") - } } err := t.imageClient.ImageRemove(v.DeliveredPath) - if err != nil { + + // 如果删除镜像失败 并且不是镜像不存在的错误 + if err != nil && !(errdefs.IsNotFound(err) || dockercli.IsErrNotFound(err)) { logrus.Error(err) continue } + if err := db.GetManager().VersionInfoDao().DeleteVersionInfo(v); err != nil { logrus.Error(err) continue @@ -120,8 +120,11 @@ func (t *Manager) Start(errchan chan error) error { if v.DeliveredType == "slug" { filePath := v.DeliveredPath if err := os.Remove(filePath); err != nil { - logrus.Error(err) - continue + // 如果删除文件失败 并且不是文件不存在的错误 + if !errors.Is(err, os.ErrNotExist) { + logrus.Error(err) + continue + } } if err := db.GetManager().VersionInfoDao().DeleteVersionInfo(v); err != nil { logrus.Error(err) @@ -131,7 +134,14 @@ func (t *Manager) Start(errchan chan error) error { } } } - + // only registry garbage-collect + cmd := []string{"registry", "garbage-collect", "/etc/docker/registry/config.yml"} + out, b, err := t.PodExecCmd(t.config, t.clientset, "rbd-hub", cmd) + if err != nil { + logrus.Error("rbd-hub exec cmd fail: ", out.String(), b.String(), err.Error()) + } else { + logrus.Info("rbd-hub exec cmd success.") + } return nil }, 1*time.Hour) if err != nil { @@ -148,3 +158,46 @@ func (t *Manager) Stop() error { t.cancel() return nil } + +// PodExecCmd registry garbage-collect +func (t *Manager) PodExecCmd(config *rest.Config, clientset *kubernetes.Clientset, podName string, cmd []string) (stdout bytes.Buffer, stderr bytes.Buffer, err error) { + labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"name": podName}} + listOptions := metav1.ListOptions{ + LabelSelector: labels.Set(labelSelector.MatchLabels).String(), + } + pods, err := clientset.CoreV1().Pods("rbd-system").List(context.TODO(), listOptions) + if err != nil { + return stdout, stderr, err + } + + for _, pod := range pods.Items { + req := clientset.CoreV1().RESTClient().Post(). + Namespace("rbd-system"). + Resource("pods"). + Name(pod.Name). + SubResource("exec"). + VersionedParams(&corev1.PodExecOptions{ + Command: cmd, + Stdin: false, + Stdout: true, + Stderr: true, + TTY: false, + }, scheme.ParameterCodec) + + exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL()) + if err != nil { + return stdout, stderr, err + } + err = exec.Stream(remotecommand.StreamOptions{ + Stdin: nil, + Stdout: &stdout, + Stderr: &stderr, + Tty: false, + }) + if err != nil { + return stdout, stderr, err + } + return stdout, stderr, nil + } + return stdout, stderr, nil +} diff --git a/builder/sources/registry/clean_repo.go b/builder/sources/registry/clean_repo.go index beedd897ee..67f62c75da 100644 --- a/builder/sources/registry/clean_repo.go +++ b/builder/sources/registry/clean_repo.go @@ -1,16 +1,7 @@ package registry import ( - "bytes" - "context" "github.com/sirupsen/logrus" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/remotecommand" "sort" ) @@ -45,46 +36,3 @@ func (registry *Registry) CleanRepoByTag(repository string, tag string) error { logrus.Info("delete rbd-hub tag: ", tag) return nil } - -// PodExecCmd registry garbage-collect -func (registry *Registry) PodExecCmd(config *rest.Config, clientset *kubernetes.Clientset, podName string, cmd []string) (stdout bytes.Buffer, stderr bytes.Buffer, err error) { - labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"name": podName}} - listOptions := metav1.ListOptions{ - LabelSelector: labels.Set(labelSelector.MatchLabels).String(), - } - pods, err := clientset.CoreV1().Pods("rbd-system").List(context.TODO(), listOptions) - if err != nil { - return stdout, stderr, err - } - - for _, pod := range pods.Items { - req := clientset.CoreV1().RESTClient().Post(). - Namespace("rbd-system"). - Resource("pods"). - Name(pod.Name). - SubResource("exec"). - VersionedParams(&corev1.PodExecOptions{ - Command: cmd, - Stdin: false, - Stdout: true, - Stderr: true, - TTY: false, - }, scheme.ParameterCodec) - - exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL()) - if err != nil { - return stdout, stderr, err - } - err = exec.Stream(remotecommand.StreamOptions{ - Stdin: nil, - Stdout: &stdout, - Stderr: &stderr, - Tty: false, - }) - if err != nil { - return stdout, stderr, err - } - return stdout, stderr, nil - } - return stdout, stderr, nil -}