Skip to content

Commit

Permalink
create multiple snapshot for same group
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvindthiru committed Dec 19, 2023
1 parent a7ba7a1 commit c35c8e1
Showing 1 changed file with 81 additions and 28 deletions.
109 changes: 81 additions & 28 deletions pkg/controllers/clusterresourceplacement/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"go.goms.io/fleet/pkg/utils/labels"
)

const TotalResourcesSize int = 1024 * (1 << 10) // 1 MB

func (r *Reconciler) Reconcile(ctx context.Context, key controller.QueueKey) (ctrl.Result, error) {
name, ok := key.(string)
if !ok {
Expand Down Expand Up @@ -406,6 +408,7 @@ func (r *Reconciler) deleteRedundantResourceSnapshots(ctx context.Context, crp *

// TODO handle all the resources selected by placement larger than 1MB size limit of k8s objects.
func (r *Reconciler) getOrCreateClusterResourceSnapshot(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, envelopeObjCount int, resourceSnapshotSpec *fleetv1beta1.ResourceSnapshotSpec, revisionHistoryLimit int) (*fleetv1beta1.ClusterResourceSnapshot, error) {
klog.V(2).InfoS("here in getOrCreateClusterResourceSnapshot")
resourceHash, err := generateResourceHash(resourceSnapshotSpec)
crpKObj := klog.KObj(crp)
if err != nil {
Expand Down Expand Up @@ -455,38 +458,88 @@ func (r *Reconciler) getOrCreateClusterResourceSnapshot(ctx context.Context, crp
return nil, err
}

// create a new resource snapshot
// create new resource snapshots
selectedResourcesList := splitSelectedResources(resourceSnapshotSpec.SelectedResources)
klog.V(2).InfoS("selectedResourcesList", "length", len(selectedResourcesList))
latestResourceSnapshotIndex++
latestResourceSnapshot = &fleetv1beta1.ClusterResourceSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(fleetv1beta1.ResourceSnapshotNameFmt, crp.Name, latestResourceSnapshotIndex),
Labels: map[string]string{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
fleetv1beta1.ResourceIndexLabel: strconv.Itoa(latestResourceSnapshotIndex),
},
Annotations: map[string]string{
fleetv1beta1.ResourceGroupHashAnnotation: resourceHash,
// TODO: need to update this once we support multiple snapshots
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "1",
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: strconv.Itoa(envelopeObjCount),
},
},
Spec: *resourceSnapshotSpec,
}
resourceSnapshotKObj := klog.KObj(latestResourceSnapshot)
if err := controllerutil.SetControllerReference(crp, latestResourceSnapshot, r.Scheme); err != nil {
klog.ErrorS(err, "Failed to set owner reference", "clusterResourceSnapshot", resourceSnapshotKObj)
// should never happen
return nil, controller.NewUnexpectedBehaviorError(err)
var resourceSnapshot *fleetv1beta1.ClusterResourceSnapshot
resourceSnapshotSubIndex := 0
for i, _ := range selectedResourcesList {

Check failure on line 467 in pkg/controllers/clusterresourceplacement/controller.go

View workflow job for this annotation

GitHub Actions / staticcheck

unnecessary assignment to the blank identifier (S1005)
klog.V(2).InfoS("here inside loop")
if i == 0 {
resourceSnapshot = &fleetv1beta1.ClusterResourceSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(fleetv1beta1.ResourceSnapshotNameFmt, crp.Name, latestResourceSnapshotIndex),
Labels: map[string]string{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
fleetv1beta1.ResourceIndexLabel: strconv.Itoa(latestResourceSnapshotIndex),
},
Annotations: map[string]string{
fleetv1beta1.ResourceGroupHashAnnotation: resourceHash,
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: strconv.Itoa(len(selectedResourcesList)),
fleetv1beta1.NumberOfEnvelopedObjectsAnnotation: strconv.Itoa(envelopeObjCount),
},
},
Spec: fleetv1beta1.ResourceSnapshotSpec{
SelectedResources: selectedResourcesList[i],
},
}
latestResourceSnapshot = resourceSnapshot
} else {
resourceSnapshotSubIndex++
resourceSnapshot = &fleetv1beta1.ClusterResourceSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(fleetv1beta1.ResourceSnapshotNameWithSubindexFmt, crp.Name, latestResourceSnapshotIndex, resourceSnapshotSubIndex),
Labels: map[string]string{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.ResourceIndexLabel: strconv.Itoa(latestResourceSnapshotIndex),
},
Annotations: map[string]string{
fleetv1beta1.SubindexOfResourceSnapshotAnnotation: strconv.Itoa(resourceSnapshotSubIndex),
},
},
Spec: fleetv1beta1.ResourceSnapshotSpec{
SelectedResources: selectedResourcesList[i],
},
}
}
resourceSnapshotKObj := klog.KObj(resourceSnapshot)
if err := controllerutil.SetControllerReference(crp, resourceSnapshot, r.Scheme); err != nil {
klog.ErrorS(err, "Failed to set owner reference", "clusterResourceSnapshot", resourceSnapshotKObj)
// should never happen
return nil, controller.NewUnexpectedBehaviorError(err)
}
// should we check to see if object already exists ?.
if err := r.Client.Create(ctx, resourceSnapshot); err != nil {
klog.ErrorS(err, "Failed to create new clusterResourceSnapshot", "clusterResourceSnapshot", resourceSnapshotKObj)
return nil, controller.NewAPIServerError(false, err)
}
klog.V(2).InfoS("Created new clusterResourceSnapshot", "clusterResourcePlacement", klog.KObj(crp), "clusterResourcePolicySnapshot", resourceSnapshotKObj)
}
return latestResourceSnapshot, nil
}

if err := r.Client.Create(ctx, latestResourceSnapshot); err != nil {
klog.ErrorS(err, "Failed to create new clusterResourceSnapshot", "clusterResourceSnapshot", resourceSnapshotKObj)
return nil, controller.NewAPIServerError(false, err)
func splitSelectedResources(selectedResources []fleetv1beta1.ResourceContent) [][]fleetv1beta1.ResourceContent {
klog.V(2).InfoS("here in splitSelectedResources")
var selectedResourcesList [][]fleetv1beta1.ResourceContent
i := 0
for i < len(selectedResources) {
j := i
currentSize := 0
var snapshotResources []fleetv1beta1.ResourceContent
for j < len(selectedResources) {
currentSize += len(selectedResources[j].Raw)
if currentSize > TotalResourcesSize {
break
}
snapshotResources = append(snapshotResources, selectedResources[j])
j++
}
selectedResourcesList = append(selectedResourcesList, snapshotResources)
i = j
}
klog.V(2).InfoS("Created new clusterResourceSnapshot", "clusterResourcePlacement", klog.KObj(crp), "clusterSchedulingPolicySnapshot", resourceSnapshotKObj)
return latestResourceSnapshot, nil
return selectedResourcesList
}

// ensureLatestPolicySnapshot ensures the latest policySnapshot has the isLatest label and the numberOfClusters are updated.
Expand Down

0 comments on commit c35c8e1

Please sign in to comment.