Skip to content

Commit

Permalink
[release-2.0] Fix etcd script to delete other files when there is no …
Browse files Browse the repository at this point in the history
…space left on device

Signed-off-by: 24sama <[email protected]>
  • Loading branch information
24sama committed Nov 22, 2022
1 parent ff9d30b commit dd79782
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions apis/kubekey/v1alpha2/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package v1alpha2

import (
"fmt"
"github.com/kubesphere/kubekey/pkg/core/util"
"os"
"strings"

"github.com/kubesphere/kubekey/pkg/core/util"
)

const (
Expand Down Expand Up @@ -69,7 +70,7 @@ const (
K8s = "k8s"
Registry = "registry"
DefaultEtcdBackupDir = "/var/backups/kube_etcd"
DefaultEtcdBackupPeriod = 30
DefaultEtcdBackupPeriod = 720
DefaultKeepBackNumber = 5
DefaultEtcdBackupScriptDir = "/usr/local/bin/kube-scripts"
DefaultJoinCIDR = "100.64.0.0/16"
Expand Down
10 changes: 7 additions & 3 deletions pkg/bootstrap/os/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ package os

import (
"fmt"
"path/filepath"
"strings"

osrelease "github.com/dominodatalab/os-release"
"github.com/pkg/errors"

"github.com/kubesphere/kubekey/pkg/bootstrap/os/repository"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/utils"
"github.com/pkg/errors"
"path/filepath"
"strings"
)

type NodeConfigureOS struct {
Expand Down Expand Up @@ -138,6 +140,8 @@ var (
"/etc/etcd.env",
"/etc/kubernetes",
"/etc/systemd/system/etcd.service",
"/etc/systemd/system/backup-etcd.service",
"/etc/systemd/system/backup-etcd.timer",
"/var/log/calico",
"/etc/cni",
"/var/log/pods/",
Expand Down
7 changes: 4 additions & 3 deletions pkg/etcd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ package etcd

import (
"fmt"
"github.com/kubesphere/kubekey/pkg/files"
"path/filepath"
"strings"

"github.com/pkg/errors"

kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/apis/kubekey/v1alpha2"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/action"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/core/util"
"github.com/kubesphere/kubekey/pkg/etcd/templates"
"github.com/kubesphere/kubekey/pkg/files"
"github.com/kubesphere/kubekey/pkg/utils"
"github.com/pkg/errors"
)

type EtcdNode struct {
Expand Down Expand Up @@ -389,7 +390,7 @@ func (b *BackupETCD) Execute(runtime connector.Runtime) error {
"Hostname": runtime.RemoteHost().GetName(),
"Etcdendpoint": fmt.Sprintf("https://%s:2379", runtime.RemoteHost().GetInternalAddress()),
"Backupdir": b.KubeConf.Cluster.Kubernetes.EtcdBackupDir,
"KeepbackupNumber": b.KubeConf.Cluster.Kubernetes.KeepBackupNumber,
"KeepbackupNumber": b.KubeConf.Cluster.Kubernetes.KeepBackupNumber + 1,
"EtcdBackupPeriod": b.KubeConf.Cluster.Kubernetes.EtcdBackupPeriod,
"EtcdBackupScriptDir": b.KubeConf.Cluster.Kubernetes.EtcdBackupScriptDir,
"EtcdBackupHour": templates.BackupTimeInterval(runtime, b.KubeConf),
Expand Down
14 changes: 10 additions & 4 deletions pkg/etcd/templates/backup_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@
package templates

import (
"strconv"
"text/template"

"github.com/lithammer/dedent"

"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/core/logger"
"github.com/lithammer/dedent"
"strconv"
"text/template"
)

// EtcdBackupScriptTmpl defines the template of etcd backup script.
var EtcdBackupScript = template.Must(template.New("etcd-backup.sh").Parse(
dedent.Dedent(`#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
ETCDCTL_PATH='/usr/local/bin/etcdctl'
ENDPOINTS='{{ .Etcdendpoint }}'
ETCD_DATA_DIR="/var/lib/etcd"
Expand Down Expand Up @@ -57,7 +63,7 @@ export ETCDCTL_API=3;$ETCDCTL_PATH --endpoints="$ENDPOINTS" snapshot save $BACKU
sleep 3
cd $BACKUP_DIR/../;ls -lt |awk '{if(NR > '$KEEPBACKUPNUMBER'){print "rm -rf "$9}}'|sh
cd $BACKUP_DIR/../ && ls -lt |awk '{if(NR > '$KEEPBACKUPNUMBER'){print "rm -rf "$9}}'|sh
if [[ ! $ETCDBACKUPHOUR ]]; then
time="*/$ETCDBACKUPPERIOD * * * *"
Expand Down

0 comments on commit dd79782

Please sign in to comment.