Skip to content

Commit

Permalink
Remove unused loading code from Loader
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Jun 20, 2020
1 parent 75a3fe9 commit 35347da
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 235 deletions.
1 change: 0 additions & 1 deletion cmd/nodeup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ go_library(
deps = [
"//:go_default_library",
"//nodeup/pkg/bootstrap:go_default_library",
"//upup/models:go_default_library",
"//upup/pkg/fi/nodeup:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
Expand Down
2 changes: 0 additions & 2 deletions cmd/nodeup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/klog"
"k8s.io/kops"
"k8s.io/kops/nodeup/pkg/bootstrap"
"k8s.io/kops/upup/models"
"k8s.io/kops/upup/pkg/fi/nodeup"
)

Expand Down Expand Up @@ -117,7 +116,6 @@ func main() {
Target: target,
CacheDir: flagCacheDir,
FSRoot: flagRootFS,
ModelDir: models.NewAssetPath("nodeup"),
}
err = cmd.Run(os.Stdout)
if err == nil {
Expand Down
1 change: 0 additions & 1 deletion upup/pkg/fi/nodeup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ go_library(
"//pkg/apis/nodeup:go_default_library",
"//pkg/assets:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/loader:go_default_library",
"//upup/pkg/fi/nodeup/cloudinit:go_default_library",
"//upup/pkg/fi/nodeup/local:go_default_library",
"//upup/pkg/fi/nodeup/nodetasks:go_default_library",
Expand Down
5 changes: 2 additions & 3 deletions upup/pkg/fi/nodeup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type NodeUpCommand struct {
CacheDir string
ConfigLocation string
FSRoot string
ModelDir vfs.Path
Target string
cluster *api.Cluster
config *nodeup.Config
Expand Down Expand Up @@ -237,7 +236,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
return err
}

loader := NewLoader(c.config, c.cluster, assetStore, nodeTags)
loader := &Loader{}
loader.Builders = append(loader.Builders, &model.NTPBuilder{NodeupModelContext: modelContext})
loader.Builders = append(loader.Builders, &model.MiscUtilsBuilder{NodeupModelContext: modelContext})
loader.Builders = append(loader.Builders, &model.DirectoryBuilder{NodeupModelContext: modelContext})
Expand Down Expand Up @@ -273,7 +272,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
loader.Builders = append(loader.Builders, &networking.KuberouterBuilder{NodeupModelContext: modelContext})
loader.Builders = append(loader.Builders, &networking.LyftVPCBuilder{NodeupModelContext: modelContext})

taskMap, err := loader.Build(c.ModelDir)
taskMap, err := loader.Build()
if err != nil {
return fmt.Errorf("error building loader: %v", err)
}
Expand Down
188 changes: 9 additions & 179 deletions upup/pkg/fi/nodeup/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,210 +17,40 @@ limitations under the License.
package nodeup

import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"

"k8s.io/apimachinery/pkg/util/sets"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/nodeup"
"k8s.io/klog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/loader"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
"k8s.io/kops/util/pkg/vfs"

"k8s.io/klog"
)

type Loader struct {
Builders []fi.ModelBuilder

config *nodeup.Config
cluster *api.Cluster

assets *fi.AssetStore
tasks map[string]fi.Task

tags sets.String
TemplateFunctions template.FuncMap
}

func NewLoader(config *nodeup.Config, cluster *api.Cluster, assets *fi.AssetStore, tags sets.String) *Loader {
l := &Loader{}
l.assets = assets
l.tasks = make(map[string]fi.Task)
l.config = config
l.cluster = cluster
l.TemplateFunctions = make(template.FuncMap)
l.tags = tags

return l
}

func (l *Loader) executeTemplate(key string, d string) (string, error) {
t := template.New(key)

funcMap := make(template.FuncMap)
for k, fn := range l.TemplateFunctions {
funcMap[k] = fn
}
t.Funcs(funcMap)

context := l.cluster.Spec

_, err := t.Parse(d)
if err != nil {
return "", fmt.Errorf("error parsing template %q: %v", key, err)
}

t.Option("missingkey=zero")

var buffer bytes.Buffer
err = t.ExecuteTemplate(&buffer, key, context)
if err != nil {
return "", fmt.Errorf("error executing template %q: %v", key, err)
}

return buffer.String(), nil
}

func ignoreHandler(i *loader.TreeWalkItem) error {
return nil
}

// Build is responsible for running the build tasks for nodeup
func (l *Loader) Build(baseDir vfs.Path) (map[string]fi.Task, error) {
// First pass: load options
tw := &loader.TreeWalker{
DefaultHandler: ignoreHandler,
Contexts: map[string]loader.Handler{
"files": ignoreHandler,
},
Tags: l.tags,
}

err := tw.Walk(baseDir)
if err != nil {
return nil, err
}

// Second pass: load everything else
tw = &loader.TreeWalker{
DefaultHandler: l.handleFile,
Contexts: map[string]loader.Handler{
"files": l.handleFile,
},
Tags: l.tags,
}

err = tw.Walk(baseDir)
if err != nil {
return nil, err
}

func (l *Loader) Build() (map[string]fi.Task, error) {
tasks := make(map[string]fi.Task)
for _, builder := range l.Builders {
context := &fi.ModelBuilderContext{
Tasks: l.tasks,
Tasks: tasks,
}
err := builder.Build(context)
if err != nil {
return nil, err
}
l.tasks = context.Tasks
tasks = context.Tasks
}

// If there is a package task, we need an update packages task
for _, t := range l.tasks {
for _, t := range tasks {
if _, ok := t.(*nodetasks.Package); ok {
klog.Infof("Package task found; adding UpdatePackages task")
l.tasks["UpdatePackages"] = nodetasks.NewUpdatePackages()
tasks["UpdatePackages"] = nodetasks.NewUpdatePackages()
break
}
}
if l.tasks["UpdatePackages"] == nil {
if tasks["UpdatePackages"] == nil {
klog.Infof("No package task found; won't update packages")
}

return l.tasks, nil
}

type TaskBuilder func(name string, contents string, meta string) (fi.Task, error)

func (l *Loader) handleFile(i *loader.TreeWalkItem) error {
var task *nodetasks.File
defaultFileType := nodetasks.FileType_File

if strings.HasSuffix(i.RelativePath, ".template") {
contents, err := i.ReadString()
if err != nil {
return err
}

// TODO: Use template resource here to defer execution?
destPath := "/" + strings.TrimSuffix(i.RelativePath, ".template")
name := strings.TrimSuffix(i.Name, ".template")
expanded, err := l.executeTemplate(name, contents)
if err != nil {
return fmt.Errorf("error executing template %q: %v", i.RelativePath, err)
}

task, err = nodetasks.NewFileTask(name, fi.NewStringResource(expanded), destPath, i.Meta)
if err != nil {
return fmt.Errorf("error building task %q: %v", i.RelativePath, err)
}
} else if strings.HasSuffix(i.RelativePath, ".asset") {
contents, err := i.ReadBytes()
if err != nil {
return err
}

destPath := "/" + strings.TrimSuffix(i.RelativePath, ".asset")
name := strings.TrimSuffix(i.Name, ".asset")

def := &nodetasks.AssetDefinition{}
err = json.Unmarshal(contents, def)
if err != nil {
return fmt.Errorf("error parsing json for asset %q: %v", name, err)
}

asset, err := l.assets.Find(name, def.AssetPath)
if err != nil {
return fmt.Errorf("error trying to locate asset %q: %v", name, err)
}
if asset == nil {
return fmt.Errorf("unable to locate asset %q", name)
}

task, err = nodetasks.NewFileTask(i.Name, asset, destPath, i.Meta)
if err != nil {
return fmt.Errorf("error building task %q: %v", i.RelativePath, err)
}
} else {
var err error
var contents fi.Resource
if vfs.IsDirectory(i.Path) {
defaultFileType = nodetasks.FileType_Directory
} else {
contents = fi.NewVFSResource(i.Path)
}
task, err = nodetasks.NewFileTask(i.Name, contents, "/"+i.RelativePath, i.Meta)
if err != nil {
return fmt.Errorf("error building task %q: %v", i.RelativePath, err)
}
}

if task.Type == "" {
task.Type = defaultFileType
}

klog.V(2).Infof("path %q -> task %v", i.Path, task)

if task != nil {
key := "file/" + i.RelativePath
l.tasks[key] = task
}

return nil
return tasks, nil
}
1 change: 0 additions & 1 deletion upup/pkg/fi/nodeup/nodetasks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go_library(
name = "go_default_library",
srcs = [
"archive.go",
"asset.go",
"bindmount.go",
"chattr.go",
"createsdir.go",
Expand Down
22 changes: 0 additions & 22 deletions upup/pkg/fi/nodeup/nodetasks/asset.go

This file was deleted.

27 changes: 1 addition & 26 deletions upup/pkg/fi/nodeup/nodetasks/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ import (
"strings"
"syscall"

"k8s.io/klog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/cloudinit"
"k8s.io/kops/upup/pkg/fi/nodeup/local"
"k8s.io/kops/upup/pkg/fi/utils"

"k8s.io/klog"
)

const (
Expand Down Expand Up @@ -59,29 +57,6 @@ var _ fi.Task = &File{}
var _ fi.HasDependencies = &File{}
var _ fi.HasName = &File{}

func NewFileTask(name string, src fi.Resource, destPath string, meta string) (*File, error) {
f := &File{
//Name: name,
Contents: src,
Path: destPath,
}

if meta != "" {
err := utils.YamlUnmarshal([]byte(meta), f)
if err != nil {
return nil, fmt.Errorf("error parsing meta for file %q: %v", name, err)
}
}

if f.Symlink != nil && f.Type == "" {
f.Type = FileType_Symlink
}

return f, nil
}

var _ fi.HasDependencies = &File{}

// GetDependencies implements HasDependencies::GetDependencies
func (e *File) GetDependencies(tasks map[string]fi.Task) []fi.Task {
var deps []fi.Task
Expand Down

0 comments on commit 35347da

Please sign in to comment.