Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nikimanoledaki committed Feb 16, 2022
1 parent c1633cd commit 28a502e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
9 changes: 6 additions & 3 deletions pkg/helm/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
sourcev1beta1 "github.com/fluxcd/source-controller/api/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
apimachinery "k8s.io/apimachinery/pkg/util/yaml"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
kyaml "sigs.k8s.io/yaml"
)

const DefaultBufferSize = 2048

// MakeHelmRelease returns a HelmRelease object given a name, version, cluster, namespace, and HelmRepository's name and namespace.
func MakeHelmRelease(name, version, cluster, namespace string, helmRepository types.NamespacedName) *helmv2beta1.HelmRelease {
return &helmv2beta1.HelmRelease{
Expand Down Expand Up @@ -44,7 +46,8 @@ func MakeHelmRelease(name, version, cluster, namespace string, helmRepository ty
}
}

// AppendHelmReleaseToString appends a HelmRelease to a string.
// AppendHelmReleaseToString appends "---" and a HelmRelease to string that may or may not be empty.
// This creates the content of a manifest that contains HelmReleases separated by "---".
func AppendHelmReleaseToString(content string, newRelease *helmv2beta1.HelmRelease) (string, error) {
var sb strings.Builder
if content != "" {
Expand All @@ -65,7 +68,7 @@ func AppendHelmReleaseToString(content string, newRelease *helmv2beta1.HelmRelea
func SplitHelmReleaseYAML(resources []byte) ([]*helmv2beta1.HelmRelease, error) {
var helmReleaseList []*helmv2beta1.HelmRelease

decoder := apimachinery.NewYAMLOrJSONDecoder(bytes.NewReader(resources), 100000000)
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(resources), DefaultBufferSize)

for {
var value helmv2beta1.HelmRelease
Expand Down
17 changes: 8 additions & 9 deletions pkg/services/profiles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/fluxcd/go-git-providers/gitprovider"
helmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/types"
)

const UpdateCommitMessage = "Update Profile manifests"
Expand Down Expand Up @@ -47,7 +46,7 @@ func (s *ProfilesSvc) Update(ctx context.Context, gitProvider gitproviders.GitPr
return fmt.Errorf("failed to get default branch: %w", err)
}

helmRepo, version, err := s.discoverHelmRepository(ctx, GetOptions{
_, version, err := s.discoverHelmRepository(ctx, GetOptions{
Name: opts.Name,
Version: opts.Version,
Cluster: opts.Cluster,
Expand All @@ -65,12 +64,7 @@ func (s *ProfilesSvc) Update(ctx context.Context, gitProvider gitproviders.GitPr
return fmt.Errorf("failed to get files in '%s' of config repository %q: %s", git.GetSystemPath(opts.Cluster), configRepoURL, err)
}

fileContent := getGitCommitFileContent(files, git.GetProfilesPath(opts.Cluster, models.WegoProfilesPath))
if fileContent == "" {
return fmt.Errorf("failed to find installed profiles in '%s' of config repo %q", git.GetProfilesPath(opts.Cluster, models.WegoProfilesPath), configRepoURL)
}

content, err := updateHelmRelease(helmRepo, fileContent, opts.Name, opts.Version, opts.Cluster, opts.Namespace)
content, err := updateHelmRelease(files, opts.Name, opts.Version, opts.Cluster, opts.Namespace)
if err != nil {
return fmt.Errorf("failed to update HelmRelease for profile '%s' in %s: %w", opts.Name, models.WegoProfilesPath, err)
}
Expand Down Expand Up @@ -115,7 +109,12 @@ func (s *ProfilesSvc) printUpdateSummary(opts UpdateOptions) {
s.Logger.Println("Namespace: %s\n", opts.Namespace)
}

func updateHelmRelease(helmRepo types.NamespacedName, fileContent, name, version, cluster, ns string) (string, error) {
func updateHelmRelease(files []*gitprovider.CommitFile, name, version, cluster, ns string) (string, error) {
fileContent := getGitCommitFileContent(files, git.GetProfilesPath(cluster, models.WegoProfilesPath))
if fileContent == "" {
return "", fmt.Errorf("failed to find installed profiles in '%s'", git.GetProfilesPath(cluster, models.WegoProfilesPath))
}

existingReleases, err := helm.SplitHelmReleaseYAML([]byte(fileContent))
if err != nil {
return "", fmt.Errorf("error splitting into YAML: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/profiles/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ var _ = Describe("Update Profile(s)", func() {
gitProviders.GetRepoDirFilesReturns(makeTestFiles(), nil)

err := profilesSvc.Update(context.TODO(), gitProviders, updateOptions)
Expect(err).To(MatchError("failed to find installed profiles in '.weave-gitops/clusters/prod/system/profiles.yaml' of config repo \"ssh://git@github.com/owner/config-repo.git\""))
Expect(err).To(MatchError(ContainSubstring("failed to find installed profiles in '.weave-gitops/clusters/prod/system/profiles.yaml'")))

Expect(gitProviders.GetRepoDirFilesCallCount()).To(Equal(1))
})
Expand Down

0 comments on commit 28a502e

Please sign in to comment.