component.go 4.97 KB
Newer Older
bryanl's avatar
bryanl committed
1
// Copyright 2018 The ksonnet authors
bryanl's avatar
bryanl committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package component

import (
	"path/filepath"
	"strings"

22
	"github.com/google/go-jsonnet/ast"
bryanl's avatar
bryanl committed
23
	"github.com/ksonnet/ksonnet/pkg/app"
24
	"github.com/ksonnet/ksonnet/pkg/schema"
bryanl's avatar
bryanl committed
25
	jsonnetutil "github.com/ksonnet/ksonnet/pkg/util/jsonnet"
bryanl's avatar
bryanl committed
26

bryanl's avatar
bryanl committed
27 28 29 30
	"github.com/pkg/errors"
	"github.com/spf13/afero"
)

bryanl's avatar
bryanl committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// ParamOptions is options for parameters.
type ParamOptions struct {
	Index int
}

// Summary summarizes items found in components.
type Summary struct {
	ComponentName string
	Type          string
	APIVersion    string
	Kind          string
	Name          string
}

// GVK converts a summary to a group - version - kind.
46 47
func (s *Summary) typeSpec() (*schema.TypeSpec, error) {
	return schema.NewTypeSpec(s.APIVersion, s.Kind)
bryanl's avatar
bryanl committed
48 49 50 51 52
}

// Component is a ksonnet Component interface.
type Component interface {
	// DeleteParam deletes a component parameter.
53
	DeleteParam(path []string) error
54 55
	// Name is the component name.
	Name(wantsNamedSpaced bool) string
bryanl's avatar
bryanl committed
56 57
	// Params returns a list of all parameters for a component. If envName is a
	// blank string, it will report the local parameters.
58
	Params(envName string) ([]ModuleParameter, error)
59
	// SetParams sets a component paramaters.
60
	SetParam(path []string, value interface{}) error
bryanl's avatar
bryanl committed
61
	// Summarize returns a summary of the component.
62
	Summarize() (Summary, error)
63 64
	// ToNode converts a component to a Jsonnet node.
	ToNode(envName string) (string, ast.Node, error)
65 66
	// Type returns the type of component.
	Type() string
bryanl's avatar
bryanl committed
67 68
}

bryanl's avatar
bryanl committed
69 70 71 72 73 74 75
const (
	// componentsDir is the name of the directory which houses components.
	componentsRoot = "components"
	// paramsFile is the params file for a component namespace.
	paramsFile = "params.libsonnet"
)

76 77
// LocateComponent locates a component given a module and a name.
func LocateComponent(ksApp app.App, module, name string) (Component, error) {
bryanl's avatar
bryanl committed
78
	path := make([]string, 0)
79 80
	if module != "" && module != "/" {
		path = append(path, module)
bryanl's avatar
bryanl committed
81 82 83 84 85 86
	}

	path = append(path, name)
	return ExtractComponent(ksApp, strings.Join(path, "/"))
}

bryanl's avatar
bryanl committed
87
// Path returns returns the file system path for a component.
bryanl's avatar
bryanl committed
88
func Path(a app.App, name string) (string, error) {
89
	ns, localName := ExtractModuleComponent(a, name)
bryanl's avatar
bryanl committed
90

bryanl's avatar
bryanl committed
91
	fis, err := afero.ReadDir(a.Fs(), ns.Dir())
bryanl's avatar
bryanl committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	if err != nil {
		return "", err
	}

	var fileName string
	files := make(map[string]bool)

	for _, fi := range fis {
		if fi.IsDir() {
			continue
		}

		base := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name()))
		if _, ok := files[base]; ok {
			return "", errors.Errorf("Found multiple component files with component name %q", name)
		}
		files[base] = true

		if base == localName {
			fileName = fi.Name()
		}
	}

	if fileName == "" {
		return "", errors.Errorf("No component name %q found", name)
	}

	return filepath.Join(ns.Dir(), fileName), nil
}

bryanl's avatar
bryanl committed
122 123
// ExtractComponent extracts a component from a path.
func ExtractComponent(a app.App, path string) (Component, error) {
124
	ns, componentName := ExtractModuleComponent(a, path)
bryanl's avatar
bryanl committed
125
	members, err := ns.Components()
bryanl's avatar
bryanl committed
126 127 128 129
	if err != nil {
		return nil, err
	}

bryanl's avatar
bryanl committed
130 131 132
	for _, member := range members {
		if componentName == member.Name(false) {
			return member, nil
bryanl's avatar
bryanl committed
133 134 135
		}
	}

bryanl's avatar
bryanl committed
136
	return nil, errors.Errorf("unable to find component %q", componentName)
bryanl's avatar
bryanl committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

func isComponentDir(fs afero.Fs, path string) (bool, error) {
	files, err := afero.ReadDir(fs, path)
	if err != nil {
		return false, errors.Wrapf(err, "read files in %s", path)
	}

	for _, file := range files {
		if file.Name() == paramsFile {
			return true, nil
		}
	}

	return false, nil
}

// MakePaths creates a slice of component paths
bryanl's avatar
bryanl committed
155 156
func MakePaths(a app.App, env string) ([]string, error) {
	cpl, err := newComponentPathLocator(a, env)
bryanl's avatar
bryanl committed
157 158 159 160
	if err != nil {
		return nil, errors.Wrap(err, "create component path locator")
	}

bryanl's avatar
bryanl committed
161
	return cpl.Locate()
bryanl's avatar
bryanl committed
162
}
bryanl's avatar
bryanl committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

func envParams(a app.App, moduleName, envName string) (string, error) {
	libPath, err := a.LibPath(envName)
	if err != nil {
		return "", err
	}

	ns, err := GetModule(a, moduleName)
	if err != nil {
		return "", err
	}

	paramsStr, err := ns.ResolvedParams()
	if err != nil {
		return "", err
	}

	data, err := a.EnvironmentParams(envName)
	if err != nil {
		return "", err
	}

	env, err := a.Environment(envName)
	if err != nil {
		return "", err
	}

	envParams := upgradeParams(envName, data)

	vm := jsonnetutil.NewVM()
	vm.AddJPath(
		libPath,
		env.MakePath(a.Root()),
		filepath.Join(a.Root(), "vendor"),
	)
	vm.ExtCode("__ksonnet/params", paramsStr)
	return vm.EvaluateSnippet("snippet", string(envParams))
}