Skip to content

Commit 437a537

Browse files
committed
use common policy match func
1 parent e236adf commit 437a537

File tree

3 files changed

+81
-21
lines changed

3 files changed

+81
-21
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package selector
2+
3+
import (
4+
"context"
5+
"workspace-engine/pkg/oapi"
6+
)
7+
8+
func NewBasicReleaseTarget(environment *oapi.Environment, deployment *oapi.Deployment, resource *oapi.Resource) *BasicReleaseTarget {
9+
return &BasicReleaseTarget{
10+
environment: environment,
11+
deployment: deployment,
12+
resource: resource,
13+
}
14+
}
15+
16+
type BasicReleaseTarget struct {
17+
environment *oapi.Environment
18+
deployment *oapi.Deployment
19+
resource *oapi.Resource
20+
}
21+
22+
func (b *BasicReleaseTarget) Environment() *oapi.Environment {
23+
return b.environment
24+
}
25+
26+
func (b *BasicReleaseTarget) Deployment() *oapi.Deployment {
27+
return b.deployment
28+
}
29+
30+
func (b *BasicReleaseTarget) Resource() *oapi.Resource {
31+
return b.resource
32+
}
33+
34+
func MatchPolicy(ctx context.Context, policy *oapi.Policy, releaseTarget *BasicReleaseTarget) bool {
35+
for _, policyTarget := range policy.Selectors {
36+
if policyTarget.EnvironmentSelector != nil {
37+
if ok, _ := Match(ctx, policyTarget.EnvironmentSelector, releaseTarget.Environment()); !ok {
38+
continue
39+
}
40+
}
41+
if policyTarget.DeploymentSelector != nil {
42+
if ok, _ := Match(ctx, policyTarget.DeploymentSelector, releaseTarget.Deployment()); !ok {
43+
continue
44+
}
45+
}
46+
if policyTarget.ResourceSelector != nil {
47+
if ok, _ := Match(ctx, policyTarget.ResourceSelector, releaseTarget.Resource()); !ok {
48+
continue
49+
}
50+
}
51+
return true
52+
}
53+
return false
54+
}

apps/workspace-engine/pkg/server/openapi/policies/policies.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,38 @@ package policies
33
import (
44
"net/http"
55
"workspace-engine/pkg/oapi"
6+
"workspace-engine/pkg/server/openapi/utils"
67

78
"github.com/gin-gonic/gin"
89
)
910

1011
type Policies struct{}
1112

1213
func (p *Policies) GetReleaseTargetsForPolicy(c *gin.Context, workspaceId oapi.WorkspaceId, policyId oapi.PolicyId) {
14+
ws := utils.GetWorkspace(c, workspaceId)
15+
if ws == nil {
16+
c.JSON(http.StatusNotFound, gin.H{
17+
"error": "Workspace not found",
18+
})
19+
return
20+
}
21+
22+
// policy, ok := ws.Policies().Get(policyId)
23+
// if !ok {
24+
// c.JSON(http.StatusNotFound, gin.H{
25+
// "error": "Policy not found",
26+
// })
27+
// return
28+
// }
29+
30+
// releaseTargets, err := ws.ReleaseTargets().Items(c.Request.Context())
31+
// if err != nil {
32+
// c.JSON(http.StatusInternalServerError, gin.H{
33+
// "error": "Failed to get release targets for policy: " + err.Error(),
34+
// })
35+
// return
36+
// }
37+
1338
c.JSON(http.StatusInternalServerError, gin.H{
1439
"error": "Not implemented",
1540
})

apps/workspace-engine/pkg/workspace/store/release_targets.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,31 +149,12 @@ func (r *ReleaseTargets) computePolicies(ctx context.Context, releaseTarget *oap
149149
return nil, fmt.Errorf("resource %s not found", releaseTarget.ResourceId)
150150
}
151151

152+
resolvedReleaseTarget := selector.NewBasicReleaseTarget(environments, deployments, resources)
152153
matchingPolicies := make(map[string]*oapi.Policy)
153154

154155
for policyItem := range r.store.Policies.IterBuffered() {
155156
policy := policyItem.Val
156-
hasMatch := false
157-
for _, policyTarget := range policy.Selectors {
158-
if policyTarget.EnvironmentSelector != nil {
159-
if ok, _ := selector.Match(ctx, policyTarget.EnvironmentSelector, environments); !ok {
160-
continue
161-
}
162-
}
163-
if policyTarget.DeploymentSelector != nil {
164-
if ok, _ := selector.Match(ctx, policyTarget.DeploymentSelector, deployments); !ok {
165-
continue
166-
}
167-
}
168-
if policyTarget.ResourceSelector != nil {
169-
if ok, _ := selector.Match(ctx, policyTarget.ResourceSelector, resources); !ok {
170-
continue
171-
}
172-
}
173-
hasMatch = true
174-
break
175-
}
176-
if hasMatch {
157+
if selector.MatchPolicy(ctx, policy, resolvedReleaseTarget) {
177158
matchingPolicies[policy.Id] = policy
178159
}
179160
}

0 commit comments

Comments
 (0)