2018-03-06 09:54:12 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2022-01-10 11:35:49 +00:00
|
|
|
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
|
2018-03-06 09:54:12 +00:00
|
|
|
//
|
|
|
|
// 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 storage
|
|
|
|
|
|
|
|
import (
|
2021-03-23 15:47:28 +00:00
|
|
|
"context"
|
|
|
|
|
2022-06-30 18:39:07 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-07-11 11:49:47 +00:00
|
|
|
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
2018-03-06 09:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// inspectPVCs queries all PVC's and checks if there is a need to
|
|
|
|
// build new persistent volumes.
|
|
|
|
// Returns the PVC's that need a volume.
|
2022-06-30 18:39:07 +00:00
|
|
|
func (ls *LocalStorage) inspectPVCs() ([]core.PersistentVolumeClaim, error) {
|
2018-03-06 09:54:12 +00:00
|
|
|
ns := ls.apiObject.GetNamespace()
|
2022-06-30 18:39:07 +00:00
|
|
|
list, err := ls.deps.Client.Kubernetes().CoreV1().PersistentVolumeClaims(ns).List(context.Background(), meta.ListOptions{})
|
2018-03-06 09:54:12 +00:00
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-03-06 09:54:12 +00:00
|
|
|
}
|
|
|
|
spec := ls.apiObject.Spec
|
2022-06-30 18:39:07 +00:00
|
|
|
var result []core.PersistentVolumeClaim
|
2018-03-06 09:54:12 +00:00
|
|
|
for _, pvc := range list.Items {
|
|
|
|
if !pvcMatchesStorageClass(pvc, spec.StorageClass.Name, spec.StorageClass.IsDefault) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !pvcNeedsVolume(pvc) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, pvc)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pvcMatchesStorageClass checks if the given pvc requests a volume
|
|
|
|
// of the given storage class.
|
2022-06-30 18:39:07 +00:00
|
|
|
func pvcMatchesStorageClass(pvc core.PersistentVolumeClaim, storageClassName string, isDefault bool) bool {
|
2018-03-06 09:54:12 +00:00
|
|
|
scn := pvc.Spec.StorageClassName
|
|
|
|
if scn == nil {
|
|
|
|
// No storage class specified, default is used
|
|
|
|
return isDefault
|
|
|
|
}
|
|
|
|
return *scn == storageClassName
|
|
|
|
}
|
|
|
|
|
|
|
|
// pvcNeedsVolume checks if the given pvc is in need of a persistent volume.
|
2022-06-30 18:39:07 +00:00
|
|
|
func pvcNeedsVolume(pvc core.PersistentVolumeClaim) bool {
|
|
|
|
return pvc.Status.Phase == core.ClaimPending
|
2018-03-06 09:54:12 +00:00
|
|
|
}
|