Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete outdated helm charts when pulling new helm charts #486

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cmd/local-artifact-mirror/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -129,10 +130,26 @@ var helmChartsCommand = &cli.Command{
dst := defaults.EmbeddedClusterChartsSubDir()
src := filepath.Join(location, HelmChartsArtifactName)
logrus.Infof("uncompressing %s", src)
if err := tgzutils.Decompress(src, dst); err != nil {
newFiles, err := tgzutils.Decompress(src, dst)
if err != nil {
return fmt.Errorf("unable to uncompress helm charts: %w", err)
}

// check which files are present in the directory but not newFiles, and remove them
files, err := os.ReadDir(dst)
if err != nil {
return fmt.Errorf("unable to read directory: %w", err)
}
for _, f := range files {
if !slices.Contains(newFiles, f.Name()) {
fp := filepath.Join(dst, f.Name())
logrus.Infof("removing %s", fp)
if err := os.RemoveAll(fp); err != nil {
return fmt.Errorf("unable to remove file: %w", err)
}
}
}

logrus.Infof("helm charts materialized under %s", dst)
return nil
},
Expand Down
52 changes: 26 additions & 26 deletions e2e/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,32 +289,32 @@ func CreateProxy(in *Input) string {
func ConfigureProxy(in *Input) {
// starts by installing dependencies, setting up the second network interface ip
// address and configuring iptables to allow dns requests forwarding (nat).
proxyName := fmt.Sprintf("node-%s-proxy", in.id)
for _, cmd := range [][]string{
{"apt-get", "update", "-y"},
{"apt-get", "install", "-y", "iptables", "squid"},
{"ip", "addr", "add", "10.0.0.254/24", "dev", "eth1"},
{"ip", "link", "set", "eth1", "up"},
{"sysctl", "-w", "net.ipv4.ip_forward=1"},
{"iptables", "-t", "nat", "-o", "eth0", "-A", "POSTROUTING", "-p", "udp", "--dport", "53", "-j", "MASQUERADE"},
} {
RunCommandOnNode(in, cmd, proxyName)
}

// create a simple squid configuration that allows for localnet access. upload it
// to the proxy in the right location. restart squid to apply the configuration.
tmpfile, err := os.CreateTemp("", "squid-config-*.conf")
if err != nil {
in.T.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err = tmpfile.WriteString("http_access allow localnet\n"); err != nil {
in.T.Fatalf("Failed to write to temp file: %v", err)
}
file := File{SourcePath: tmpfile.Name(), DestPath: "/etc/squid/conf.d/ec.conf", Mode: 0644}
tmpfile.Close()
CopyFileToNode(in, proxyName, file)
RunCommandOnNode(in, []string{"systemctl", "restart", "squid"}, proxyName)
//proxyName := fmt.Sprintf("node-%s-proxy", in.id)
//for _, cmd := range [][]string{
// {"apt-get", "update", "-y"},
// {"apt-get", "install", "-y", "iptables", "squid"},
// {"ip", "addr", "add", "10.0.0.254/24", "dev", "eth1"},
// {"ip", "link", "set", "eth1", "up"},
// {"sysctl", "-w", "net.ipv4.ip_forward=1"},
// {"iptables", "-t", "nat", "-o", "eth0", "-A", "POSTROUTING", "-p", "udp", "--dport", "53", "-j", "MASQUERADE"},
//} {
// RunCommandOnNode(in, cmd, proxyName)
//}
//
//// create a simple squid configuration that allows for localnet access. upload it
//// to the proxy in the right location. restart squid to apply the configuration.
//tmpfile, err := os.CreateTemp("", "squid-config-*.conf")
//if err != nil {
// in.T.Fatalf("Failed to create temp file: %v", err)
//}
//defer os.Remove(tmpfile.Name())
//if _, err = tmpfile.WriteString("http_access allow localnet\n"); err != nil {
// in.T.Fatalf("Failed to write to temp file: %v", err)
//}
//file := File{SourcePath: tmpfile.Name(), DestPath: "/etc/squid/conf.d/ec.conf", Mode: 0644}
//tmpfile.Close()
//CopyFileToNode(in, proxyName, file)
//RunCommandOnNode(in, []string{"systemctl", "restart", "squid"}, proxyName)

// set the default route on all other nodes to point to the proxy we just created.
// this makes it easier to ensure no internet will work on them other than dns and
Expand Down
23 changes: 13 additions & 10 deletions pkg/tgzutils/tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,55 @@ import (
)

// Decompress decompresses a .tgz file into a directory.
func Decompress(tgz, dst string) error {
func Decompress(tgz, dst string) ([]string, error) {
fp, err := os.Open(tgz)
if err != nil {
return fmt.Errorf("unable to open tgz file: %v", err)
return nil, fmt.Errorf("unable to open tgz file: %v", err)
}
defer fp.Close()

gzreader, err := gzip.NewReader(fp)
if err != nil {
return fmt.Errorf("unable to create gzip reader: %v", err)
return nil, fmt.Errorf("unable to create gzip reader: %v", err)
}

filenames := []string{}

tarreader := tar.NewReader(gzreader)
for {
header, err := tarreader.Next()
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("unable to read tar header: %v", err)
return nil, fmt.Errorf("unable to read tar header: %v", err)
}

switch header.Typeflag {
case tar.TypeDir:
mode := os.FileMode(header.Mode)
dst := filepath.Join(dst, header.Name)
if err := os.Mkdir(dst, mode); err != nil {
return fmt.Errorf("unable to create directory: %v", err)
return nil, fmt.Errorf("unable to create directory: %v", err)
}
case tar.TypeReg:
mode := os.FileMode(header.Mode)
dst := filepath.Join(dst, header.Name)
opts := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
outfp, err := os.OpenFile(dst, opts, mode)
if err != nil {
return fmt.Errorf("unable to create file: %v", err)
return nil, fmt.Errorf("unable to create file: %v", err)
}
if _, err := io.Copy(outfp, tarreader); err != nil {
return fmt.Errorf("unable to write file: %v", err)
return nil, fmt.Errorf("unable to write file: %v", err)
}
outfp.Close()
if err := os.Chmod(dst, os.FileMode(header.Mode)); err != nil {
return fmt.Errorf("unable to chmod file: %v", err)
return nil, fmt.Errorf("unable to chmod file: %v", err)
}
filenames = append(filenames, dst)
default:
return fmt.Errorf("unknown type: %v", header.Typeflag)
return nil, fmt.Errorf("unknown type: %v", header.Typeflag)
}
}
return nil
return filenames, nil
}
Loading