mirror of https://github.com/fluxcd/flux2.git
Refactor manifests generation
- introduce manifestgen pkg, to be consumed by the CLI and Terraform provider - consolidate defaults in manifestgen/install pkg - introduce Manifest as the returning type of manifest generation - add helper function to Manifest for writing multi-doc YAMLs on disk Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>pull/387/head
parent
41d4e7e15b
commit
9bc250d027
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2020 The Flux authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package manifestgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Manifest holds the data of a multi-doc YAML
|
||||
type Manifest struct {
|
||||
// Relative path to the YAML file
|
||||
Path string
|
||||
// Content in YAML format
|
||||
Content string
|
||||
}
|
||||
|
||||
// WriteFile writes the YAML content to a file inside the the root path.
|
||||
// If the file does not exist, WriteFile creates it with permissions perm,
|
||||
// otherwise WriteFile overwrites the file, without changing permissions.
|
||||
func (m *Manifest) WriteFile(rootDir string) (string, error) {
|
||||
if err := os.MkdirAll(path.Join(rootDir, filepath.Dir(m.Path)), os.ModePerm); err != nil {
|
||||
return "", fmt.Errorf("unable to create dir, error: %w", err)
|
||||
}
|
||||
|
||||
filePath := path.Join(rootDir, m.Path)
|
||||
if err := ioutil.WriteFile(filePath, []byte(m.Content), os.ModePerm); err != nil {
|
||||
return "", fmt.Errorf("unable to write file, error: %w", err)
|
||||
}
|
||||
|
||||
return filePath, nil
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright 2020 The Flux authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package sync
|
||||
|
||||
import "time"
|
||||
|
||||
type Options struct {
|
||||
Interval time.Duration
|
||||
URL string
|
||||
Name string
|
||||
Namespace string
|
||||
Branch string
|
||||
TargetPath string
|
||||
ManifestFile string
|
||||
}
|
||||
|
||||
func MakeDefaultOptions() Options {
|
||||
return Options{
|
||||
Interval: 1 * time.Minute,
|
||||
URL: "",
|
||||
Name: "gotk-system",
|
||||
Namespace: "gotk-system",
|
||||
Branch: "main",
|
||||
ManifestFile: "gotk-sync.yaml",
|
||||
TargetPath: "",
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright 2020 The Flux CD contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
opts := MakeDefaultOptions()
|
||||
output, err := Generate(opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, apiVersion := range []string{sourcev1.GroupVersion.String(), kustomizev1.GroupVersion.String()} {
|
||||
if !strings.Contains(output.Content, apiVersion) {
|
||||
t.Errorf("apiVersion '%s' not found", apiVersion)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(output.Content)
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package sync
|
||||
|
||||
import "time"
|
||||
|
||||
type Options struct {
|
||||
Interval time.Duration
|
||||
URL string
|
||||
Name string
|
||||
Namespace string
|
||||
Branch string
|
||||
TargetPath string
|
||||
}
|
||||
|
||||
func MakeDefaultOptions() Options {
|
||||
return Options{
|
||||
Interval: 1 * time.Minute,
|
||||
URL: "",
|
||||
Name: "gotk-system",
|
||||
Namespace: "gotk-system",
|
||||
Branch: "main",
|
||||
TargetPath: "",
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue