Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Refactor dependsOn into References
Browse files Browse the repository at this point in the history
See #233 for discussion
  • Loading branch information
James Haggerty authored and ash2k committed Mar 29, 2018
1 parent d075e6b commit 7ced147
Show file tree
Hide file tree
Showing 19 changed files with 495 additions and 279 deletions.
6 changes: 4 additions & 2 deletions it/sc/service_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ func TestServiceCatalog(t *testing.T) {
},
},
{
Name: smith_v1.ResourceName(binding.Name),
DependsOn: []smith_v1.ResourceName{smith_v1.ResourceName(instance.Name)},
Name: smith_v1.ResourceName(binding.Name),
References: []smith_v1.Reference{
{Resource: smith_v1.ResourceName(instance.Name)},
},
Spec: smith_v1.ResourceSpec{
Object: binding,
},
Expand Down
6 changes: 4 additions & 2 deletions it/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func TestWorkflow(t *testing.T) {
Spec: smith_v1.BundleSpec{
Resources: []smith_v1.Resource{
{
Name: "config1res",
DependsOn: []smith_v1.ResourceName{"secret2res"},
Name: "config1res",
References: []smith_v1.Reference{
{Resource: "secret2res"},
},
Spec: smith_v1.ResourceSpec{
Object: c1,
},
Expand Down
21 changes: 20 additions & 1 deletion pkg/apis/smith/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func (bs *BundleStatus) GetResourceStatus(resName ResourceName) (int, *ResourceS
// ResourceName is a reference to another Resource in the same bundle.
type ResourceName string

// ReferenceName is a the name of a reference which can be used inside a resource.
type ReferenceName string

// PluginName is a name of a plugin to be invoked.
type PluginName string

Expand All @@ -194,11 +197,27 @@ type Resource struct {
Name ResourceName `json:"name"`

// Explicit dependencies.
DependsOn []ResourceName `json:"dependsOn,omitempty"`
References []Reference `json:"reference,omitempty"`

Spec ResourceSpec `json:"spec"`
}

// +k8s:deepcopy-gen=true
// Refer to a part of another object
type Reference struct {
Name ReferenceName `json:"name,omitempty"`
Resource ResourceName `json:"resource"`
Path string `json:"path,omitempty"`
Example interface{} `json:"example,omitempty"`
Modifier string `json:"modifier,omitempty"`
}

// DeepCopyInto is an deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Reference) DeepCopyInto(out *Reference) {
*out = *in
out.Example = runtime.DeepCopyJSONValue(in.Example)
}

// +k8s:deepcopy-gen=true
// ResourceSpec is a union type - either object of plugin can be specified.
type ResourceSpec struct {
Expand Down
36 changes: 22 additions & 14 deletions pkg/apis/smith/v1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions pkg/controller/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_library(
"//vendor/github.com/ash2k/stager/wait:go_default_library",
"//vendor/github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/go.uber.org/multierr:go_default_library",
"//vendor/go.uber.org/zap:go_default_library",
"//vendor/golang.org/x/crypto/bcrypt:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/bundle_sync_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ func sortBundle(bundle *smith_v1.Bundle) (*graph.Graph, []graph.V, error) {
}

for _, res := range bundle.Spec.Resources {
for _, d := range res.DependsOn {
if err := g.AddEdge(res.Name, d); err != nil {
for _, reference := range res.References {
if err := g.AddEdge(res.Name, reference.Resource); err != nil {
return nil, nil, err
}
}
Expand Down
90 changes: 78 additions & 12 deletions pkg/controller/controller_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ func TestBundleSort(t *testing.T) {
Spec: smith_v1.BundleSpec{
Resources: []smith_v1.Resource{
{
Name: "a",
DependsOn: []smith_v1.ResourceName{"c"},
Name: "a",
References: []smith_v1.Reference{
{
Resource: "c",
},
},
},
{
Name: "b",
},
{
Name: "c",
DependsOn: []smith_v1.ResourceName{"b"},
Name: "c",
References: []smith_v1.Reference{
{
Resource: "b",
},
},
},
{
Name: "d",
DependsOn: []smith_v1.ResourceName{"e"},
Name: "d",
References: []smith_v1.Reference{
{
Resource: "e",
},
},
},
{
Name: "e",
Expand All @@ -48,19 +60,31 @@ func TestBundleSortMissingDependency(t *testing.T) {
Spec: smith_v1.BundleSpec{
Resources: []smith_v1.Resource{
{
Name: "a",
DependsOn: []smith_v1.ResourceName{"x"},
Name: "a",
References: []smith_v1.Reference{
{
Resource: "x",
},
},
},
{
Name: "b",
},
{
Name: "c",
DependsOn: []smith_v1.ResourceName{"b"},
Name: "c",
References: []smith_v1.Reference{
{
Resource: "b",
},
},
},
{
Name: "d",
DependsOn: []smith_v1.ResourceName{"e"},
Name: "d",
References: []smith_v1.Reference{
{
Resource: "e",
},
},
},
{
Name: "e",
Expand All @@ -71,3 +95,45 @@ func TestBundleSortMissingDependency(t *testing.T) {
_, sorted, err := sortBundle(&bundle)
require.EqualError(t, err, "vertex \"x\" not found", "%v", sorted)
}

func TestBundleSortSelfReference(t *testing.T) {
t.Parallel()
bundle := smith_v1.Bundle{
Spec: smith_v1.BundleSpec{
Resources: []smith_v1.Resource{
{
Name: "a",
References: []smith_v1.Reference{
{
Resource: "a",
},
},
},
{
Name: "b",
},
{
Name: "c",
References: []smith_v1.Reference{
{
Resource: "b",
},
},
},
{
Name: "d",
References: []smith_v1.Reference{
{
Resource: "e",
},
},
},
{
Name: "e",
},
},
},
}
_, sorted, err := sortBundle(&bundle)
require.EqualError(t, err, "cycle error: [a a]", "%v", sorted)
}
Loading

0 comments on commit 7ced147

Please sign in to comment.