-
Notifications
You must be signed in to change notification settings - Fork 49
/
utils.go
93 lines (76 loc) · 2.59 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Copyright 2020 NVIDIA
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.
*/
package utils
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/go-logr/logr"
netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"github.com/Mellanox/network-operator/pkg/consts"
)
const PodTemplateGenerationLabel = "pod-template-generation"
// GetFilesWithSuffix returns all files under a given base directory that have a specific suffix
// The operation is performed recurively on sub directories as well
func GetFilesWithSuffix(baseDir string, suffixes ...string) ([]string, error) {
var files []string
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
// Error during traversal
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// Skip non suffix files
base := info.Name()
for _, s := range suffixes {
if strings.HasSuffix(base, s) {
files = append(files, path)
}
}
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "error traversing directory tree")
}
return files, nil
}
func GetNetworkAttachmentDefLink(netAttDef *netattdefv1.NetworkAttachmentDefinition) (link string) {
link = fmt.Sprintf("%s/namespaces/%s/%s/%s",
netAttDef.APIVersion, netAttDef.Namespace, netAttDef.Kind, netAttDef.Name)
return
}
// CreateK8sInterface creates a new ClientSet for interacting with K8s API
func CreateK8sInterface() (kubernetes.Interface, error) {
k8sConfig := config.GetConfigOrDie()
k8sInterface, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, fmt.Errorf("unable to create a k8sInterface: %v", err)
}
return k8sInterface, nil
}
func GetPodTemplateGeneration(pod *v1.Pod, log logr.Logger) (int64, error) {
generation, err := strconv.ParseInt(pod.Labels[PodTemplateGenerationLabel], 10, 0)
if err != nil {
log.V(consts.LogLevelInfo).Error(err, "Failed to get pod template generation", "pod", pod)
return 0, err
}
return generation, nil
}