Skip to content

Commit

Permalink
Fix and major refactor of make for releases (aws#722)
Browse files Browse the repository at this point in the history
* Removed KUBE_GIT_VERSION_FILE code for releases

* Fix and major refactor of make for releases

* Fixed broken func

* Commiting to see if problem fixed

* Did dumb things

* Fixed weird go issue

* Finally got number working

* Last commit was a lie

* Fixed docs

* Docs were, in fact, not fixed

* Removed comment out stuff and ran go fmt

* Updated comment explaining docs

* Minor clean up
  • Loading branch information
Kirsten Schumy authored Feb 17, 2022
1 parent cb7936b commit 44d6813
Show file tree
Hide file tree
Showing 22 changed files with 328 additions and 360 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ IMAGE_REPO?=$(if $(AWS_ACCOUNT_ID),$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazo
RELEASE_AWS_PROFILE?=default

IS_BOT?=false
USE_PREV_RELEASE_MANIFEST?=true
USE_PREV_RELEASE_MANIFEST?=false
OPEN_PR?=true
IS_LOCAL_RELEASE_NUMBER_FOR_NEW_RELEASE?=true

RELEASE_GIT_TAG?=v$(RELEASE_BRANCH)-eks-$(RELEASE)
RELEASE_GIT_COMMIT_HASH?=$(shell git rev-parse @)
Expand Down Expand Up @@ -181,7 +182,8 @@ update-release-number:
go vet ./cmd/release/number
go run ./cmd/release/number/main.go \
--branch=$(RELEASE_BRANCH) \
--isBot=$(IS_BOT)
--isBot=$(IS_BOT) \
--openPR=$(OPEN_PR)

.PHONY: update-all-release-numbers
update-all-release-numbers:
Expand All @@ -194,7 +196,8 @@ release-docs:
--branch=$(RELEASE_BRANCH) \
--isBot=$(IS_BOT) \
--usePrevReleaseManifestForComponentTable=$(USE_PREV_RELEASE_MANIFEST) \
--openPR=$(OPEN_PR)
--openPR=$(OPEN_PR) \
--isLocalReleaseNumberForNewRelease=$(IS_LOCAL_RELEASE_NUMBER_FOR_NEW_RELEASE)

.PHONY: only-index-md-from-existing-release-manifest
only-index-md-from-existing-release-manifest:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package internal
package existing_docs

import (
. "../../internal"
. "../internal"
"bytes"
"errors"
"fmt"
Expand All @@ -19,12 +19,10 @@ var (
// If the value of provided 'force' is 'true', does not check for sequential numbering; otherwise, returns error if
// updating the doc would result in non-sequential number.
func UpdateREADME(release *Release, force bool) (DocStatus, error) {
ds := GetEmptyDocStatus()

readmePath := READMEPath
data, err := ioutil.ReadFile(readmePath)
if err != nil {
return ds, fmt.Errorf("failed to read file because error: %v", err)
return GetEmptyDocStatus(), fmt.Errorf("failed to read README because error: %v", err)
}

splitData := bytes.Split(data, linebreak)
Expand Down Expand Up @@ -55,23 +53,21 @@ func UpdateREADME(release *Release, force bool) (DocStatus, error) {
}

if !hasFoundLine {
return ds, errors.New("failed to find line needed to update version tag in README")
return GetEmptyDocStatus(), errors.New("failed to find line needed to update version tag in README")
}

ds = DocStatus{path: readmePath, isAlreadyExisting: true}
ds, _ := InitializeDocStatus(readmePath)
return ds, os.WriteFile(readmePath, bytes.Join(splitData, linebreak), 0644)
}

// UpdateDocsIndex updates the doc's directory index.md file for the current release.
// If the value of provided 'force' is 'true', does not check for sequential numbering; otherwise, returns error if
// updating the doc would result in non-sequential number.
func UpdateDocsIndex(release *Release, force bool) (DocStatus, error) {
ds := GetEmptyDocStatus()

docsIndexPath := DocsIndexPath
data, err := ioutil.ReadFile(docsIndexPath)
if err != nil {
return ds, fmt.Errorf("failed to read file because error: %v", err)
return GetEmptyDocStatus(), fmt.Errorf("failed to read doc index file because error: %v", err)
}

splitData := bytes.Split(data, linebreak)
Expand All @@ -86,7 +82,7 @@ func UpdateDocsIndex(release *Release, force bool) (DocStatus, error) {
continue
}
if bytes.Compare(lineToUpdate, splitData[i+1]) != 0 && !force {
return ds, fmt.Errorf("expected line %q but found %q", lineToUpdate, splitData[i+1])
return GetEmptyDocStatus(), fmt.Errorf("expected line %q but found %q", lineToUpdate, splitData[i+1])
}
splitData[i+1] = []byte("RELEASE=" + release.Number())
}
Expand All @@ -98,7 +94,7 @@ func UpdateDocsIndex(release *Release, force bool) (DocStatus, error) {
for i := 0; i < len(splitData)-1; i++ {
if bytes.Compare(sectionHeader, splitData[i]) == 0 {
if !nextLineMatch.Match(splitData[i+1]) && !force {
return ds, errors.New("non-sequential Version Dependencies list")
return GetEmptyDocStatus(), errors.New("non-sequential Version Dependencies list")
}
appendLine := fmt.Sprintf(
`* [%s](%s/index.md) (%s)`,
Expand All @@ -111,7 +107,7 @@ func UpdateDocsIndex(release *Release, force bool) (DocStatus, error) {
}
}

ds = DocStatus{path: docsIndexPath, isAlreadyExisting: true}
ds, _ := InitializeDocStatus(docsIndexPath)
return ds, os.WriteFile(docsIndexPath, bytes.Join(splitData, linebreak), 0644)
}

Expand Down
39 changes: 39 additions & 0 deletions cmd/release/docs/internal/directories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
. "../../utils"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)

var (
gitRootDirectory = GetGitRootDirectory()

READMEPath = filepath.Join(gitRootDirectory, "README.md")

docsContentsDirectory = filepath.Join(gitRootDirectory, "docs/contents")
DocsIndexPath = filepath.Join(docsContentsDirectory, "index.md")
)

// GetKubernetesReleaseGitTag returns the trimmed value of Kubernetes release GIT_TAG
func GetKubernetesReleaseGitTag(releaseBranch string) (string, error) {
fileOutput, err := ioutil.ReadFile(fmt.Sprintf("%s/projects/kubernetes/release/%s/GIT_TAG", gitRootDirectory, releaseBranch))
if err != nil {
return "", err
}
return strings.TrimSpace(string(fileOutput)), nil
}

// formatReleaseDocsDirectory returns path to the directory for the docs' directory for provided release.
// Expects branch and number to be non-empty values. Returned path is not guaranteed to exist or be valid.
func formatReleaseDocsDirectory(branch, number string) string {
return filepath.Join(docsContentsDirectory, FormatRelativeReleaseDocsDirectory(branch, number))
}

// FormatRelativeReleaseDocsDirectory return relative path to (example: "releases/1-20/1").
// Expects branch and number to be non-empty values.
func FormatRelativeReleaseDocsDirectory(branch, number string) string {
return filepath.Join("releases", branch, number)
}
1 change: 0 additions & 1 deletion cmd/release/docs/internal/docs_pr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package internal

import (
. "../../internal"
. "../../pull_request"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package internal

import (
. "../../utils"
"encoding/json"
"errors"
"fmt"
"log"
"strconv"
"strings"
)

const (
defaultEnvironment = Production
minNumber = 1
Environment = Production
minNumber = 1
)

type Release struct {
branch string
number string
previousNumber string

environment ReleaseEnvironment

// File paths, which are not guaranteed to be valid or existing
KubeGitVersionFilePath string
DocsDirectoryPath string
ProductionReleasePath string
DevelopmentReleasePath string
DocsDirectoryPath string

// Version notations
BranchEKSNumber string // e.g. 1-20-eks-2
Expand All @@ -40,8 +37,8 @@ type Release struct {
VBranchEKSPreviousNumber string // e.g. v1-20-eks-1
VBranchWithDotNumber string // e.g. v1.20-2

// URL for release manifest, which is not guaranteed to be valid or existing
// e.g. https://distro.eks.amazonaws.com/kubernetes-1-20/kubernetes-1-20-eks-2.yaml
// URL for release manifest, which is not guaranteed to be valid or existing.
// Example: https://distro.eks.amazonaws.com/kubernetes-1-20/kubernetes-1-20-eks-2.yaml
ManifestURL string
PreviousManifestURL string
}
Expand All @@ -53,55 +50,42 @@ func NewReleaseWithOverrideNumber(inputBranch string, overrideNumber int) (Relea
if overrideNumber < minNumber {
return Release{}, fmt.Errorf("override number %d cannot be less than %d", overrideNumber, minNumber)
}
return newRelease(inputBranch, defaultEnvironment, &overrideNumber)
}

// NewReleaseWithOverrideEnvironment returns complete Release based on the provided input.
func NewReleaseWithOverrideEnvironment(inputBranch string, inputEnvironment ReleaseEnvironment) (Release, error) {
return newRelease(inputBranch, inputEnvironment, nil)
return newRelease(inputBranch, strconv.Itoa(overrideNumber), strconv.Itoa(overrideNumber-1))
}

// NewRelease returns complete Release based on the provided inputBranch
func NewRelease(inputBranch string) (Release, error) {
return NewReleaseWithOverrideEnvironment(inputBranch, defaultEnvironment)
func NewRelease(inputBranch string, isLocalReleaseNumberForNewRelease bool) (Release, error) {
rn, err := CreateReleaseNumber(inputBranch, Environment)
if err != nil {
return Release{}, fmt.Errorf("error determining number: %v", err)
}

if isLocalReleaseNumberForNewRelease {
return newRelease(inputBranch, rn.Current(), rn.Previous())
} else {
return newRelease(inputBranch, rn.Next(), rn.Current())
}
}

func newRelease(inputBranch string, inputEnvironment ReleaseEnvironment, overrideNumber *int) (Release, error) {
func newRelease(inputBranch string, num, prevNum string) (Release, error) {
inputBranch = strings.TrimSpace(inputBranch)
if len(inputBranch) == 0 {
return Release{}, errors.New("branch cannot be an empty string")
}
var err error

release := Release{
branch: inputBranch,
environment: inputEnvironment,
}

release.KubeGitVersionFilePath = FormatKubeGitVersionFilePath(&release)

if overrideNumber != nil {
release.number, release.previousNumber = convertToNumberAndPrevNumber(*overrideNumber)
} else {
release.previousNumber, err = determinePreviousReleaseNumber(&release)
if err != nil {
return Release{}, fmt.Errorf("error determining previous number: %v", err)
}
release.number, err = determineReleaseNumber(&release)
if err != nil {
return Release{}, fmt.Errorf("error determining number: %v", err)
}
branch: inputBranch,
number: num,
previousNumber: prevNum,
}

release.DocsDirectoryPath = formatReleaseDocsDirectory(release.branch, release.number)
release.ProductionReleasePath = formatEnvironmentReleasePath(release.branch, Production)
release.DevelopmentReleasePath = formatEnvironmentReleasePath(release.branch, Development)

branchEKS := release.branch + "-eks"
release.BranchEKSNumber = fmt.Sprintf("%s-%s", branchEKS, release.number)
release.BranchEKSPreviousNumber = fmt.Sprintf("%s-%s", branchEKS, release.previousNumber)
release.BranchWithDot = strings.Replace(release.branch, "-", ".", 1)
release.BranchWithDotNumber = fmt.Sprintf("%s-%s", release.BranchWithDot, release.number)
release.BranchWithDot = GetBranchWithDotFormat(release.branch)
release.BranchWithDotNumber = GetBranchWithDotAndNumberWithDashFormat(release.BranchWithDot, release.number)
release.EKSBranchNumber = fmt.Sprintf("eks-%s-%s", release.branch, release.number)
release.EKSBranchPreviousNumber = fmt.Sprintf("eks-%s-%s", release.branch, release.previousNumber)
release.K8sBranchEKS = "kubernetes-" + branchEKS
Expand Down Expand Up @@ -142,3 +126,7 @@ func formatReleaseManifestURL(branch, branchEKSNumber string) string {
branch,
branchEKSNumber)
}

func convertToNumberAndPrevNumber(overrideNumber int) (num, prevNum string) {
return strconv.Itoa(overrideNumber), strconv.Itoa(overrideNumber - 1)
}
18 changes: 18 additions & 0 deletions cmd/release/docs/internal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ type DocStatus struct {
isAlreadyExisting bool
}

func InitializeDocStatus(filePath string) (DocStatus, error) {
var isExistingFile bool

_, err := os.Stat(filePath)
if os.IsNotExist(err) {
isExistingFile = false
} else if err == nil {
isExistingFile = true
} else {
return DocStatus{}, fmt.Errorf("encountered error checking file: %v", err)
}
return DocStatus{path: filePath, isAlreadyExisting: isExistingFile}, nil
}

func UndoChanges(docStatuses []DocStatus) {
log.Println("Encountered error processing docs. Attempting to undo all changes... ")
for _, ds := range docStatuses {
Expand Down Expand Up @@ -71,6 +85,10 @@ func GetEmptyDocStatus() DocStatus {
return DocStatus{path: "", isAlreadyExisting: true}
}

func (docStatus *DocStatus) IsAlreadyExisting() bool {
return docStatus.isAlreadyExisting
}

func (docStatus *DocStatus) isEmpty() bool {
return len(docStatus.path) == 0
}
Expand Down
25 changes: 0 additions & 25 deletions cmd/release/docs/internal/templates.go

This file was deleted.

Loading

0 comments on commit 44d6813

Please sign in to comment.