Skip to content

Commit

Permalink
feat: add hold file for chart upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Gee <[email protected]>
  • Loading branch information
rgee0 committed Nov 9, 2024
1 parent b0cf2fb commit d817a39
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mc
/arkade-*
/faas-cli*
test.out
docker-compose.yaml
docker-compose.yaml*
58 changes: 55 additions & 3 deletions cmd/chart/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chart

import (
"bufio"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -96,9 +97,22 @@ Otherwise, it returns a non-zero exit code and the updated values.yaml file.`,
}

if verbose {
if len(filtered) > 0 {
log.Printf("Found %d images\n", len(filtered))
}
log.Printf("Found %d images\n", len(filtered))
}

holdfile := fmt.Sprintf("%s.hold", file)
imagesToHold, err := readFileLines(holdfile)
if err != nil {
return err
}

if verbose {
log.Printf("Found %d images to hold/ignore in %s", len(imagesToHold), holdfile)
}

filtered = removeHoldImages(filtered, imagesToHold)
if verbose {
log.Printf("Processing %d images\n", len(filtered))
}

wg := sync.WaitGroup{}
Expand Down Expand Up @@ -252,3 +266,41 @@ func getTagAttributes(t string) tagAttributes {
original: t,
}
}

func readFileLines(filename string) ([]string, error) {

if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, nil
}

file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading file: %w", err)
}

return lines, nil
}

func removeHoldImages(fullset map[string]string, held []string) map[string]string {

for _, h := range held {
for k := range fullset {
if strings.EqualFold(h, k[len(k)-len(h):]) {
delete(fullset, k)
}
}
}

return fullset
}
118 changes: 118 additions & 0 deletions cmd/chart/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chart

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -372,3 +373,120 @@ func TestGetTagAttributes(t *testing.T) {
})
}
}

func TestRemoveHoldImages(t *testing.T) {
tests := []struct {
name string
fullset map[string]string
held []string
expected map[string]string
}{
{
name: "Basic exclusion",
fullset: map[string]string{
"registry/img1:16": "going",
"registry/img1:17": "staying",
"registry/img1:18": "staying",
},
held: []string{
"img1:16",
},
expected: map[string]string{
"registry/img1:17": "staying",
"registry/img1:18": "staying",
},
},
{
name: "Basic exclusion / muli-match",
fullset: map[string]string{
"registry/img1:16": "going",
"registry/img1:17": "going",
"registry/img1:18": "staying",
},
held: []string{
"img1:16",
"img1:17",
},
expected: map[string]string{
"registry/img1:18": "staying",
},
},
{
name: "No match",
fullset: map[string]string{
"registry/img1:17": "staying",
"registry/img1:18": "staying",
"registry/img1:19": "staying",
},
held: []string{
"img1:16",
},
expected: map[string]string{
"registry/img1:17": "staying",
"registry/img1:18": "staying",
"registry/img1:19": "staying",
},
},
{
name: "Different Repos images match",
fullset: map[string]string{
"registry/repo/img1:16": "going",
"registry/repo2/img1:16": "going",
"registry/repo3/img1:18": "staying",
},
held: []string{
"img1:16",
},
expected: map[string]string{
"registry/repo3/img1:18": "staying",
},
},
{
name: "Different Repos images match / full path exclude",
fullset: map[string]string{
"registry/repo/img1:16": "going",
"registry/repo2/img1:16": "staying",
"registry/repo3/img1:18": "staying",
},
held: []string{
"registry/repo/img1:16",
},
expected: map[string]string{
"registry/repo2/img1:16": "staying",
"registry/repo3/img1:18": "staying",
},
},
{
name: "Different Repos images match / two exclude",
fullset: map[string]string{
"registry/repo/img1:16": "going",
"registry/repo2/img1:16": "going",
"registry/repo3/img1:18": "staying",
},
held: []string{
"registry/repo/img1:16",
"img1:16",
},
expected: map[string]string{
"registry/repo3/img1:18": "staying",
},
},
{
name: "Empty fullset",
fullset: map[string]string{},
held: []string{
"hold",
},
expected: map[string]string{},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := removeHoldImages(tc.fullset, tc.held)
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("\n%s \n got = %v\n want = %v", tc.name, result, tc.expected)
}
})
}
}

0 comments on commit d817a39

Please sign in to comment.