|
| 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 | +} |
0 commit comments