Skip to content

Commit

Permalink
fix: auto clean rbd-hub (#1755)
Browse files Browse the repository at this point in the history
fix: auto clean rbd-hub and local images
  • Loading branch information
DokiDoki1103 authored Sep 5, 2023
1 parent d581da4 commit 43ae608
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 72 deletions.
93 changes: 73 additions & 20 deletions builder/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
52 changes: 0 additions & 52 deletions builder/sources/registry/clean_repo.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 43ae608

Please sign in to comment.