forked from ksonnet/ksonnet
/
interface.go
108 lines (94 loc) · 3.96 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2017 The kubecfg authors
//
//
// 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 metadata
import (
"os"
"github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/app"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/metadata/parts"
"github.com/ksonnet/ksonnet/metadata/registry"
"github.com/ksonnet/ksonnet/prototype"
"github.com/spf13/afero"
)
var appFS afero.Fs
var defaultFolderPermissions = os.FileMode(0755)
var defaultFilePermissions = os.FileMode(0644)
// Manager abstracts over a ksonnet application's metadata, allowing users to do
// things like: create and delete environments; search for prototypes; vendor
// libraries; and other non-core-application tasks.
type Manager interface {
// App returns the object for the application.
App() (app.App, error)
Root() string
LibPaths() (envPath, vendorPath string)
EnvPaths(env string) (libPath, mainPath, paramsPath string, err error)
// Components API.
ComponentPaths() ([]string, error)
GetAllComponents() ([]string, error)
CreateComponent(name string, text string, params param.Params, templateType prototype.TemplateType) error
DeleteComponent(name string) error
// Params API.
SetComponentParams(component string, params param.Params) error
GetComponentParams(name string) (param.Params, error)
GetAllComponentParams(cwd string) (map[string]param.Params, error)
// GetEnvironmentParams will take the name of an environment and return a
// mapping of parameters of the form:
// componentName => {param key => param val}
// i.e.: "nginx" => {"replicas" => 1, "name": "nginx"}
GetEnvironmentParams(name, nsName string) (map[string]param.Params, error)
SetEnvironmentParams(env, component string, params param.Params) error
// Environment API.
CreateEnvironment(name, uri, namespace, spec string) error
DeleteEnvironment(name string) error
GetEnvironments() (map[string]env.Env, error)
GetEnvironment(name string) (*env.Env, error)
SetEnvironment(name, desiredName string) error
GetDestination(envName string) (env.Destination, error)
// Dependency/registry API.
AddRegistry(name, protocol, uri, version string) (*registry.Spec, error)
GetRegistry(name string) (*registry.Spec, string, error)
GetPackage(registryName, libID string) (*parts.Spec, error)
CacheDependency(registryName, libID, libName, libVersion string) (*parts.Spec, error)
GetDependency(libName string) (*parts.Spec, error)
GetAllPrototypes() (prototype.SpecificationSchemas, error)
}
// Find will recursively search the current directory and its parents for a
// `.ksonnet` folder, which marks the application root. Returns error if there
// is no application root.
func Find(path string) (Manager, error) {
return findManager(path, afero.NewOsFs())
}
// Init will generate the directory tree for a ksonnet project.
func Init(name, rootPath string, k8sSpecFlag, serverURI, namespace *string) (Manager, error) {
// Generate `incubator` registry. We do this before before creating
// directory tree, in case the network call fails.
const (
defaultIncubatorRegName = "incubator"
defaultIncubatorURI = "github.com/ksonnet/parts/tree/master/" + defaultIncubatorRegName
)
gh, err := makeGitHubRegistryManager(&app.RegistryRefSpec{
Name: "incubator",
Protocol: "github",
URI: defaultIncubatorURI,
})
if err != nil {
return nil, err
}
return initManager(name, rootPath, k8sSpecFlag, serverURI, namespace, gh, appFS)
}
func init() {
appFS = afero.NewOsFs()
}