Skip to content

Commit

Permalink
corresponding controller changes
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour committed Feb 2, 2024
1 parent 97065dd commit ea8aaba
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 234 deletions.
2 changes: 1 addition & 1 deletion internal/controller/promotion/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func applyArgoCDSourceUpdate(
return source, nil
}
for _, chart := range newFreight.Charts {
if chart.RegistryURL == source.RepoURL && chart.Name == source.Chart {
if chart.RepoURL == source.RepoURL && chart.Name == source.Chart {
source.TargetRevision = chart.Version
break
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/promotion/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,9 @@ func TestApplyArgoCDSourceUpdate(t *testing.T) {
newFreight: kargoapi.FreightReference{
Charts: []kargoapi.Chart{
{
RegistryURL: "fake-url",
Name: "fake-chart",
Version: "fake-version",
RepoURL: "fake-url",
Name: "fake-chart",
Version: "fake-version",
},
},
},
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/promotion/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package promotion
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/pkg/errors"
Expand Down Expand Up @@ -191,7 +192,8 @@ func buildChartDependencyChanges(
// Build a table of charts --> versions
versionsByChart := make(map[string]string, len(charts))
for _, chart := range charts {
key := fmt.Sprintf("%s:%s", chart.RegistryURL, chart.Name)
// path.Join accounts for the possibility that chart.Name is empty
key := path.Join(chart.RepoURL, chart.Name)
versionsByChart[key] = chart.Version
}

Expand Down Expand Up @@ -222,7 +224,7 @@ func buildChartDependencyChanges(
errors.Wrapf(err, "error unmarshaling %q", absChartYAMLPath)
}
for i, dependency := range chartYAMLObj.Dependencies {
chartKey := fmt.Sprintf("%s:%s", dependency.Repository, dependency.Name)
chartKey := fmt.Sprintf("%s/%s", dependency.Repository, dependency.Name)
version, found := versionsByChart[chartKey]
if !found {
continue
Expand Down
30 changes: 15 additions & 15 deletions internal/controller/promotion/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func TestBuildChartDependencyChanges(t *testing.T) {
err = os.WriteFile(
filepath.Join(testFooChartDir, "Chart.yaml"),
[]byte(`dependencies:
- repository: fake-registry
- repository: fake-repo
name: fake-chart
version: placeholder
`),
Expand All @@ -376,10 +376,10 @@ func TestBuildChartDependencyChanges(t *testing.T) {
filepath.Join(testBarChartDir, "Chart.yaml"),
// This fake chart has TWO dependencies -- one of which shouldn't be updated
[]byte(`dependencies:
- repository: another-fake-registry
- repository: another-fake-repo
name: another-fake-chart
version: placeholder
- repository: yet-another-fake-registry
- repository: yet-another-fake-repo
name: yet-another-fake-chart
version: placeholder
`),
Expand All @@ -390,28 +390,28 @@ func TestBuildChartDependencyChanges(t *testing.T) {
// New charts
charts := []kargoapi.Chart{
{
RegistryURL: "fake-registry",
Name: "fake-chart",
Version: "fake-version",
RepoURL: "fake-repo",
Name: "fake-chart",
Version: "fake-version",
},
{
RegistryURL: "another-fake-registry",
Name: "another-fake-chart",
Version: "another-fake-version",
RepoURL: "another-fake-repo",
Name: "another-fake-chart",
Version: "another-fake-version",
},
}

// Instructions for how to update Chart.yaml files
chartUpdates := []kargoapi.HelmChartDependencyUpdate{
{
RegistryURL: "fake-registry",
Name: "fake-chart",
ChartPath: "charts/foo",
Repository: "fake-repo",
Name: "fake-chart",
ChartPath: "charts/foo",
},
{
RegistryURL: "another-fake-registry",
Name: "another-fake-chart",
ChartPath: "charts/bar",
Repository: "another-fake-repo",
Name: "another-fake-chart",
ChartPath: "charts/bar",
},
// Note there is no mention of how to update bar's second dependency, so
// we expect it to be left alone.
Expand Down
28 changes: 16 additions & 12 deletions internal/controller/stages/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stages
import (
"context"
"fmt"
"path"

kargoapi "github.com/akuity/kargo/api/v1alpha1"
argocd "github.com/akuity/kargo/internal/controller/argocd/api/v1alpha1"
Expand Down Expand Up @@ -119,21 +120,24 @@ func (r *reconciler) checkHealth(
}

var desiredRevision string
sourceGitRepoURL := git.NormalizeGitURL(app.Spec.Source.RepoURL)
for _, commit := range currentFreight.Commits {
if git.NormalizeGitURL(commit.RepoURL) == sourceGitRepoURL {
if commit.HealthCheckCommit != "" {
desiredRevision = commit.HealthCheckCommit
} else {
desiredRevision = commit.ID
if app.Spec.Source.Chart == "" {
// This source points to a git repository
sourceGitRepoURL := git.NormalizeGitURL(app.Spec.Source.RepoURL)
for _, commit := range currentFreight.Commits {
if git.NormalizeGitURL(commit.RepoURL) == sourceGitRepoURL {
if commit.HealthCheckCommit != "" {
desiredRevision = commit.HealthCheckCommit
} else {
desiredRevision = commit.ID
}
}
break
}
break
}
if desiredRevision == "" {
} else {
// This source points to a Helm chart
for _, chart := range currentFreight.Charts {
if chart.RegistryURL == app.Spec.Source.RepoURL &&
chart.Name == app.Spec.Source.Chart {
// path.Join accounts for the possibility that chart.Name is empty
if path.Join(chart.RepoURL, chart.Name) == path.Join(app.Spec.Source.RepoURL, app.Spec.Source.Chart) {
desiredRevision = chart.Version
break
}
Expand Down
47 changes: 31 additions & 16 deletions internal/controller/warehouses/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package warehouses

import (
"context"
"path"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/credentials"
Expand All @@ -26,18 +26,20 @@ func (r *reconciler) getLatestCharts(

sub := s.Chart

logger := logging.LoggerFromContext(ctx).WithFields(log.Fields{
"registry": sub.RegistryURL,
"chart": sub.Name,
})
logger := logging.LoggerFromContext(ctx).WithField("repoURL", sub.RepoURL)
if sub.Name != "" {
logger = logger.WithField("chart", sub.Name)
}

// path.Join accounts for the possibility that chart.Name is empty
chartURL := path.Join(sub.RepoURL, sub.Name)
creds, ok, err :=
r.credentialsDB.Get(ctx, namespace, credentials.TypeHelm, sub.RegistryURL)
r.credentialsDB.Get(ctx, namespace, credentials.TypeHelm, chartURL)
if err != nil {
return nil, errors.Wrapf(
err,
"error obtaining credentials for chart registry %q",
sub.RegistryURL,
"error obtaining credentials for chart %q",
chartURL,
)
}

Expand All @@ -54,26 +56,39 @@ func (r *reconciler) getLatestCharts(

vers, err := r.getLatestChartVersionFn(
ctx,
sub.RegistryURL,
sub.RepoURL,
sub.Name,
sub.SemverConstraint,
helmCreds,
)
if err != nil {
if sub.Name == "" {
return nil, errors.Wrapf(
err,
"error searching for latest version of chart in repository %q",
sub.RepoURL,
)
}
return nil, errors.Wrapf(
err,
"error searching for latest version of chart %q in registry %q",
"error searching for latest version of chart %q in repository %q",
sub.Name,
sub.RegistryURL,
sub.RepoURL,
)
}

if vers == "" {
logger.Error("found no suitable chart version")
if sub.Name == "" {
return nil, errors.Errorf(
"found no suitable version of chart in repository %q",
sub.RepoURL,
)
}
return nil, errors.Errorf(
"found no suitable version of chart %q in registry %q",
"found no suitable version of chart %q in repository %q",
sub.Name,
sub.RegistryURL,
sub.RepoURL,
)
}
logger.WithField("version", vers).
Expand All @@ -82,9 +97,9 @@ func (r *reconciler) getLatestCharts(
charts = append(
charts,
kargoapi.Chart{
RegistryURL: sub.RegistryURL,
Name: sub.Name,
Version: vers,
RepoURL: sub.RepoURL,
Name: sub.Name,
Version: vers,
},
)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/warehouses/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGetLatestCharts(t *testing.T) {
assertions func([]kargoapi.Chart, error)
}{
{
name: "error getting registry credentials",
name: "error getting repository credentials",
credentialsDB: &credentials.FakeDB{
GetFn: func(
context.Context,
Expand All @@ -43,7 +43,7 @@ func TestGetLatestCharts(t *testing.T) {
require.Contains(
t,
err.Error(),
"error obtaining credentials for chart registry",
"error obtaining credentials for chart",
)
require.Contains(t, err.Error(), "something went wrong")
},
Expand Down Expand Up @@ -135,9 +135,9 @@ func TestGetLatestCharts(t *testing.T) {
require.Equal(
t,
kargoapi.Chart{
RegistryURL: "fake-url",
Name: "fake-chart",
Version: "1.0.0",
RepoURL: "fake-url",
Name: "fake-chart",
Version: "1.0.0",
},
charts[0],
)
Expand All @@ -156,8 +156,8 @@ func TestGetLatestCharts(t *testing.T) {
[]kargoapi.RepoSubscription{
{
Chart: &kargoapi.ChartSubscription{
RegistryURL: "fake-url",
Name: "fake-chart",
RepoURL: "fake-url",
Name: "fake-chart",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/warehouses/warehouses.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type reconciler struct {

getLatestChartVersionFn func(
ctx context.Context,
registryURL string,
repoURL string,
chart string,
semverConstraint string,
creds *helm.Credentials,
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/warehouses/warehouses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ func TestGetLatestFreightFromRepos(t *testing.T) {
) ([]kargoapi.Chart, error) {
return []kargoapi.Chart{
{
RegistryURL: "fake-registry",
Name: "fake-chart",
Version: "fake-version",
RepoURL: "fake-repo",
Name: "fake-chart",
Version: "fake-version",
},
}, nil
},
Expand Down Expand Up @@ -371,9 +371,9 @@ func TestGetLatestFreightFromRepos(t *testing.T) {
},
Charts: []kargoapi.Chart{
{
RegistryURL: "fake-registry",
Name: "fake-chart",
Version: "fake-version",
RepoURL: "fake-repo",
Name: "fake-chart",
Version: "fake-version",
},
},
},
Expand Down
28 changes: 21 additions & 7 deletions internal/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,9 @@ func getCredentialsSecret(
repoURL string,
acceptPrefixMatch bool,
) (*corev1.Secret, error) {
if credType == TypeGit {
// This is important. We don't want the presence or absence of ".git" at the
// end of the URL to affect credential lookups.
repoURL = git.NormalizeGitURL(repoURL)
}
repoURL = normalizeChartRepositoryURL( // This should be safe even on non-chart repo URLs
git.NormalizeGitURL(repoURL), // This should be safe even on non-Git URLs
)

secrets := corev1.SecretList{}
if err := kubeClient.List(
Expand All @@ -291,17 +289,33 @@ func getCredentialsSecret(
if !ok {
continue
}
url := string(urlBytes)
url := normalizeChartRepositoryURL( // This should be safe even on non-chart repo URLs
git.NormalizeGitURL( // This should be safe even on non-Git URLs
string(urlBytes),
),
)
if acceptPrefixMatch && strings.HasPrefix(repoURL, url) {
return &secret, nil
}
if !acceptPrefixMatch && git.NormalizeGitURL(url) == repoURL {
if !acceptPrefixMatch && url == repoURL {
return &secret, nil
}
}
return nil, nil
}

// NormalizeURL normalizes a chart repository URL for purposes of comparison.
// Crucially, this function removes the oci:// prefix from the URL if there is
// one.
func normalizeChartRepositoryURL(repo string) string {
return strings.TrimPrefix(
strings.ToLower(
strings.TrimSpace(repo),
),
"oci://",
)
}

func secretToCreds(secret *corev1.Secret) Credentials {
return Credentials{
Username: string(secret.Data["username"]),
Expand Down
Loading

0 comments on commit ea8aaba

Please sign in to comment.