1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00
kube-arangodb/pkg/storage/pv_inspector.go
2021-01-08 15:35:38 +01:00

78 lines
2.3 KiB
Go

//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Ewout Prangsma
//
package storage
import (
"time"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// inspectPVs queries all PersistentVolume's and triggers a cleanup for
// released volumes.
// Returns the number of available PV's.
func (ls *LocalStorage) inspectPVs() (int, error) {
log := ls.deps.Log
list, err := ls.deps.KubeCli.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
if err != nil {
return 0, errors.WithStack(err)
}
spec := ls.apiObject.Spec
availableVolumes := 0
cleanupBeforeTimestamp := time.Now().Add(time.Hour * -24)
for _, pv := range list.Items {
if pv.Spec.StorageClassName != spec.StorageClass.Name {
// Not our storage class
continue
}
switch pv.Status.Phase {
case v1.VolumeAvailable:
// Is this an old volume?
if pv.GetObjectMeta().GetCreationTimestamp().Time.Before(cleanupBeforeTimestamp) {
// Let's clean it up
if ls.isOwnerOf(&pv) {
// Cleanup this volume
log.Debug().Str("name", pv.GetName()).Msg("Added PersistentVolume to cleaner")
ls.pvCleaner.Add(pv)
} else {
log.Debug().Str("name", pv.GetName()).Msg("PersistentVolume is not owned by us")
availableVolumes++
}
} else {
availableVolumes++
}
case v1.VolumeReleased:
if ls.isOwnerOf(&pv) {
// Cleanup this volume
log.Debug().Str("name", pv.GetName()).Msg("Added PersistentVolume to cleaner")
ls.pvCleaner.Add(pv)
} else {
log.Debug().Str("name", pv.GetName()).Msg("PersistentVolume is not owned by us")
}
}
}
return availableVolumes, nil
}