From 3e763afe11062fe302d919330070164b3e892ed1 Mon Sep 17 00:00:00 2001 From: Haishi Bai Date: Thu, 16 May 2024 15:40:49 -0700 Subject: [PATCH 1/3] remove unasssigned targets from deployment --- api/pkg/apis/v1alpha1/utils/symphony-api.go | 15 ++++- .../apis/v1alpha1/utils/symphony-api_test.go | 59 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/api/pkg/apis/v1alpha1/utils/symphony-api.go b/api/pkg/apis/v1alpha1/utils/symphony-api.go index 2820825f6..733517db9 100644 --- a/api/pkg/apis/v1alpha1/utils/symphony-api.go +++ b/api/pkg/apis/v1alpha1/utils/symphony-api.go @@ -773,6 +773,13 @@ 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 } @@ -780,7 +787,7 @@ func AssignComponentsToTargets(components []model.ComponentSpec, targets map[str //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 != "" { @@ -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 diff --git a/api/pkg/apis/v1alpha1/utils/symphony-api_test.go b/api/pkg/apis/v1alpha1/utils/symphony-api_test.go index 3d0f44fe4..b64a5d906 100644 --- a/api/pkg/apis/v1alpha1/utils/symphony-api_test.go +++ b/api/pkg/apis/v1alpha1/utils/symphony-api_test.go @@ -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{ From 701a704bc3a25f7a8d6b1285d2bbee5979660474 Mon Sep 17 00:00:00 2001 From: Haishi Bai Date: Thu, 16 May 2024 17:17:20 -0700 Subject: [PATCH 2/3] allow empty assignments --- api/pkg/apis/v1alpha1/utils/symphony-api.go | 8 ++++---- api/pkg/apis/v1alpha1/utils/symphony-api_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/api/pkg/apis/v1alpha1/utils/symphony-api.go b/api/pkg/apis/v1alpha1/utils/symphony-api.go index 733517db9..55edbb070 100644 --- a/api/pkg/apis/v1alpha1/utils/symphony-api.go +++ b/api/pkg/apis/v1alpha1/utils/symphony-api.go @@ -775,7 +775,7 @@ func CreateSymphonyDeployment(instance model.InstanceState, solution model.Solut // Remove targets not in assignments for targetName := range ret.Targets { - if _, ok := ret.Assignments[targetName]; !ok { + if v, ok := ret.Assignments[targetName]; !ok || v == "" { delete(ret.Targets, targetName) } } @@ -805,9 +805,9 @@ func AssignComponentsToTargets(components []model.ComponentSpec, targets map[str } } // Only add the key to the map if assignments is not empty - if assignments != "" { - ret[key] = assignments - } + //if assignments != "" { + ret[key] = assignments + //} } return ret, nil diff --git a/api/pkg/apis/v1alpha1/utils/symphony-api_test.go b/api/pkg/apis/v1alpha1/utils/symphony-api_test.go index b64a5d906..206db0399 100644 --- a/api/pkg/apis/v1alpha1/utils/symphony-api_test.go +++ b/api/pkg/apis/v1alpha1/utils/symphony-api_test.go @@ -700,7 +700,7 @@ func TestCreateSymphonyDeploymentTargetTrim(t *testing.T) { 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)) + require.Equal(t, 2, len(res.Assignments)) } func TestCreateSymphonyDeployment(t *testing.T) { From 1a2eb32806874dd60a3a13e84b58f7cb4e44c374 Mon Sep 17 00:00:00 2001 From: Haishi Bai Date: Thu, 16 May 2024 19:37:03 -0700 Subject: [PATCH 3/3] test with tracing --- .../v1alpha1/managers/solution/solution-manager-state.go | 6 ++++++ api/pkg/apis/v1alpha1/utils/symphony-api.go | 8 ++++---- api/pkg/apis/v1alpha1/utils/symphony-api_test.go | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/api/pkg/apis/v1alpha1/managers/solution/solution-manager-state.go b/api/pkg/apis/v1alpha1/managers/solution/solution-manager-state.go index 0685a1f83..06e111ae1 100644 --- a/api/pkg/apis/v1alpha1/managers/solution/solution-manager-state.go +++ b/api/pkg/apis/v1alpha1/managers/solution/solution-manager-state.go @@ -7,6 +7,7 @@ package solution import ( + "encoding/json" "fmt" "sort" "strings" @@ -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 } diff --git a/api/pkg/apis/v1alpha1/utils/symphony-api.go b/api/pkg/apis/v1alpha1/utils/symphony-api.go index 55edbb070..733517db9 100644 --- a/api/pkg/apis/v1alpha1/utils/symphony-api.go +++ b/api/pkg/apis/v1alpha1/utils/symphony-api.go @@ -775,7 +775,7 @@ func CreateSymphonyDeployment(instance model.InstanceState, solution model.Solut // Remove targets not in assignments for targetName := range ret.Targets { - if v, ok := ret.Assignments[targetName]; !ok || v == "" { + if _, ok := ret.Assignments[targetName]; !ok { delete(ret.Targets, targetName) } } @@ -805,9 +805,9 @@ func AssignComponentsToTargets(components []model.ComponentSpec, targets map[str } } // Only add the key to the map if assignments is not empty - //if assignments != "" { - ret[key] = assignments - //} + if assignments != "" { + ret[key] = assignments + } } return ret, nil diff --git a/api/pkg/apis/v1alpha1/utils/symphony-api_test.go b/api/pkg/apis/v1alpha1/utils/symphony-api_test.go index 206db0399..b64a5d906 100644 --- a/api/pkg/apis/v1alpha1/utils/symphony-api_test.go +++ b/api/pkg/apis/v1alpha1/utils/symphony-api_test.go @@ -700,7 +700,7 @@ func TestCreateSymphonyDeploymentTargetTrim(t *testing.T) { 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, 2, len(res.Assignments)) + require.Equal(t, 1, len(res.Assignments)) } func TestCreateSymphonyDeployment(t *testing.T) {