github.com/fafucoder/cilium@v1.6.11/pkg/endpoint/restore.go (about) 1 // Copyright 2018-2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package endpoint 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "strings" 23 24 "github.com/cilium/cilium/common" 25 "github.com/cilium/cilium/pkg/endpoint/regeneration" 26 "github.com/cilium/cilium/pkg/logging/logfields" 27 "github.com/sirupsen/logrus" 28 ) 29 30 // ReadEPsFromDirNames returns a mapping of endpoint ID to endpoint of endpoints 31 // from a list of directory names that can possible contain an endpoint. 32 func ReadEPsFromDirNames(owner regeneration.Owner, basePath string, eptsDirNames []string) map[uint16]*Endpoint { 33 completeEPDirNames, incompleteEPDirNames := partitionEPDirNamesByRestoreStatus(eptsDirNames) 34 35 if len(incompleteEPDirNames) > 0 { 36 for _, epDirName := range incompleteEPDirNames { 37 scopedLog := log.WithFields(logrus.Fields{ 38 logfields.EndpointID: epDirName, 39 }) 40 fullDirName := filepath.Join(basePath, epDirName) 41 scopedLog.Warning(fmt.Sprintf("Found incomplete restore directory %s. Removing it...", fullDirName)) 42 if err := os.RemoveAll(epDirName); err != nil { 43 scopedLog.WithError(err).Warn(fmt.Sprintf("Error while removing directory %s. Ignoring it...", fullDirName)) 44 } 45 } 46 } 47 48 possibleEPs := map[uint16]*Endpoint{} 49 for _, epDirName := range completeEPDirNames { 50 epDir := filepath.Join(basePath, epDirName) 51 readDir := func() string { 52 scopedLog := log.WithFields(logrus.Fields{ 53 logfields.EndpointID: epDirName, 54 logfields.Path: filepath.Join(epDir, common.CHeaderFileName), 55 }) 56 scopedLog.Debug("Reading directory") 57 epFiles, err := ioutil.ReadDir(epDir) 58 if err != nil { 59 scopedLog.WithError(err).Warn("Error while reading directory. Ignoring it...") 60 return "" 61 } 62 cHeaderFile := common.FindEPConfigCHeader(epDir, epFiles) 63 if cHeaderFile == "" { 64 return "" 65 } 66 return cHeaderFile 67 } 68 // There's an odd issue where the first read dir doesn't work. 69 cHeaderFile := readDir() 70 if cHeaderFile == "" { 71 cHeaderFile = readDir() 72 } 73 74 scopedLog := log.WithFields(logrus.Fields{ 75 logfields.EndpointID: epDirName, 76 logfields.Path: cHeaderFile, 77 }) 78 79 if cHeaderFile == "" { 80 scopedLog.Warning("C header file not found. Ignoring endpoint") 81 continue 82 } 83 84 scopedLog.Debug("Found endpoint C header file") 85 86 strEp, err := common.GetCiliumVersionString(cHeaderFile) 87 if err != nil { 88 scopedLog.WithError(err).Warn("Unable to read the C header file") 89 continue 90 } 91 ep, err := ParseEndpoint(owner, strEp) 92 if err != nil { 93 scopedLog.WithError(err).Warn("Unable to parse the C header file") 94 continue 95 } 96 if _, ok := possibleEPs[ep.ID]; ok { 97 // If the endpoint already exists then give priority to the directory 98 // that contains an endpoint that didn't fail to be build. 99 if strings.HasSuffix(ep.DirectoryPath(), epDirName) { 100 possibleEPs[ep.ID] = ep 101 } 102 } else { 103 possibleEPs[ep.ID] = ep 104 } 105 } 106 return possibleEPs 107 } 108 109 // partitionEPDirNamesByRestoreStatus partitions the provided list of directory 110 // names that can possibly contain an endpoint, into two lists, containing those 111 // names that represent an incomplete endpoint restore and those that do not. 112 func partitionEPDirNamesByRestoreStatus(eptsDirNames []string) (complete []string, incomplete []string) { 113 dirNames := make(map[string]struct{}) 114 for _, epDirName := range eptsDirNames { 115 dirNames[epDirName] = struct{}{} 116 } 117 118 incompleteSuffixes := []string{nextDirectorySuffix, nextFailedDirectorySuffix} 119 incompleteSet := make(map[string]struct{}) 120 121 for _, epDirName := range eptsDirNames { 122 for _, suff := range incompleteSuffixes { 123 if strings.HasSuffix(epDirName, suff) { 124 if _, exists := dirNames[epDirName[:len(epDirName)-len(suff)]]; exists { 125 incompleteSet[epDirName] = struct{}{} 126 } 127 } 128 } 129 } 130 131 for epDirName := range dirNames { 132 if _, exists := incompleteSet[epDirName]; exists { 133 incomplete = append(incomplete, epDirName) 134 } else { 135 complete = append(complete, epDirName) 136 } 137 } 138 139 return 140 }