-
Notifications
You must be signed in to change notification settings - Fork 220
Change for helm v3 client #643
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"math" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/globalsign/mgo/bson" | ||
"github.com/gorilla/mux" | ||
|
@@ -459,3 +460,74 @@ func newChartVersionListResponse(c *models.Chart) apiListResponse { | |
|
||
return cvl | ||
} | ||
|
||
func redirectToChartVersionPackage(w http.ResponseWriter, req *http.Request) { | ||
// The URL can be in one of two forms: | ||
// - /v1/redirect/charts/stable/aerospike | ||
// - /v1/redirect/charts/stable/aerospike/v1.2.3 | ||
// And either of these can optionally have a trailing / | ||
|
||
// Make sure the path is valid | ||
ct := strings.TrimPrefix(req.URL.Path, "/v1/redirect/charts/") | ||
|
||
// check if URL for provenance | ||
prov := strings.HasSuffix(ct, ".prov") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we may also want to consider handling the provenance file in a separate handler There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how to handle that with mux (see |
||
ct = strings.TrimSuffix(ct, ".prov") | ||
|
||
ct = strings.TrimSuffix(ct, "/") // Removing the optional / on the end | ||
parts := strings.Split(ct, "/") | ||
|
||
// Not enough parts passed in to the path | ||
if len(parts) < 2 || len(parts) > 3 { | ||
response.NewErrorResponse(http.StatusNotFound, "could not find chart").Write(w) | ||
return | ||
} | ||
|
||
// Decide if latest or a version | ||
var version string | ||
if len(parts) == 3 { | ||
version = parts[2] | ||
} | ||
|
||
// Look it up. This will be different if there is a version or we are getting | ||
// the latest | ||
db, closer := dbSession.DB() | ||
defer closer() | ||
var chart models.Chart | ||
chartID := fmt.Sprintf("%s/%s", parts[0], parts[1]) | ||
|
||
if version == "" { | ||
if err := db.C(chartCollection).FindId(chartID).One(&chart); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're starting to repeat these MongoDB calls, we may want to extract them out to separate functions and call them from here and in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do this as part of a separate change? This query is used 4 times by 4 different callbacks. If the logic were to be extracted I would suggest doing it uniformly and updating all of them. It's worth being tested separately from this feature addition IMHO. Repeated the same database query is common here as it stands. |
||
log.WithError(err).Errorf("could not find chart with id %s", chartID) | ||
response.NewErrorResponse(http.StatusNotFound, "could not find chart").Write(w) | ||
return | ||
} | ||
} else { | ||
if err := db.C(chartCollection).Find(bson.M{ | ||
"_id": chartID, | ||
"chartversions": bson.M{"$elemMatch": bson.M{"version": version}}, | ||
}).Select(bson.M{ | ||
"name": 1, "repo": 1, "description": 1, "home": 1, "keywords": 1, "maintainers": 1, "sources": 1, | ||
"chartversions.$": 1, | ||
}).One(&chart); err != nil { | ||
log.WithError(err).Errorf("could not find chart with id %s", chartID) | ||
response.NewErrorResponse(http.StatusNotFound, "could not find chart version").Write(w) | ||
return | ||
} | ||
} | ||
|
||
// Respond with proper redirect for tarball and prov | ||
if len(chart.ChartVersions) > 0 { | ||
cv := chart.ChartVersions[0] | ||
if len(cv.URLs) > 0 { | ||
if prov { | ||
http.Redirect(w, req, cv.URLs[0]+".prov", http.StatusTemporaryRedirect) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a thought, instead of redirecting, it might make sense to stream the file directly back to the client. I know the intention of this is for public repositories, but this could be useful for instances of Monocular pointing to private repositories that might not be accessible by the client. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that but if we do that for the Helm Hub it means we are eating a lot more egress cost as we stream all the packages out from the hub. It also means more system resources are required. I was attempting to be cost sensitive with this implementation. |
||
} else { | ||
http.Redirect(w, req, cv.URLs[0], http.StatusTemporaryRedirect) | ||
} | ||
return | ||
} | ||
} | ||
|
||
response.NewErrorResponse(http.StatusNotFound, "could not find chart version").Write(w) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would split the handlers up and extract the shared logic to other functions. We should try to use the router as much as possible to handle path parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So... here is my problem and maybe you can help with it. Let's say I have the path
my-repo/my-chart/0.1.0/
. Now we also need to get the provenance file for that. This would bemy-repo/my-chart/0.1.0/.prov
. Notice the placements of the path separators. Or, what if we have the pathmy-repo/my-chart/0.1.0
and need to getmy-repo/my-chart/0.1.0.prov
. How do we separate the chart tarball from the provenance one with mux?There is some variation to the possible inputs which makes handling some of this in the router difficult. Any pointers on that?