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

Testing PR #3022, #3039 #1

Closed
wants to merge 12 commits into from
6 changes: 3 additions & 3 deletions pkg/cluster/kubernetes/resource/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func debyte(r resource.Resource) resource.Resource {
func TestLoadSome(t *testing.T) {
dir, cleanup := testfiles.TempDir(t)
defer cleanup()
if err := testfiles.WriteTestFiles(dir); err != nil {
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
t.Fatal(err)
}
objs, err := Load(dir, []string{dir}, false)
Expand All @@ -283,7 +283,7 @@ func TestLoadSome(t *testing.T) {
func TestChartTracker(t *testing.T) {
dir, cleanup := testfiles.TempDir(t)
defer cleanup()
if err := testfiles.WriteTestFiles(dir); err != nil {
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -331,7 +331,7 @@ func TestChartTracker(t *testing.T) {
func TestLoadSomeWithSopsNoneEncrypted(t *testing.T) {
dir, cleanup := testfiles.TempDir(t)
defer cleanup()
if err := testfiles.WriteTestFiles(dir); err != nil {
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
t.Fatal(err)
}
objs, err := Load(dir, []string{dir}, true)
Expand Down
59 changes: 57 additions & 2 deletions pkg/cluster/kubernetes/testfiles/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TempDir(t *testing.T) (string, func()) {
}

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

// WriteSopsEncryptedTestFiles ... given a directory, create files in it, based on predetermined file content.
Expand Down Expand Up @@ -358,6 +358,61 @@ spec:
`,
}

var FilesMultidoc = map[string]string{
"namespaces.yaml": `---
apiVersion: v1
kind: Namespace
metadata:
name: foo
annotations:
key: value
---
apiVersion: v1
kind: Namespace
metadata:
name: bar
annotations:
key: value
`,
}

// -+- .flux.yaml
// +- base/ -+- kustomization.yaml
// | +- foo.yaml
// +- staging/ -+- kustomization.yaml
// +- staging.yaml

var FilesForKustomize = map[string]string{
".flux.yaml": `version: 1
patchUpdated:
generators:
- command: kustomize build .
patchFile: flux-patch.yaml
`,
"base/kustomization.yaml": `resources:
- foo.yaml
`,
"base/foo.yaml": `apiVersion: v1
kind: Namespace
metadata:
name: foo
annotations:
key: value
`,
"staging/kustomization.yaml": `bases:
- ../base/
patches:
- staging.yaml
`,
"staging/staging.yaml": `apiVersion: v1
kind: Namespace
metadata:
name: foo
annotations:
env: staging
`,
}

var SopsEncryptedFiles = map[string]string{
"garbage": "This should just be ignored, since it is not YAML",
"helloworld-deploy.yaml": `apiVersion: ENC[AES256_GCM,data:N/68Js00AtWIvks/pt+be5AW,iv:9Ke36D3faRNrMzm82Z9ETl3lOMhhWy8fh907K5e2Ar4=,tag:EfAzs1AQvLRH/tIQ+iZttw==,type:str]
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/kubernetes/testfiles/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestWriteTestFiles(t *testing.T) {
dir, cleanup := TempDir(t)
defer cleanup()

if err := WriteTestFiles(dir); err != nil {
if err := WriteTestFiles(dir, Files); err != nil {
cleanup()
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/fluxcd/flux/pkg/api/v9"
"github.com/fluxcd/flux/pkg/cluster"
"github.com/fluxcd/flux/pkg/cluster/kubernetes"
"github.com/fluxcd/flux/pkg/cluster/kubernetes/testfiles"
"github.com/fluxcd/flux/pkg/cluster/mock"
"github.com/fluxcd/flux/pkg/event"
"github.com/fluxcd/flux/pkg/git"
Expand Down Expand Up @@ -664,7 +665,7 @@ func mockDaemon(t *testing.T) (*Daemon, func(), func(), *mock.Mock, *mockEventWr
},
}

repo, repoCleanup := gittest.Repo(t)
repo, repoCleanup := gittest.Repo(t, testfiles.Files)

syncTag := "flux-test"
params := git.Config{
Expand Down
20 changes: 15 additions & 5 deletions pkg/daemon/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/fluxcd/flux/pkg/git"
fluxmetrics "github.com/fluxcd/flux/pkg/metrics"
"github.com/fluxcd/flux/pkg/resource"
fluxsync "github.com/fluxcd/flux/pkg/sync"
)

Expand Down Expand Up @@ -180,26 +181,34 @@ func (d *LoopVars) AskForAutomatedWorkloadImageUpdates() {
}
}

// -- internals to keep track of sync tag state
// -- internals to keep track of sync tag and resources state
type lastKnownSyncState struct {
logger log.Logger
state fluxsync.State

// bookkeeping
revision string
resources map[string]resource.Resource
warnedAboutChange bool
}

// Current returns the revision from the state
func (s *lastKnownSyncState) Current(ctx context.Context) (string, error) {
// CurrentRevision returns the revision from the state
func (s *lastKnownSyncState) CurrentRevision(ctx context.Context) (string, error) {
return s.state.GetRevision(ctx)
}

// CurrentResources returns the synced resources from the state.
// If the state is not initialized (i.e. `Update()` has not been called yet), the returned value
// will be `nil`.
func (s *lastKnownSyncState) CurrentResources(ctx context.Context) (map[string]resource.Resource, error) {
return s.resources, nil
}

// Update records the synced revision in persistent storage (the
// sync.State). In addition, it checks that the old revision matches
// the last sync revision before making the update; mismatches suggest
// multiple Flux daemons are using the same state, so we log these.
func (s *lastKnownSyncState) Update(ctx context.Context, oldRev, newRev string) (bool, error) {
func (s *lastKnownSyncState) Update(ctx context.Context, oldRev, newRev string, resources map[string]resource.Resource) (bool, error) {
// Check if something other than the current instance of fluxd
// changed the sync tag. This is likely caused by another instance
// using the same tag. Having multiple instances fight for the same
Expand All @@ -220,8 +229,9 @@ func (s *lastKnownSyncState) Update(ctx context.Context, oldRev, newRev string)
return false, err
}

// Update in-memory revision
// Update in-memory revision and resources
s.revision = newRev
s.resources = resources

s.logger.Log("state", s.state.String(), "old", oldRev, "new", newRev)
return true, nil
Expand Down
Loading