-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dkirillov/gh-26: Renderer and Executor determined by the shore config #73
Open
dkirillov
wants to merge
16
commits into
main
Choose a base branch
from
dkirillov/gh-26-use-shore-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
813c00c
Added NewDependencies
dkirillov f82e7a3
shore config added to dependencies
dkirillov 3b2be3a
make use of NewDependencies
dkirillov 4919b44
added a comment
dkirillov 5ddaa4a
added NewDependencies tests
dkirillov a8e8eca
initializing of depedencies happens in init
dkirillov d9e4aaf
fixed unit tests
dkirillov 68ae59f
added case insensitive test case
dkirillov 42786e8
:memo: comment
dkirillov ce95e85
Changed to PersistentPreRunE
dkirillov de2469c
error message now says executor and not backend
dkirillov ba2a7e2
fixed unit test
dkirillov db350ad
Added ShoreConfigOpts struct
dkirillov 809093d
assign ShoreConfig
dkirillov 43b3810
Load also sets the shore config opts
dkirillov d9e3b8b
created init for both renderer and backend
dkirillov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,81 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Autodesk/shore/pkg/backend" | ||
"github.com/Autodesk/shore/pkg/backend/spinnaker" | ||
"github.com/Autodesk/shore/pkg/config" | ||
"github.com/Autodesk/shore/pkg/project" | ||
"github.com/Autodesk/shore/pkg/renderer" | ||
"github.com/Autodesk/shore/pkg/renderer/jsonnet" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Renderer Enum | ||
const ( | ||
// JSONNET - Jsonnet Renderer | ||
JSONNET string = "jsonnet" | ||
) | ||
|
||
// Backend Enum | ||
const ( | ||
// SPINNAKER - Spinnaker Backend | ||
SPINNAKER string = "spinnaker" | ||
) | ||
|
||
// Dependencies - Shared dependencies all controller MAY require | ||
type Dependencies struct { | ||
Renderer renderer.Renderer | ||
Backend backend.Backend | ||
Logger logrus.FieldLogger | ||
Project *project.Project | ||
Renderer renderer.Renderer | ||
Backend backend.Backend | ||
Logger logrus.FieldLogger | ||
Project *project.Project | ||
ShoreConfig config.ShoreConfig | ||
ShoreConfigOpts config.ShoreConfigOpts | ||
} | ||
|
||
// Load - loads the shore config and sets the renderer and backend | ||
func (d *Dependencies) Load(profileName, execConfigName string) error { | ||
d.ShoreConfigOpts = config.ShoreConfigOpts{ | ||
ProfileName: profileName, | ||
ExecutorConfigName: execConfigName, | ||
} | ||
|
||
var err error | ||
d.ShoreConfig, err = config.LoadShoreConfig(d.Project) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Renderer, err = d.initRenderer(d.ShoreConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Backend, err = d.initBackend(d.ShoreConfig) | ||
return err | ||
} | ||
|
||
// initRenderer initializes the Renderer based on the shore config | ||
func (d *Dependencies) initRenderer(shoreConfig config.ShoreConfig) (renderer.Renderer, error) { | ||
switch strings.ToLower(shoreConfig.Renderer[`type`].(string)) { | ||
case JSONNET: | ||
d.Logger.Debug("Using the Jsonnet Renderer") | ||
return jsonnet.NewRenderer(d.Project.FS, d.Project.Log), nil | ||
default: | ||
return nil, fmt.Errorf("the following Renderer is undefined: %s", shoreConfig.Renderer[`type`].(string)) | ||
} | ||
|
||
} | ||
|
||
// initRenderer initializes the Backend based on the shore config | ||
func (d *Dependencies) initBackend(shoreConfig config.ShoreConfig) (backend.Backend, error) { | ||
switch strings.ToLower(shoreConfig.Executor[`type`].(string)) { | ||
case SPINNAKER: | ||
d.Logger.Debug("Using the Spinnaker Backend") | ||
return spinnaker.NewClient(d.Project.Log), nil | ||
default: | ||
return nil, fmt.Errorf("the following Executor is undefined: %s", shoreConfig.Executor[`type`].(string)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/Autodesk/shore/pkg/backend/spinnaker" | ||
"github.com/Autodesk/shore/pkg/config" | ||
"github.com/Autodesk/shore/pkg/project" | ||
"github.com/Autodesk/shore/pkg/renderer/jsonnet" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var testPath = "/test" | ||
var shoreConfigTemplate = `{ | ||
"renderer": { | ||
"type": "%s" | ||
}, | ||
"executor": { | ||
"type": "%s", | ||
"config": { | ||
"default": "~/.spin/sb-config", | ||
"prodSpin": "~/.spin/prod-config" | ||
} | ||
}, | ||
"profiles": { | ||
"default": { | ||
"application": "test1test2test3", | ||
"pipeline": "simple-pipeline-test", | ||
"render": "render.yaml", | ||
"exec": "exec.yaml", | ||
"e2e": "e2e.yaml" | ||
} | ||
} | ||
}` | ||
|
||
func SetupTestDependencies() *Dependencies { | ||
os.Setenv("LOCAL", "true") | ||
os.Setenv("SHORE_PROJECT_PATH", testPath) | ||
|
||
memFs := afero.NewMemMapFs() | ||
memFs.Mkdir(testPath, os.ModePerm) | ||
|
||
logger, _ := test.NewNullLogger() | ||
|
||
return &Dependencies{ | ||
Project: project.NewShoreProject(memFs, logger), | ||
Logger: logger, | ||
ShoreConfigOpts: config.ShoreConfigOpts{ | ||
ProfileName: "default", | ||
ExecutorConfigName: "default", | ||
}, | ||
} | ||
} | ||
|
||
func TestPassingLoad(t *testing.T) { | ||
// Given | ||
deps := SetupTestDependencies() | ||
|
||
tests := []struct { | ||
name string | ||
configuredBackend string | ||
configuredRenderer string | ||
expectedBackend interface{} | ||
expectedRenderer interface{} | ||
}{ | ||
{ | ||
name: "spinnaker/jsonnet", | ||
configuredBackend: "spinnaker", | ||
configuredRenderer: "jsonnet", | ||
expectedBackend: &spinnaker.SpinClient{}, | ||
expectedRenderer: &jsonnet.Jsonnet{}, | ||
}, | ||
{ | ||
name: "insensetive-spinnaker/jsonnet", | ||
configuredBackend: "sPiNnAkEr", | ||
configuredRenderer: "JsOnNeT", | ||
expectedBackend: &spinnaker.SpinClient{}, | ||
expectedRenderer: &jsonnet.Jsonnet{}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
shoreConfig := fmt.Sprintf(shoreConfigTemplate, test.configuredRenderer, test.configuredBackend) | ||
afero.WriteFile(deps.Project.FS, path.Join(testPath, "shore.json"), []byte(shoreConfig), os.ModePerm) | ||
|
||
// When | ||
err := deps.Load("default", "default") | ||
|
||
// Then | ||
assert.NoError(t, err) | ||
assert.IsType(t, test.expectedRenderer, deps.Renderer) | ||
assert.IsType(t, test.expectedBackend, deps.Backend) | ||
assert.NotEmpty(t, deps.ShoreConfig) | ||
assert.NotEmpty(t, deps.ShoreConfigOpts) | ||
}) | ||
} | ||
} | ||
|
||
func TestFailingLoad(t *testing.T) { | ||
// Given | ||
deps := SetupTestDependencies() | ||
|
||
tests := []struct { | ||
name string | ||
configuredBackend string | ||
configuredRenderer string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "wrong-backend", | ||
configuredBackend: "yolo", | ||
configuredRenderer: "jsonnet", | ||
expectedError: "Executor is undefined", | ||
}, | ||
{ | ||
name: "wrong-renderer", | ||
configuredBackend: "spinnaker", | ||
configuredRenderer: "yolo", | ||
expectedError: "Renderer is undefined", | ||
}, | ||
{ | ||
name: "malformed-config", | ||
configuredBackend: "\"", | ||
configuredRenderer: "yolo", | ||
expectedError: "object not ended with", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
shoreConfig := fmt.Sprintf(shoreConfigTemplate, test.configuredRenderer, test.configuredBackend) | ||
afero.WriteFile(deps.Project.FS, path.Join(testPath, "shore.json"), []byte(shoreConfig), os.ModePerm) | ||
|
||
// When | ||
err := deps.Load("default", "default") | ||
|
||
// Then | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, test.expectedError) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from tests, where are these fields used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, they're not being used anywhere.
The intent is to have them there early so when render/save/execute/test-remote commands start making use of the shore config the information will already be there.
For instance, when the render command is ran it will have the shore config loaded in the dependencies as well as the chosen profile - and so it will be able to use the correct config based on the profile (all of which it will have access to).