Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow user to pass only git commit for git ref #423

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/apis/cartographer/v1alpha1/workload_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}

Expand Down
125 changes: 120 additions & 5 deletions pkg/apis/cartographer/v1alpha1/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1402,7 +1421,7 @@ func TestWorkloadSpec_MergeGit(t *testing.T) {
},
},
}, {
name: "update",
name: "update url",
seed: &WorkloadSpec{
Source: &Source{
Git: &GitSource{
Expand All @@ -1416,16 +1435,14 @@ func TestWorkloadSpec_MergeGit(t *testing.T) {
},
git: GitSource{
URL: "git@github.com:example/repo.git",
Ref: GitRef{
Branch: "main",
},
},
want: &WorkloadSpec{
Source: &Source{
Git: &GitSource{
URL: "git@github.com:example/repo.git",
Ref: GitRef{
Branch: "main",
Tag: "v1.0.0",
},
},
},
Expand Down Expand Up @@ -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{
Expand All @@ -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",
},
},
Expand Down
69 changes: 65 additions & 4 deletions pkg/commands/workload_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
},
},
},
Expand All @@ -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"
Expand Down
46 changes: 43 additions & 3 deletions testing/e2e/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -660,7 +696,10 @@ func TestCreateFromGitWithAnnotations(t *testing.T) {
Name: "Update the created workload",
warango4 marked this conversation as resolved.
Show resolved Hide resolved
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{
Expand All @@ -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",
},
},
},
Expand Down