From 31875e45523e84d39fe817141ebbc3f8723ba693 Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Fri, 5 Apr 2024 21:07:44 +0900 Subject: [PATCH 1/3] delete outdated helm charts when pulling new helm charts --- cmd/local-artifact-mirror/pull.go | 27 ++++++++++++++++++++++++++- pkg/tgzutils/tgz.go | 23 +++++++++++++---------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/cmd/local-artifact-mirror/pull.go b/cmd/local-artifact-mirror/pull.go index 5b2cb433e..ee826682f 100644 --- a/cmd/local-artifact-mirror/pull.go +++ b/cmd/local-artifact-mirror/pull.go @@ -129,15 +129,40 @@ 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 !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 }, } +func contains(haystack []string, needle string) bool { + for _, h := range haystack { + if h == needle { + return true + } + } + return false +} + // binariesCommands pulls the binary artifact from the registry running in the cluster and stores // it locally. This command is used during cluster upgrades when we want to fetch the most up to // date binaries. The binaries is stored in the /usr/local/bin directory and they overwrite the diff --git a/pkg/tgzutils/tgz.go b/pkg/tgzutils/tgz.go index 81fba5db5..cae2ff055 100644 --- a/pkg/tgzutils/tgz.go +++ b/pkg/tgzutils/tgz.go @@ -10,25 +10,27 @@ 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 { @@ -36,7 +38,7 @@ func Decompress(tgz, dst string) error { 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) @@ -44,18 +46,19 @@ func Decompress(tgz, dst string) error { 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 } From e17d1299dc7cc4720ba7cd40727e578c7cb47e24 Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Fri, 5 Apr 2024 22:30:32 +0900 Subject: [PATCH 2/3] there is a function for this --- cmd/local-artifact-mirror/pull.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/cmd/local-artifact-mirror/pull.go b/cmd/local-artifact-mirror/pull.go index ee826682f..5d484ba27 100644 --- a/cmd/local-artifact-mirror/pull.go +++ b/cmd/local-artifact-mirror/pull.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" @@ -140,7 +141,7 @@ var helmChartsCommand = &cli.Command{ return fmt.Errorf("unable to read directory: %w", err) } for _, f := range files { - if !contains(newFiles, f.Name()) { + if !slices.Contains(newFiles, f.Name()) { fp := filepath.Join(dst, f.Name()) logrus.Infof("removing %s", fp) if err := os.RemoveAll(fp); err != nil { @@ -154,15 +155,6 @@ var helmChartsCommand = &cli.Command{ }, } -func contains(haystack []string, needle string) bool { - for _, h := range haystack { - if h == needle { - return true - } - } - return false -} - // binariesCommands pulls the binary artifact from the registry running in the cluster and stores // it locally. This command is used during cluster upgrades when we want to fetch the most up to // date binaries. The binaries is stored in the /usr/local/bin directory and they overwrite the From a58f7258e4770438e3dc43f87171c4cc8f6d7a24 Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Thu, 25 Apr 2024 19:01:23 +0900 Subject: [PATCH 3/3] disable setting up squid on proxy node --- e2e/cluster/cluster.go | 52 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/e2e/cluster/cluster.go b/e2e/cluster/cluster.go index a6af67b60..ddb858638 100644 --- a/e2e/cluster/cluster.go +++ b/e2e/cluster/cluster.go @@ -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