Implement functions to find referencing ConfigMaps and Secrets in Kubernetes manifests#5409
Implement functions to find referencing ConfigMaps and Secrets in Kubernetes manifests#5409
Conversation
…ernetes manifests Signed-off-by: Shinnosuke Sawada-Dazai <shin@warashi.dev>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5409 +/- ##
==========================================
+ Coverage 25.74% 25.81% +0.06%
==========================================
Files 449 450 +1
Lines 48452 48520 +68
==========================================
+ Hits 12474 12524 +50
- Misses 35005 35019 +14
- Partials 973 977 +4 ☔ View full report in Codecov by Sentry. |
| } | ||
| } | ||
|
|
||
| func TestFindReferencingConfigMaps(t *testing.T) { |
There was a problem hiding this comment.
This test is mainly copied from below
| } | ||
| } | ||
|
|
||
| func TestFindReferencingSecrets(t *testing.T) { |
There was a problem hiding this comment.
this test is mainly copied from below
| // - spec.template.spec.initContainers.envFrom.configMapRef.name | ||
| // - spec.template.spec.containers.env.valueFrom.configMapKeyRef.name | ||
| // - spec.template.spec.containers.envFrom.configMapRef.name | ||
| func FindReferencingConfigMaps(m *unstructured.Unstructured) []string { |
There was a problem hiding this comment.
I implemented this function with the reference below.
| // - spec.template.spec.initContainers.envFrom.secretRef.name | ||
| // - spec.template.spec.containers.env.valueFrom.secretKeyRef.name | ||
| // - spec.template.spec.containers.envFrom.secretRef.name | ||
| func FindReferencingSecrets(m *unstructured.Unstructured) []string { |
There was a problem hiding this comment.
I implemented this function with the reference below.
| // nestedStringSlice extracts a string slice from the given object by following the fields. | ||
| // It returns the extracted string slice. | ||
| // If there is []map[string]any in the middle of the fields, it will be flattened. | ||
| func nestedStringSlice(obj any, fields ...string) []string { |
There was a problem hiding this comment.
I don't want to convert the unstructured.Unstructured to appsv1.Deployment or something, so I implemented this.
We cannot use unstructured.NestedStringSlice because it handles the []map[string]any differently.
There was a problem hiding this comment.
@Warashi
[Ask]
We cannot use unstructured.NestedStringSlice because it handles the []map[string]any differently.
What's the difference between unstructured.NestedStringSlice and nestedStringSlice ?
There was a problem hiding this comment.
@ffjlabo
unstructured.NestedStringSlice returns an error when there is []map[string]any in the middle of the fields. On the other hand, nestedStringSlice flattens it.
There was a problem hiding this comment.
@Warashi
I got your point. I tried to use unstructured.NestedStringSlice with the data below.
We can't find and flatten slices in the []interface{}{map[string]interface{}{}}.
obj := map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"volumes": []interface{}{
map[string]interface{}{
"configMap": map[string]interface{}{
"name": "canary-by-config-change",
},
},
},
},
},
},
}
res, exists, err := unstructured.NestedStringSlice(obj, "spec", "template", "spec", "volumes[]", "configMap", "name")
assert.True(t, exists)
assert.NoError(t, err)
assert.Equal(t, res, []string{"canary-by-config-change"})
=== RUN Test
Error: Should be true
Error: Received unexpected error:
.spec.template.spec.volumes.configMap accessor error: [map[configMap:map[name:canary-by-config-change]]] is of the type []interface {}, expected map[string]interface{}
Error: Not equal:
expected: []string(nil)
actual : []string{"canary-by-config-change"}
Diff:
--- Expected
+++ Actual
@@ -1,2 +1,4 @@
-([]string) <nil>
+([]string) (len=1) {
+ (string) (len=23) "canary-by-config-change"
+}
Test: Test
The test for unstructured.NestedFieldNoCopy (used in the unstructured.NestedStringSlice) shows it.
https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/unstructured/helpers_test.go#L122
What this PR does:
as title
Why we need it:
I want to implement
annotateConfigHashin the k8s plugin.To implement this, we need to find the configmap and secrets referenced in the deployments.
pipecd/pkg/app/piped/executor/kubernetes/kubernetes.go
Lines 582 to 587 in e276b89
Which issue(s) this PR fixes:
Part of #4980
Does this PR introduce a user-facing change?: No