From d03f4fa4c46a71743e2cdfec92ad1b67fbed45a3 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Thu, 10 Sep 2020 14:11:38 +0200 Subject: [PATCH] Change advertised artifact URLs on hostname change --- controllers/gitrepository_controller.go | 4 ++++ controllers/helmchart_controller.go | 14 ++++++++++---- controllers/helmrepository_controller.go | 9 ++++++--- controllers/storage.go | 12 ++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index d1d0815a..d62603bf 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -199,6 +199,10 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour // return early on unchanged revision artifact := r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", commit.Hash.String())) if repository.GetArtifact() != nil && repository.GetArtifact().Revision == revision { + if artifact.URL != repository.GetArtifact().URL { + r.Storage.SetArtifactURL(repository.GetArtifact()) + repository.Status.URL = r.Storage.SetHostname(repository.Status.URL) + } return repository, nil } diff --git a/controllers/helmchart_controller.go b/controllers/helmchart_controller.go index 76c42613..9ebe88d7 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -196,7 +196,12 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context, } // return early on unchanged chart version + artifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version)) if repository.GetArtifact() != nil && repository.GetArtifact().Revision == cv.Version { + if artifact.URL != repository.GetArtifact().URL { + r.Storage.SetArtifactURL(repository.GetArtifact()) + repository.Status.URL = r.Storage.SetHostname(repository.Status.URL) + } return chart, nil } @@ -260,8 +265,6 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context, return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPullFailedReason, err.Error()), err } - artifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version)) - // create artifact dir err = r.Storage.MkdirAll(artifact) if err != nil { @@ -359,12 +362,15 @@ func (r *HelmChartReconciler) reconcileFromGitRepository(ctx context.Context, } // return early on unchanged chart version + artifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version, fmt.Sprintf("%s-%s.tgz", chartMetadata.Name, chartMetadata.Version)) if chart.GetArtifact() != nil && chart.GetArtifact().Revision == chartMetadata.Version { + if artifact.URL != repository.GetArtifact().URL { + r.Storage.SetArtifactURL(repository.GetArtifact()) + repository.Status.URL = r.Storage.SetHostname(repository.Status.URL) + } return chart, nil } - artifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version, fmt.Sprintf("%s-%s.tgz", chartMetadata.Name, chartMetadata.Version)) - // create artifact dir err = r.Storage.MkdirAll(artifact) if err != nil { diff --git a/controllers/helmrepository_controller.go b/controllers/helmrepository_controller.go index 7e7b6238..a368ac40 100644 --- a/controllers/helmrepository_controller.go +++ b/controllers/helmrepository_controller.go @@ -217,7 +217,13 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou } // return early on unchanged generation + artifact := r.Storage.NewArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(), i.Generated.Format(time.RFC3339Nano), + fmt.Sprintf("index-%s.yaml", url.PathEscape(i.Generated.Format(time.RFC3339Nano)))) if repository.GetArtifact() != nil && repository.GetArtifact().Revision == i.Generated.Format(time.RFC3339Nano) { + if artifact.URL != repository.GetArtifact().URL { + r.Storage.SetArtifactURL(repository.GetArtifact()) + repository.Status.URL = r.Storage.SetHostname(repository.Status.URL) + } return repository, nil } @@ -227,9 +233,6 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou return sourcev1.HelmRepositoryNotReady(repository, sourcev1.IndexationFailedReason, err.Error()), err } - artifact := r.Storage.NewArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(), i.Generated.Format(time.RFC3339Nano), - fmt.Sprintf("index-%s.yaml", url.PathEscape(i.Generated.Format(time.RFC3339Nano)))) - // create artifact dir err = r.Storage.MkdirAll(artifact) if err != nil { diff --git a/controllers/storage.go b/controllers/storage.go index 15648411..41883cc9 100644 --- a/controllers/storage.go +++ b/controllers/storage.go @@ -26,6 +26,7 @@ import ( "hash" "io" "io/ioutil" + "net/url" "os" "path/filepath" "strings" @@ -89,6 +90,17 @@ func (s Storage) SetArtifactURL(artifact *sourcev1.Artifact) { artifact.URL = fmt.Sprintf("http://%s/%s", s.Hostname, artifact.Path) } +// SetHostname sets the hostname of the given URL string to the current Storage.Hostname +// and returns the result. +func (s Storage) SetHostname(URL string) string { + u, err := url.Parse(URL) + if err != nil { + return "" + } + u.Host = s.Hostname + return u.String() +} + // MkdirAll calls os.MkdirAll for the given v1alpha1.Artifact base dir. func (s *Storage) MkdirAll(artifact sourcev1.Artifact) error { dir := filepath.Dir(s.LocalPath(artifact))