Skip to content

Commit

Permalink
Merge pull request #9649 from johngmyers/loader-cleanup
Browse files Browse the repository at this point in the history
Cleanup unused loader features
  • Loading branch information
k8s-ci-robot authored Jul 30, 2020
2 parents be78301 + c9813f9 commit 24ff70e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 107 deletions.
2 changes: 0 additions & 2 deletions upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion upup/pkg/fi/cloudup/awstasks/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions upup/pkg/fi/cloudup/awstasks/launchtemplate_fitask.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 5 additions & 18 deletions upup/pkg/fi/cloudup/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
type Loader struct {
Cluster *kopsapi.Cluster

Tags sets.String
TemplateFunctions template.FuncMap

Resources map[string]fi.Resource
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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, " ")
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion upup/pkg/fi/loader/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
83 changes: 3 additions & 80 deletions upup/pkg/fi/loader/tree_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,21 @@ 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 {
Context string
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) {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 24ff70e

Please sign in to comment.