Skip to content

Commit b0bf6b5

Browse files
Add storageClassName in the placementPolicy; get storage capacity from feedback rule
Signed-off-by: souravbiswassanto <[email protected]>
1 parent 4c64012 commit b0bf6b5

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

apis/apps/v1/placementpolicy_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ type ClusterSpreadConstraint struct {
8686
}
8787

8888
type DistributionRule struct {
89-
ClusterName string `json:"clusterName"`
90-
ReplicaIndices []int32 `json:"replicaIndices"`
89+
ClusterName string `json:"clusterName"`
90+
ReplicaIndices []int32 `json:"replicaIndices"`
91+
StorageClassName string `json:"storageClassName,omitempty"`
9192
}
9293

9394
type KubeSliceConfig struct {

crds/apps.k8s.appscode.com_placementpolicies.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ spec:
101101
format: int32
102102
type: integer
103103
type: array
104+
storageClassName:
105+
type: string
104106
required:
105107
- clusterName
106108
- replicaIndices

pkg/controller/petset/pet_manifest_work_control.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (om *realStatefulPodControlObjectManager) CreatePodManifestWork(ctx context
4646
pod.APIVersion = "v1"
4747
pod.Kind = "Pod"
4848
pod.ObjectMeta.GenerateName = ""
49+
pod.ObjectMeta.OwnerReferences = nil
4950
podUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
5051
if err != nil {
5152
return fmt.Errorf("failed to convert pod to unstructured: %w", err)
@@ -337,6 +338,17 @@ func (om *realStatefulPodControlObjectManager) CreateClaimManifestWork(set *api.
337338
Name: claim.Name,
338339
Namespace: claim.Namespace,
339340
},
341+
FeedbackRules: []apiworkv1.FeedbackRule{
342+
{
343+
Type: apiworkv1.JSONPathsType,
344+
JsonPaths: []apiworkv1.JsonPath{
345+
{
346+
Name: "Capacity",
347+
Path: ".status.capacity.storage",
348+
},
349+
},
350+
},
351+
},
340352
UpdateStrategy: &apiworkv1.UpdateStrategy{
341353
Type: apiworkv1.UpdateStrategyTypeServerSideApply,
342354
ServerSideApply: &apiworkv1.ServerSideApplyConfig{
@@ -525,3 +537,19 @@ func getOcmClusterName(pp *api.PlacementPolicy, ordinal int) string {
525537

526538
return clusterName
527539
}
540+
541+
func getStorageClassName(pp *api.PlacementPolicy, ordinal int) string {
542+
if pp == nil || pp.Spec.ClusterSpreadConstraint == nil || pp.Spec.ClusterSpreadConstraint.DistributionRules == nil {
543+
klog.Errorf("can't get storageClassName for distributed petset, ClusterSpreadConstraint is nil")
544+
return ""
545+
}
546+
for i := 0; i < len(pp.Spec.ClusterSpreadConstraint.DistributionRules); i++ {
547+
for j := 0; j < len(pp.Spec.ClusterSpreadConstraint.DistributionRules[i].ReplicaIndices); j++ {
548+
if ordinal == int(pp.Spec.ClusterSpreadConstraint.DistributionRules[i].ReplicaIndices[j]) {
549+
return pp.Spec.ClusterSpreadConstraint.DistributionRules[i].StorageClassName
550+
}
551+
}
552+
}
553+
554+
return ""
555+
}

pkg/controller/petset/pet_pod_control.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ func (spc *StatefulPodControl) createPersistentVolumeClaims(set *api.PetSet, pod
413413
errs = append(errs, err)
414414
}
415415
ordinal, _ := strconv.Atoi(getOrdinalFromResource(claim.Name))
416-
setOCMPlacementForPVC(set, ordinal, &claim, placementPolicy)
416+
setOCMPlacementForPVC(ordinal, &claim, placementPolicy)
417+
setStorageClassNameForPVC(ordinal, &claim, placementPolicy)
417418
}
418419

419420
err = spc.objectMgr.CreateClaim(&claim, set)

pkg/controller/petset/pet_set_utils.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ func setOCMPlacement(set *api.PetSet, ordinal int, pod *v1.Pod, pp *api.Placemen
501501
}
502502
}
503503

504-
func setOCMPlacementForPVC(set *api.PetSet, ordinal int, pvc *v1.PersistentVolumeClaim, placementPolicy *api.PlacementPolicy) {
504+
func setOCMPlacementForPVC(ordinal int, pvc *v1.PersistentVolumeClaim, placementPolicy *api.PlacementPolicy) {
505505
if placementPolicy == nil || placementPolicy.Spec.ClusterSpreadConstraint == nil || placementPolicy.Spec.ClusterSpreadConstraint.DistributionRules == nil {
506506
return
507507
}
@@ -512,6 +512,17 @@ func setOCMPlacementForPVC(set *api.PetSet, ordinal int, pvc *v1.PersistentVolum
512512
pvc.Annotations[api.ManifestWorkClusterNameLabel] = clusterName
513513
}
514514

515+
func setStorageClassNameForPVC(ordinal int, pvc *v1.PersistentVolumeClaim, placementPolicy *api.PlacementPolicy) {
516+
if placementPolicy == nil || placementPolicy.Spec.ClusterSpreadConstraint == nil || placementPolicy.Spec.ClusterSpreadConstraint.DistributionRules == nil {
517+
return
518+
}
519+
storageClassName := getStorageClassName(placementPolicy, ordinal)
520+
if storageClassName != "" {
521+
// overwrite the storageClassName only when it's set in placementPolicy
522+
pvc.Spec.StorageClassName = &storageClassName
523+
}
524+
}
525+
515526
// getPatch returns a strategic merge patch that can be applied to restore a PetSet to a
516527
// previous version. If the returned error is nil the patch is valid. The current state that we save is just the
517528
// PodSpecTemplate. We can modify this later to encompass more state (or less) and remain compatible with previously

0 commit comments

Comments
 (0)