Skip to content
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

Automated cherry pick of #5568: Avoid treating PVC managed by VolumeClaimTemplate as dependencies #5686

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -861,6 +861,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