mirror of
https://github.com/arangodb/kube-arangodb.git
synced 2024-12-15 17:51:03 +00:00
109 lines
3.5 KiB
Go
109 lines
3.5 KiB
Go
//
|
|
// DISCLAIMER
|
|
//
|
|
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
|
//
|
|
|
|
package k8sutil
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/constants"
|
|
persistentvolumeclaimv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim/v1"
|
|
)
|
|
|
|
// IsPersistentVolumeClaimMarkedForDeletion returns true if the pvc has been marked for deletion.
|
|
func IsPersistentVolumeClaimMarkedForDeletion(pvc *v1.PersistentVolumeClaim) bool {
|
|
return pvc.DeletionTimestamp != nil
|
|
}
|
|
|
|
// IsPersistentVolumeClaimFileSystemResizePending returns true if the pvc has FileSystemResizePending set to true
|
|
func IsPersistentVolumeClaimFileSystemResizePending(pvc *v1.PersistentVolumeClaim) bool {
|
|
for _, c := range pvc.Status.Conditions {
|
|
if c.Type == v1.PersistentVolumeClaimFileSystemResizePending && c.Status == v1.ConditionTrue {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ExtractStorageResourceRequirement filters resource requirements for Pods.
|
|
func ExtractStorageResourceRequirement(resources v1.ResourceRequirements) v1.ResourceRequirements {
|
|
|
|
filterStorage := func(list v1.ResourceList) v1.ResourceList {
|
|
newlist := make(v1.ResourceList)
|
|
for k, v := range list {
|
|
if k != v1.ResourceStorage && k != "iops" {
|
|
continue
|
|
}
|
|
newlist[k] = v
|
|
}
|
|
return newlist
|
|
}
|
|
|
|
return v1.ResourceRequirements{
|
|
Limits: filterStorage(resources.Limits),
|
|
Requests: filterStorage(resources.Requests),
|
|
}
|
|
}
|
|
|
|
// CreatePersistentVolumeClaim creates a persistent volume claim with given name and configuration.
|
|
// If the pvc already exists, nil is returned.
|
|
// If another error occurs, that error is returned.
|
|
func CreatePersistentVolumeClaim(ctx context.Context, pvcs persistentvolumeclaimv1.ModInterface, pvcName, deploymentName,
|
|
storageClassName, role string, enforceAntiAffinity bool, resources v1.ResourceRequirements,
|
|
vct *v1.PersistentVolumeClaim, finalizers []string, owner metav1.OwnerReference) error {
|
|
labels := LabelsForDeployment(deploymentName, role)
|
|
volumeMode := v1.PersistentVolumeFilesystem
|
|
pvc := &v1.PersistentVolumeClaim{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: pvcName,
|
|
Labels: labels,
|
|
Finalizers: finalizers,
|
|
Annotations: map[string]string{
|
|
constants.AnnotationEnforceAntiAffinity: strconv.FormatBool(enforceAntiAffinity),
|
|
},
|
|
},
|
|
}
|
|
if vct == nil {
|
|
pvc.Spec = v1.PersistentVolumeClaimSpec{
|
|
AccessModes: []v1.PersistentVolumeAccessMode{
|
|
v1.ReadWriteOnce,
|
|
},
|
|
VolumeMode: &volumeMode,
|
|
Resources: ExtractStorageResourceRequirement(resources),
|
|
}
|
|
} else {
|
|
pvc.Spec = vct.Spec
|
|
}
|
|
|
|
if storageClassName != "" {
|
|
pvc.Spec.StorageClassName = &storageClassName
|
|
}
|
|
AddOwnerRefToObject(pvc.GetObjectMeta(), &owner)
|
|
if _, err := pvcs.Create(ctx, pvc, metav1.CreateOptions{}); err != nil && !IsAlreadyExists(err) {
|
|
return errors.WithStack(err)
|
|
}
|
|
return nil
|
|
}
|