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

kubernetes-sigs / blob-csi-driver / 4961304539

12 May 2023 05:20PM UTC coverage: 80.672%. First build
4961304539

push

github

GitHub
chore(deps): bump golang.org/x/net from 0.9.0 to 0.10.0

1824 of 2261 relevant lines covered (80.67%)

5.29 hits per line

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

96.94
/pkg/util/util.go
1
/*
2
Copyright 2019 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 util
18

19
import (
20
        "fmt"
21
        "os"
22
        "regexp"
23
        "strings"
24
        "sync"
25

26
        "github.com/go-ini/ini"
27
        "github.com/pkg/errors"
28
        "k8s.io/klog/v2"
29
)
30

31
const (
32
        GiB                  = 1024 * 1024 * 1024
33
        TiB                  = 1024 * GiB
34
        tagsDelimiter        = ","
35
        tagKeyValueDelimiter = "="
36
)
37

38
// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB
39
// in the unit of Bytes
40
func RoundUpBytes(volumeSizeBytes int64) int64 {
1✔
41
        return roundUpSize(volumeSizeBytes, GiB) * GiB
1✔
42
}
1✔
43

44
// RoundUpGiB rounds up the volume size in bytes up to multiplications of GiB
45
// in the unit of GiB
46
func RoundUpGiB(volumeSizeBytes int64) int64 {
1✔
47
        return roundUpSize(volumeSizeBytes, GiB)
1✔
48
}
1✔
49

50
// BytesToGiB conversts Bytes to GiB
51
func BytesToGiB(volumeSizeBytes int64) int64 {
1✔
52
        return volumeSizeBytes / GiB
1✔
53
}
1✔
54

55
// GiBToBytes converts GiB to Bytes
56
func GiBToBytes(volumeSizeGiB int64) int64 {
1✔
57
        return volumeSizeGiB * GiB
1✔
58
}
1✔
59

60
// roundUpSize calculates how many allocation units are needed to accommodate
61
// a volume of given size. E.g. when user wants 1500MiB volume, while AWS EBS
62
// allocates volumes in gibibyte-sized chunks,
63
// RoundUpSize(1500 * 1024*1024, 1024*1024*1024) returns '2'
64
// (2 GiB is the smallest allocatable volume that can hold 1500MiB)
65
func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
2✔
66
        roundedUp := volumeSizeBytes / allocationUnitBytes
2✔
67
        if volumeSizeBytes%allocationUnitBytes > 0 {
4✔
68
                roundedUp++
2✔
69
        }
2✔
70
        return roundedUp
2✔
71
}
72

73
// GetMountOptions return options with string list separated by space
74
func GetMountOptions(options []string) string {
4✔
75
        if len(options) == 0 {
5✔
76
                return ""
1✔
77
        }
1✔
78
        str := options[0]
3✔
79
        for i := 1; i < len(options); i++ {
5✔
80
                str = str + " " + options[i]
2✔
81
        }
2✔
82
        return str
3✔
83
}
84

85
func MakeDir(pathname string, perm os.FileMode) error {
1✔
86
        err := os.MkdirAll(pathname, perm)
1✔
87
        if err != nil {
1✔
88
                if !os.IsExist(err) {
×
89
                        return err
×
90
                }
×
91
        }
92
        return nil
1✔
93
}
94

95
// LockMap used to lock on entries
96
type LockMap struct {
97
        sync.Mutex
98
        mutexMap map[string]*sync.Mutex
99
}
100

101
// NewLockMap returns a new lock map
102
func NewLockMap() *LockMap {
4✔
103
        return &LockMap{
4✔
104
                mutexMap: make(map[string]*sync.Mutex),
4✔
105
        }
4✔
106
}
4✔
107

108
// LockEntry acquires a lock associated with the specific entry
109
func (lm *LockMap) LockEntry(entry string) {
5✔
110
        lm.Lock()
5✔
111
        // check if entry does not exists, then add entry
5✔
112
        if _, exists := lm.mutexMap[entry]; !exists {
9✔
113
                lm.addEntry(entry)
4✔
114
        }
4✔
115

116
        lm.Unlock()
5✔
117
        lm.lockEntry(entry)
5✔
118
}
119

120
// UnlockEntry release the lock associated with the specific entry
121
func (lm *LockMap) UnlockEntry(entry string) {
5✔
122
        lm.Lock()
5✔
123
        defer lm.Unlock()
5✔
124

5✔
125
        if _, exists := lm.mutexMap[entry]; !exists {
6✔
126
                return
1✔
127
        }
1✔
128
        lm.unlockEntry(entry)
4✔
129
}
130

131
func (lm *LockMap) addEntry(entry string) {
4✔
132
        lm.mutexMap[entry] = &sync.Mutex{}
4✔
133
}
4✔
134

135
func (lm *LockMap) lockEntry(entry string) {
5✔
136
        lm.mutexMap[entry].Lock()
5✔
137
}
5✔
138

139
func (lm *LockMap) unlockEntry(entry string) {
4✔
140
        lm.mutexMap[entry].Unlock()
4✔
141
}
4✔
142

143
func ConvertTagsToMap(tags string) (map[string]string, error) {
8✔
144
        m := make(map[string]string)
8✔
145
        if tags == "" {
9✔
146
                return m, nil
1✔
147
        }
1✔
148
        s := strings.Split(tags, tagsDelimiter)
7✔
149
        for _, tag := range s {
17✔
150
                kv := strings.Split(tag, tagKeyValueDelimiter)
10✔
151
                if len(kv) != 2 {
12✔
152
                        return nil, fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", tags)
2✔
153
                }
2✔
154
                key := strings.TrimSpace(kv[0])
8✔
155
                if key == "" {
10✔
156
                        return nil, fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", tags)
2✔
157
                }
2✔
158
                value := strings.TrimSpace(kv[1])
6✔
159
                m[key] = value
6✔
160
        }
161
        return m, nil
3✔
162
}
163

164
type OsInfo struct {
165
        Distro  string
166
        Version string
167
}
168

169
const (
170
        keyDistribID      = "DISTRIB_ID"
171
        keyDistribRelease = "DISTRIB_RELEASE"
172
)
173

174
func GetOSInfo(f interface{}) (*OsInfo, error) {
2✔
175
        cfg, err := ini.Load(f)
2✔
176
        if err != nil {
3✔
177
                return nil, errors.Wrapf(err, "failed to read %q", f)
1✔
178
        }
1✔
179

180
        oi := &OsInfo{}
1✔
181
        oi.Distro = cfg.Section("").Key(keyDistribID).String()
1✔
182
        oi.Version = cfg.Section("").Key(keyDistribRelease).String()
1✔
183

1✔
184
        klog.V(2).Infof("get OS info: %v", oi)
1✔
185
        return oi, nil
1✔
186
}
187

188
func TrimDuplicatedSpace(s string) string {
1✔
189
        reg := regexp.MustCompile(`\s+`)
1✔
190
        s = reg.ReplaceAllString(s, " ")
1✔
191
        return s
1✔
192
}
1✔
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

© 2025 Coveralls, Inc