Skip to content

Commit

Permalink
Using latest package controller builds in dev bundle (#4793)
Browse files Browse the repository at this point in the history
* using latest package controller builds in dev bundle

Signed-off-by: jonahjon <jonahjones094@gmail.com>

* updating golden bundle files

Signed-off-by: jonahjon <jonahjones094@gmail.com>

* updating to newest packages tag

Signed-off-by: jonahjon <jonahjones094@gmail.com>

* adding update to not use dev builds on dev release branch bundles

Signed-off-by: jonahjon <jonahjones094@gmail.com>

* update golden bundle files

Signed-off-by: jonahjon <jonahjones094@gmail.com>

* golden bundle file

Signed-off-by: jonahjon <jonahjones094@gmail.com>

---------

Signed-off-by: jonahjon <jonahjones094@gmail.com>
  • Loading branch information
jonahjon authored Feb 9, 2023
1 parent 8619ec3 commit ff25652
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 262 deletions.
73 changes: 73 additions & 0 deletions release/pkg/aws/ecr/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ecr
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -101,6 +102,78 @@ func DescribeImagesPaginated(ecrClient *ecr.ECR, describeInput *ecr.DescribeImag
return images, nil
}

// FilterECRRepoByTagPrefix will take a substring, and a repository as input and find the latest pushed image matching that substring.
func FilterECRRepoByTagPrefix(ecrClient *ecr.ECR, repoName, prefix string, hasTag bool) (string, string, error) {
imageDetails, err := DescribeImagesPaginated(ecrClient, &ecr.DescribeImagesInput{
RepositoryName: aws.String(repoName),
})
if len(imageDetails) == 0 {
return "", "", fmt.Errorf("no image details obtained: %v", err)
}
if err != nil {
return "", "", errors.Cause(err)
}
var filteredImageDetails []*ecr.ImageDetail
if hasTag {
filteredImageDetails = imageTagFilter(imageDetails, prefix)
} else {
filteredImageDetails = imageTagFilterWithout(imageDetails, prefix)
}
// In case we don't find any tag substring matches, we still want to populate the bundle with the latest version.
if len(filteredImageDetails) < 1 {
filteredImageDetails = imageDetails
}
version, sha, err := getLastestOCIShaTag(filteredImageDetails)
if err != nil {
return "", "", err
}
return version, sha, nil
}

// imageTagFilter is used when filtering a list of ECR images for a specific tag or tag substring
func imageTagFilter(details []*ecr.ImageDetail, substring string) []*ecr.ImageDetail {
var filteredDetails []*ecr.ImageDetail
for _, detail := range details {
for _, tag := range detail.ImageTags {
if strings.HasPrefix(*tag, substring) {
filteredDetails = append(filteredDetails, detail)
}
}
}
return filteredDetails
}

// imageTagFilterWithout is used when filtering a list of ECR images for images without a specific tag or tag substring
func imageTagFilterWithout(details []*ecr.ImageDetail, substring string) []*ecr.ImageDetail {
var filteredDetails []*ecr.ImageDetail
for _, detail := range details {
for _, tag := range detail.ImageTags {
if !strings.HasPrefix(*tag, substring) {
filteredDetails = append(filteredDetails, detail)
}
}
}
return filteredDetails
}

// getLastestOCIShaTag is used to find the tag/sha of the latest pushed OCI image from a list.
func getLastestOCIShaTag(details []*ecr.ImageDetail) (string, string, error) {
latest := &ecr.ImageDetail{}
latest.ImagePushedAt = &time.Time{}
for _, detail := range details {
if len(details) < 1 || detail.ImagePushedAt == nil || detail.ImageDigest == nil || detail.ImageTags == nil || len(detail.ImageTags) == 0 {
continue
}
if detail.ImagePushedAt != nil && latest.ImagePushedAt.Before(*detail.ImagePushedAt) {
latest = detail
}
}
if reflect.DeepEqual(latest, ecr.ImageDetail{}) {
return "", "", fmt.Errorf("error no images found")
}
return *latest.ImageTags[0], *latest.ImageDigest, nil
}

func GetLatestImageSha(ecrClient *ecr.ECR, repoName string) (string, error) {
imageDetails, err := DescribeImagesPaginated(ecrClient, &ecr.DescribeImagesInput{
RepositoryName: aws.String(repoName),
Expand Down
41 changes: 39 additions & 2 deletions release/pkg/bundles/package-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pkg/errors"

anywherev1alpha1 "github.com/aws/eks-anywhere/release/api/v1alpha1"
"github.com/aws/eks-anywhere/release/pkg/aws/ecr"
"github.com/aws/eks-anywhere/release/pkg/constants"
releasetypes "github.com/aws/eks-anywhere/release/pkg/types"
bundleutils "github.com/aws/eks-anywhere/release/pkg/util/bundles"
Expand All @@ -36,31 +37,56 @@ func GetPackagesBundle(r *releasetypes.ReleaseConfig, imageDigests map[string]st

var sourceBranch string
var componentChecksum string
var Helmtag, Imagetag string
var Helmsha, Imagesha string
var err error
bundleImageArtifacts := map[string]anywherev1alpha1.Image{}
artifactHashes := []string{}

// Find latest Package Dev build for the Helm chart and Image which will always start with `0.0.0` and is built off of the package Github repo main on every commit.
// If we can't find the build starting with our substring, we default to the original dev tag.
if r.DevRelease && !r.DryRun {
Helmtag, Helmsha, err = ecr.FilterECRRepoByTagPrefix(r.SourceClients.ECR.EcrClient, "eks-anywhere-packages", "0.0.0", true)
if err != nil {
fmt.Printf("Error getting dev version helm tag EKS Anywhere package controller, using latest version %v", err)
}
Imagetag, Imagesha, err = ecr.FilterECRRepoByTagPrefix(r.SourceClients.ECR.EcrClient, "eks-anywhere-packages", "v0.0.0", true)
if err != nil {
fmt.Printf("Error getting dev version Image tag EKS Anywhere package controller, using latest version %v", err)
}
}
for _, componentName := range sortedComponentNames {
for _, artifact := range artifacts[componentName] {
if artifact.Image != nil {
imageArtifact := artifact.Image
sourceBranch = imageArtifact.SourcedFromBranch
bundleImageArtifact := anywherev1alpha1.Image{}
if strings.HasSuffix(imageArtifact.AssetName, "helm") {
Digest := imageDigests[imageArtifact.ReleaseImageURI]
if r.DevRelease && Helmsha != "" && Helmtag != "" {
Digest = Helmsha
imageArtifact.ReleaseImageURI = replaceTag(imageArtifact.ReleaseImageURI, Helmtag)
}
assetName := strings.TrimSuffix(imageArtifact.AssetName, "-helm")
bundleImageArtifact = anywherev1alpha1.Image{
Name: assetName,
Description: fmt.Sprintf("Helm chart for %s", assetName),
URI: imageArtifact.ReleaseImageURI,
ImageDigest: imageDigests[imageArtifact.ReleaseImageURI],
ImageDigest: Digest,
}
} else {
Digest := imageDigests[imageArtifact.ReleaseImageURI]
if r.DevRelease && Imagesha != "" && Imagetag != "" {
Digest = Imagesha
imageArtifact.ReleaseImageURI = replaceTag(imageArtifact.ReleaseImageURI, Imagetag)
}
bundleImageArtifact = anywherev1alpha1.Image{
Name: imageArtifact.AssetName,
Description: fmt.Sprintf("Container image for %s image", imageArtifact.AssetName),
OS: imageArtifact.OS,
Arch: imageArtifact.Arch,
URI: imageArtifact.ReleaseImageURI,
ImageDigest: imageDigests[imageArtifact.ReleaseImageURI],
ImageDigest: Digest,
}
}
bundleImageArtifacts[imageArtifact.AssetName] = bundleImageArtifact
Expand Down Expand Up @@ -90,3 +116,14 @@ func GetPackagesBundle(r *releasetypes.ReleaseConfig, imageDigests map[string]st
}
return bundle, nil
}

// replaceTag is used to replace the tag of an Image URI with a string.
func replaceTag(uri, tag string) string {
NewURIList := strings.Split(uri, ":")
if len(NewURIList) < 2 {
return uri
}
NewURIList[len(NewURIList)-1] = tag
uri = strings.Join(NewURIList[:], ":")
return uri
}
7 changes: 5 additions & 2 deletions release/pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func GetSourceImageURI(r *releasetypes.ReleaseConfig, name, repoName string, tag
if r.DevRelease || r.ReleaseEnvironment == "development" {
latestTag = artifactutils.GetLatestUploadDestination(r.BuildRepoBranchName)
if imageTagConfiguration.SourceLatestTagFromECR && !r.DryRun {
latestTag, err = ecr.GetLatestImageSha(r.SourceClients.ECR.EcrClient, repoName)
if (strings.Contains(name, "eks-anywhere-packages") || strings.Contains(name, "ecr-token-refresher")) && r.BuildRepoBranchName != "main" {
latestTag, _, err = ecr.FilterECRRepoByTagPrefix(r.SourceClients.ECR.EcrClient, repoName, "v0.0.0", false)
} else {
latestTag, err = ecr.GetLatestImageSha(r.SourceClients.ECR.EcrClient, repoName)
}
if err != nil {
return "", "", errors.Cause(err)
}
Expand Down Expand Up @@ -283,7 +287,6 @@ func generateFormattedTagPrefix(imageTagFormat string, tagOptions map[string]str
trimmedResult := strings.Trim(result, "<>")
formattedTag = strings.ReplaceAll(formattedTag, result, tagOptions[trimmedResult])
}

return formattedTag
}

Expand Down
Loading

0 comments on commit ff25652

Please sign in to comment.