forked from cilium/cilium
/
restore.go
140 lines (125 loc) · 4.41 KB
/
restore.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2018-2019 Authors of Cilium
//
// 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 endpoint
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cilium/cilium/common"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/sirupsen/logrus"
)
// ReadEPsFromDirNames returns a mapping of endpoint ID to endpoint of endpoints
// from a list of directory names that can possible contain an endpoint.
func ReadEPsFromDirNames(owner regeneration.Owner, basePath string, eptsDirNames []string) map[uint16]*Endpoint {
completeEPDirNames, incompleteEPDirNames := partitionEPDirNamesByRestoreStatus(eptsDirNames)
if len(incompleteEPDirNames) > 0 {
for _, epDirName := range incompleteEPDirNames {
scopedLog := log.WithFields(logrus.Fields{
logfields.EndpointID: epDirName,
})
fullDirName := filepath.Join(basePath, epDirName)
scopedLog.Warning(fmt.Sprintf("Found incomplete restore directory %s. Removing it...", fullDirName))
if err := os.RemoveAll(epDirName); err != nil {
scopedLog.WithError(err).Warn(fmt.Sprintf("Error while removing directory %s. Ignoring it...", fullDirName))
}
}
}
possibleEPs := map[uint16]*Endpoint{}
for _, epDirName := range completeEPDirNames {
epDir := filepath.Join(basePath, epDirName)
readDir := func() string {
scopedLog := log.WithFields(logrus.Fields{
logfields.EndpointID: epDirName,
logfields.Path: filepath.Join(epDir, common.CHeaderFileName),
})
scopedLog.Debug("Reading directory")
epFiles, err := ioutil.ReadDir(epDir)
if err != nil {
scopedLog.WithError(err).Warn("Error while reading directory. Ignoring it...")
return ""
}
cHeaderFile := common.FindEPConfigCHeader(epDir, epFiles)
if cHeaderFile == "" {
return ""
}
return cHeaderFile
}
// There's an odd issue where the first read dir doesn't work.
cHeaderFile := readDir()
if cHeaderFile == "" {
cHeaderFile = readDir()
}
scopedLog := log.WithFields(logrus.Fields{
logfields.EndpointID: epDirName,
logfields.Path: cHeaderFile,
})
if cHeaderFile == "" {
scopedLog.Warning("C header file not found. Ignoring endpoint")
continue
}
scopedLog.Debug("Found endpoint C header file")
strEp, err := common.GetCiliumVersionString(cHeaderFile)
if err != nil {
scopedLog.WithError(err).Warn("Unable to read the C header file")
continue
}
ep, err := ParseEndpoint(owner, strEp)
if err != nil {
scopedLog.WithError(err).Warn("Unable to parse the C header file")
continue
}
if _, ok := possibleEPs[ep.ID]; ok {
// If the endpoint already exists then give priority to the directory
// that contains an endpoint that didn't fail to be build.
if strings.HasSuffix(ep.DirectoryPath(), epDirName) {
possibleEPs[ep.ID] = ep
}
} else {
possibleEPs[ep.ID] = ep
}
}
return possibleEPs
}
// partitionEPDirNamesByRestoreStatus partitions the provided list of directory
// names that can possibly contain an endpoint, into two lists, containing those
// names that represent an incomplete endpoint restore and those that do not.
func partitionEPDirNamesByRestoreStatus(eptsDirNames []string) (complete []string, incomplete []string) {
dirNames := make(map[string]struct{})
for _, epDirName := range eptsDirNames {
dirNames[epDirName] = struct{}{}
}
incompleteSuffixes := []string{nextDirectorySuffix, nextFailedDirectorySuffix}
incompleteSet := make(map[string]struct{})
for _, epDirName := range eptsDirNames {
for _, suff := range incompleteSuffixes {
if strings.HasSuffix(epDirName, suff) {
if _, exists := dirNames[epDirName[:len(epDirName)-len(suff)]]; exists {
incompleteSet[epDirName] = struct{}{}
}
}
}
}
for epDirName := range dirNames {
if _, exists := incompleteSet[epDirName]; exists {
incomplete = append(incomplete, epDirName)
} else {
complete = append(complete, epDirName)
}
}
return
}