Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #141 from dexhorthy/dex/ch12240/tf-poc-lifecycle
Browse files Browse the repository at this point in the history
Starting on Plan step
  • Loading branch information
dexhorthy authored Jul 12, 2018
2 parents 1c819f1 + 839ed63 commit d42dcd3
Show file tree
Hide file tree
Showing 758 changed files with 193,384 additions and 4,002 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

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

7 changes: 3 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
[[constraint]]
name = "github.com/go-kit/kit"
version = "0.6.0"
[[constraint]]
name = "github.com/pkg/errors"
branch = "master"

[[constraint]]
name = "github.com/go-stack/stack"
version = "1.7.0"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.1"
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ _mockgen:
mkdir -p pkg/test-mocks/helm
mkdir -p pkg/test-mocks/dockerlayer
mkdir -p pkg/test-mocks/github
mkdir -p pkg/test-mocks/inline
mockgen \
-destination pkg/test-mocks/ui/ui.go \
-package ui \
Expand Down Expand Up @@ -108,6 +109,11 @@ _mockgen:
-package github \
github.com/replicatedhq/ship/pkg/lifecycle/render/github \
Renderer
mockgen \
-destination pkg/test-mocks/inline/inline_mock.go \
-package inline \
github.com/replicatedhq/ship/pkg/lifecycle/render/inline \
Renderer

mockgen: _mockgen fmt

Expand Down
3 changes: 2 additions & 1 deletion examples/terraform/ship.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ config:
- name: gce_project
title: Google Cloud project name
type: text
required: true - name: gce_region
required: true
- name: gce_region
title: Google Cloud compute region
type: text
default: us-central1
Expand Down
5 changes: 4 additions & 1 deletion pkg/lifecycle/render/config/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import "encoding/json"

type Progress struct {
Source string `json:"source"`
Type string `json:"type"` // string, json, etc
Type string `json:"type"` // string, json, etc
Level string `json:"level"` // string, json, etc
Detail string `json:"detail,omitempty"`
}

func StringProgress(source, detail string) Progress {
return Progress{
Source: source,
Type: "string",
Level: "info",
Detail: detail,
}
}
Expand All @@ -21,6 +23,7 @@ func JSONProgress(source string, detail interface{}) Progress {
return Progress{
Source: source,
Type: "json",
Level: "info",
Detail: string(d),
}
}
47 changes: 41 additions & 6 deletions pkg/lifecycle/render/terraform/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package terraform
import (
"context"

"path"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/replicatedhq/libyaml"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/lifecycle/render/inline"
"github.com/replicatedhq/ship/pkg/version"
)

// Renderer is something that can render a terraform asset as part of a planner.Plan
Expand All @@ -18,26 +23,56 @@ type Renderer interface {
) func(ctx context.Context) error
}

