Skip to content

Commit 4d612a9

Browse files
authored
SQLite backed cache (#155)
Request for additional indexed fields
1 parent 19ccb44 commit 4d612a9

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package vai
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
namegen "github.com/rancher/shepherd/pkg/namegenerator"
8+
v1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
)
11+
12+
var vaiOnlySecretFilterCases = []secretFilterTestCase{
13+
{
14+
name: "Filter by project-scoped-secret-copy annotation",
15+
createSecrets: func() ([]v1.Secret, []string, []string, []string) {
16+
suffix := namegen.RandStringLower(randomStringLength)
17+
ns := fmt.Sprintf("namespace-%s", suffix)
18+
name1 := fmt.Sprintf("secret1-%s", namegen.RandStringLower(randomStringLength))
19+
name2 := fmt.Sprintf("secret2-%s", namegen.RandStringLower(randomStringLength))
20+
name3 := fmt.Sprintf("secret3-%s", namegen.RandStringLower(randomStringLength))
21+
22+
projectID := "local:p-test123"
23+
24+
secrets := []v1.Secret{
25+
{
26+
ObjectMeta: metav1.ObjectMeta{
27+
Name: name1,
28+
Namespace: ns,
29+
Annotations: map[string]string{
30+
"management.cattle.io/project-scoped-secret-copy": projectID,
31+
},
32+
},
33+
Type: v1.SecretTypeOpaque,
34+
},
35+
{
36+
ObjectMeta: metav1.ObjectMeta{
37+
Name: name2,
38+
Namespace: ns,
39+
Annotations: map[string]string{
40+
"management.cattle.io/project-scoped-secret-copy": projectID,
41+
},
42+
},
43+
Type: v1.SecretTypeOpaque,
44+
},
45+
{
46+
ObjectMeta: metav1.ObjectMeta{
47+
Name: name3,
48+
Namespace: ns,
49+
// No project annotation - this secret should NOT be returned
50+
},
51+
Type: v1.SecretTypeOpaque,
52+
},
53+
}
54+
55+
expectedNames := []string{name1, name2}
56+
allNamespaces := []string{ns}
57+
expectedNamespaces := []string{ns}
58+
59+
return secrets, expectedNames, allNamespaces, expectedNamespaces
60+
},
61+
filter: func(namespaces []string) url.Values {
62+
return url.Values{
63+
"filter": []string{`metadata.annotations[management.cattle.io/project-scoped-secret-copy]="local:p-test123"`},
64+
"projectsornamespaces": namespaces,
65+
}
66+
},
67+
supportedWithVai: true,
68+
},
69+
{
70+
name: "Filter by project-scoped-secret-copy annotation - different projects",
71+
createSecrets: func() ([]v1.Secret, []string, []string, []string) {
72+
suffix := namegen.RandStringLower(randomStringLength)
73+
ns := fmt.Sprintf("namespace-%s", suffix)
74+
name1 := fmt.Sprintf("secret1-%s", namegen.RandStringLower(randomStringLength))
75+
name2 := fmt.Sprintf("secret2-%s", namegen.RandStringLower(randomStringLength))
76+
name3 := fmt.Sprintf("secret3-%s", namegen.RandStringLower(randomStringLength))
77+
78+
secrets := []v1.Secret{
79+
{
80+
ObjectMeta: metav1.ObjectMeta{
81+
Name: name1,
82+
Namespace: ns,
83+
Annotations: map[string]string{
84+
"management.cattle.io/project-scoped-secret-copy": "local:p-project1",
85+
},
86+
},
87+
Type: v1.SecretTypeOpaque,
88+
},
89+
{
90+
ObjectMeta: metav1.ObjectMeta{
91+
Name: name2,
92+
Namespace: ns,
93+
Annotations: map[string]string{
94+
"management.cattle.io/project-scoped-secret-copy": "local:p-project2",
95+
},
96+
},
97+
Type: v1.SecretTypeOpaque,
98+
},
99+
{
100+
ObjectMeta: metav1.ObjectMeta{
101+
Name: name3,
102+
Namespace: ns,
103+
Annotations: map[string]string{
104+
"management.cattle.io/project-scoped-secret-copy": "local:p-project1",
105+
},
106+
},
107+
Type: v1.SecretTypeOpaque,
108+
},
109+
}
110+
111+
// Only expecting secrets from project1
112+
expectedNames := []string{name1, name3}
113+
allNamespaces := []string{ns}
114+
expectedNamespaces := []string{ns}
115+
116+
return secrets, expectedNames, allNamespaces, expectedNamespaces
117+
},
118+
filter: func(namespaces []string) url.Values {
119+
return url.Values{
120+
"filter": []string{`metadata.annotations[management.cattle.io/project-scoped-secret-copy]="local:p-project1"`},
121+
"projectsornamespaces": namespaces,
122+
}
123+
},
124+
supportedWithVai: true,
125+
},
126+
{
127+
name: "Filter by project-scoped-secret-copy annotation with negation",
128+
createSecrets: func() ([]v1.Secret, []string, []string, []string) {
129+
suffix := namegen.RandStringLower(randomStringLength)
130+
ns := fmt.Sprintf("namespace-%s", suffix)
131+
name1 := fmt.Sprintf("secret1-%s", namegen.RandStringLower(randomStringLength))
132+
name2 := fmt.Sprintf("secret2-%s", namegen.RandStringLower(randomStringLength))
133+
name3 := fmt.Sprintf("secret3-%s", namegen.RandStringLower(randomStringLength))
134+
135+
secrets := []v1.Secret{
136+
{
137+
ObjectMeta: metav1.ObjectMeta{
138+
Name: name1,
139+
Namespace: ns,
140+
Annotations: map[string]string{
141+
"management.cattle.io/project-scoped-secret-copy": "local:p-exclude",
142+
},
143+
},
144+
Type: v1.SecretTypeOpaque,
145+
},
146+
{
147+
ObjectMeta: metav1.ObjectMeta{
148+
Name: name2,
149+
Namespace: ns,
150+
Annotations: map[string]string{
151+
"management.cattle.io/project-scoped-secret-copy": "local:p-include",
152+
},
153+
},
154+
Type: v1.SecretTypeOpaque,
155+
},
156+
{
157+
ObjectMeta: metav1.ObjectMeta{
158+
Name: name3,
159+
Namespace: ns,
160+
// No annotation - this should be included in results
161+
},
162+
Type: v1.SecretTypeOpaque,
163+
},
164+
}
165+
166+
// Expecting all secrets EXCEPT the one with "local:p-exclude"
167+
expectedNames := []string{name2, name3}
168+
allNamespaces := []string{ns}
169+
expectedNamespaces := []string{ns}
170+
171+
return secrets, expectedNames, allNamespaces, expectedNamespaces
172+
},
173+
filter: func(namespaces []string) url.Values {
174+
return url.Values{
175+
"filter": []string{`metadata.annotations[management.cattle.io/project-scoped-secret-copy]!="local:p-exclude"`},
176+
"projectsornamespaces": namespaces,
177+
}
178+
},
179+
supportedWithVai: true,
180+
},
181+
}

validation/steve/vai/vai_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@ func (v *VaiTestSuite) TestVaiEnabled() {
995995
v.runSecretFilterTestCases(supportedWithVai)
996996
})
997997

998+
v.Run("VaiOnlySecretFilters", func() {
999+
supportedWithVai := filterTestCases(vaiOnlySecretFilterCases, true)
1000+
v.runSecretFilterTestCases(supportedWithVai)
1001+
})
1002+
9981003
v.Run("PodFilters", func() {
9991004
supportedWithVai := filterTestCases(podFilterTestCases, true)
10001005
v.runPodFilterTestCases(supportedWithVai)

0 commit comments

Comments
 (0)