Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.

Commit bb83626

Browse files
authored
Merge pull request #3022 from ordovicia/fix-3018
2 parents 943acd4 + ee13093 commit bb83626

File tree

9 files changed

+185
-25
lines changed

9 files changed

+185
-25
lines changed

pkg/cluster/kubernetes/resource/load_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func debyte(r resource.Resource) resource.Resource {
268268
func TestLoadSome(t *testing.T) {
269269
dir, cleanup := testfiles.TempDir(t)
270270
defer cleanup()
271-
if err := testfiles.WriteTestFiles(dir); err != nil {
271+
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
272272
t.Fatal(err)
273273
}
274274
objs, err := Load(dir, []string{dir}, false)
@@ -283,7 +283,7 @@ func TestLoadSome(t *testing.T) {
283283
func TestChartTracker(t *testing.T) {
284284
dir, cleanup := testfiles.TempDir(t)
285285
defer cleanup()
286-
if err := testfiles.WriteTestFiles(dir); err != nil {
286+
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
287287
t.Fatal(err)
288288
}
289289

@@ -331,7 +331,7 @@ func TestChartTracker(t *testing.T) {
331331
func TestLoadSomeWithSopsNoneEncrypted(t *testing.T) {
332332
dir, cleanup := testfiles.TempDir(t)
333333
defer cleanup()
334-
if err := testfiles.WriteTestFiles(dir); err != nil {
334+
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
335335
t.Fatal(err)
336336
}
337337
objs, err := Load(dir, []string{dir}, true)

pkg/cluster/kubernetes/testfiles/data.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func TempDir(t *testing.T) (string, func()) {
2727
}
2828

2929
// WriteTestFiles ... given a directory, create files in it, based on predetermined file content
30-
func WriteTestFiles(dir string) error {
31-
return writeFiles(dir, Files)
30+
func WriteTestFiles(dir string, files map[string]string) error {
31+
return writeFiles(dir, files)
3232
}
3333

3434
// WriteSopsEncryptedTestFiles ... given a directory, create files in it, based on predetermined file content.
@@ -358,6 +358,43 @@ spec:
358358
`,
359359
}
360360

361+
// -+- .flux.yaml
362+
// +- base/ -+- kustomization.yaml
363+
// | +- foo.yaml
364+
// +- staging/ -+- kustomization.yaml
365+
// +- staging.yaml
366+
367+
var FilesForKustomize = map[string]string{
368+
".flux.yaml": `version: 1
369+
patchUpdated:
370+
generators:
371+
- command: kustomize build .
372+
patchFile: flux-patch.yaml
373+
`,
374+
"base/kustomization.yaml": `resources:
375+
- foo.yaml
376+
`,
377+
"base/foo.yaml": `apiVersion: v1
378+
kind: Namespace
379+
metadata:
380+
name: foo
381+
annotations:
382+
key: value
383+
`,
384+
"staging/kustomization.yaml": `bases:
385+
- ../base/
386+
patches:
387+
- staging.yaml
388+
`,
389+
"staging/staging.yaml": `apiVersion: v1
390+
kind: Namespace
391+
metadata:
392+
name: foo
393+
annotations:
394+
env: staging
395+
`,
396+
}
397+
361398
var SopsEncryptedFiles = map[string]string{
362399
"garbage": "This should just be ignored, since it is not YAML",
363400
"helloworld-deploy.yaml": `apiVersion: ENC[AES256_GCM,data:N/68Js00AtWIvks/pt+be5AW,iv:9Ke36D3faRNrMzm82Z9ETl3lOMhhWy8fh907K5e2Ar4=,tag:EfAzs1AQvLRH/tIQ+iZttw==,type:str]

pkg/cluster/kubernetes/testfiles/data_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestWriteTestFiles(t *testing.T) {
1010
dir, cleanup := TempDir(t)
1111
defer cleanup()
1212

13-
if err := WriteTestFiles(dir); err != nil {
13+
if err := WriteTestFiles(dir, Files); err != nil {
1414
cleanup()
1515
t.Fatal(err)
1616
}

pkg/daemon/daemon_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/fluxcd/flux/pkg/api/v9"
2222
"github.com/fluxcd/flux/pkg/cluster"
2323
"github.com/fluxcd/flux/pkg/cluster/kubernetes"
24+
"github.com/fluxcd/flux/pkg/cluster/kubernetes/testfiles"
2425
"github.com/fluxcd/flux/pkg/cluster/mock"
2526
"github.com/fluxcd/flux/pkg/event"
2627
"github.com/fluxcd/flux/pkg/git"
@@ -664,7 +665,7 @@ func mockDaemon(t *testing.T) (*Daemon, func(), func(), *mock.Mock, *mockEventWr
664665
},
665666
}
666667

667-
repo, repoCleanup := gittest.Repo(t)
668+
repo, repoCleanup := gittest.Repo(t, testfiles.Files)
668669

669670
syncTag := "flux-test"
670671
params := git.Config{

pkg/daemon/sync.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (d *Daemon) Sync(ctx context.Context, started time.Time, newRevision string
6565
}
6666

6767
// Retrieve change set of commits we need to sync
68-
c, err := getChangeSet(ctx, ratchet, newRevision, d.Repo, d.GitTimeout, d.GitConfig.Paths)
68+
c, err := d.getChangeSet(ctx, ratchet, newRevision)
6969
if err != nil {
7070
return err
7171
}
@@ -127,7 +127,7 @@ func (d *Daemon) Sync(ctx context.Context, started time.Time, newRevision string
127127

128128
// getChangeSet returns the change set of commits for this sync,
129129
// including the revision range and if it is an initial sync.
130-
func getChangeSet(ctx context.Context, state revisionRatchet, headRev string, repo *git.Repo, timeout time.Duration, paths []string) (changeSet, error) {
130+
func (d *Daemon) getChangeSet(ctx context.Context, state revisionRatchet, headRev string) (changeSet, error) {
131131
var c changeSet
132132
var err error
133133

@@ -139,12 +139,17 @@ func getChangeSet(ctx context.Context, state revisionRatchet, headRev string, re
139139
c.oldTagRev = currentRev
140140
c.newTagRev = headRev
141141

142-
ctx, cancel := context.WithTimeout(ctx, timeout)
142+
paths := d.GitConfig.Paths
143+
if d.ManifestGenerationEnabled {
144+
paths = []string{}
145+
}
146+
147+
ctx, cancel := context.WithTimeout(ctx, d.GitTimeout)
143148
if c.oldTagRev != "" {
144-
c.commits, err = repo.CommitsBetween(ctx, c.oldTagRev, c.newTagRev, false, paths...)
149+
c.commits, err = d.Repo.CommitsBetween(ctx, c.oldTagRev, c.newTagRev, false, paths...)
145150
} else {
146151
c.initialSync = true
147-
c.commits, err = repo.CommitsBefore(ctx, c.newTagRev, false, paths...)
152+
c.commits, err = d.Repo.CommitsBefore(ctx, c.newTagRev, false, paths...)
148153
}
149154
cancel()
150155

pkg/daemon/sync_test.go

+124-8
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ var (
4141
events *mockEventWriter
4242
)
4343

44-
func daemon(t *testing.T) (*Daemon, func()) {
45-
repo, repoCleanup := gittest.Repo(t)
44+
func daemon(t *testing.T, files map[string]string) (*Daemon, func()) {
45+
repo, repoCleanup := gittest.Repo(t, files)
4646

4747
k8s = &mock.Mock{}
4848
k8s.ExportFunc = func(ctx context.Context) ([]byte, error) { return nil, nil }
@@ -132,7 +132,7 @@ func checkSyncManifestsMetrics(t *testing.T, manifestSuccess, manifestFailures i
132132
}
133133

134134
func TestPullAndSync_InitialSync(t *testing.T) {
135-
d, cleanup := daemon(t)
135+
d, cleanup := daemon(t, testfiles.Files)
136136
defer cleanup()
137137

138138
syncCalled := 0
@@ -199,7 +199,7 @@ func TestPullAndSync_InitialSync(t *testing.T) {
199199
}
200200

201201
func TestDoSync_NoNewCommits(t *testing.T) {
202-
d, cleanup := daemon(t)
202+
d, cleanup := daemon(t, testfiles.Files)
203203
defer cleanup()
204204

205205
var syncTag = "syncity"
@@ -279,7 +279,7 @@ func TestDoSync_NoNewCommits(t *testing.T) {
279279
}
280280

281281
func TestDoSync_WithNewCommit(t *testing.T) {
282-
d, cleanup := daemon(t)
282+
d, cleanup := daemon(t, testfiles.Files)
283283
defer cleanup()
284284

285285
ctx := context.Background()
@@ -376,7 +376,7 @@ func TestDoSync_WithNewCommit(t *testing.T) {
376376
t.Errorf("Sync was called with a nil syncDef")
377377
}
378378

379-
// The emitted event has no workload ids
379+
// An event is emitted and it only has a changed workload id
380380
es, err := events.AllEvents(time.Time{}, -1, time.Time{})
381381
if err != nil {
382382
t.Error(err)
@@ -387,7 +387,6 @@ func TestDoSync_WithNewCommit(t *testing.T) {
387387
} else {
388388
gotResourceIDs := es[0].ServiceIDs
389389
resource.IDs(gotResourceIDs).Sort()
390-
// Event should only have changed workload ids
391390
if !reflect.DeepEqual(gotResourceIDs, []resource.ID{resource.MustParseID("default:deployment/helloworld")}) {
392391
t.Errorf("Unexpected event workload ids: %#v, expected: %#v", gotResourceIDs, []resource.ID{resource.MustParseID("default:deployment/helloworld")})
393392
}
@@ -406,8 +405,125 @@ func TestDoSync_WithNewCommit(t *testing.T) {
406405
}
407406
}
408407

408+
func TestDoSync_WithKustomize(t *testing.T) {
409+
d, cleanup := daemon(t, testfiles.FilesForKustomize)
410+
defer cleanup()
411+
412+
d.GitConfig.Paths = []string{"staging"}
413+
d.ManifestGenerationEnabled = true
414+
415+
ctx := context.Background()
416+
417+
var syncTag = "syncy-mcsyncface"
418+
// Set the sync tag to head
419+
var oldRevision, newRevision string
420+
err := d.WithWorkingClone(ctx, func(checkout *git.Checkout) error {
421+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
422+
defer cancel()
423+
424+
var err error
425+
tagAction := git.TagAction{
426+
Tag: syncTag,
427+
Revision: "master",
428+
Message: "Sync pointer",
429+
}
430+
err = checkout.MoveTagAndPush(ctx, tagAction)
431+
if err != nil {
432+
return err
433+
}
434+
oldRevision, err = checkout.HeadRevision(ctx)
435+
if err != nil {
436+
return err
437+
}
438+
439+
// Push some new changes
440+
absolutePath := path.Join(checkout.Dir(), "base", "foo.yaml")
441+
def, err := ioutil.ReadFile(absolutePath)
442+
if err != nil {
443+
return err
444+
}
445+
446+
newDef := bytes.Replace(def, []byte("key: value"), []byte("key: value2"), -1)
447+
if err := ioutil.WriteFile(absolutePath, newDef, 0600); err != nil {
448+
return err
449+
}
450+
451+
commitAction := git.CommitAction{Author: "", Message: "test commit"}
452+
err = checkout.CommitAndPush(ctx, commitAction, nil, true)
453+
if err != nil {
454+
return err
455+
}
456+
newRevision, err = checkout.HeadRevision(ctx)
457+
return err
458+
})
459+
if err != nil {
460+
t.Fatal(err)
461+
}
462+
463+
err = d.Repo.Refresh(ctx)
464+
if err != nil {
465+
t.Error(err)
466+
}
467+
468+
syncCalled := 0
469+
var syncDef *cluster.SyncSet
470+
k8s.SyncFunc = func(def cluster.SyncSet) error {
471+
syncCalled++
472+
syncDef = &def
473+
return nil
474+
}
475+
476+
head, err := d.Repo.BranchHead(ctx)
477+
if err != nil {
478+
t.Fatal(err)
479+
}
480+
481+
gitSync, _ := fluxsync.NewGitTagSyncProvider(d.Repo, syncTag, "", fluxsync.VerifySignaturesModeNone, d.GitConfig)
482+
syncState := &lastKnownSyncState{logger: d.Logger, state: gitSync}
483+
484+
if err := d.Sync(ctx, time.Now().UTC(), head, syncState); err != nil {
485+
t.Error(err)
486+
}
487+
488+
// It applies everything
489+
if syncCalled != 1 {
490+
t.Errorf("Sync was not called once, was called %d times", syncCalled)
491+
} else if syncDef == nil {
492+
t.Errorf("Sync was called with a nil syncDef")
493+
}
494+
495+
// An event is emitted and it only has a changed workload id
496+
es, err := events.AllEvents(time.Time{}, -1, time.Time{})
497+
if err != nil {
498+
t.Error(err)
499+
} else if len(es) != 1 {
500+
t.Errorf("Unexpected events: %#v", es)
501+
} else if es[0].Type != event.EventSync {
502+
t.Errorf("Unexpected event type: %#v", es[0])
503+
} else {
504+
gotResourceIDs := es[0].ServiceIDs
505+
resource.IDs(gotResourceIDs).Sort()
506+
if !reflect.DeepEqual(gotResourceIDs, []resource.ID{resource.MustParseID("default:namespace/foo")}) {
507+
t.Errorf("Unexpected event workload ids: %#v, expected: %#v", gotResourceIDs, []resource.ID{resource.MustParseID("default:namespace/foo")})
508+
}
509+
}
510+
511+
// It moves the tag
512+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
513+
defer cancel()
514+
if err := d.Repo.Refresh(ctx); err != nil {
515+
t.Errorf("pulling sync tag: %v", err)
516+
} else if revs, err := d.Repo.CommitsBetween(ctx, oldRevision, syncTag, false); err != nil {
517+
t.Errorf("finding revisions before sync tag: %v", err)
518+
} else if len(revs) <= 0 {
519+
t.Errorf("Should have moved sync tag forward")
520+
} else if revs[len(revs)-1].Revision != newRevision {
521+
t.Errorf("Should have moved sync tag to HEAD (%s), but was moved to: %s", newRevision, revs[len(revs)-1].Revision)
522+
}
523+
}
524+
409525
func TestDoSync_WithErrors(t *testing.T) {
410-
d, cleanup := daemon(t)
526+
d, cleanup := daemon(t, testfiles.Files)
411527
defer cleanup()
412528

413529
expectedResourceIDs := resource.IDs{}

pkg/git/gittest/repo.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// Repo creates a new clone-able git repo, pre-populated with some kubernetes
1616
// files and a few commits. Also returns a cleanup func to clean up after.
17-
func Repo(t *testing.T) (*git.Repo, func()) {
17+
func Repo(t *testing.T, files map[string]string) (*git.Repo, func()) {
1818
newDir, cleanup := testfiles.TempDir(t)
1919

2020
filesDir := filepath.Join(newDir, "files")
@@ -36,7 +36,8 @@ func Repo(t *testing.T) (*git.Repo, func()) {
3636
cleanup()
3737
t.Fatal(err)
3838
}
39-
if err = testfiles.WriteTestFiles(filesDir); err != nil {
39+
40+
if err = testfiles.WriteTestFiles(filesDir, files); err != nil {
4041
cleanup()
4142
t.Fatal(err)
4243
}
@@ -79,7 +80,7 @@ func CheckoutWithConfig(t *testing.T, config git.Config, syncTag string) (*git.C
7980
// This is to make sure that git commands don't have ambiguity problems between revisions and files.
8081
testfiles.Files[config.Branch] = "Filename doctored to create a conflict with the git branch name"
8182
testfiles.Files[syncTag] = "Filename doctored to create a conflict with the git sync tag"
82-
repo, cleanup := Repo(t)
83+
repo, cleanup := Repo(t, testfiles.Files)
8384
if err := repo.Ready(context.Background()); err != nil {
8485
cleanup()
8586
t.Fatal(err)

pkg/git/gittest/repo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func TestSignedTag(t *testing.T) {
159159
}
160160

161161
func TestCheckout(t *testing.T) {
162-
repo, cleanup := Repo(t)
162+
repo, cleanup := Repo(t, testfiles.Files)
163163
defer cleanup()
164164

165165
sd, sg := make(chan struct{}), &sync.WaitGroup{}

pkg/git/operations_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func createRepo(dir string, subdirs []string) error {
365365
return err
366366
}
367367

368-
if err = testfiles.WriteTestFiles(fullPath); err != nil {
368+
if err = testfiles.WriteTestFiles(fullPath, testfiles.Files); err != nil {
369369
return err
370370
}
371371
if err = execCommand("git", "-C", dir, "add", "--all"); err != nil {

0 commit comments

Comments
 (0)