forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-detect Helm repos + support Helm basic auth + fix bugs (argoproj…
- Loading branch information
Showing
16 changed files
with
281 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package repo | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
log "github.com/sirupsen/logrus" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var indexCache = cache.New(5*time.Minute, 5*time.Minute) | ||
|
||
type entry struct { | ||
Version string | ||
Created time.Time | ||
} | ||
|
||
type index struct { | ||
Entries map[string][]entry | ||
} | ||
|
||
func Index(url, username, password string) (*index, error) { | ||
|
||
cachedIndex, found := indexCache.Get(url) | ||
if found { | ||
log.WithFields(log.Fields{"url": url}).Debug("index cache hit") | ||
i := cachedIndex.(index) | ||
return &i, nil | ||
} | ||
|
||
start := time.Now() | ||
|
||
req, err := http.NewRequest("GET", url+"/index.yaml", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if username != "" { | ||
// only basic supported | ||
token := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) | ||
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", token)) | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode != 200 { | ||
return nil, errors.New("failed to get index: " + resp.Status) | ||
} | ||
|
||
index := &index{} | ||
err = yaml.NewDecoder(resp.Body).Decode(index) | ||
|
||
log.WithFields(log.Fields{"seconds": time.Since(start).Seconds()}).Info("took to get index") | ||
|
||
indexCache.Set(url, *index, cache.DefaultExpiration) | ||
|
||
return index, err | ||
} | ||
|
||
func (i *index) contains(chart string) bool { | ||
_, ok := i.Entries[chart] | ||
return ok | ||
} | ||
|
||
func (i *index) entry(chart, version string) (*entry, error) { | ||
for _, entry := range i.Entries[chart] { | ||
if entry.Version == version { | ||
return &entry, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("unknown chart \"%s/%s\"", chart, version) | ||
} | ||
|
||
func (i *index) latest(chart string) (string, error) { | ||
for chartName := range i.Entries { | ||
if chartName == chart { | ||
return i.Entries[chartName][0].Version, nil | ||
} | ||
} | ||
return "", fmt.Errorf("failed to find chart %s", chart) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package repo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIndex(t *testing.T) { | ||
t.Run("Invalid", func(t *testing.T) { | ||
_, err := Index("", "", "") | ||
assert.Error(t, err) | ||
}) | ||
t.Run("Stable", func(t *testing.T) { | ||
index, err := Index("https://kubernetes-charts.storage.googleapis.com", "", "") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, index) | ||
}) | ||
t.Run("BasicAuth", func(t *testing.T) { | ||
index, err := Index("https://kubernetes-charts.storage.googleapis.com", "my-username", "my-password") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, index) | ||
}) | ||
} |
Oops, something went wrong.