Skip to content
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

remove unassigned targets from deployment #267

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package solution

import (
"encoding/json"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -52,6 +53,11 @@ func PlanForDeployment(deployment model.DeploymentSpec, state model.DeploymentSt
}
}
}
test := ret.RevisedForDeletion()
jTestData, _ := json.Marshal(test)
fmt.Println("--------------------------------------------------")
fmt.Println(string(jTestData))
fmt.Println("--------------------------------------------------")
return ret.RevisedForDeletion(), nil
}

Expand Down
15 changes: 13 additions & 2 deletions api/pkg/apis/v1alpha1/utils/symphony-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,14 +773,21 @@ func CreateSymphonyDeployment(instance model.InstanceState, solution model.Solut
ret.Assignments[k] = v
}

// Remove targets not in assignments
for targetName := range ret.Targets {
if _, ok := ret.Assignments[targetName]; !ok {
delete(ret.Targets, targetName)
}
}

return ret, nil
}

func AssignComponentsToTargets(components []model.ComponentSpec, targets map[string]model.TargetState) (map[string]string, error) {
//TODO: evaluate constraints
ret := make(map[string]string)
for key, target := range targets {
ret[key] = ""
assignments := ""
for _, component := range components {
match := true
if component.Constraints != "" {
Expand All @@ -794,9 +801,13 @@ func AssignComponentsToTargets(components []model.ComponentSpec, targets map[str
match = (val == "true" || val == true)
}
if match {
ret[key] += "{" + component.Name + "}"
assignments += "{" + component.Name + "}"
}
}
// Only add the key to the map if assignments is not empty
if assignments != "" {
ret[key] = assignments
}
}

return ret, nil
Expand Down
59 changes: 59 additions & 0 deletions api/pkg/apis/v1alpha1/utils/symphony-api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,65 @@ func TestCreateSymphonyDeploymentFromTarget(t *testing.T) {
require.True(t, ret)
}

func TestCreateSymphonyDeploymentTargetTrim(t *testing.T) {
res, err := CreateSymphonyDeployment(model.InstanceState{
ObjectMeta: model.ObjectMeta{
Name: "someInstance",
},
Spec: &model.InstanceSpec{
Target: model.TargetSelector{
Selector: map[string]string{
"OS": "windows",
},
},
},
Status: model.InstanceStatus{},
}, model.SolutionState{
ObjectMeta: model.ObjectMeta{
Name: "someSolution",
},
Spec: &model.SolutionSpec{
Components: []model.ComponentSpec{
{
Name: "com1",
Type: "type1",
Constraints: "${{$equal($property(OS),windows)}}",
},
{
Name: "com2",
Type: "type2",
Constraints: "${{$equal($property(OS),windows)}}",
},
},
},
}, []model.TargetState{
{
ObjectMeta: model.ObjectMeta{
Name: "target1",
},
Spec: &model.TargetSpec{
Properties: map[string]string{
"OS": "windows",
},
},
},
{
ObjectMeta: model.ObjectMeta{
Name: "target2",
},
Spec: &model.TargetSpec{
Properties: map[string]string{
"OS": "linux",
},
},
},
}, []model.DeviceState{}, "default")
require.NoError(t, err)
// If a target is not assigned, it should be excluded in the final deployment
require.Equal(t, 1, len(res.Targets))
require.Equal(t, 1, len(res.Assignments))
}

func TestCreateSymphonyDeployment(t *testing.T) {
res, err := CreateSymphonyDeployment(model.InstanceState{
ObjectMeta: model.ObjectMeta{
Expand Down
Loading