mirror of https://github.com/kubernetes/kops.git
Remove dead code in OptionsLoader
This commit is contained in:
parent
abe4c12794
commit
4f2d3e4687
|
|
@ -21,7 +21,6 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"k8s.io/klog"
|
||||
|
||||
|
|
@ -31,7 +30,6 @@ import (
|
|||
"k8s.io/kops/pkg/assets"
|
||||
"k8s.io/kops/pkg/client/simple"
|
||||
"k8s.io/kops/pkg/dns"
|
||||
"k8s.io/kops/pkg/model"
|
||||
"k8s.io/kops/pkg/model/components"
|
||||
"k8s.io/kops/pkg/model/components/etcdmanager"
|
||||
nodeauthorizer "k8s.io/kops/pkg/model/components/node-authorizer"
|
||||
|
|
@ -252,26 +250,6 @@ func (c *populateClusterSpec) run(clientset simple.Clientset) error {
|
|||
cluster.Spec.DNSZone = dnsZone
|
||||
}
|
||||
|
||||
tags, err := buildCloudupTags(cluster)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tf := &TemplateFunctions{
|
||||
KopsModelContext: model.KopsModelContext{
|
||||
Cluster: cluster,
|
||||
},
|
||||
tags: tags,
|
||||
}
|
||||
|
||||
templateFunctions := make(template.FuncMap)
|
||||
|
||||
err = tf.AddTo(templateFunctions, secretStore)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cluster.Spec.KubernetesVersion == "" {
|
||||
return fmt.Errorf("KubernetesVersion is required")
|
||||
}
|
||||
|
|
@ -308,8 +286,7 @@ func (c *populateClusterSpec) run(clientset simple.Clientset) error {
|
|||
}
|
||||
|
||||
specBuilder := &SpecBuilder{
|
||||
OptionsLoader: loader.NewOptionsLoader(templateFunctions, codeModels),
|
||||
Tags: tags,
|
||||
OptionsLoader: loader.NewOptionsLoader(codeModels),
|
||||
}
|
||||
|
||||
completed, err := specBuilder.BuildCompleteSpec(&cluster.Spec)
|
||||
|
|
@ -324,7 +301,6 @@ func (c *populateClusterSpec) run(clientset simple.Clientset) error {
|
|||
fullCluster := &kopsapi.Cluster{}
|
||||
*fullCluster = *cluster
|
||||
fullCluster.Spec = *completed
|
||||
tf.Cluster = fullCluster
|
||||
|
||||
if errs := validation.ValidateCluster(fullCluster, true); len(errs) != 0 {
|
||||
return fmt.Errorf("Completed cluster failed validation: %v", errs.ToAggregate())
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||
package cloudup
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/klog"
|
||||
kopsapi "k8s.io/kops/pkg/apis/kops"
|
||||
"k8s.io/kops/upup/pkg/fi"
|
||||
|
|
@ -27,8 +26,6 @@ import (
|
|||
|
||||
type SpecBuilder struct {
|
||||
OptionsLoader *loader.OptionsLoader
|
||||
|
||||
Tags sets.String
|
||||
}
|
||||
|
||||
func (l *SpecBuilder) BuildCompleteSpec(clusterSpec *kopsapi.ClusterSpec) (*kopsapi.ClusterSpec, error) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ go_library(
|
|||
importpath = "k8s.io/kops/upup/pkg/fi/loader",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//upup/pkg/fi/utils:go_default_library",
|
||||
"//util/pkg/reflectutils:go_default_library",
|
||||
"//util/pkg/vfs:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
|
|
|
|||
|
|
@ -17,33 +17,16 @@ limitations under the License.
|
|||
package loader
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"k8s.io/klog"
|
||||
"k8s.io/kops/upup/pkg/fi/utils"
|
||||
"k8s.io/kops/util/pkg/reflectutils"
|
||||
)
|
||||
|
||||
const maxIterations = 10
|
||||
|
||||
type OptionsTemplate struct {
|
||||
Name string
|
||||
Tags []string
|
||||
Template *template.Template
|
||||
}
|
||||
|
||||
type OptionsLoader struct {
|
||||
templates OptionsTemplateList
|
||||
|
||||
TemplateFunctions template.FuncMap
|
||||
|
||||
Builders []OptionsBuilder
|
||||
}
|
||||
|
||||
|
|
@ -51,47 +34,14 @@ type OptionsBuilder interface {
|
|||
BuildOptions(options interface{}) error
|
||||
}
|
||||
|
||||
type OptionsTemplateList []*OptionsTemplate
|
||||
|
||||
func (a OptionsTemplateList) Len() int {
|
||||
return len(a)
|
||||
}
|
||||
func (a OptionsTemplateList) Swap(i, j int) {
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
func (a OptionsTemplateList) Less(i, j int) bool {
|
||||
l := a[i]
|
||||
r := a[j]
|
||||
|
||||
// First ordering criteria: Execute things with fewer tags first (more generic)
|
||||
if len(l.Tags) != len(r.Tags) {
|
||||
return len(l.Tags) < len(r.Tags)
|
||||
}
|
||||
|
||||
// TODO: lexicographic sort on tags, for full determinism?
|
||||
|
||||
// Final ordering criteria: order by name
|
||||
return l.Name < r.Name
|
||||
}
|
||||
|
||||
func NewOptionsLoader(templateFunctions template.FuncMap, builders []OptionsBuilder) *OptionsLoader {
|
||||
func NewOptionsLoader(builders []OptionsBuilder) *OptionsLoader {
|
||||
l := &OptionsLoader{}
|
||||
l.TemplateFunctions = make(template.FuncMap)
|
||||
for k, v := range templateFunctions {
|
||||
l.TemplateFunctions[k] = v
|
||||
}
|
||||
l.Builders = builders
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *OptionsLoader) AddTemplate(t *OptionsTemplate) {
|
||||
l.templates = append(l.templates, t)
|
||||
}
|
||||
|
||||
// iterate performs a single iteration of all the templates, executing each template in order
|
||||
func (l *OptionsLoader) iterate(userConfig interface{}, current interface{}) (interface{}, error) {
|
||||
sort.Sort(l.templates)
|
||||
|
||||
t := reflect.TypeOf(current).Elem()
|
||||
|
||||
next := reflect.New(t).Interface()
|
||||
|
|
@ -99,33 +49,6 @@ func (l *OptionsLoader) iterate(userConfig interface{}, current interface{}) (in
|
|||
// Copy the current state before applying rules; they act as defaults
|
||||
reflectutils.JSONMergeStruct(next, current)
|
||||
|
||||
for _, t := range l.templates {
|
||||
klog.V(2).Infof("executing template %s (tags=%s)", t.Name, t.Tags)
|
||||
|
||||
var buffer bytes.Buffer
|
||||
err := t.Template.ExecuteTemplate(&buffer, t.Name, current)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error executing template %q: %v", t.Name, err)
|
||||
}
|
||||
|
||||
yamlBytes := buffer.Bytes()
|
||||
|
||||
jsonBytes, err := utils.YAMLToJSON(yamlBytes)
|
||||
if err != nil {
|
||||
// TODO: It would be nice if yaml returned us the line number here
|
||||
klog.Infof("error parsing yaml. yaml follows:")
|
||||
for i, line := range strings.Split(string(yamlBytes), "\n") {
|
||||
fmt.Fprintf(os.Stderr, "%3d: %s\n", i, line)
|
||||
}
|
||||
return nil, fmt.Errorf("error parsing yaml %q: %v", t.Name, err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonBytes, next)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing yaml (converted to JSON) %q: %v", t.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, t := range l.Builders {
|
||||
klog.V(2).Infof("executing builder %T", t)
|
||||
|
||||
|
|
@ -164,29 +87,3 @@ func (l *OptionsLoader) Build(userConfig interface{}) (interface{}, error) {
|
|||
options = nextOptions
|
||||
}
|
||||
}
|
||||
|
||||
// HandleOptions is the file handler for options files
|
||||
// It builds a template with the file, and adds it to the list of options templates
|
||||
func (l *OptionsLoader) HandleOptions(i *TreeWalkItem) error {
|
||||
contents, err := i.ReadString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.New(i.RelativePath)
|
||||
t.Funcs(l.TemplateFunctions)
|
||||
|
||||
_, err = t.Parse(contents)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing options template %q: %v", i.Path, err)
|
||||
}
|
||||
|
||||
t.Option("missingkey=zero")
|
||||
|
||||
l.AddTemplate(&OptionsTemplate{
|
||||
Name: i.RelativePath,
|
||||
Tags: i.Tags,
|
||||
Template: t,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue