From 5b1c4222084111982181059acde208374b39924f Mon Sep 17 00:00:00 2001 From: James Strong Date: Mon, 19 Jun 2023 14:23:09 -0400 Subject: [PATCH] refactor Signed-off-by: James Strong --- ingressctl/cmd/helm.go | 5 +- ingressctl/cmd/release.go | 150 ++------------------------------- ingressctl/cmd/releaseNotes.go | 147 ++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 143 deletions(-) create mode 100644 ingressctl/cmd/releaseNotes.go diff --git a/ingressctl/cmd/helm.go b/ingressctl/cmd/helm.go index 7b7d3d1f5..4b7d58f06 100644 --- a/ingressctl/cmd/helm.go +++ b/ingressctl/cmd/helm.go @@ -61,7 +61,7 @@ func UpdateVersion(appVersion string) { chart, err := chartutil.LoadChartfile(HelmChartPath) CheckIfError(err, "HELM Could not Load Chart") - Info("HELM Ingress-Nginx App Version: %s Chart AppVersion: %s", appVersion, chart.AppVersion) + Info("HELM Ingress-Nginx NEW Chart App Version: %s Previous Chart AppVersion: %s", appVersion, chart.AppVersion) if appVersion == chart.AppVersion { Warning("HELM Ingress NGINX Version didnt change Ingress-Nginx App Version: %s Chart AppVersion: %s", appVersion, chart.AppVersion) return @@ -77,7 +77,7 @@ func UpdateVersion(appVersion string) { os.Exit(1) } chart.Version = cTag.String() - Debug("HELM Updated Chart Version: %v", chart.Version) + Info("HELM Updated Chart Version: %v", chart.Version) err = chartutil.SaveChartfile(HelmChartPath, chart) CheckIfError(err, "HELM Saving new Chart") @@ -148,6 +148,7 @@ func HelmDocs() error { if err != nil { return err } + Info("HELM - Running Helm Docs") return sh.Command("helm-docs", fmt.Sprintf("--chart-search-root=%s", CHART_PATH)).Run() } diff --git a/ingressctl/cmd/release.go b/ingressctl/cmd/release.go index 175841f7f..4dd88ce6a 100644 --- a/ingressctl/cmd/release.go +++ b/ingressctl/cmd/release.go @@ -30,9 +30,7 @@ import ( "github.com/google/go-github/v48/github" "github.com/spf13/cobra" "golang.org/x/oauth2" - "gopkg.in/yaml.v3" - "regexp" "strings" "time" ) @@ -90,7 +88,7 @@ var controllerReleaseCmd = &cobra.Command{ Short: "Release Ingress-nginx Controller", Long: "Release a new version of ingress-nginx controller", Run: func(cmd *cobra.Command, args []string) { - NewRelease(version) + ControllerNewRelease(version) }, } @@ -120,19 +118,6 @@ type IngressRelease struct { Release *github.RepositoryRelease } -// ReleaseNote - All the pieces of information/documents that get updated during a release -type ReleaseNote struct { - Version string //released version - NewControllerVersion string //the new controller version being release - PreviousControllerVersion string //the previous controller tag/release - ControllerImages []ControllerImage //the full image digests - DepUpdates []string //list of dependabot updates to put in the changelog - Updates []string //updates with no category - HelmUpdates []string //updates to the ingress-nginx helm chart - NewHelmChartVersion string //update to the helm chart version - PreviousHelmChartVersion string //previous helm chart version -} - // IMAGES_YAML returns this data structure type ImageYamls []ImageElement @@ -150,7 +135,7 @@ func PromoteImage(version, sha string) { } // Release Create a new release of ingress nginx controller -func NewRelease(version string) { +func ControllerNewRelease(version string) { //update ingress-nginx version //This is the step that kicks all the release process @@ -171,15 +156,10 @@ func NewRelease(version string) { NewControllerTag(version) //make release notes - releaseNotes, err := ReleaseNotes(version) + releaseNotes, err := ControllerReleaseNotes(version) CheckIfError(err, "RELEASE Creating Release Notes for version %s", version) Info("RELEASE Release Notes %s completed", releaseNotes.Version) - //update chart values.yaml new controller tag and image digest - releaseNotes.PreviousHelmChartVersion = currentChartVersion() - - //update the Helm Chart appVersion aka the controller tag - updateChartValue("controller.image.tag", fmt.Sprintf("v%s", releaseNotes.Version)) Debug("releaseNotes.ControllerImages[0].Name %s", releaseNotes.ControllerImages[0].Name) Debug("releaseNotes.ControllerImages[1].Name %s", releaseNotes.ControllerImages[1].Name) @@ -195,6 +175,12 @@ func NewRelease(version string) { updateChartValue("controller.image.digestChroot", releaseNotes.ControllerImages[1].Digest) } + //update the Helm Chart appVersion aka the controller tag + updateChartValue("controller.image.tag", fmt.Sprintf("v%s", releaseNotes.Version)) + + //update chart values.yaml new controller tag and image digest + releaseNotes.PreviousHelmChartVersion = currentChartVersion() + //update helm chart app version UpdateVersion(version) @@ -297,124 +283,6 @@ func commitsBetweenTags() []string { return strings.Split(string(commitLog), "\n") } -// Generate Release Notes -func ReleaseNotes(newVersion string) (*ReleaseNote, error) { - var newReleaseNotes = ReleaseNote{} - - newReleaseNotes.Version = newVersion - allControllerTags := getAllControllerTags() - - //new version - newReleaseNotes.NewControllerVersion = allControllerTags[0] - newControllerVersion := fmt.Sprintf("controller-v%s", newVersion) - - //the newControllerVersion should match the latest tag - if newControllerVersion != allControllerTags[0] { - return nil, errors.New(fmt.Sprintf("Generating release new version %s didnt match the current latest tag %s", newControllerVersion, allControllerTags[0])) - } - //previous version - newReleaseNotes.PreviousControllerVersion = allControllerTags[1] - - Info("New Version: %s Old Version: %s", newReleaseNotes.NewControllerVersion, newReleaseNotes.PreviousControllerVersion) - - commits := commitsBetweenTags() - - //dependency_updates - //all_updates - var allUpdates []string - var depUpdates []string - var helmUpdates []string - prRegex := regexp.MustCompile("\\(#\\d+\\)") - depBot := regexp.MustCompile("^(\\w){1,10} Bump ") - helmRegex := regexp.MustCompile("helm|chart") - for i, s := range commits { - //matches on PR - if prRegex.Match([]byte(s)) { - //matches a dependant bot update - if depBot.Match([]byte(s)) { // - Debug("#%v DEPENDABOT %v", i, s) - u := strings.SplitN(s, " ", 2) - depUpdates = append(depUpdates, u[1]) - } else { // add it to the all updates slice - Debug("#%v ALL UPDATES %v", i, s) - u := strings.SplitN(s, " ", 2) - allUpdates = append(allUpdates, u[1]) - - //helm chart updates - if helmRegex.Match([]byte(s)) { - u := strings.SplitN(s, " ", 2) - helmUpdates = append(helmUpdates, u[1]) - } - } - - } - } - - helmUpdates = append(helmUpdates, fmt.Sprintf("Update Ingress-Nginx version %s", newReleaseNotes.NewControllerVersion)) - - newReleaseNotes.Updates = allUpdates - newReleaseNotes.DepUpdates = depUpdates - newReleaseNotes.HelmUpdates = helmUpdates - - // Get the latest controller image digests from k8s.io promoter - imagesYaml, err := downloadFile(IMAGES_YAML) - if err != nil { - ErrorF("Could not download file %s : %s", IMAGES_YAML, err) - return nil, err - } - Debug("%s", imagesYaml) - - data := ImageYamls{} - - err = yaml.Unmarshal([]byte(imagesYaml), &data) - if err != nil { - ErrorF("Could not unmarshal images yaml %s", err) - return nil, err - } - - //controller - controllerDigest := findImageDigest(data, "controller", newVersion) - if len(controllerDigest) == 0 { - ErrorF("Controller Digest could not be found") - return nil, errors.New("Controller digest could not be found") - } - - controllerChrootDigest := findImageDigest(data, "controller-chroot", newVersion) - if len(controllerChrootDigest) == 0 { - ErrorF("Controller Chroot Digest could not be found") - return nil, errors.New("Controller Chroot digest could not be found") - } - - Debug("Latest Controller Digest %v", controllerDigest) - Debug("Latest Controller Chroot Digest %v", controllerChrootDigest) - c1 := ControllerImage{ - Digest: controllerDigest, - Registry: INGRESS_REGISTRY, - Name: "ingress-nginx/controller", - Tag: fmt.Sprintf("v%s", newReleaseNotes.Version), - } - - c2 := ControllerImage{ - Digest: controllerChrootDigest, - Registry: INGRESS_REGISTRY, - Name: "ingress-nginx/controller-chroot", - Tag: fmt.Sprintf("v%s", newReleaseNotes.Version), - } - - newReleaseNotes.ControllerImages = append(newReleaseNotes.ControllerImages, c1) - newReleaseNotes.ControllerImages = append(newReleaseNotes.ControllerImages, c2) - Debug("New Release Controller Images %s %s", newReleaseNotes.ControllerImages[0].Digest, newReleaseNotes.ControllerImages[1].Digest) - - if DEBUG { - newReleaseNotes.printRelease() - } - - //write it all out to the changelog file - newReleaseNotes.template() - - return &newReleaseNotes, nil -} - func (i ControllerImage) print() string { return fmt.Sprintf("%s/%s:%s@%s", i.Registry, i.Name, i.Tag, i.Digest) } diff --git a/ingressctl/cmd/releaseNotes.go b/ingressctl/cmd/releaseNotes.go new file mode 100644 index 000000000..b05247b81 --- /dev/null +++ b/ingressctl/cmd/releaseNotes.go @@ -0,0 +1,147 @@ +package cmd + +import ( + "errors" + "fmt" + "regexp" + "strings" + + "gopkg.in/yaml.v3" +) + +// ReleaseNote - All the pieces of information/documents that get updated during a release +type ReleaseNote struct { + Version string //released version + NewControllerVersion string //the new controller version being release + PreviousControllerVersion string //the previous controller tag/release + ControllerImages []ControllerImage //the full image digests + DepUpdates []string //list of dependabot updates to put in the changelog + Updates []string //updates with no category + HelmUpdates []string //updates to the ingress-nginx helm chart + NewHelmChartVersion string //update to the helm chart version + PreviousHelmChartVersion string //previous helm chart version +} + +// Generate Release Notes +func ControllerReleaseNotes(newVersion string) (*ReleaseNote, error) { + var newReleaseNotes = ReleaseNote{} + + newReleaseNotes.Version = newVersion + allControllerTags := getAllControllerTags() + + //new version + newReleaseNotes.NewControllerVersion = allControllerTags[0] + newControllerVersion := fmt.Sprintf("controller-v%s", newVersion) + + //the newControllerVersion should match the latest tag + if newControllerVersion != allControllerTags[0] { + return nil, errors.New(fmt.Sprintf("Generating release new version %s didnt match the current latest tag %s", newControllerVersion, allControllerTags[0])) + } + //previous version + newReleaseNotes.PreviousControllerVersion = allControllerTags[1] + + Info("New Version: %s Old Version: %s", newReleaseNotes.NewControllerVersion, newReleaseNotes.PreviousControllerVersion) + + allUpdates, depUpdates, helmUpdates := getCommitUpdates(newVersion) + + newReleaseNotes.Updates = allUpdates + newReleaseNotes.DepUpdates = depUpdates + newReleaseNotes.HelmUpdates = helmUpdates + + // Get the latest controller image digests from k8s.io promoter + imagesYaml, err := downloadFile(IMAGES_YAML) + if err != nil { + ErrorF("Could not download file %s : %s", IMAGES_YAML, err) + return nil, err + } + Debug("%s", imagesYaml) + + data := ImageYamls{} + + err = yaml.Unmarshal([]byte(imagesYaml), &data) + if err != nil { + ErrorF("Could not unmarshal images yaml %s", err) + return nil, err + } + + //controller + controllerDigest := findImageDigest(data, "controller", newVersion) + if len(controllerDigest) == 0 { + ErrorF("Controller Digest could not be found") + return nil, errors.New("Controller digest could not be found") + } + + controllerChrootDigest := findImageDigest(data, "controller-chroot", newVersion) + if len(controllerChrootDigest) == 0 { + ErrorF("Controller Chroot Digest could not be found") + return nil, errors.New("Controller Chroot digest could not be found") + } + + Debug("Latest Controller Digest %v", controllerDigest) + Debug("Latest Controller Chroot Digest %v", controllerChrootDigest) + c1 := ControllerImage{ + Digest: controllerDigest, + Registry: INGRESS_REGISTRY, + Name: "ingress-nginx/controller", + Tag: fmt.Sprintf("v%s", newReleaseNotes.Version), + } + + c2 := ControllerImage{ + Digest: controllerChrootDigest, + Registry: INGRESS_REGISTRY, + Name: "ingress-nginx/controller-chroot", + Tag: fmt.Sprintf("v%s", newReleaseNotes.Version), + } + + newReleaseNotes.ControllerImages = append(newReleaseNotes.ControllerImages, c1) + newReleaseNotes.ControllerImages = append(newReleaseNotes.ControllerImages, c2) + Debug("New Release Controller Images %s %s", newReleaseNotes.ControllerImages[0].Digest, newReleaseNotes.ControllerImages[1].Digest) + + if DEBUG { + newReleaseNotes.printRelease() + } + + //write it all out to the changelog file + newReleaseNotes.template() + + return &newReleaseNotes, nil +} + +func getCommitUpdates(newVersion string) ([]string, []string, []string) { + + commits := commitsBetweenTags() + + //dependency_updates + //all_updates + var allUpdates []string + var depUpdates []string + var helmUpdates []string + prRegex := regexp.MustCompile("\\(#\\d+\\)") + depBot := regexp.MustCompile("^(\\w){1,10} Bump ") + helmRegex := regexp.MustCompile("helm|chart") + for i, s := range commits { + //matches on PR + if prRegex.Match([]byte(s)) { + //matches a dependant bot update + if depBot.Match([]byte(s)) { // + Debug("#%v DEPENDABOT %v", i, s) + u := strings.SplitN(s, " ", 2) + depUpdates = append(depUpdates, u[1]) + } else { // add it to the all updates slice + Debug("#%v ALL UPDATES %v", i, s) + u := strings.SplitN(s, " ", 2) + allUpdates = append(allUpdates, u[1]) + + //helm chart updates + if helmRegex.Match([]byte(s)) { + u := strings.SplitN(s, " ", 2) + helmUpdates = append(helmUpdates, u[1]) + } + } + + } + } + + helmUpdates = append(helmUpdates, fmt.Sprintf("Update Ingress-Nginx version %s", newVersion)) + return allUpdates, depUpdates, helmUpdates +}