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

Commit

Permalink
Implement "files" directive in config files
Browse files Browse the repository at this point in the history
This adds a special case for a config file that acts as a reset
switch, by having a `files: ` entry instead of `patchUpdated` or
`commandUpdated`.

Since the desired behaviour is to act as though there were _no_ config
file in effect, by far the most direct implementation is to add the
path in question to the collection of raw files paths when a config
file containing the directive is found.
  • Loading branch information
squaremo committed Jan 21, 2020
1 parent 4f3695f commit 7efb1b4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/manifests/configaware.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func splitConfigFilesAndRawManifestPaths(baseDir string, paths []string) ([]*Con
if err != nil {
return nil, nil, fmt.Errorf("cannot parse config file: %s", err)
}
if cf.IsJustFiles() {
rawManifestPaths = append(rawManifestPaths, path)
continue
}
configFiles = append(configFiles, cf)
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/manifests/configaware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,30 @@ func TestDuplicateInGenerators(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate")
}

func TestJustFiles(t *testing.T) {
// +-- config
// +-- .flux.yaml (patchUpdated)
// +-- rawfiles
// +-- .flux.yaml (files)
// +-- manifest.yaml

manifestyaml := `
apiVersion: v1
kind: Namespace
metadata:
name: foo-ns
`

config, baseDir, cleanup := setup(t, []string{filepath.Join("config", "rawfiles")},
config{path: "config", fluxyaml: patchUpdatedEchoConfigFile},
config{path: filepath.Join("config", "rawfiles"), fluxyaml: "version: 1\nfiles: {}\n"},
)
defer cleanup()

assert.NoError(t, ioutil.WriteFile(filepath.Join(baseDir, "config", "rawfiles", "manifest.yaml"), []byte(manifestyaml), 0600))

res, err := config.GetAllResourcesByID(context.Background())
assert.NoError(t, err)
assert.Contains(t, res, "default:namespace/foo-ns")
}
9 changes: 9 additions & 0 deletions pkg/manifests/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ type PatchUpdated struct {
type Files struct {
}

// IsJustFiles returns true if the config file indicates that the
// directory should be treated as containing YAML files (i.e., should
// act as though there was no config file in operation). This can be
// used to reset the directive given by a .flux.yaml higher in the
// directory structure.
func (cf *ConfigFile) IsJustFiles() bool {
return cf.Files != nil
}

func ParseConfigFile(fileBytes []byte, result *ConfigFile) error {
// The file contents are unmarshaled into a map so that we will
// see any extraneous fields. This is important, for example, for
Expand Down
15 changes: 15 additions & 0 deletions pkg/manifests/configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ files: {}
}
}

const justFilesConfigFile = `
version: 1
files: {}
`

func TestJustFileDirective(t *testing.T) {
var cf ConfigFile
err := ParseConfigFile([]byte(justFilesConfigFile), &cf)
assert.NoError(t, err)

assert.True(t, cf.IsJustFiles())
}

const patchUpdatedConfigFile = `---
version: 1
patchUpdated:
Expand All @@ -96,6 +109,7 @@ func TestParsePatchUpdatedConfigFile(t *testing.T) {
if err := ParseConfigFile([]byte(patchUpdatedConfigFile), &cf); err != nil {
t.Fatal(err)
}
assert.False(t, cf.IsJustFiles())
assert.NotNil(t, cf.PatchUpdated)
assert.Nil(t, cf.CommandUpdated)
assert.Equal(t, 1, cf.Version)
Expand Down Expand Up @@ -126,6 +140,7 @@ func TestParseCmdUpdatedConfigFile(t *testing.T) {
if err := ParseConfigFile([]byte(echoCmdUpdatedConfigFile), &cf); err != nil {
t.Fatal(err)
}
assert.False(t, cf.IsJustFiles())
assert.NotNil(t, cf.CommandUpdated)
assert.Nil(t, cf.PatchUpdated)
assert.Equal(t, 1, cf.Version)
Expand Down
2 changes: 2 additions & 0 deletions pkg/manifests/rawfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type rawFiles struct {
manifests Manifests
}

// NewRawFiles constructs a `Store` that assumes the provided
// directories contain plain YAML files
func NewRawFiles(baseDir string, paths []string, manifests Manifests) *rawFiles {
return &rawFiles{
baseDir: baseDir,
Expand Down

0 comments on commit 7efb1b4

Please sign in to comment.