• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / sig-storage-local-static-provisioner / 14219133648

02 Apr 2025 12:04PM UTC coverage: 51.195% (-2.4%) from 53.609%
14219133648

push

github

web-flow
Merge pull request #487 from andyzhangx/go1.24

chore: use go1.24

1 of 1 new or added line in 1 file covered. (100.0%)

45 existing lines in 1 file now uncovered.

964 of 1883 relevant lines covered (51.19%)

7.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/cache/cache.go
1
/*
2
Copyright 2017 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package cache
18

19
import (
20
        "sync"
21

22
        "k8s.io/klog/v2"
23

24
        v1 "k8s.io/api/core/v1"
25
)
26

27
// VolumeCache keeps all the PersistentVolumes that have been created by this provisioner.
28
// It is periodically updated by the Populator.
29
// The Deleter and Discoverer use the VolumeCache to check on created PVs
30
type VolumeCache struct {
31
        mutex sync.Mutex
32
        pvs   map[string]*v1.PersistentVolume
33
}
34

35
// NewVolumeCache creates a new PV cache object for storing PVs created by this provisioner.
UNCOV
36
func NewVolumeCache() *VolumeCache {
×
UNCOV
37
        return &VolumeCache{pvs: map[string]*v1.PersistentVolume{}}
×
UNCOV
38
}
×
39

40
// GetPV returns the PV object given the PV name
UNCOV
41
func (cache *VolumeCache) GetPV(pvName string) (*v1.PersistentVolume, bool) {
×
UNCOV
42
        cache.mutex.Lock()
×
UNCOV
43
        defer cache.mutex.Unlock()
×
UNCOV
44

×
UNCOV
45
        pv, exists := cache.pvs[pvName]
×
UNCOV
46
        return pv, exists
×
UNCOV
47
}
×
48

49
// AddPV adds the PV object to the cache
UNCOV
50
func (cache *VolumeCache) AddPV(pv *v1.PersistentVolume) {
×
UNCOV
51
        cache.mutex.Lock()
×
UNCOV
52
        defer cache.mutex.Unlock()
×
UNCOV
53

×
UNCOV
54
        cache.pvs[pv.Name] = pv
×
UNCOV
55
        klog.Infof("Added pv %q to cache", pv.Name)
×
UNCOV
56
}
×
57

58
// UpdatePV updates the PV object in the cache
59
func (cache *VolumeCache) UpdatePV(pv *v1.PersistentVolume) {
×
60
        cache.mutex.Lock()
×
61
        defer cache.mutex.Unlock()
×
62

×
63
        cache.pvs[pv.Name] = pv
×
64
        klog.Infof("Updated pv %q to cache", pv.Name)
×
65
}
×
66

67
// DeletePV deletes the PV object from the cache
UNCOV
68
func (cache *VolumeCache) DeletePV(pvName string) {
×
UNCOV
69
        cache.mutex.Lock()
×
UNCOV
70
        defer cache.mutex.Unlock()
×
UNCOV
71

×
UNCOV
72
        delete(cache.pvs, pvName)
×
UNCOV
73
        klog.Infof("Deleted pv %q from cache", pvName)
×
UNCOV
74
}
×
75

76
// ListPVs returns a list of all the PVs in the cache
UNCOV
77
func (cache *VolumeCache) ListPVs() []*v1.PersistentVolume {
×
UNCOV
78
        cache.mutex.Lock()
×
UNCOV
79
        defer cache.mutex.Unlock()
×
UNCOV
80

×
UNCOV
81
        pvs := []*v1.PersistentVolume{}
×
UNCOV
82
        for _, pv := range cache.pvs {
×
UNCOV
83
                pvs = append(pvs, pv)
×
UNCOV
84
        }
×
UNCOV
85
        return pvs
×
86
}
87

88
// LookupPVsByPath returns a list of all of the PVs in the cache with a given local path
89
// Note: The PVs in the cache have been filtered by the populator so that the cache only
90
// contains volumes created by the local-static-provisioner running on this knode.
UNCOV
91
func (cache *VolumeCache) LookupPVsByPath(filePath string) []string {
×
UNCOV
92
        cache.mutex.Lock()
×
UNCOV
93
        defer cache.mutex.Unlock()
×
UNCOV
94
        var matches = make([]string, 0, 1)
×
UNCOV
95

×
UNCOV
96
        for _, pv := range cache.pvs {
×
UNCOV
97
                lvs := pv.Spec.Local
×
UNCOV
98
                if lvs != nil {
×
UNCOV
99
                        if lvs.Path == filePath {
×
UNCOV
100
                                matches = append(matches, pv.ObjectMeta.Name)
×
UNCOV
101
                        }
×
102
                }
103
        }
104

UNCOV
105
        return matches
×
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc