Skip to content

Commit

Permalink
fix for etcd-snapshot delete with --etcd-s3 flag (k3s-io#8110)
Browse files Browse the repository at this point in the history
k3s etcd-snapshot save --etcd-s3 ... is creating a local snapshot and uploading it to s3 while k3s etcd-snapshot delete --etcd-s3 ... was deleting the snapshot only on s3 buckets, this commit change the behavior of delete to do it locally and on s3

Signed-off-by: Ian Cardoso <osodracnai@gmail.com>

(cherry picked from commit e551308)
Signed-off-by: Ian Cardoso <osodracnai@gmail.com>
  • Loading branch information
osodracnai committed Aug 4, 2023
1 parent 958cd03 commit 3e034b5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
29 changes: 17 additions & 12 deletions pkg/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1708,21 +1708,26 @@ func (e *ETCD) DeleteSnapshots(ctx context.Context, snapshots []string) error {
}
}()

for {
select {
case <-ctx.Done():
logrus.Errorf("Unable to delete snapshot: %v", ctx.Err())
return e.ReconcileSnapshotData(ctx)
case <-time.After(time.Millisecond * 100):
continue
case err, ok := <-e.s3.client.RemoveObjects(ctx, e.config.EtcdS3BucketName, objectsCh, minio.RemoveObjectsOptions{}):
if err.Err != nil {
logrus.Errorf("Unable to delete snapshot: %v", err.Err)
}
if !ok {
err = func() error {
for {
select {
case <-ctx.Done():
logrus.Errorf("Unable to delete snapshot: %v", ctx.Err())
return e.ReconcileSnapshotData(ctx)
case <-time.After(time.Millisecond * 100):
continue
case err, ok := <-e.s3.client.RemoveObjects(ctx, e.config.EtcdS3BucketName, objectsCh, minio.RemoveObjectsOptions{}):
if err.Err != nil {
logrus.Errorf("Unable to delete snapshot: %v", err.Err)
}
if !ok {
return e.ReconcileSnapshotData(ctx)
}
}
}
}()
if err != nil {
return err
}
}

Expand Down
97 changes: 77 additions & 20 deletions tests/e2e/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ type Pod struct {
Ready string
Status string
Restarts string
NodeIP string
IP string
Node string
}

type ObjIP struct {
Name string
IPv4 string
IPv6 string
}

type NodeError struct {
Node string
Cmd string
Err error
}

type ObjIP struct {
Name string
IPv4 string
IPv6 string
}

func (ne *NodeError) Error() string {
return fmt.Sprintf("failed creating cluster: %s: %v", ne.Cmd, ne.Err)
}
Expand Down Expand Up @@ -294,6 +294,9 @@ func GenKubeConfigFile(serverName string) (string, error) {
if err := os.WriteFile(kubeConfigFile, []byte(kubeConfig), 0644); err != nil {
return "", err
}
if err := os.Setenv("E2E_KUBECONFIG", kubeConfigFile); err != nil {
return "", err
}
return kubeConfigFile, nil
}

Expand Down Expand Up @@ -372,16 +375,10 @@ func ParseNodes(kubeConfig string, print bool) ([]Node, error) {
return nodes, nil
}

func ParsePods(kubeConfig string, print bool) ([]Pod, error) {
func formatPods(input string) ([]Pod, error) {
pods := make([]Pod, 0, 10)
podList := ""

cmd := "kubectl get pods -o wide --no-headers -A --kubeconfig=" + kubeConfig
res, _ := RunCommand(cmd)
res = strings.TrimSpace(res)
podList = res

split := strings.Split(res, "\n")
input = strings.TrimSpace(input)
split := strings.Split(input, "\n")
for _, rec := range split {
fields := strings.Fields(string(rec))
if len(fields) < 8 {
Expand All @@ -393,11 +390,25 @@ func ParsePods(kubeConfig string, print bool) ([]Pod, error) {
Ready: fields[2],
Status: fields[3],
Restarts: fields[4],
NodeIP: fields[6],
IP: fields[6],
Node: fields[7],
}
pods = append(pods, pod)
}
return pods, nil
}

func ParsePods(kubeConfig string, print bool) ([]Pod, error) {
podList := ""

cmd := "kubectl get pods -o wide --no-headers -A"
res, _ := RunCommand(cmd)
podList = strings.TrimSpace(res)

pods, err := formatPods(res)
if err != nil {
return nil, err
}
if print {
fmt.Println(podList)
}
Expand Down Expand Up @@ -440,19 +451,25 @@ func StopCluster(nodeNames []string) error {
return nil
}

// RunCmdOnNode executes a command from within the given node
// RunCmdOnNode executes a command from within the given node as sudo
func RunCmdOnNode(cmd string, nodename string) (string, error) {
runcmd := "vagrant ssh " + nodename + " -c \"sudo " + cmd + "\""
injectEnv := ""
if _, ok := os.LookupEnv("E2E_GOCOVER"); ok && strings.HasPrefix(cmd, "k3s") {
injectEnv = "GOCOVERDIR=/tmp/k3scov "
}
runcmd := "vagrant ssh " + nodename + " -c \"sudo " + injectEnv + cmd + "\""
out, err := RunCommand(runcmd)
if err != nil {
return out, fmt.Errorf("failed to run command: %s on node %s: %s, %v", cmd, nodename, out, err)
}
return out, nil
}

// RunCommand executes a command on the host
func RunCommand(cmd string) (string, error) {
c := exec.Command("bash", "-c", cmd)
if kc, ok := os.LookupEnv("E2E_KUBECONFIG"); ok {
c.Env = append(os.Environ(), "KUBECONFIG="+kc)
}
out, err := c.CombinedOutput()
return string(out), err
}
Expand All @@ -475,6 +492,46 @@ func UpgradeCluster(nodeNames []string, local bool) error {
return nil
}

func GetCoverageReport(nodeNames []string) error {
if os.Getenv("E2E_GOCOVER") == "" {
return nil
}
covDirs := []string{}
for _, nodeName := range nodeNames {
covDir := nodeName + "-cov"
covDirs = append(covDirs, covDir)
os.MkdirAll(covDir, 0755)
cmd := "vagrant scp " + nodeName + ":/tmp/k3scov/* " + covDir
if _, err := RunCommand(cmd); err != nil {
return err
}
}
coverageFile := "coverage.out"
cmd := "go tool covdata textfmt -i=" + strings.Join(covDirs, ",") + " -o=" + coverageFile
if out, err := RunCommand(cmd); err != nil {
return fmt.Errorf("failed to generate coverage report: %s, %v", out, err)
}

f, err := os.ReadFile(coverageFile)
if err != nil {
return err
}
nf := strings.Replace(string(f),
"/go/src/github.com/k3s-io/k3s/cmd/server/main.go",
"github.com/k3s-io/k3s/cmd/server/main.go", -1)

if err = os.WriteFile(coverageFile, []byte(nf), os.ModePerm); err != nil {
return err
}

for _, covDir := range covDirs {
if err := os.RemoveAll(covDir); err != nil {
return err
}
}
return nil
}

// getPodIPs returns the IPs of all pods
func GetPodIPs(kubeConfigFile string) ([]ObjIP, error) {
cmd := `kubectl get pods -A -o=jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.podIPs[*].ip}{"\n"}{end}' --kubeconfig=` + kubeConfigFile
Expand Down

0 comments on commit 3e034b5

Please sign in to comment.