Skip to content

Commit

Permalink
Do not remove resources which is not managed by piped (#5481)
Browse files Browse the repository at this point in the history
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Jan 17, 2025
1 parent c12c663 commit eba9349
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/app/pipedv1/plugin/kubernetes/provider/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func makeResourceKey(obj *unstructured.Unstructured) ResourceKey {
}

// FindRemoveResources identifies resources that are present in the live state but not in the desired manifests.
// It doesn't return the resources that are not managed by Piped.
func FindRemoveResources(manifests, namespacedLiveResources, clusterScopedLiveResources []Manifest) []ResourceKey {
var (
removeKeys = make([]ResourceKey, 0, len(namespacedLiveResources)+len(clusterScopedLiveResources))
Expand All @@ -107,7 +108,7 @@ func FindRemoveResources(manifests, namespacedLiveResources, clusterScopedLiveRe
}

for _, r := range namespacedLiveResources {
if _, ok := normalizedKeys[r.Key().normalize()]; !ok {
if _, ok := normalizedKeys[r.Key().normalize()]; !ok && r.IsManagedByPiped() {
removeKeys = append(removeKeys, r.Key())
}
}
Expand All @@ -121,7 +122,7 @@ func FindRemoveResources(manifests, namespacedLiveResources, clusterScopedLiveRe
}
for _, r := range clusterScopedLiveResources {
// We don't care about the namespace of the cluster-scoped resources.
if _, ok := normalizedKeys[r.Key().normalize().withoutNamespace()]; !ok {
if _, ok := normalizedKeys[r.Key().normalize().withoutNamespace()]; !ok && r.IsManagedByPiped() {
removeKeys = append(removeKeys, r.Key())
}
}
Expand Down
58 changes: 58 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/provider/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,64 @@ metadata:
},
},
},
{
name: "resources not managed by piped",
manifestsYAML: `
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: default
`,
namespacedLiveResourcesYAML: `
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: default
`,
clusterScopedLiveResourcesYAML: `
apiVersion: v1
kind: Namespace
metadata:
name: test-namespace
`,
expectedRemoveKeys: []ResourceKey{},
},
{
name: "mixed managed and unmanaged resources",
manifestsYAML: `
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: default
annotations:
"pipecd.dev/managed-by": "piped"
`,
namespacedLiveResourcesYAML: `
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: default
annotations:
"pipecd.dev/managed-by": "piped"
---
apiVersion: v1
kind: Secret
metadata:
name: unmanaged-secret
namespace: default
`,
clusterScopedLiveResourcesYAML: `
apiVersion: v1
kind: Namespace
metadata:
name: unmanaged-namespace
`,
expectedRemoveKeys: []ResourceKey{},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit eba9349

Please sign in to comment.