Skip to content

Commit 7f45cfb

Browse files
committed
Allow the ignored Secret types to be configured by the Helm chart values
Signed-off-by: Richard Wall <[email protected]>
1 parent 6eed6f4 commit 7f45cfb

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

deploy/charts/venafi-kubernetes-agent/templates/configmap.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ data:
8989
resource-type:
9090
version: v1
9191
resource: secrets
92+
{{- with .Values.config.ignoredSecretTypes }}
93+
field-selectors:
94+
{{- range . }}
95+
- type!={{ . }}
96+
{{- end }}
97+
{{- end }}
9298
- kind: "k8s-dynamic"
9399
name: "k8s/certificates"
94100
config:
@@ -202,5 +208,3 @@ data:
202208
version: v1
203209
resource: issuers
204210
{{- end }}
205-
206-

deploy/charts/venafi-kubernetes-agent/values.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ config:
186186
# -- Description for the cluster resource if it needs to be created in Venafi Control Plane
187187
clusterDescription: ""
188188

189+
# -- Reduce the memory usage of the agent and reduce the load on the Kubernetes
190+
# API server by omitting various common Secret types when listing Secrets.
191+
# These Secret types will be added to a "type!=<type>" field selector in the
192+
# agent config.
193+
# * https://docs.venafi.cloud/vaas/k8s-components/t-cfg-tlspk-agent/#configuration
194+
# * https://kubernetes.io/docs/concepts/configuration/secret/#secret-types
195+
# * https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/#list-of-supported-fields
196+
ignoredSecretTypes:
197+
- kubernetes.io/service-account-token
198+
- kubernetes.io/dockercfg
199+
- kubernetes.io/dockerconfigjson
200+
- kubernetes.io/basic-auth
201+
- kubernetes.io/ssh-auth,
202+
- bootstrap.kubernetes.io/token
203+
- helm.sh/release.v1
204+
189205
# -- Specify ConfigMap details to load config from an existing resource.
190206
# This should be blank by default unless you have you own config.
191207
configmap:

examples/one-shot-secret.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ cluster_id: "my_cluster"
1414
period: 1m
1515
data-gatherers:
1616
- kind: "k8s-dynamic"
17-
name: "k8s/secrets.v1"
17+
name: "k8s/secrets"
1818
config:
1919
resource-type:
2020
version: v1
2121
resource: secrets
22+
field-selectors:
23+
- type!=kubernetes.io/service-account-token
24+
- type!=kubernetes.io/dockercfg
25+
- type!=kubernetes.io/dockerconfigjson
26+
- type!=kubernetes.io/basic-auth
27+
- type!=kubernetes.io/ssh-auth,
28+
- type!=bootstrap.kubernetes.io/token
29+
- type!=helm.sh/release.v1

pkg/datagatherer/k8s/dynamic.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type ConfigDynamic struct {
3939
ExcludeNamespaces []string `yaml:"exclude-namespaces"`
4040
// IncludeNamespaces is a list of namespaces to include.
4141
IncludeNamespaces []string `yaml:"include-namespaces"`
42+
// FieldSelectors is a list of field selectors to use when listing this resource
43+
FieldSelectors []string `yaml:"field-selectors"`
4244
}
4345

4446
// UnmarshalYAML unmarshals the ConfigDynamic resolving GroupVersionResource.
@@ -52,6 +54,7 @@ func (c *ConfigDynamic) UnmarshalYAML(unmarshal func(interface{}) error) error {
5254
} `yaml:"resource-type"`
5355
ExcludeNamespaces []string `yaml:"exclude-namespaces"`
5456
IncludeNamespaces []string `yaml:"include-namespaces"`
57+
FieldSelectors []string `yaml:"field-selectors"`
5558
}{}
5659
err := unmarshal(&aux)
5760
if err != nil {
@@ -64,6 +67,7 @@ func (c *ConfigDynamic) UnmarshalYAML(unmarshal func(interface{}) error) error {
6467
c.GroupVersionResource.Resource = aux.ResourceType.Resource
6568
c.ExcludeNamespaces = aux.ExcludeNamespaces
6669
c.IncludeNamespaces = aux.IncludeNamespaces
70+
c.FieldSelectors = aux.FieldSelectors
6771

6872
return nil
6973
}
@@ -79,6 +83,13 @@ func (c *ConfigDynamic) validate() error {
7983
errors = append(errors, "invalid configuration: GroupVersionResource.Resource cannot be empty")
8084
}
8185

86+
for _, selectorString := range c.FieldSelectors {
87+
_, err := fields.ParseSelector(selectorString)
88+
if err != nil {
89+
errors = append(errors, fmt.Sprintf("invalid field selector %q: %s", selectorString, err))
90+
}
91+
}
92+
8293
if len(errors) > 0 {
8394
return fmt.Errorf(strings.Join(errors, ", "))
8495
}
@@ -151,26 +162,10 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
151162
}
152163
// init shared informer for selected namespaces
153164
fieldSelector := generateFieldSelector(c.ExcludeNamespaces)
154-
// Reduce the memory usage and reduce the load on the Kubernetes API server
155-
// by omitting various common Secret types when listing Secrets.
156-
// * https://kubernetes.io/docs/concepts/configuration/secret/#secret-types
157-
//
158-
// It would be better to include only TLS and Opaque Secrets rather than excluding the other types,
159-
// because we can never know all the possible Secret types that a cluster may have,
160-
// but field selectors do not yet support set based operators:
161-
// * https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/#supported-operators
162-
// * https://github.com/kubernetes/kubernetes/issues/32946
163-
if c.GroupVersionResource.Group == "" && c.GroupVersionResource.Version == "v1" && c.GroupVersionResource.Resource == "secrets" {
164-
fieldSelector = fields.AndSelectors(
165-
fieldSelector,
166-
fields.OneTermNotEqualSelector("type", "kubernetes.io/service-account-token"),
167-
fields.OneTermNotEqualSelector("type", "kubernetes.io/dockercfg"),
168-
fields.OneTermNotEqualSelector("type", "kubernetes.io/dockerconfigjson"),
169-
fields.OneTermNotEqualSelector("type", "kubernetes.io/basic-auth"),
170-
fields.OneTermNotEqualSelector("type", "kubernetes.io/ssh-auth"),
171-
fields.OneTermNotEqualSelector("type", "bootstrap.kubernetes.io/token"),
172-
fields.OneTermNotEqualSelector("type", "helm.sh/release.v1"),
173-
)
165+
166+
// add any custom field selectors to the namespace selector
167+
for _, selectorString := range c.FieldSelectors {
168+
fieldSelector = fields.AndSelectors(fieldSelector, fields.ParseSelectorOrDie(selectorString))
174169
}
175170

176171
// init cache to store gathered resources

0 commit comments

Comments
 (0)