Skip to content

Commit d396592

Browse files
hiddecodarkowlzz
authored andcommitted
internal/util: introduce temp dir/path helpers
In most of the reconcilers we have a repetative pattern of using part of the object metadata to construct a temporary file path. This commit introduces helpers as an abstraction, for both the creation of a temporary directory based on `client.Object` type and object metadata, and the generation of an arbitrary random temporary path string. Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent affc27e commit d396592

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

internal/util/temp.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2021 The Flux 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+
"crypto/rand"
21+
"encoding/hex"
22+
"fmt"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
)
29+
30+
// TempDirForObj creates a new temporary directory in the directory dir
31+
// in the format of 'Kind-Namespace-Name-*', and returns the
32+
// pathname of the new directory.
33+
func TempDirForObj(dir string, obj client.Object) (string, error) {
34+
return os.MkdirTemp(dir, pattern(obj))
35+
}
36+
37+
// TempPathForObj creates a temporary file path in the format of
38+
// '<dir>/Kind-Namespace-Name-<random bytes><suffix>'.
39+
// If the given dir is empty, os.TempDir is used as a default.
40+
func TempPathForObj(dir, suffix string, obj client.Object) string {
41+
if dir == "" {
42+
dir = os.TempDir()
43+
}
44+
randBytes := make([]byte, 16)
45+
rand.Read(randBytes)
46+
return filepath.Join(dir, pattern(obj)+hex.EncodeToString(randBytes)+suffix)
47+
}
48+
49+
func pattern(obj client.Object) (p string) {
50+
kind := strings.ToLower(obj.GetObjectKind().GroupVersionKind().Kind)
51+
return fmt.Sprintf("%s-%s-%s-", kind, obj.GetNamespace(), obj.GetName())
52+
}

internal/util/temp_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2021 The Flux 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+
"os"
21+
"testing"
22+
23+
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
func TestTempDirForObj(t *testing.T) {
30+
g := NewWithT(t)
31+
32+
got, err := TempDirForObj("", mockObj())
33+
g.Expect(err).ToNot(HaveOccurred())
34+
g.Expect(got).To(BeADirectory())
35+
defer os.RemoveAll(got)
36+
37+
got2, err := TempDirForObj(got, mockObj())
38+
g.Expect(err).ToNot(HaveOccurred())
39+
g.Expect(got2).To(BeADirectory())
40+
defer os.RemoveAll(got2)
41+
g.Expect(got2).To(ContainSubstring(got))
42+
}
43+
44+
func TestTempPathForObj(t *testing.T) {
45+
tests := []struct {
46+
name string
47+
dir string
48+
suffix string
49+
want string
50+
}{
51+
{
52+
name: "default",
53+
want: os.TempDir() + "/secret-default-foo-",
54+
},
55+
{
56+
name: "with directory",
57+
dir: "/foo",
58+
want: "/foo/secret-default-foo-",
59+
},
60+
}
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
g := NewWithT(t)
64+
got := TempPathForObj(tt.dir, tt.suffix, mockObj())
65+
g.Expect(got[:len(got)-32]).To(Equal(tt.want))
66+
})
67+
}
68+
}
69+
70+
func Test_pattern(t *testing.T) {
71+
g := NewWithT(t)
72+
g.Expect(pattern(mockObj())).To(Equal("secret-default-foo-"))
73+
}
74+
75+
func mockObj() client.Object {
76+
return &corev1.Secret{
77+
TypeMeta: metav1.TypeMeta{
78+
Kind: "Secret",
79+
},
80+
ObjectMeta: metav1.ObjectMeta{
81+
Name: "foo",
82+
Namespace: "default",
83+
},
84+
}
85+
}

0 commit comments

Comments
 (0)