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

feat: resize pvc #1904

Closed
wants to merge 16 commits into from
2 changes: 2 additions & 0 deletions helm/operator/templates/minio.min.io_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,8 @@ spec:
pools:
items:
properties:
additionalStorage:
type: string
affinity:
properties:
nodeAffinity:
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/minio.min.io/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,13 @@ type Pool struct {
// If true. Will delete the storage when tenant has been deleted.
// +optional
ReclaimStorage *bool `json:"reclaimStorage,omitempty"`
// *Optional* +
//
// If set. Operator will try to expansion storage. And statefulset will not restart. We just update the pvc only.
// Examples: 2Gi
// Total = Request + AdditionalStorage
// +optional
AdditionalStorage *string `json:"additionalStorage"`
}

// EqualImage returns true if config image and current input image are same
Expand Down
23 changes: 2 additions & 21 deletions pkg/controller/main-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,26 +818,7 @@ func (c *Controller) syncHandler(key string) (Result, error) {
// Just output the error. Will not retry.
runtime.HandleError(fmt.Errorf("DeletePrometheusAddlConfig '%s/%s' error:%s", namespace, tenantName, err.Error()))
}
// try to delete pvc if set ReclaimStorageLabel:true
pvcList := corev1.PersistentVolumeClaimList{}
listOpt := client.ListOptions{
Namespace: namespace,
}
client.MatchingLabels{
"v1.min.io/tenant": tenantName,
}.ApplyToList(&listOpt)
err := c.k8sClient.List(ctx, &pvcList, &listOpt)
if err != nil {
runtime.HandleError(fmt.Errorf("PersistentVolumeClaimList '%s/%s' error:%s", namespace, tenantName, err.Error()))
}
for _, pvc := range pvcList.Items {
if pvc.Labels[statefulsets.ReclaimStorageLabel] == "true" {
err := c.k8sClient.Delete(ctx, &pvc)
if err != nil {
runtime.HandleError(fmt.Errorf("Delete PersistentVolumeClaim '%s/%s/%s' error:%s", namespace, tenantName, pvc.Name, err.Error()))
}
}
}
c.TryToDeletePVCS(ctx, namespace, tenantName)
return WrapResult(Result{}, nil)
}
// will retry after 5sec
Expand Down Expand Up @@ -1430,7 +1411,7 @@ func (c *Controller) syncHandler(key string) (Result, error) {
if err != nil {
return WrapResult(Result{}, err)
}

c.ResizePVCS(ctx, tenant)
return WrapResult(Result{}, err)
}

Expand Down
89 changes: 89 additions & 0 deletions pkg/controller/pvc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2024, MinIO, Inc.
//
// This code is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License, version 3,
// along with this program. If not, see <http://www.gnu.org/licenses/>

package controller
jiuker marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"fmt"

miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
"github.com/minio/operator/pkg/resources/statefulsets"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// TryToDeletePVCS - try to delete pvc if set ReclaimStorageLabel:true
func (c *Controller) TryToDeletePVCS(ctx context.Context, namespace string, tenantName string) {
pvcList := corev1.PersistentVolumeClaimList{}
listOpt := client.ListOptions{
Namespace: namespace,
}
client.MatchingLabels{
"v1.min.io/tenant": tenantName,
}.ApplyToList(&listOpt)
err := c.k8sClient.List(ctx, &pvcList, &listOpt)
if err != nil {
runtime.HandleError(fmt.Errorf("PersistentVolumeClaimList '%s/%s' error:%s", namespace, tenantName, err.Error()))
}
for _, pvc := range pvcList.Items {
if pvc.Labels[statefulsets.ReclaimStorageLabel] == "true" {
err := c.k8sClient.Delete(ctx, &pvc)
if err != nil {
runtime.HandleError(fmt.Errorf("Delete PersistentVolumeClaim '%s/%s/%s' error:%s", namespace, tenantName, pvc.Name, err.Error()))
}
}
}
}

// ResizePVCS - try to resize pvc to Request+AdditionalStorage if set AdditionalStorage to pool
func (c *Controller) ResizePVCS(ctx context.Context, tenant *miniov2.Tenant) {
for _, pool := range tenant.Spec.Pools {
if pool.AdditionalStorage != nil {
q, err := resource.ParseQuantity(*pool.AdditionalStorage)
if err != nil {
continue
}
storageRequest := pool.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
if storageRequest != nil {
q.Add(*storageRequest)
}
pvcList := corev1.PersistentVolumeClaimList{}
listOpt := client.ListOptions{
Namespace: tenant.Namespace,
}
client.MatchingLabels{
"v1.min.io/tenant": tenant.Name,
"v1.min.io/pool": pool.Name,
}.ApplyToList(&listOpt)
err = c.k8sClient.List(ctx, &pvcList, &listOpt)
if err != nil {
runtime.HandleError(fmt.Errorf("PersistentVolumeClaimList '%s/%s/%s' error:%s", tenant.Namespace, tenant.Name, pool.Name, err.Error()))
}
for _, pvc := range pvcList.Items {
// if already resized with a bigger or equal size, do nothing
if pvc.Spec.Resources.Requests.Storage().Cmp(q) != -1 {
continue
}
pvc.Spec.Resources.Requests[corev1.ResourceStorage] = q
err := c.k8sClient.Update(ctx, &pvc)
if err != nil {
runtime.HandleError(fmt.Errorf("Update PersistentVolumeClaim '%s/%s' to %s error:%s", tenant.Namespace, pvc.Name, q.String(), err.Error()))
}
}
}
}
}
2 changes: 2 additions & 0 deletions resources/base/crds/minio.min.io_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,8 @@ spec:
pools:
items:
properties:
additionalStorage:
type: string
affinity:
properties:
nodeAffinity:
Expand Down
Loading