Skip to content

Commit

Permalink
Merge pull request #382 from mohitsharma-in/adding-branch-removal-sup…
Browse files Browse the repository at this point in the history
…port

Adding support for removing branch
  • Loading branch information
k8s-ci-robot authored Jan 2, 2024
2 parents 4448fac + 494bbe8 commit 3cb4242
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 13 deletions.
10 changes: 8 additions & 2 deletions cmd/update-rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
39 changes: 30 additions & 9 deletions cmd/update-rules/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,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")

Expand All @@ -52,10 +54,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()
}
Expand Down Expand Up @@ -85,8 +90,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)
Expand Down Expand Up @@ -120,9 +124,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
Expand Down
54 changes: 52 additions & 2 deletions cmd/update-rules/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package main

import "testing"
import (
"reflect"
"testing"
)

var (
testdataRules = "testdata/rules.yaml"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
})
}
}

0 comments on commit 3cb4242

Please sign in to comment.