// a VendorRenderer renders a terraform asset by vendoring in terraform source code
type VendorRenderer struct {
// a LocalRenderer renders a terraform asset by vendoring in terraform source code
type LocalRenderer struct {
Logger log.Logger
Inline inline.Renderer
}

var _ Renderer = &VendorRenderer{}
var _ Renderer = &LocalRenderer{}

func NewRenderer(logger log.Logger) Renderer {
return &VendorRenderer{
func NewRenderer(
logger log.Logger,
inline inline.Renderer,
) Renderer {
return &LocalRenderer{
Logger: logger,
Inline: inline,
}
}

func (r *VendorRenderer) Execute(
func (r *LocalRenderer) Execute(
asset api.TerraformAsset,
meta api.ReleaseMetadata,
configGroups []libyaml.ConfigGroup,
templateContext map[string]interface{},
) func(ctx context.Context) error {
return func(ctx context.Context) error {

if asset.Inline == "" {
return errors.New("online \"inline\" terraform assets are supported")
}

// todo this is duped from lifecycle/terraformer. And we should maybe put this
// in state instead of the FS anyway
assetsPath := path.Join("/tmp", "ship-terraform", version.RunAtEpoch, "asset", "main.tf")

// write the inline spec
err := r.Inline.Execute(
api.InlineAsset{
Contents: asset.Inline,
AssetShared: api.AssetShared{
Dest: assetsPath,
},
},
meta,
templateContext,
configGroups,
)(ctx)

if err != nil {
return errors.Wrap(err, "write tf config")
}
return nil
}
}
41 changes: 36 additions & 5 deletions pkg/lifecycle/render/terraform/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"context"
"testing"

"strings"

"github.com/golang/mock/gomock"
"github.com/replicatedhq/libyaml"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/test-mocks/inline"
"github.com/replicatedhq/ship/pkg/testing/logger"
"github.com/replicatedhq/ship/pkg/testing/matchers"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,23 +23,49 @@ func TestRenderer(t *testing.T) {
{
name: "empty",
asset: api.TerraformAsset{
Inline: "",
Inline: "some tf config",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req := require.New(t)
mc := gomock.NewController(t)
mockInline := inline.NewMockRenderer(mc)

renderer := &VendorRenderer{
renderer := &LocalRenderer{
Logger: &logger.TestLogger{T: t},
Inline: mockInline,
}

assetMatcher := &matchers.Is{
Describe: "inline asset",
Test: func(v interface{}) bool {
asset, ok := v.(api.InlineAsset)
if !ok {
return false
}
return asset.Contents == test.asset.Inline &&
strings.HasPrefix(asset.Dest, "/tmp/ship-terraform")
},
}

metadata := api.ReleaseMetadata{}
groups := []libyaml.ConfigGroup{}
templateContext := map[string]interface{}{}

mockInline.EXPECT().Execute(
assetMatcher,
metadata,
templateContext,
groups,
).Return(func(ctx context.Context) error { return nil })

err := renderer.Execute(
test.asset,
api.ReleaseMetadata{},
[]libyaml.ConfigGroup{},
map[string]interface{}{},
metadata,
groups,
templateContext,
)(context.Background())

req.NoError(err)
Expand Down
29 changes: 23 additions & 6 deletions pkg/lifecycle/terraform/terraformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package terraform
import (
"context"

"path"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/lifecycle/render/config"
"github.com/replicatedhq/ship/pkg/version"
)

type Terraformer interface {
Execute(ctx context.Context, release api.Release, step api.Terraform) error
WithDaemon(d config.Daemon) Terraformer
}

type VendorTerraformer struct {
type ForkTerraformer struct {
Logger log.Logger
Daemon config.Daemon
}
Expand All @@ -22,19 +26,32 @@ func NewTerraformer(
logger log.Logger,
daemon config.Daemon,
) Terraformer {
return &VendorTerraformer{
return &ForkTerraformer{
Logger: logger,
Daemon: daemon,
}
}

func (t *VendorTerraformer) WithDaemon(daemon config.Daemon) Terraformer {
return &VendorTerraformer{
func (t *ForkTerraformer) WithDaemon(daemon config.Daemon) Terraformer {
return &ForkTerraformer{
Logger: t.Logger,
Daemon: daemon,
}
}

func (t *VendorTerraformer) Execute(ctx context.Context, release api.Release, step api.Terraform) error {
panic("implement me")
func (t *ForkTerraformer) Execute(ctx context.Context, release api.Release, step api.Terraform) error {

assetsPath := path.Join("/tmp", "ship-terraform", version.RunAtEpoch, "asset")
_, err := t.plan(assetsPath)
// create plan, save to state
// push infra plan step
// maybe exit
// set progress applying
return errors.Wrapf(err, "create plan for %s", assetsPath)
}

func (t *ForkTerraformer) plan(modulePath string) (string, error) {
// we really shouldn't write plan to a file, but this will do for now
planOut := path.Join("tmp", "ship-terraform", version.RunAtEpoch, "plan")
return planOut, nil
}
39 changes: 39 additions & 0 deletions pkg/lifecycle/terraform/terraformer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package terraform

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/test-mocks/config"
"github.com/replicatedhq/ship/pkg/testing/logger"
)

func TestTerraformer(t *testing.T) {
tests := []struct {
name string
}{
{
name: "zero",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
//req := require.New(t)
mc := gomock.NewController(t)
mockDaemon := config.NewMockDaemon(mc)
tf := &ForkTerraformer{
Logger: &logger.TestLogger{T: t},
Daemon: mockDaemon,
}
//err := tf.Execute(
tf.Execute(
context.Background(),
api.Release{},
api.Terraform{},
)
//req.NoError(err)
})
}
}
49 changes: 49 additions & 0 deletions pkg/test-mocks/inline/inline_mock.go

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

Loading

0 comments on commit d42dcd3

Please sign in to comment.