Merge pull request #9649 from johngmyers/loader-cleanup

Cleanup unused loader features
This commit is contained in:
Kubernetes Prow Robot 2020-07-29 20:06:31 -07:00 committed by GitHub
commit 24ff70e437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 107 deletions

View File

@ -467,8 +467,6 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
tags: clusterTags,
}
l.Tags = clusterTags
{
templates, err := templates.LoadTemplates(cluster, models.NewAssetPath("cloudup/resources"))
if err != nil {

View File

@ -27,7 +27,8 @@ import (
"k8s.io/klog"
)
// LaunchTemplate defines the specificate for a template
// LaunchTemplate defines the specification for a launch template.
// +kops:fitask
type LaunchTemplate struct {
// Name is the name of the configuration
Name *string

View File

@ -1,5 +1,7 @@
// +build !ignore_autogenerated
/*
Copyright 2019 The Kubernetes Authors.
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -14,16 +16,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by fitask. DO NOT EDIT.
package awstasks
import (
"k8s.io/kops/upup/pkg/fi"
)
var (
_ fi.HasLifecycle = &LaunchTemplate{}
_ fi.HasName = &LaunchTemplate{}
)
// LaunchTemplate
var _ fi.HasLifecycle = &LaunchTemplate{}
// GetLifecycle returns the Lifecycle of the object, implementing fi.HasLifecycle
func (o *LaunchTemplate) GetLifecycle() *fi.Lifecycle {
@ -35,6 +38,8 @@ func (o *LaunchTemplate) SetLifecycle(lifecycle fi.Lifecycle) {
o.Lifecycle = &lifecycle
}
var _ fi.HasName = &LaunchTemplate{}
// GetName returns the Name of the object, implementing fi.HasName
func (o *LaunchTemplate) GetName() *string {
return o.Name

View File

@ -38,7 +38,6 @@ import (
type Loader struct {
Cluster *kopsapi.Cluster
Tags sets.String
TemplateFunctions template.FuncMap
Resources map[string]fi.Resource
@ -82,6 +81,8 @@ func (l *Loader) Init() {
}
func (l *Loader) executeTemplate(key string, d string, args []string) (string, error) {
// TODO remove after proving it's dead code
klog.Fatalf("need to execute template %q", key)
t := template.New(key)
funcMap := make(template.FuncMap)
@ -114,23 +115,12 @@ func (l *Loader) executeTemplate(key string, d string, args []string) (string, e
return buffer.String(), nil
}
func ignoreHandler(i *loader.TreeWalkItem) error {
// TODO remove after proving it's dead code
klog.Fatalf("ignoreHandler called on %s", i.Path)
return fmt.Errorf("ignoreHandler not implemented")
}
func (l *Loader) BuildTasks(modelStore vfs.Path, assetBuilder *assets.AssetBuilder, lifecycle *fi.Lifecycle, lifecycleOverrides map[string]fi.Lifecycle) (map[string]fi.Task, error) {
// Second pass: load everything else
tw := &loader.TreeWalker{
DefaultHandler: l.objectHandler,
Contexts: map[string]loader.Handler{
"resources": l.resourceHandler,
},
Extensions: map[string]loader.Handler{
".options": ignoreHandler,
},
Tags: l.Tags,
}
modelDir := modelStore.Join("cloudup")
@ -226,6 +216,7 @@ func (l *Loader) addAssetFileCopyTasks(assets []*assets.FileAsset, lifecycle *fi
}
func (l *Loader) processDeferrals() error {
// TODO remove after proving it's not used
for taskKey, task := range l.tasks {
taskValue := reflect.ValueOf(task)
@ -250,6 +241,7 @@ func (l *Loader) processDeferrals() error {
typeNameForTask := fi.TypeNameForTask(intf)
primary := l.tasks[typeNameForTask+"/"+*name]
if primary == nil {
klog.Fatalf("task %q needed deferral resolution", typeNameForTask+"/"+*name)
primary = l.tasks[*name]
}
if primary == nil {
@ -271,6 +263,7 @@ func (l *Loader) processDeferrals() error {
return reflectutils.SkipReflection
} else if rh, ok := intf.(*fi.ResourceHolder); ok {
if rh.Resource == nil {
klog.Fatalf("resource %s needed deferral resolution", rh.Name)
//Resources can contain template 'arguments', separated by spaces
// <resourcename> <arg1> <arg2>
tokens := strings.Split(rh.Name, " ")
@ -336,12 +329,6 @@ func (l *Loader) resourceHandler(i *loader.TreeWalkItem) error {
return nil
}
func (l *Loader) objectHandler(i *loader.TreeWalkItem) error {
// TODO remove after proving it's dead code
klog.Fatalf("objectHandler called on %s", i.Path)
return fmt.Errorf("objectHandler not implemented")
}
func (l *Loader) populateResource(rh *fi.ResourceHolder, resource fi.Resource, args []string) error {
if resource == nil {
return nil

View File

@ -11,7 +11,6 @@ go_library(
deps = [
"//util/pkg/reflectutils:go_default_library",
"//util/pkg/vfs:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
)

View File

@ -18,20 +18,14 @@ package loader
import (
"fmt"
"os"
"path"
"strings"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog"
"k8s.io/kops/util/pkg/vfs"
)
type TreeWalker struct {
Contexts map[string]Handler
Extensions map[string]Handler
DefaultHandler Handler
Tags sets.String
Contexts map[string]Handler
}
type TreeWalkItem struct {
@ -39,16 +33,6 @@ type TreeWalkItem struct {
Name string
Path vfs.Path
RelativePath string
Meta string
Tags []string
}
func (i *TreeWalkItem) ReadString() (string, error) {
b, err := i.ReadBytes()
if err != nil {
return "", err
}
return string(b), nil
}
func (i *TreeWalkItem) ReadBytes() ([]byte, error) {
@ -61,16 +45,11 @@ func (i *TreeWalkItem) ReadBytes() ([]byte, error) {
type Handler func(item *TreeWalkItem) error
func IsTag(name string) bool {
return len(name) != 0 && name[0] == '_'
}
func (t *TreeWalker) Walk(basedir vfs.Path) error {
i := &TreeWalkItem{
Context: "",
Path: basedir,
RelativePath: "",
Tags: nil,
}
return t.walkDirectory(i)
@ -92,40 +71,12 @@ func (t *TreeWalker) walkDirectory(parent *TreeWalkItem) error {
Path: f,
RelativePath: path.Join(parent.RelativePath, fileName),
Name: fileName,
Tags: parent.Tags,
}
klog.V(4).Infof("visit %q", f)
hasMeta := false
{
metaPath := parent.Path.Join(fileName + ".meta")
metaBytes, err := metaPath.ReadFile()
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("error reading file %q: %v", metaPath, err)
}
metaBytes = nil
}
if metaBytes != nil {
hasMeta = true
i.Meta = string(metaBytes)
}
}
if _, err := f.ReadDir(); err == nil {
if IsTag(fileName) {
// Only descend into the tag directory if we have the tag
_, found := t.Tags[fileName]
if !found {
klog.V(2).Infof("Skipping directory %q as tag %q not present", f, fileName)
continue
} else {
i.Tags = append(i.Tags, fileName)
klog.V(2).Infof("Descending into directory, as tag is present: %q", f)
err = t.walkDirectory(i)
}
} else if _, found := t.Contexts[fileName]; found {
if _, found := t.Contexts[fileName]; found {
// Entering a new context (mode of operation)
if parent.Context != "" {
return fmt.Errorf("found context %q inside context %q at %q", fileName, parent.Context, f)
@ -141,38 +92,10 @@ func (t *TreeWalker) walkDirectory(parent *TreeWalkItem) error {
return err
}
// So that we can manage directories, we do not ignore directories which have a .meta file
if hasMeta {
klog.V(4).Infof("Found .meta file for directory %q; will process", f)
} else {
continue
}
}
if strings.HasSuffix(fileName, ".meta") {
// We'll read it when we see the actual file
// But check the actual file is there
primaryPath := strings.TrimSuffix(f.Base(), ".meta")
if _, err := parent.Path.Join(primaryPath).ReadFile(); os.IsNotExist(err) {
return fmt.Errorf("found .meta file without corresponding file: %q", f)
}
continue
}
var handler Handler
if i.Context != "" {
handler = t.Contexts[i.Context]
} else {
// TODO: Just remove extensions.... we barely use them!
// (or remove default handler and replace with lots of small files?)
extension := path.Ext(fileName)
handler = t.Extensions[extension]
if handler == nil {
handler = t.DefaultHandler
}
}
handler := t.Contexts[i.Context]
err = handler(i)
if err != nil {
return fmt.Errorf("error handling file %q: %v", f, err)