|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "golang.org/x/mod/modfile" |
| 26 | + "k8s.io/publishing-bot/cmd/publishing-bot/config" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + rulesFile string |
| 31 | + componentsDirectory string |
| 32 | +) |
| 33 | + |
| 34 | +// getGoModDependencies gets all the staging dependencies for all the modules |
| 35 | +// in the given directory |
| 36 | +func getGoModDependencies(dir string) (map[string][]string, error) { |
| 37 | + allDependencies := make(map[string][]string) |
| 38 | + components, err := os.ReadDir(dir) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + for _, component := range components { |
| 43 | + componentName := component.Name() |
| 44 | + if !component.IsDir() { |
| 45 | + // currently there is no hard check that the staging directory should not contain |
| 46 | + // other files |
| 47 | + continue |
| 48 | + } |
| 49 | + gomodFilePath := filepath.Join(dir, componentName, "go.mod") |
| 50 | + gomodFileContent, err := os.ReadFile(gomodFilePath) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + fmt.Printf("%s dependencies", componentName) |
| 56 | + |
| 57 | + allDependencies[componentName] = make([]string, 0) |
| 58 | + |
| 59 | + gomodFile, err := modfile.Parse(gomodFilePath, gomodFileContent, nil) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + // get all the other dependencies from within staging, i.e all the modules in replace |
| 64 | + // section |
| 65 | + for _, module := range gomodFile.Replace { |
| 66 | + dep := strings.TrimPrefix(module.Old.Path, "k8s.io/") |
| 67 | + if dep == componentName { |
| 68 | + continue |
| 69 | + } |
| 70 | + allDependencies[componentName] = append(allDependencies[componentName], dep) |
| 71 | + } |
| 72 | + } |
| 73 | + return allDependencies, nil |
| 74 | +} |
| 75 | + |
| 76 | +// diffSlice returns the difference of s1-s2 |
| 77 | +func diffSlice(s1, s2 []string) []string { |
| 78 | + var diff []string |
| 79 | + set := make(map[string]struct{}, len(s2)) |
| 80 | + for _, s := range s2 { |
| 81 | + set[s] = struct{}{} |
| 82 | + } |
| 83 | + for _, s := range s1 { |
| 84 | + if _, ok := set[s]; !ok { |
| 85 | + diff = append(diff, s) |
| 86 | + } |
| 87 | + } |
| 88 | + return diff |
| 89 | +} |
| 90 | + |
| 91 | +// getKeys returns a slice with only the keys of the given map |
| 92 | +func getKeys[K comparable, V any](m map[K]V) []K { |
| 93 | + var keys []K |
| 94 | + for k := range m { |
| 95 | + keys = append(keys, k) |
| 96 | + } |
| 97 | + return keys |
| 98 | +} |
| 99 | + |
| 100 | +// checkValidSourceDirectory checks if proper source directory fields are used in rules |
| 101 | +func checkValidSourceDirectory(rule config.RepositoryRule) error { |
| 102 | + for _, branch := range rule.Branches { |
| 103 | + if branch.Source.Dir != "" { |
| 104 | + return fmt.Errorf("use of deprecated `dir` field in rules for `%s`", rule.DestinationRepository) |
| 105 | + } |
| 106 | + if len(branch.Source.Dirs) > 1 { |
| 107 | + return fmt.Errorf("cannot have more than one directory (%s) per source branch `%s` of `%s`", |
| 108 | + branch.Source.Dirs, |
| 109 | + branch.Source.Branch, |
| 110 | + rule.DestinationRepository, |
| 111 | + ) |
| 112 | + } |
| 113 | + if !strings.HasSuffix(branch.Source.Dirs[0], rule.DestinationRepository) { |
| 114 | + return fmt.Errorf("copy/paste error `%s` refers to `%s`", rule.DestinationRepository, branch.Source.Dirs[0]) |
| 115 | + } |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// checkMasterBranch checks if the master branch of destination repository refers to the master |
| 121 | +// of the source |
| 122 | +func checkMasterBranch(rule config.RepositoryRule) error { |
| 123 | + branch := rule.Branches[0] |
| 124 | + if branch.Name != "master" { |
| 125 | + return fmt.Errorf("cannot find master branch for destination `%s`", rule.DestinationRepository) |
| 126 | + } |
| 127 | + |
| 128 | + if branch.Source.Branch != "master" { |
| 129 | + return fmt.Errorf("cannot find master source branch for destination `%s`", rule.DestinationRepository) |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func checkDependencies(rule config.RepositoryRule, gomodDependencies map[string][]string) error { |
| 135 | + var processedDeps []string |
| 136 | + branch := rule.Branches[0] |
| 137 | + for _, dep := range gomodDependencies[rule.DestinationRepository] { |
| 138 | + found := false |
| 139 | + if len(branch.Dependencies) > 0 { |
| 140 | + for _, dep2 := range branch.Dependencies { |
| 141 | + processedDeps = append(processedDeps, dep2.Repository) |
| 142 | + if dep2.Branch != "master" { |
| 143 | + return fmt.Errorf("looking for master branch of %s and found : %s for destination", dep2.Repository, rule.DestinationRepository) |
| 144 | + } |
| 145 | + if dep2.Repository == dep { |
| 146 | + found = true |
| 147 | + } |
| 148 | + } |
| 149 | + } else { |
| 150 | + return fmt.Errorf("Please add %s as dependencies under destination %s", gomodDependencies[rule.DestinationRepository], rule.DestinationRepository) |
| 151 | + } |
| 152 | + if !found { |
| 153 | + return fmt.Errorf("Please add %s as a dependency under destination %s", dep, rule.DestinationRepository) |
| 154 | + } else { |
| 155 | + fmt.Printf("dependency %s found\n", dep) |
| 156 | + } |
| 157 | + } |
| 158 | + // check if all deps are processed. |
| 159 | + extraDeps := diffSlice(processedDeps, gomodDependencies[rule.DestinationRepository]) |
| 160 | + if len(extraDeps) > 0 { |
| 161 | + return fmt.Errorf("extra dependencies in rules for %s: %s", rule.DestinationRepository, strings.Join(extraDeps, ",")) |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func verifyPublishingBotRules() error { |
| 167 | + rules, err := config.LoadRules(rulesFile) |
| 168 | + if err != nil { |
| 169 | + return fmt.Errorf("error loading rules: %v", err) |
| 170 | + } |
| 171 | + |
| 172 | + gomodDependencies, err := getGoModDependencies(componentsDirectory) |
| 173 | + |
| 174 | + var processedRepos []string |
| 175 | + for _, rule := range rules.Rules { |
| 176 | + branch := rule.Branches[0] |
| 177 | + |
| 178 | + // if this no longer exists in master |
| 179 | + if _, ok := gomodDependencies[rule.DestinationRepository]; !ok { |
| 180 | + // make sure we dont include a rule to publish it from master |
| 181 | + for _, branch := range rule.Branches { |
| 182 | + if branch.Name == "master" { |
| 183 | + err := fmt.Errorf("cannot find master branch for destination `%s`", rule.DestinationRepository) |
| 184 | + panic(err) |
| 185 | + } |
| 186 | + } |
| 187 | + // and skip the validation of publishing rules for it |
| 188 | + continue |
| 189 | + } |
| 190 | + |
| 191 | + if err := checkValidSourceDirectory(rule); err != nil { |
| 192 | + return fmt.Errorf("error validating source directory: %v", err) |
| 193 | + } |
| 194 | + |
| 195 | + if err := checkMasterBranch(rule); err != nil { |
| 196 | + return fmt.Errorf("error validating master branch: %v", err) |
| 197 | + } |
| 198 | + |
| 199 | + // we specify the go version for all master branches through `default-go-version` |
| 200 | + // so ensure we don't specify explicit go version for master branch in rules |
| 201 | + if branch.GoVersion != "" { |
| 202 | + err := fmt.Errorf("go version must not be specified for master branch for destination `%s`", rule.DestinationRepository) |
| 203 | + panic(err) |
| 204 | + } |
| 205 | + |
| 206 | + fmt.Printf("processing : %s", rule.DestinationRepository) |
| 207 | + if _, ok := gomodDependencies[rule.DestinationRepository]; !ok { |
| 208 | + err := fmt.Errorf("missing go.mod for `%s`", rule.DestinationRepository) |
| 209 | + panic(err) |
| 210 | + } |
| 211 | + processedRepos = append(processedRepos, rule.DestinationRepository) |
| 212 | + |
| 213 | + if err := checkDependencies(rule, gomodDependencies); err != nil { |
| 214 | + return fmt.Errorf("error validating dependencies: %v", err) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + // check if all repos are processed. |
| 219 | + items := diffSlice(getKeys(gomodDependencies), processedRepos) |
| 220 | + if len(items) > 0 { |
| 221 | + err := fmt.Errorf("missing rules for %s", strings.Join(items, ",")) |
| 222 | + panic(err) |
| 223 | + } |
| 224 | + return nil |
| 225 | +} |
| 226 | + |
| 227 | +func main() { |
| 228 | + if len(os.Args) != 2 { |
| 229 | + panic("invalid number of arguments") |
| 230 | + } |
| 231 | + |
| 232 | + kubeRoot := os.Args[1] |
| 233 | + stagingDirectory := kubeRoot + "/staging/" |
| 234 | + rulesFile = stagingDirectory + "publishing/rules.yaml" |
| 235 | + componentsDirectory = stagingDirectory + "src/k8s.io/" |
| 236 | + |
| 237 | + if err := verifyPublishingBotRules(); err != nil { |
| 238 | + panic(err) |
| 239 | + } |
| 240 | +} |
0 commit comments