Skip to content

Commit d03f4fa

Browse files
committed
Change advertised artifact URLs on hostname change
1 parent 7a3a593 commit d03f4fa

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

controllers/gitrepository_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
199199
// return early on unchanged revision
200200
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
201201
if repository.GetArtifact() != nil && repository.GetArtifact().Revision == revision {
202+
if artifact.URL != repository.GetArtifact().URL {
203+
r.Storage.SetArtifactURL(repository.GetArtifact())
204+
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)
205+
}
202206
return repository, nil
203207
}
204208

controllers/helmchart_controller.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
196196
}
197197

198198
// return early on unchanged chart version
199+
artifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version))
199200
if repository.GetArtifact() != nil && repository.GetArtifact().Revision == cv.Version {
201+
if artifact.URL != repository.GetArtifact().URL {
202+
r.Storage.SetArtifactURL(repository.GetArtifact())
203+
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)
204+
}
200205
return chart, nil
201206
}
202207

@@ -260,8 +265,6 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
260265
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), err
261266
}
262267

263-
artifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version))
264-
265268
// create artifact dir
266269
err = r.Storage.MkdirAll(artifact)
267270
if err != nil {
@@ -359,12 +362,15 @@ func (r *HelmChartReconciler) reconcileFromGitRepository(ctx context.Context,
359362
}
360363

361364
// return early on unchanged chart version
365+
artifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version, fmt.Sprintf("%s-%s.tgz", chartMetadata.Name, chartMetadata.Version))
362366
if chart.GetArtifact() != nil && chart.GetArtifact().Revision == chartMetadata.Version {
367+
if artifact.URL != repository.GetArtifact().URL {
368+
r.Storage.SetArtifactURL(repository.GetArtifact())
369+
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)
370+
}
363371
return chart, nil
364372
}
365373

366-
artifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version, fmt.Sprintf("%s-%s.tgz", chartMetadata.Name, chartMetadata.Version))
367-
368374
// create artifact dir
369375
err = r.Storage.MkdirAll(artifact)
370376
if err != nil {

controllers/helmrepository_controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou
217217
}
218218

219219
// return early on unchanged generation
220+
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(), i.Generated.Format(time.RFC3339Nano),
221+
fmt.Sprintf("index-%s.yaml", url.PathEscape(i.Generated.Format(time.RFC3339Nano))))
220222
if repository.GetArtifact() != nil && repository.GetArtifact().Revision == i.Generated.Format(time.RFC3339Nano) {
223+
if artifact.URL != repository.GetArtifact().URL {
224+
r.Storage.SetArtifactURL(repository.GetArtifact())
225+
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)
226+
}
221227
return repository, nil
222228
}
223229

@@ -227,9 +233,6 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou
227233
return sourcev1.HelmRepositoryNotReady(repository, sourcev1.IndexationFailedReason, err.Error()), err
228234
}
229235

230-
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(), i.Generated.Format(time.RFC3339Nano),
231-
fmt.Sprintf("index-%s.yaml", url.PathEscape(i.Generated.Format(time.RFC3339Nano))))
232-
233236
// create artifact dir
234237
err = r.Storage.MkdirAll(artifact)
235238
if err != nil {

controllers/storage.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"hash"
2727
"io"
2828
"io/ioutil"
29+
"net/url"
2930
"os"
3031
"path/filepath"
3132
"strings"
@@ -89,6 +90,17 @@ func (s Storage) SetArtifactURL(artifact *sourcev1.Artifact) {
8990
artifact.URL = fmt.Sprintf("http://%s/%s", s.Hostname, artifact.Path)
9091
}
9192

93+
// SetHostname sets the hostname of the given URL string to the current Storage.Hostname
94+
// and returns the result.
95+
func (s Storage) SetHostname(URL string) string {
96+
u, err := url.Parse(URL)
97+
if err != nil {
98+
return ""
99+
}
100+
u.Host = s.Hostname
101+
return u.String()
102+
}
103+
92104
// MkdirAll calls os.MkdirAll for the given v1alpha1.Artifact base dir.
93105
func (s *Storage) MkdirAll(artifact sourcev1.Artifact) error {
94106
dir := filepath.Dir(s.LocalPath(artifact))

0 commit comments

Comments
 (0)