diff --git a/pkg/apis/cartographer/v1alpha1/workload_helpers.go b/pkg/apis/cartographer/v1alpha1/workload_helpers.go index 281867e3c..4417ccc1f 100644 --- a/pkg/apis/cartographer/v1alpha1/workload_helpers.go +++ b/pkg/apis/cartographer/v1alpha1/workload_helpers.go @@ -208,8 +208,8 @@ func (w *GitSource) Validate() validation.FieldErrors { errs = errs.Also(validation.ErrMissingField(flags.GitRepoFlagName)) } - if w.Ref.Branch == "" && w.Ref.Tag == "" { - errs = errs.Also(validation.ErrMissingOneOf(flags.GitBranchFlagName, flags.GitTagFlagName)) + if w.Ref.Branch == "" && w.Ref.Tag == "" && w.Ref.Commit == "" { + errs = errs.Also(validation.ErrMissingOneOf(flags.GitBranchFlagName, flags.GitTagFlagName, flags.GitCommitFlagName)) } return errs @@ -340,6 +340,12 @@ func (w *WorkloadSpec) MergeGit(git GitSource) { if w.Source.Git.Ref.Branch == "" { w.Source.Git.Ref.Branch = stash.Git.Ref.Branch } + if w.Source.Git.Ref.Tag == "" { + w.Source.Git.Ref.Tag = stash.Git.Ref.Tag + } + if w.Source.Git.Ref.Commit == "" { + w.Source.Git.Ref.Commit = stash.Git.Ref.Commit + } } } diff --git a/pkg/apis/cartographer/v1alpha1/workload_test.go b/pkg/apis/cartographer/v1alpha1/workload_test.go index 57ad90688..e8e6f45ad 100644 --- a/pkg/apis/cartographer/v1alpha1/workload_test.go +++ b/pkg/apis/cartographer/v1alpha1/workload_test.go @@ -491,6 +491,25 @@ func TestWorkload_Validate(t *testing.T) { }, }, want: validation.FieldErrors{}, + }, { + name: "valid git using --git-commit", + workload: Workload{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-workload", + Namespace: "default", + }, + Spec: WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com/example/repo.git", + Ref: GitRef{ + Commit: "abcd123", + }, + }, + }, + }, + }, + want: validation.FieldErrors{}, }, { name: "valid git using --git-tag with subPath", workload: Workload{ @@ -526,7 +545,7 @@ func TestWorkload_Validate(t *testing.T) { }, want: validation.FieldErrors{}.Also( validation.ErrMissingField(flags.GitRepoFlagName), - validation.ErrMissingOneOf(flags.GitBranchFlagName, flags.GitTagFlagName), + validation.ErrMissingOneOf(flags.GitBranchFlagName, flags.GitTagFlagName, flags.GitCommitFlagName), ), }, { name: "valid source image", @@ -1402,7 +1421,7 @@ func TestWorkloadSpec_MergeGit(t *testing.T) { }, }, }, { - name: "update", + name: "update url", seed: &WorkloadSpec{ Source: &Source{ Git: &GitSource{ @@ -1416,9 +1435,6 @@ func TestWorkloadSpec_MergeGit(t *testing.T) { }, git: GitSource{ URL: "git@github.com:example/repo.git", - Ref: GitRef{ - Branch: "main", - }, }, want: &WorkloadSpec{ Source: &Source{ @@ -1426,6 +1442,7 @@ func TestWorkloadSpec_MergeGit(t *testing.T) { URL: "git@github.com:example/repo.git", Ref: GitRef{ Branch: "main", + Tag: "v1.0.0", }, }, }, @@ -1459,6 +1476,103 @@ func TestWorkloadSpec_MergeGit(t *testing.T) { }, }, }, + }, { + name: "update commit", + seed: &WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "main", + Tag: "v1.0.0", + Commit: "abcd1234", + }, + }, + }, + }, + git: GitSource{ + Ref: GitRef{ + Commit: "efgh5678", + }, + }, + want: &WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "main", + Tag: "v1.0.0", + Commit: "efgh5678", + }, + }, + }, + }, + }, { + name: "update branch", + seed: &WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "main", + Tag: "v1.0.0", + Commit: "abcd1234", + }, + }, + }, + }, + git: GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "my-new-branch", + }, + }, + want: &WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "my-new-branch", + Tag: "v1.0.0", + Commit: "abcd1234", + }, + }, + }, + }, + }, { + name: "update all git ref", + seed: &WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "main", + Tag: "v1.0.0", + Commit: "abcd1234", + }, + }, + }, + }, + git: GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "my-new-branch", + Tag: "v1.1", + Commit: "efgh5678", + }, + }, + want: &WorkloadSpec{ + Source: &Source{ + Git: &GitSource{ + URL: "git@github.com:example/repo.git", + Ref: GitRef{ + Branch: "my-new-branch", + Tag: "v1.1", + Commit: "efgh5678", + }, + }, + }, + }, }, { name: "update git source without deleting subpath", seed: &WorkloadSpec{ @@ -1482,6 +1596,7 @@ func TestWorkloadSpec_MergeGit(t *testing.T) { Git: &GitSource{ URL: "git@github.com:example/repo.git", Ref: GitRef{ + Tag: "v1.0.0", Branch: "main", }, }, diff --git a/pkg/commands/workload_apply_test.go b/pkg/commands/workload_apply_test.go index bbd78aa76..384167399 100644 --- a/pkg/commands/workload_apply_test.go +++ b/pkg/commands/workload_apply_test.go @@ -3288,7 +3288,66 @@ To see logs: "tanzu apps workload tail spring-petclinic --timestamp --since 1h To get status: "tanzu apps workload get spring-petclinic" `, - }, { + }, + { + Name: "update git fields", + Args: []string{workloadName, flags.GitBranchFlagName, "accelerator", flags.GitCommitFlagName, "abcd1234", flags.YesFlagName}, + GivenObjects: []client.Object{ + parent. + SpecDie( + func(d *diecartov1alpha1.WorkloadSpecDie) { + d.Source(&cartov1alpha1.Source{ + Git: &cartov1alpha1.GitSource{ + URL: "https://github.com/sample-accelerators/spring-petclinic", + Ref: cartov1alpha1.GitRef{ + Branch: "main", + Tag: "tap-1.1", + }, + }, + }) + }), + }, + ExpectUpdates: []client.Object{ + &cartov1alpha1.Workload{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: defaultNamespace, + Name: workloadName, + Labels: map[string]string{}, + }, + Spec: cartov1alpha1.WorkloadSpec{ + Source: &cartov1alpha1.Source{ + Git: &cartov1alpha1.GitSource{ + URL: "https://github.com/sample-accelerators/spring-petclinic", + Ref: cartov1alpha1.GitRef{ + Branch: "accelerator", + Tag: "tap-1.1", + Commit: "abcd1234", + }, + }, + }, + }, + }, + }, + ExpectOutput: ` +🔎 Update workload: +... + 7, 7 |spec: + 8, 8 | source: + 9, 9 | git: + 10, 10 | ref: + 11 - | branch: main + 11 + | branch: accelerator + 12 + | commit: abcd1234 + 12, 13 | tag: tap-1.1 + 13, 14 | url: https://github.com/sample-accelerators/spring-petclinic +👍 Updated workload "my-workload" + +To see logs: "tanzu apps workload tail my-workload --timestamp --since 1h" +To get status: "tanzu apps workload get my-workload" + +`, + }, + { Name: "git source with non-allowed env var", Prepare: func(t *testing.T, ctx context.Context, config *cli.Config, tc *clitesting.CommandTestCase) (context.Context, error) { os.Setenv("TANZU_APPS_LABEL", "foo=var") @@ -5133,7 +5192,9 @@ To get status: "tanzu apps workload get spring-petclinic" }, { Name: "update - replace source", - Args: []string{flags.FilePathFlagName, "testdata/replace-update-strategy/replace-source.yaml", flags.UpdateStrategyFlagName, "replace", flags.YesFlagName}, + Args: []string{flags.FilePathFlagName, "testdata/replace-update-strategy/replace-source.yaml", + flags.GitCommitFlagName, "efgh456", + flags.UpdateStrategyFlagName, "replace", flags.YesFlagName}, GivenObjects: []client.Object{ parent. MetadataDie(func(d *diemetav1.ObjectMetaDie) { @@ -5168,7 +5229,7 @@ To get status: "tanzu apps workload get spring-petclinic" URL: "https://github.com/sample-accelerators/spring-petclinic", Ref: cartov1alpha1.GitRef{ Tag: "tap-1.1", - Commit: "abcd123", + Commit: "efgh456", }, }, }, @@ -5186,7 +5247,7 @@ To get status: "tanzu apps workload get spring-petclinic" 13, 13 | ref: 14 - | branch: main 15 - | url: https://github.com/spring-projects/spring-petclinic.git - 14 + | commit: abcd123 + 14 + | commit: efgh456 15 + | tag: tap-1.1 16 + | url: https://github.com/sample-accelerators/spring-petclinic 👍 Updated workload "spring-petclinic" diff --git a/testing/e2e/workload_test.go b/testing/e2e/workload_test.go index d8a6d3ffa..57e49181b 100644 --- a/testing/e2e/workload_test.go +++ b/testing/e2e/workload_test.go @@ -78,7 +78,6 @@ func TestCreateFromGitWithAnnotations(t *testing.T) { if ctx, err = createPtyTerminal(ctx); err != nil { t.Fatalf("error while opening pty %v", err) } - return ctx, nil }, ExpectedObject: &cartov1alpha1.Workload{ @@ -213,6 +212,43 @@ func TestCreateFromGitWithAnnotations(t *testing.T) { }, }, }, + { + Name: "Create workload from repo using git commit", + WorkloadName: "test-create-git-commit-workload", + Command: func() it.CommandLine { + c := *it.NewTanzuAppsCommandLine( + "workload", "apply", "test-create-git-commit-workload", + "--app=test-create-git-commit-workload", + "--git-commit=425ae9a", + "--git-repo=https://github.com/sample-accelerators/spring-petclinic", + namespaceFlag, + "--type=web", + ) + c.SurveyAnswer("y") + return c + }(), + ExpectedObject: &cartov1alpha1.Workload{ + TypeMeta: workloadTypeMeta, + ObjectMeta: metav1.ObjectMeta{ + Name: "test-create-git-commit-workload", + Namespace: it.TestingNamespace, + Labels: map[string]string{ + "app.kubernetes.io/part-of": "test-create-git-commit-workload", + "apps.tanzu.vmware.com/workload-type": "web", + }, + }, + Spec: cartov1alpha1.WorkloadSpec{ + Source: &cartov1alpha1.Source{ + Git: &cartov1alpha1.GitSource{ + URL: "https://github.com/sample-accelerators/spring-petclinic", + Ref: cartov1alpha1.GitRef{ + Commit: "425ae9a", + }, + }, + }, + }, + }, + }, { Name: "Create workload with maven flags", WorkloadName: "test-create-git-annotations-workload", @@ -660,7 +696,10 @@ func TestCreateFromGitWithAnnotations(t *testing.T) { Name: "Update the created workload", WorkloadName: "test-create-git-annotations-workload", Command: *it.NewTanzuAppsCommandLine( - "workload", "apply", "test-create-git-annotations-workload", namespaceFlag, "--annotation=min-instances=3", "--annotation=max-instances=5", "-y"), + "workload", "apply", "test-create-git-annotations-workload", namespaceFlag, + "--annotation=min-instances=3", "--annotation=max-instances=5", + "--git-commit", "425ae9a", + "-y"), ExpectedObject: &cartov1alpha1.Workload{ TypeMeta: workloadTypeMeta, ObjectMeta: metav1.ObjectMeta{ @@ -682,7 +721,8 @@ func TestCreateFromGitWithAnnotations(t *testing.T) { Git: &cartov1alpha1.GitSource{ URL: "https://github.com/sample-accelerators/spring-petclinic", Ref: cartov1alpha1.GitRef{ - Tag: "tap-1.2", + Tag: "tap-1.2", + Commit: "425ae9a", }, }, },