-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement functions to find referencing ConfigMaps and Secrets in Kubernetes manifests #5409
Conversation
…ernetes manifests Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is mainly copied from below
func TestFindReferencingConfigMapsInDeployment(t *testing.T) { |
} | ||
} | ||
|
||
func TestFindReferencingSecrets(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test is mainly copied from below
func TestFindReferencingSecretsInDeployment(t *testing.T) { |
// - 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented this function with the reference below.
func FindReferencingConfigMapsInDeployment(d *appsv1.Deployment) []string { |
// - 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented this function with the reference below.
func FindReferencingSecretsInDeployment(d *appsv1.Deployment) []string { |
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🙆♂️
What this PR does:
as title
Why we need it:
I want to implement
annotateConfigHash
in 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