Skip to content

Commit

Permalink
Add function to find ConfigMaps and Secrets in manifests (#5319)
Browse files Browse the repository at this point in the history
* Add function to find ConfigMaps and Secrets in manifests

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

* Rename findConfigs to findConfigsAndSecrets and add a comment

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>

---------

Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Nov 13, 2024
1 parent 090a6c9 commit 1763f92
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/determine.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,17 @@ func findUpdatedWorkloads(olds, news []provider.Manifest) []workloadPair {
}
return pairs
}

// findConfigsAndSecrets returns the manifests that are ConfigMap or Secret.
func findConfigsAndSecrets(manifests []provider.Manifest) map[provider.ResourceKey]provider.Manifest {
configs := make(map[provider.ResourceKey]provider.Manifest)
for _, m := range manifests {
if m.Key.IsConfigMap() {
configs[m.Key] = m
}
if m.Key.IsSecret() {
configs[m.Key] = m
}
}
return configs
}
215 changes: 215 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/determine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,3 +849,218 @@ spec:
})
}
}
func TestFindConfigs(t *testing.T) {
tests := []struct {
name string
manifests []string
want map[provider.ResourceKey]provider.Manifest
}{
{
name: "find ConfigMap and Secret",
manifests: []string{
`
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: default
data:
key: value
`,
`
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: default
data:
key: dmFsdWU=
`,
`
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.19.3
`,
},
want: map[provider.ResourceKey]provider.Manifest{
{
APIVersion: "v1",
Kind: "ConfigMap",
Name: "my-config",
Namespace: "default",
}: mustParseManifests(t, `
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: default
data:
key: value
`)[0],
{
APIVersion: "v1",
Kind: "Secret",
Name: "my-secret",
Namespace: "default",
}: mustParseManifests(t, `
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: default
data:
key: dmFsdWU=
`)[0],
},
},
{
name: "no ConfigMap or Secret",
manifests: []string{
`
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.19.3
`,
},
want: map[provider.ResourceKey]provider.Manifest{},
},
{
name: "only ConfigMap",
manifests: []string{
`
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: default
data:
key: value
`,
},
want: map[provider.ResourceKey]provider.Manifest{
{
APIVersion: "v1",
Kind: "ConfigMap",
Name: "my-config",
Namespace: "default",
}: mustParseManifests(t, `
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: default
data:
key: value
`)[0],
},
},
{
name: "only Secret",
manifests: []string{
`
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: default
data:
key: dmFsdWU=
`,
},
want: map[provider.ResourceKey]provider.Manifest{
{
APIVersion: "v1",
Kind: "Secret",
Name: "my-secret",
Namespace: "default",
}: mustParseManifests(t, `
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: default
data:
key: dmFsdWU=
`)[0],
},
},
{
name: "non-default namespace",
manifests: []string{
`
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: custom-namespace
data:
key: value
`,
`
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: custom-namespace
data:
key: dmFsdWU=
`,
},
want: map[provider.ResourceKey]provider.Manifest{
{
APIVersion: "v1",
Kind: "ConfigMap",
Name: "my-config",
Namespace: "custom-namespace",
}: mustParseManifests(t, `
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: custom-namespace
data:
key: value
`)[0],
{
APIVersion: "v1",
Kind: "Secret",
Name: "my-secret",
Namespace: "custom-namespace",
}: mustParseManifests(t, `
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: custom-namespace
data:
key: dmFsdWU=
`)[0],
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var manifests []provider.Manifest
for _, data := range tt.manifests {
manifests = append(manifests, mustParseManifests(t, data)...)
}
got := findConfigsAndSecrets(manifests)
assert.Equal(t, tt.want, got)
})
}
}
11 changes: 11 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/provider/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var builtInAPIVersions = map[string]struct{}{
const (
KindDeployment = "Deployment"
KindSecret = "Secret"
KindConfigMap = "ConfigMap"

DefaultNamespace = "default"
)
Expand Down Expand Up @@ -89,6 +90,16 @@ func MakeResourceKey(obj *unstructured.Unstructured) ResourceKey {
return k
}

func (k ResourceKey) IsConfigMap() bool {
if k.Kind != KindConfigMap {
return false
}
if !IsKubernetesBuiltInResource(k.APIVersion) {
return false
}
return true
}

func (k ResourceKey) IsSecret() bool {
if k.Kind != KindSecret {
return false
Expand Down

0 comments on commit 1763f92

Please sign in to comment.