airshipctl/pkg/document/plugin/templater/templater.go
Matthew Fuller 52c5aedc63 Generate additional CRDs from API types
'make manifests' attempts to generate all airshipctl API types,
but several types throw errors and the resulting CRDs can't be
properly generated. This change:

- updates the controller-gen version to pull in a bug fix for the
  error 'map values must be a named type, not *ast.StarExpr'
- modifies the Templater type's Values member to be of type JSON
  since controller-gen does not support map[string]interface{},
  and unmarshals it separately in the templater itself
- adds 'image_repositories' to the VersionsCatalogueSpec struct
  to ensure it doesn't get deleted from the schema when manifests
  get regenerated
- checks in generated copies of all CRDs from airshipctl's API
  types, not just catalogues, to be used for validation

Closes: #574
Change-Id: I89a12cfd307a08da9aaec9773eac01169ea43ace
2021-06-25 19:10:25 +00:00

102 lines
2.3 KiB
Go

/*
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
https://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 templater
import (
"bytes"
"encoding/json"
"fmt"
"text/template"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
sprig "github.com/Masterminds/sprig/v3"
extlib "opendev.org/airship/airshipctl/pkg/document/plugin/templater/extlib"
)
var _ kio.Filter = &plugin{}
type plugin struct {
*airshipv1.Templater
}
// New creates new instance of the plugin
func New(obj map[string]interface{}) (kio.Filter, error) {
cfg := &airshipv1.Templater{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, cfg)
if err != nil {
return nil, err
}
return &plugin{
Templater: cfg,
}, nil
}
func funcMapAppend(fma, fmb template.FuncMap) template.FuncMap {
for k, v := range fmb {
_, ok := fma[k]
if ok {
panic(fmt.Errorf("trying to redefine function %s that already exists", k))
}
fma[k] = v
}
return fma
}
func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
out := &bytes.Buffer{}
funcMap := template.FuncMap{}
funcMap = funcMapAppend(funcMap, sprig.TxtFuncMap())
funcMap = funcMapAppend(funcMap, extlib.GenericFuncMap())
tmpl, err := template.New("tmpl").Funcs(funcMap).Parse(t.Template)
if err != nil {
return nil, err
}
var values interface{}
if t.Values != nil {
if err = json.Unmarshal(t.Values.Raw, &values); err != nil {
return nil, err
}
}
if err = tmpl.Execute(out, values); err != nil {
return nil, err
}
p := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: out}},
Outputs: []kio.Writer{&kio.PackageBuffer{}},
}
err = p.Execute()
if err != nil {
return nil, err
}
res, ok := p.Outputs[0].(*kio.PackageBuffer)
if !ok {
return nil, fmt.Errorf("output conversion error")
}
return append(items, res.Nodes...), nil
}