Skip to content

Commit

Permalink
Merge pull request #5688 from seanlaii/automated-cherry-pick-of-#5568…
Browse files Browse the repository at this point in the history
…-upstream-release-1.9

Automated cherry pick of #5568: Avoid treating PVC managed by VolumeClaimTemplate as dependencies
  • Loading branch information
karmada-bot authored Oct 14, 2024
2 parents 0806039 + 9731f6c commit 6b00f7b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/resourceinterpreter/default/native/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
mcsv1alpha1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
Expand Down Expand Up @@ -132,7 +133,36 @@ func getStatefulSetDependencies(object *unstructured.Unstructured) ([]configv1al
return nil, err
}

return helper.GetDependenciesFromPodTemplate(podObj)
deps, err := helper.GetDependenciesFromPodTemplate(podObj)
if err != nil {
return nil, err
}

if len(statefulSetObj.Spec.VolumeClaimTemplates) == 0 {
return deps, nil
}

// ignore the PersistentVolumeClaim dependency if it was created by the StatefulSet VolumeClaimTemplates
// the PVC dependency is not needed because the StatefulSet will manage the pvcs in the member cluster,
// if it exists here it was just a placeholder not a real PVC
var validDeps []configv1alpha1.DependentObjectReference
volumeClaimTemplateNames := sets.Set[string]{}
for i := range statefulSetObj.Spec.VolumeClaimTemplates {
volumeClaimTemplateNames.Insert(statefulSetObj.Spec.VolumeClaimTemplates[i].Name)
}

for i := range deps {
if deps[i].Kind != util.PersistentVolumeClaimKind {
validDeps = append(validDeps, deps[i])
continue
}
if volumeClaimTemplateNames.Has(deps[i].Name) {
continue
}
validDeps = append(validDeps, deps[i])
}

return validDeps, nil
}

func getIngressDependencies(object *unstructured.Unstructured) ([]configv1alpha1.DependentObjectReference, error) {
Expand Down
33 changes: 33 additions & 0 deletions pkg/resourceinterpreter/default/native/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,39 @@ func Test_getStatefulSetDependencies(t *testing.T) {
want: testPairs[2].dependentObjectReference,
wantErr: false,
},
{
name: "statefulset with partial dependencies 4",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "StatefulSet",
"metadata": map[string]interface{}{
"name": "fake-statefulset",
"namespace": namespace,
},
"spec": map[string]interface{}{
"serviceName": "fake-service",
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"app": "fake",
},
},
"template": map[string]interface{}{
"spec": testPairs[0].podSpecsWithDependencies.Object,
},
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
"name": "test-pvc",
},
},
},
},
},
},
want: testPairs[0].dependentObjectReference[:3], // remove the pvc dependency because it was found in the volumeClaimTemplates
wantErr: false,
},
}

for i := range tests {
Expand Down

0 comments on commit 6b00f7b

Please sign in to comment.