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>
This commit is contained in:
195
pkg/manifestgen/install/templates.go
Normal file
195
pkg/manifestgen/install/templates.go
Normal file
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
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 install
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var kustomizationTmpl = `---
|
||||
{{- $eventsAddr := .EventsAddr }}
|
||||
{{- $watchAllNamespaces := .WatchAllNamespaces }}
|
||||
{{- $registry := .Registry }}
|
||||
{{- $arch := .Arch }}
|
||||
{{- $logLevel := .LogLevel }}
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
namespace: {{.Namespace}}
|
||||
|
||||
transformers:
|
||||
- labels.yaml
|
||||
|
||||
resources:
|
||||
- namespace.yaml
|
||||
{{- if .NetworkPolicy }}
|
||||
- policies.yaml
|
||||
{{- end }}
|
||||
- roles
|
||||
{{- range .Components }}
|
||||
- {{.}}.yaml
|
||||
{{- end }}
|
||||
|
||||
patches:
|
||||
- path: node-selector.yaml
|
||||
target:
|
||||
kind: Deployment
|
||||
|
||||
patchesJson6902:
|
||||
{{- range $i, $component := .Components }}
|
||||
{{- if eq $component "notification-controller" }}
|
||||
- target:
|
||||
group: apps
|
||||
version: v1
|
||||
kind: Deployment
|
||||
name: {{$component}}
|
||||
patch: |-
|
||||
- op: replace
|
||||
path: /spec/template/spec/containers/0/args/0
|
||||
value: --watch-all-namespaces={{$watchAllNamespaces}}
|
||||
- op: replace
|
||||
path: /spec/template/spec/containers/0/args/1
|
||||
value: --log-level={{$logLevel}}
|
||||
{{- else }}
|
||||
- target:
|
||||
group: apps
|
||||
version: v1
|
||||
kind: Deployment
|
||||
name: {{$component}}
|
||||
patch: |-
|
||||
- op: replace
|
||||
path: /spec/template/spec/containers/0/args/0
|
||||
value: --events-addr={{$eventsAddr}}
|
||||
- op: replace
|
||||
path: /spec/template/spec/containers/0/args/1
|
||||
value: --watch-all-namespaces={{$watchAllNamespaces}}
|
||||
- op: replace
|
||||
path: /spec/template/spec/containers/0/args/2
|
||||
value: --log-level={{$logLevel}}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- if $registry }}
|
||||
images:
|
||||
{{- range $i, $component := .Components }}
|
||||
- name: fluxcd/{{$component}}
|
||||
{{- if eq $arch "amd64" }}
|
||||
newName: {{$registry}}/{{$component}}
|
||||
{{- else }}
|
||||
newName: {{$registry}}/{{$component}}-arm64
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
`
|
||||
|
||||
var kustomizationRolesTmpl = `---
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
resources:
|
||||
- rbac.yaml
|
||||
nameSuffix: -{{.Namespace}}
|
||||
`
|
||||
|
||||
var nodeSelectorTmpl = `---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: all
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
nodeSelector:
|
||||
kubernetes.io/arch: {{.Arch}}
|
||||
kubernetes.io/os: linux
|
||||
{{- if .ImagePullSecret }}
|
||||
imagePullSecrets:
|
||||
- name: {{.ImagePullSecret}}
|
||||
{{- end }}
|
||||
`
|
||||
|
||||
var labelsTmpl = `---
|
||||
apiVersion: builtin
|
||||
kind: LabelTransformer
|
||||
metadata:
|
||||
name: labels
|
||||
labels:
|
||||
app.kubernetes.io/instance: {{.Namespace}}
|
||||
app.kubernetes.io/version: "{{.Version}}"
|
||||
fieldSpecs:
|
||||
- path: metadata/labels
|
||||
create: true
|
||||
`
|
||||
|
||||
var namespaceTmpl = `---
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: {{.Namespace}}
|
||||
`
|
||||
|
||||
func execTemplate(obj interface{}, tmpl, filename string) error {
|
||||
t, err := template.New("tmpl").Parse(tmpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var data bytes.Buffer
|
||||
writer := bufio.NewWriter(&data)
|
||||
if err := t.Execute(writer, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.WriteString(file, data.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return file.Sync()
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return out.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user