Skip to content

Commit be026d3

Browse files
authored
Merge pull request #628 from fmount/annotations
Add more annotations related functions
2 parents 7cb2f32 + 4f3c49e commit be026d3

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

modules/common/annotations/network.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package annotations
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"strconv"
2223
)
2324

2425
// NetworkAttachmentAnnot pod annotation for network-attachment-definition
@@ -54,3 +55,28 @@ func GetNADAnnotation(namespace string, nads []string) (map[string]string, error
5455

5556
return map[string]string{NetworkAttachmentAnnot: string(networks)}, nil
5657
}
58+
59+
// GetBoolFromAnnotation - it returns a boolean from a string annotation
60+
// e.g. glance.openstack.org/wsgi: "true" returns true as a boolean type
61+
//
62+
// Cases covered by this function:
63+
// 1. the annotation does not exist -> false, false, nil
64+
// 2. the annotation exist and is not a valid boolean -> false, true, error
65+
// 3. the annotation exists and is a valid False bool -> false, true, nil
66+
// 4. the annotation exists and is a valid True bool -> true, true, nil
67+
func GetBoolFromAnnotation(
68+
ann map[string]string,
69+
key string,
70+
) (bool, bool, error) {
71+
// Try to get the value associated to the annotation key
72+
value, exists := ann[key]
73+
if !exists {
74+
return false, false, nil
75+
}
76+
result, err := strconv.ParseBool(value)
77+
if err != nil {
78+
// the annotation is not a valid boolean, return an error
79+
return false, exists, err
80+
}
81+
return result, exists, nil
82+
}

modules/common/annotations/network_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ limitations under the License.
1717
package annotations
1818

1919
import (
20-
"testing"
21-
2220
. "github.com/onsi/gomega"
21+
"testing"
2322
)
2423

2524
func TestGetNADAnnotation(t *testing.T) {
@@ -61,3 +60,42 @@ func TestGetNADAnnotation(t *testing.T) {
6160
})
6261
}
6362
}
63+
64+
func TestGetBoolFromAnnotation(t *testing.T) {
65+
ann := map[string]string{}
66+
testKey := "service.example.org/key"
67+
var value bool
68+
var exists bool
69+
var err error
70+
71+
t.Run("", func(t *testing.T) {
72+
g := NewWithT(t)
73+
74+
// Case 1: empty annotation map (the key does not exist)
75+
value, exists, err = GetBoolFromAnnotation(ann, testKey)
76+
g.Expect(exists).To(BeFalse())
77+
g.Expect(value).To(BeFalse())
78+
g.Expect(err).NotTo(HaveOccurred())
79+
80+
// Case 2: testKey exists but is not a valid bool
81+
ann[testKey] = "foo"
82+
value, exists, err = GetBoolFromAnnotation(ann, testKey)
83+
g.Expect(value).To(BeFalse())
84+
g.Expect(exists).To(BeTrue())
85+
g.Expect(err).To(HaveOccurred())
86+
87+
// Case 3: testKey exists and is False
88+
ann[testKey] = "false"
89+
value, exists, err = GetBoolFromAnnotation(ann, testKey)
90+
g.Expect(value).To(BeFalse())
91+
g.Expect(exists).To(BeTrue())
92+
g.Expect(err).ToNot(HaveOccurred())
93+
94+
// Case 4: testKey exists and is True
95+
ann[testKey] = "true"
96+
value, exists, err = GetBoolFromAnnotation(ann, testKey)
97+
g.Expect(value).To(BeTrue())
98+
g.Expect(exists).To(BeTrue())
99+
g.Expect(err).ToNot(HaveOccurred())
100+
})
101+
}

0 commit comments

Comments
 (0)