mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 09:51:23 +00:00
add varialber reference support
This commit is contained in:
@@ -19,8 +19,11 @@ package app
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/golang/glog"
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||
interror "github.com/kubernetes-sigs/kustomize/pkg/internal/error"
|
||||
@@ -41,6 +44,8 @@ type Application interface {
|
||||
// 1) untransformed resources from current kustomization file
|
||||
// 2) transformed resources from sub packages
|
||||
RawResources() (resmap.ResMap, error)
|
||||
// Vars returns all the variables defined by the app
|
||||
Vars() ([]types.Var, error)
|
||||
}
|
||||
|
||||
var _ Application = &applicationImpl{}
|
||||
@@ -74,7 +79,7 @@ func (a *applicationImpl) Resources() (resmap.ResMap, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, err := a.getHashAndReferenceTransformer()
|
||||
t, err := a.getHashAndReferenceTransformer(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -128,7 +133,6 @@ func (a *applicationImpl) SemiResources() (resmap.ResMap, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return allRes, nil
|
||||
}
|
||||
|
||||
@@ -140,7 +144,7 @@ func (a *applicationImpl) RawResources() (resmap.ResMap, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, err := a.getHashAndReferenceTransformer()
|
||||
t, err := a.getHashAndReferenceTransformer(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -194,6 +198,28 @@ func (a *applicationImpl) subAppResources() (resmap.ResMap, *interror.Kustomizat
|
||||
return allResources, errs
|
||||
}
|
||||
|
||||
func (a *applicationImpl) subApp() ([]Application, error) {
|
||||
var apps []Application
|
||||
errs := &interror.KustomizationErrors{}
|
||||
for _, basePath := range a.kustomization.Bases {
|
||||
subloader, err := a.loader.New(basePath)
|
||||
if err != nil {
|
||||
errs.Append(err)
|
||||
continue
|
||||
}
|
||||
subapp, err := New(subloader)
|
||||
if err != nil {
|
||||
errs.Append(err)
|
||||
continue
|
||||
}
|
||||
apps = append(apps, subapp)
|
||||
}
|
||||
if len(errs.Get()) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return apps, nil
|
||||
}
|
||||
|
||||
// getTransformer generates the following transformers:
|
||||
// 1) apply overlay
|
||||
// 2) name prefix
|
||||
@@ -234,7 +260,8 @@ func (a *applicationImpl) getTransformer(patches []*resource.Resource) (transfor
|
||||
// getHashAndReferenceTransformer generates the following transformers:
|
||||
// 1) name hash for configmap and secrests
|
||||
// 2) apply name reference
|
||||
func (a *applicationImpl) getHashAndReferenceTransformer() (transformers.Transformer, error) {
|
||||
// 3) apply reference variables
|
||||
func (a *applicationImpl) getHashAndReferenceTransformer(allRes resmap.ResMap) (transformers.Transformer, error) {
|
||||
ts := []transformers.Transformer{}
|
||||
nht := transformers.NewNameHashTransformer()
|
||||
ts = append(ts, nht)
|
||||
@@ -244,9 +271,30 @@ func (a *applicationImpl) getHashAndReferenceTransformer() (transformers.Transfo
|
||||
return nil, err
|
||||
}
|
||||
ts = append(ts, nrt)
|
||||
t, err := a.getVariableReferenceTransformer(allRes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ts = append(ts, t)
|
||||
|
||||
return transformers.NewMultiTransformer(ts), nil
|
||||
}
|
||||
|
||||
func (a *applicationImpl) getVariableReferenceTransformer(allRes resmap.ResMap) (transformers.Transformer, error) {
|
||||
refvars, err := a.resolveRefVars(allRes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
glog.Infof("found all the refvars: %+v", refvars)
|
||||
|
||||
varExpander, err := transformers.NewRefVarTransformer(refvars)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return varExpander, nil
|
||||
}
|
||||
|
||||
func unmarshal(y []byte, o interface{}) error {
|
||||
j, err := yaml.YAMLToJSON(y)
|
||||
if err != nil {
|
||||
@@ -257,3 +305,51 @@ func unmarshal(y []byte, o interface{}) error {
|
||||
dec.DisallowUnknownFields()
|
||||
return dec.Decode(o)
|
||||
}
|
||||
|
||||
func (a *applicationImpl) resolveRefVars(resources resmap.ResMap) (map[string]string, error) {
|
||||
refvars := map[string]string{}
|
||||
vars, err := a.Vars()
|
||||
if err != nil {
|
||||
return refvars, err
|
||||
}
|
||||
|
||||
for _, refvar := range vars {
|
||||
refGVKN := gvkn(refvar)
|
||||
if r, found := resources[refGVKN]; found {
|
||||
s, err := getFieldAsString(r.Unstruct().UnstructuredContent(), strings.Split(refvar.FieldRef.FieldPath, "."))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to resolve referred var: %+v", refvar)
|
||||
}
|
||||
refvars[refvar.Name] = s
|
||||
} else {
|
||||
glog.Infof("couldn't resolve refvar: %v", refvar)
|
||||
}
|
||||
}
|
||||
return refvars, nil
|
||||
}
|
||||
|
||||
// Vars returns all the variables defined at the app and subapps of the app
|
||||
func (a *applicationImpl) Vars() ([]types.Var, error) {
|
||||
vars := []types.Var{}
|
||||
errs := &interror.KustomizationErrors{}
|
||||
|
||||
apps, err := a.subApp()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: computing vars and resources for subApps can be combined
|
||||
for _, subApp := range apps {
|
||||
subAppVars, err := subApp.Vars()
|
||||
if err != nil {
|
||||
errs.Append(err)
|
||||
continue
|
||||
}
|
||||
vars = append(vars, subAppVars...)
|
||||
}
|
||||
vars = append(vars, a.kustomization.Vars...)
|
||||
if len(errs.Get()) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return vars, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user