diff --git a/cmd/update-rules/README.md b/cmd/update-rules/README.md index 3b42770a..cc710c05 100644 --- a/cmd/update-rules/README.md +++ b/cmd/update-rules/README.md @@ -50,7 +50,7 @@ Run the command line as: ``` update-rules -h - Usage: update-rules --branch BRANCH --rules PATHorURL [--go VERSION | -o PATH] + Usage: update-rules --branch BRANCH --rules PATHorURL [--go VERSION | -o PATH | --delete] Examples: # Update rules for branch release-1.23 with go version 1.16.4 @@ -60,7 +60,10 @@ Run the command line as: update-rules -branch release-1.23 -go 1.16.4 -rules https://raw.githubusercontent.com/kubernetes/kubernetes/master/staging/publishing/rules.yaml # Update rules and export to /tmp/rules.yaml - update-rules -branch release-1.24 -go 1.17.1 -o /tmp/rules.yaml -rules /go/src/k8s.io/kubernetes/staging/publishing/rules.yaml + update-rules -branch release-1.24 -go 1.17.1 -o /tmp/rules.yaml -rules /go/src/k8s.io/kubernetes/staging/publishing/rules.yaml + + # Delete rules and export to /tmp/rules.yaml + update-rules -branch release-1.24 -delete -o /tmp/rules.yaml -rules /go/src/k8s.io/kubernetes/staging/publishing/rules.yaml -alsologtostderr log to standard error as well as files @@ -76,6 +79,8 @@ Run the command line as: log to standard error instead of files -o string Path to export the updated rules to, e.g. -o /tmp/rules.yaml + -delete + Delete old rules of deprecated branch -rules string [required] URL or Path of the rules file to update rules for, e.g. --rules path/or/url/to/rules/file.yaml -stderrthreshold value @@ -94,4 +99,5 @@ Run the command line as: #### Optional flags: - `-go` flag refers to golang version which should be pinned for given branch, if not given an empty string is set +- `-delete` flag refers to removing the branch from rules, if not set defaults to false - `-o` flag refers to output file where the processed rules should be exported, otherwise rules are printed on stdout diff --git a/cmd/update-rules/main.go b/cmd/update-rules/main.go index 6e74f286..1af932aa 100644 --- a/cmd/update-rules/main.go +++ b/cmd/update-rules/main.go @@ -31,16 +31,18 @@ import ( const GitDefaultBranch = "master" type options struct { - branch string - rulesFile string - goVersion string - out string + branch string + deleteRule bool + rulesFile string + goVersion string + out string } func parseOptions() options { var o options flag.StringVar(&o.branch, "branch", "", "[required] Branch to update rules for, e.g. --branch release-x.yy") flag.StringVar(&o.rulesFile, "rules", "", "[required] URL or Path of the rules file to update rules for, e.g. --rules path/or/url/to/rules/file.yaml") + flag.BoolVar(&o.deleteRule, "delete", false, "Remove old rules of deprecated branch") flag.StringVar(&o.goVersion, "go", "", "Golang version to pin for this branch, e.g. --go 1.16.1") flag.StringVar(&o.out, "o", "", "Path to export the updated rules to, e.g. -o /tmp/rules.yaml") @@ -53,10 +55,13 @@ func parseOptions() options { update-rules -branch release-1.21 -go 1.16.4 -rules https://raw.githubusercontent.com/kubernetes/kubernetes/master/staging/publishing/rules.yaml # Update rules and export to /tmp/rules.yaml - update-rules -branch release-1.22 -go 1.17.1 -o /tmp/rules.yaml -rules /go/src/k8s.io/kubernetes/staging/publishing/rules.yaml` + update-rules -branch release-1.22 -go 1.17.1 -o /tmp/rules.yaml -rules /go/src/k8s.io/kubernetes/staging/publishing/rules.yaml + + # Update rules to remove deprecated branch and export to /tmp/rules.yaml + update-rules -branch release-1.22 -delete -o /tmp/rules.yaml -rules /go/src/k8s.io/kubernetes/staging/publishing/rules.yaml` flag.Usage = func() { - fmt.Fprintf(os.Stdout, "\n Usage: update-rules --branch BRANCH --rules PATHorURL [--go VERSION | -o PATH]") + fmt.Fprintf(os.Stdout, "\n Usage: update-rules --branch BRANCH --rules PATHorURL [--go VERSION | -o PATH | --delete ]") fmt.Fprintf(os.Stdout, "\n %s\n\n", examples) flag.PrintDefaults() } @@ -86,8 +91,7 @@ func main() { } // update rules for all destination repos - UpdateRules(rules, o.branch, o.goVersion) - + UpdateRules(rules, o.branch, o.goVersion, o.deleteRule) // validate rules after update if err := config.Validate(rules); err != nil { glog.Fatalf("update failed, found invalid rules after update: %v", err) @@ -121,9 +125,26 @@ func load(rulesFile string) (*config.RepositoryRules, error) { return rules, nil } -func UpdateRules(rules *config.RepositoryRules, branch, goVer string) { +func UpdateRules(rules *config.RepositoryRules, branch, goVer string, deleteRule bool) { // run the update per destination repo in the rules for j, r := range rules.Rules { + var deletedBranch bool + // To Check and Remove the existing/deprecated branch + if deleteRule { + for i := range r.Branches { + if rules.Rules[j].Branches[i].Name == branch { + glog.Infof("remove rule %s for %s", branch, r.DestinationRepository) + rules.Rules[j].Branches = append(rules.Rules[j].Branches[:i], rules.Rules[j].Branches[i+1:]...) + deletedBranch = true + break + } + } + if !deletedBranch { + glog.Infof("skipping delete of branch rule %s that doesn't exists for %s", branch, r.DestinationRepository) + } + continue + } + var mainBranchRuleFound bool var newBranchRule config.BranchRule // find the mainBranch rules diff --git a/cmd/update-rules/main_test.go b/cmd/update-rules/main_test.go index c485b04a..c687013c 100644 --- a/cmd/update-rules/main_test.go +++ b/cmd/update-rules/main_test.go @@ -16,7 +16,10 @@ limitations under the License. package main -import "testing" +import ( + "reflect" + "testing" +) var ( testdataRules = "testdata/rules.yaml" @@ -109,7 +112,7 @@ func TestUpdateRules(t *testing.T) { if err != nil { t.Errorf("error loading test rules file %v", err) } - UpdateRules(rules, tt.branch, tt.goVersion) + UpdateRules(rules, tt.branch, tt.goVersion, false) for _, repoRule := range rules.Rules { var masterRulePresent, branchRulePresent bool @@ -163,3 +166,50 @@ func TestUpdateRules(t *testing.T) { }) } } + +func TestDeleteRules(t *testing.T) { + tests := []struct { + name string + branch string + goVersion string + isBranchExist bool + }{ + { + "deleting rule for non existing branch", + "release-1.20", + "1.17.1", + true, + }, + { + "deleting rule for non existing branch 1.25", + "release-1.25", + "1.17.1", + false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rules, err := load(testdataRules) + if err != nil { + t.Errorf("error loading test rules file %v", err) + } + UpdateRules(rules, tt.branch, tt.goVersion, true) + if tt.isBranchExist { + for _, repoRule := range rules.Rules { + for _, branchRule := range repoRule.Branches { + if branchRule.Name == tt.branch { + t.Errorf("failed to delete %s branch rule from for repo %s", tt.name, repoRule.DestinationRepository) + } + } + } + } else { + if loadedRules, err := load(testdataRules); err != nil { + t.Errorf("error loading test rules file for comparison %v", err) + } else if !reflect.DeepEqual(loadedRules, rules) { + t.Errorf("rules changed after deleting a non existent branch %s", tt.branch) + } + } + }) + } +}