Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #796 from bryanl/remove-component
Browse files Browse the repository at this point in the history
Remove component in module with
  • Loading branch information
bryanl committed Jul 30, 2018
2 parents 230769d + d617aed commit b6b33c5
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 18 deletions.
6 changes: 4 additions & 2 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Component interface {
// Params returns a list of all parameters for a component. If envName is a
// blank string, it will report the local parameters.
Params(envName string) ([]ModuleParameter, error)
// Remove removes the component
Remove() error
// SetParams sets a component paramaters.
SetParam(path []string, value interface{}) error
// Summarize returns a summary of the component.
Expand Down Expand Up @@ -87,7 +89,7 @@ func LocateComponent(ksApp app.App, module, name string) (Component, error) {

// Path returns returns the file system path for a component.
func Path(a app.App, name string) (string, error) {
ns, localName := ExtractModuleComponent(a, name)
ns, localName := extractModuleComponent(a, name)

fis, err := afero.ReadDir(a.Fs(), ns.Dir())
if err != nil {
Expand Down Expand Up @@ -122,7 +124,7 @@ func Path(a app.App, name string) (string, error) {

// ExtractComponent extracts a component from a path.
func ExtractComponent(a app.App, path string) (Component, error) {
ns, componentName := ExtractModuleComponent(a, path)
ns, componentName := extractModuleComponent(a, path)
members, err := ns.Components()
if err != nil {
return nil, err
Expand Down
38 changes: 27 additions & 11 deletions pkg/component/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,35 @@ import (
// the directory structure in a half-finished state.
func Delete(a app.App, name string) error {
log.Debugf("deleting component %s", name)
componentPath, err := Path(a, name)

moduleName, componentName, err := extractPathParts(a, name)
if err != nil {
return err
}

ns, _ := ExtractModuleComponent(a, name)
m := NewModule(a, moduleName)

var c Component
components, err := m.Components()
if err != nil {
return err
}
for i := range components {
if components[i].Name(false) == componentName {
c = components[i]
}
}

if c == nil {
return errors.Errorf("unable to find component %q", name)
}

// Build the new component/params.libsonnet file.
componentParamsFile, err := afero.ReadFile(a.Fs(), ns.ParamsPath())
// Build the new component params.libsonnet file.
componentParamsFile, err := afero.ReadFile(a.Fs(), m.ParamsPath())
if err != nil {
return err
}
componentJsonnet, err := param.DeleteComponent(name, string(componentParamsFile))
componentJsonnet, err := param.DeleteComponent(c.Name(false), string(componentParamsFile))
if err != nil {
return err
}
Expand Down Expand Up @@ -71,8 +87,8 @@ func Delete(a app.App, name string) error {
log.Infof("Removing component parameter references ...")

// Remove the references in component/params.libsonnet.
log.Debugf("... deleting references in %s", ns.ParamsPath())
err = afero.WriteFile(a.Fs(), ns.ParamsPath(), []byte(componentJsonnet), defaultFilePermissions)
log.Debugf("... deleting references in %s", m.ParamsPath())
err = afero.WriteFile(a.Fs(), m.ParamsPath(), []byte(componentJsonnet), defaultFilePermissions)
if err != nil {
return err
}
Expand All @@ -84,8 +100,8 @@ func Delete(a app.App, name string) error {
//
// Delete the component file in components/.
//
log.Infof("Deleting component '%s' at path '%s'", name, componentPath)
if err := a.Fs().Remove(componentPath); err != nil {
log.Infof("Deleting component %q", name)
if err := c.Remove(); err != nil {
return err
}

Expand All @@ -110,8 +126,8 @@ func collectEnvParams(a app.App, env *app.EnvironmentConfig, componentName, envN
return ecr.Remove(componentName, string(envParamsFile))
}

/// updateEnvParam removes the component references in each environment's
// paramss.libsonnet.
// updateEnvParam removes the component references in each environment's
// params.libsonnet.
func updateEnvParam(a app.App, envs app.EnvironmentConfigs, envParams map[string]string) error {
for envName := range envs {
path := filepath.Join(a.Root(), "environments", envName, "params.libsonnet")
Expand Down
30 changes: 30 additions & 0 deletions pkg/component/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ func TestDelete(t *testing.T) {
)
})
}

func TestDeleteWithModule(t *testing.T) {
test.WithApp(t, "/app", func(a *mocks.App, fs afero.Fs) {
test.StageDir(t, fs, "delete", "/app")

envs := app.EnvironmentConfigs{
"default": &app.EnvironmentConfig{},
}
a.On("Environments").Return(envs, nil)

err := Delete(a, "nested.guestbook-ui")
require.NoError(t, err)

base := filepath.Join("/app", "components", "nested")

test.AssertNotExists(t, fs, filepath.Join(base, "guestbook-ui.jsonnet"))
test.AssertContents(
t,
fs,
"delete-params.libsonnet",
filepath.Join(base, "params.libsonnet"),
)
test.AssertContents(
t,
fs,
"delete-env-params-nested.libsonnet",
filepath.Join("/app", "environments", "default", "params.libsonnet"),
)
})
}
11 changes: 11 additions & 0 deletions pkg/component/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ func (j *Jsonnet) Type() string {
return TypeJsonnet
}

// Remove removes the component.
func (j *Jsonnet) Remove() error {
m := NewModule(j.app, j.module)
path := filepath.Join(m.Dir(), j.Name(false)+"."+j.Type())
if err := j.app.Fs().Remove(path); err != nil {
return errors.Wrapf(err, "removing %q", path)
}

return nil
}

// SetParam set parameter for a component.
func (j *Jsonnet) SetParam(path []string, value interface{}) error {
paramsData, err := j.readModuleParams()
Expand Down
2 changes: 1 addition & 1 deletion pkg/component/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func extractPathParts(ksApp app.App, path string) (string, string, error) {
}

path = strings.Replace(path, ".", string(filepath.Separator), -1)
module, componentName := ExtractModuleComponent(ksApp, path)
module, componentName := extractModuleComponent(ksApp, path)
base := filepath.Join(module.Dir(), componentName)

exts := []string{".yaml", ".jsonnet", ".json"}
Expand Down
14 changes: 14 additions & 0 deletions pkg/component/mocks/Component.go

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

4 changes: 2 additions & 2 deletions pkg/component/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func NewModule(ksApp app.App, path string) *FilesystemModule {
return &FilesystemModule{app: ksApp, path: path}
}

// ExtractModuleComponent extracts a module and a component from a filesystem path.
func ExtractModuleComponent(a app.App, path string) (Module, string) {
// extractModuleComponent extracts a module and a component from a filesystem path.
func extractModuleComponent(a app.App, path string) (Module, string) {
dir, file := filepath.Split(path)
componentName := strings.TrimSuffix(file, filepath.Ext(file))

Expand Down
2 changes: 1 addition & 1 deletion pkg/component/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestExtractModuleComponent(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
test.WithApp(t, "/app", func(a *mocks.App, fs afero.Fs) {
m, c := ExtractModuleComponent(a, tc.in)
m, c := extractModuleComponent(a, tc.in)

assert.Equal(t, tc.m, m.Name())
assert.Equal(t, tc.c, c)
Expand Down
9 changes: 9 additions & 0 deletions pkg/component/testdata/delete-env-params-nested.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local params = import '../../components/params.libsonnet';

params {
components+: {
"guestbook-ui"+: {
name: 'guestbook-dev',
},
},
}
6 changes: 5 additions & 1 deletion pkg/component/testdata/delete-env-params.libsonnet
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local params = import '../../components/params.libsonnet';

params {
components+: {},
components+: {
"nested.guestbook-ui"+: {
name: 'guestbook-dev',
},
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local env = std.extVar("__ksonnet/environments");
local params = std.extVar("__ksonnet/params").components["guestbook-ui"];
local k = import "k.libsonnet";
local deployment = k.apps.v1beta1.deployment;
local container = k.apps.v1beta1.deployment.mixin.spec.template.spec.containersType;
local containerPort = container.portsType;
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;

local targetPort = params.containerPort;
local labels = {app: params.name};

local appService = service
.new(
params.name,
labels,
servicePort.new(params.servicePort, targetPort))
.withType(params.type);

local appDeployment = deployment
.new(
params.name,
params.replicas,
container
.new(params.name, params.image)
.withPorts(containerPort.new(targetPort)),
labels);

k.core.v1.list.new([appService, appDeployment])
19 changes: 19 additions & 0 deletions pkg/component/testdata/delete/components/nested/params.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
global: {
// User-defined global parameters; accessible to all component and environments, Ex:
// replicas: 4,
},
components: {
// Component-level parameters, defined initially from 'ks prototype use ...'
// Each object below should correspond to a component in the components/ directory
"guestbook-ui": {
containerPort: 80,
image: "gcr.io/heptio-images/ks-guestbook-demo:0.1",
name: "guiroot",
replicas: 1,
servicePort: 80,
type: "ClusterIP",
obj: {a: "b"},
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ params {
"guestbook-ui" +: {
name: "guestbook-dev",
},
"nested.guestbook-ui" +: {
name: "guestbook-dev",
},
},
}
11 changes: 11 additions & 0 deletions pkg/component/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func (y *YAML) Type() string {
return TypeYAML
}

// Remove removes the component.
func (y *YAML) Remove() error {
m := NewModule(y.app, y.module)
path := filepath.Join(m.Dir(), y.Name(false)+"."+y.Type())
if err := y.app.Fs().Remove(path); err != nil {
return errors.Wrapf(err, "removing %q", path)
}

return nil
}

// Params returns params for a component.
func (y *YAML) Params(envName string) ([]ModuleParameter, error) {
y.log().WithField("env-name", envName).Debug("getting component params")
Expand Down

0 comments on commit b6b33c5

Please sign in to comment.