1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Refactor manifest generation

Signed-off-by: Philip Laine <philip.laine@xenit.se>
This commit is contained in:
Philip Laine
2020-10-28 15:47:24 +01:00
parent bd4d4d927e
commit b0d2a38ff6
9 changed files with 186 additions and 93 deletions

View File

@@ -28,13 +28,13 @@ import (
// Generate returns the install manifests as a multi-doc YAML.
// The manifests are built from a GitHub release or from a
// Kustomize overlay if the supplied Options.BaseURL is a local path.
func Generate(options Options) ([]byte, error) {
func Generate(options Options) (string, string, error) {
ctx, cancel := context.WithTimeout(context.Background(), options.Timeout)
defer cancel()
tmpDir, err := ioutil.TempDir("", options.Namespace)
if err != nil {
return nil, fmt.Errorf("temp dir error: %w", err)
return "", "", fmt.Errorf("temp dir error: %w", err)
}
defer os.RemoveAll(tmpDir)
@@ -42,26 +42,26 @@ func Generate(options Options) ([]byte, error) {
if !strings.HasPrefix(options.BaseURL, "http") {
if err := build(options.BaseURL, output); err != nil {
return nil, err
return "", "", err
}
} else {
if err := fetch(ctx, options.BaseURL, options.Version, tmpDir); err != nil {
return nil, err
return "", "", err
}
if err := generate(tmpDir, options); err != nil {
return nil, err
return "", "", err
}
if err := build(tmpDir, output); err != nil {
return nil, err
return "", "", err
}
}
content, err := ioutil.ReadFile(output)
if err != nil {
return nil, err
return "", "", err
}
return content, nil
return path.Join(options.TargetPath, options.Namespace, options.ManifestsFile), string(content), nil
}

View File

@@ -24,7 +24,7 @@ import (
func TestGenerate(t *testing.T) {
opts := MakeDefaultOptions()
output, err := Generate(opts)
_, output, err := Generate(opts)
if err != nil {
t.Fatal(err)
}

View File

@@ -33,6 +33,7 @@ type Options struct {
NotificationController string
ManifestsFile string
Timeout time.Duration
TargetPath string
}
func MakeDefaultOptions() Options {
@@ -51,6 +52,7 @@ func MakeDefaultOptions() Options {
NotificationController: "notification-controller",
ManifestsFile: "toolkit-components.yaml",
Timeout: time.Minute,
TargetPath: "",
}
}

23
pkg/sync/options.go Normal file
View File

@@ -0,0 +1,23 @@
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: "",
}
}

88
pkg/sync/sync.go Normal file
View File

@@ -0,0 +1,88 @@
package sync
import (
"fmt"
"path/filepath"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
)
const (
bootstrapSourceManifest = "toolkit-source.yaml"
bootstrapKustomizationManifest = "toolkit-kustomization.yaml"
)
func Generate(options Options) ([]map[string]string, error) {
files := []map[string]string{}
gvk := sourcev1.GroupVersion.WithKind(sourcev1.GitRepositoryKind)
gitRepository := sourcev1.GitRepository{
TypeMeta: metav1.TypeMeta{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: options.Name,
Namespace: options.Namespace,
},
Spec: sourcev1.GitRepositorySpec{
URL: options.URL,
Interval: metav1.Duration{
Duration: options.Interval,
},
Reference: &sourcev1.GitRepositoryRef{
Branch: options.Branch,
},
SecretRef: &corev1.LocalObjectReference{
Name: options.Name,
},
},
}
gitData, err := yaml.Marshal(gitRepository)
if err != nil {
return nil, err
}
files = append(files, map[string]string{"file_path": filepath.Join(options.TargetPath, options.Namespace, bootstrapSourceManifest), "content": string(gitData)})
gvk = kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind)
kustomization := kustomizev1.Kustomization{
TypeMeta: metav1.TypeMeta{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: options.Name,
Namespace: options.Namespace,
},
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{
Duration: 10 * time.Minute,
},
Path: fmt.Sprintf("./%s", strings.TrimPrefix(options.TargetPath, "./")),
Prune: true,
SourceRef: kustomizev1.CrossNamespaceSourceReference{
Kind: sourcev1.GitRepositoryKind,
Name: options.Name,
},
Validation: "client",
},
}
ksData, err := yaml.Marshal(kustomization)
if err != nil {
return nil, err
}
files = append(files, map[string]string{"file_path": filepath.Join(options.TargetPath, options.Namespace, bootstrapKustomizationManifest), "content": string(ksData)})
return files, nil
}

32
pkg/sync/sync_test.go Normal file
View File

@@ -0,0 +1,32 @@
/*
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"
"testing"
)
func TestGenerate(t *testing.T) {
opts := MakeDefaultOptions()
output, err := Generate(opts)
if err != nil {
t.Fatal(err)
}
fmt.Println(output)
}