Skip to content

Commit

Permalink
add minversion to bundle (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisdiamond authored Mar 9, 2023
1 parent ae094cd commit 0e6ec78
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
21 changes: 21 additions & 0 deletions generatebundlefile/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/kms/types"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

api "github.com/aws/eks-anywhere-packages/api/v1alpha1"
Expand All @@ -29,6 +31,7 @@ var (
FullExcludesAnnotation: Excludes,
}
)
var generatedMetadataFields = []string{"creationTimestamp", "generation", "managedFields", "uid", "resourceVersion"}

type BundleGenerateOpt func(config *BundleGenerate)

Expand Down Expand Up @@ -164,3 +167,21 @@ func GetBundleSignature(ctx context.Context, bundle *api.PackageBundle, key stri
}
return base64.StdEncoding.EncodeToString(out.Signature), nil
}

func serializeBundle(bundle *api.PackageBundle) ([]byte, error) {
out, err := json.Marshal(bundle)
if err != nil {
return nil, err
}
raw := make(map[string]interface{})
err = json.Unmarshal(out, &raw)
if err != nil {
return nil, err
}
delete(raw, "status")
meta := raw["metadata"].(map[string]interface{})
for _, f := range generatedMetadataFields {
delete(meta, f)
}
return yaml.Marshal(raw)
}
4 changes: 4 additions & 0 deletions generatebundlefile/bundle_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type Input struct {
Packages []Org `json:"packages,omitempty"`
Name string `json:"name,omitempty"`
KubernetesVersion string `json:"kubernetesVersion,omitempty"`

// +kubebuilder:validation:Optional
// Minimum required packages controller version
MinVersion string `json:"minControllerVersion"`
}

// Org object containing the input file gitHub org and repo locations
Expand Down
3 changes: 3 additions & 0 deletions generatebundlefile/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (c *SDKClients) NewBundleFromInput(Input *Input) (api.PackageBundleSpec, st
version := strings.Split(Input.KubernetesVersion, ".")
name = fmt.Sprintf("v1-%s-%s", version[1], name)
}
if Input.MinVersion != "" {
packageBundleSpec.MinVersion = Input.MinVersion
}
for _, org := range Input.Packages {
for _, project := range org.Projects {
bundlePkg, err := c.NewPackageFromInput(project)
Expand Down
35 changes: 13 additions & 22 deletions generatebundlefile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
cloudwatchtypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
"gopkg.in/yaml.v2"
ctrl "sigs.k8s.io/controller-runtime"

api "github.com/aws/eks-anywhere-packages/api/v1alpha1"
Expand Down Expand Up @@ -206,7 +205,7 @@ func cmdRegion(opts *Options) error {
}
}

//Creating AWS Clients with profile
// Creating AWS Clients with profile
Profile := "default"
val, ok := os.LookupEnv("AWS_PROFILE")
if ok {
Expand Down Expand Up @@ -429,7 +428,9 @@ func cmdGenerate(opts *Options) error {
}
dockerReleaseStruct = &DockerAuth{
Auths: map[string]DockerAuthRegistry{
fmt.Sprintf("public.ecr.aws/%s", clients.ecrPublicClient.SourceRegistry): {clients.ecrPublicClient.AuthConfig},
fmt.Sprintf("public.ecr.aws/%s", clients.ecrPublicClient.SourceRegistry): {
clients.ecrPublicClient.AuthConfig,
},
"public.ecr.aws": {clients.ecrPublicClientRelease.AuthConfig},
},
}
Expand Down Expand Up @@ -484,8 +485,12 @@ func cmdGenerate(opts *Options) error {
}
dockerReleaseStruct = &DockerAuth{
Auths: map[string]DockerAuthRegistry{
fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", clients.stsClient.AccountID, ecrRegion): {clients.ecrClient.AuthConfig},
fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", clients.stsClientRelease.AccountID, ecrRegion): {clients.ecrClientRelease.AuthConfig},
fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", clients.stsClient.AccountID, ecrRegion): {
clients.ecrClient.AuthConfig,
},
fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com", clients.stsClientRelease.AccountID, ecrRegion): {
clients.ecrClientRelease.AuthConfig,
},
},
}
dockerAuth, err = NewAuthFile(dockerReleaseStruct)
Expand Down Expand Up @@ -516,29 +521,15 @@ func cmdGenerate(opts *Options) error {
return nil
}

bundle.Annotations[FullExcludesAnnotation] = Excludes
signature, err := GetBundleSignature(context.Background(), bundle, opts.key)
if err != nil {
BundleLog.Error(err, "Unable to sign bundle with kms key")
os.Exit(1)
}
bundle.Annotations[FullSignatureAnnotation] = signature

//Remove excludes before generating YAML so that registry + repository remains
bundle.ObjectMeta.Annotations[FullExcludesAnnotation] = ""
_, yml, err := sig.GetDigest(bundle, sig.EksaDomain)
if err != nil {
BundleLog.Error(err, "Unable to retrieve and generate Digest from manifest")
os.Exit(1)
}
manifest := make(map[interface{}]interface{})
err = yaml.Unmarshal(yml, &manifest)
if err != nil {
BundleLog.Error(err, "Unable to marshal manifest into yaml bytes")
os.Exit(1)
}
anno := manifest["metadata"].(map[interface{}]interface{})["annotations"].(map[interface{}]interface{})
anno[FullSignatureAnnotation] = signature
anno[FullExcludesAnnotation] = Excludes
yml, err = yaml.Marshal(manifest)
yml, err := serializeBundle(bundle)
if err != nil {
BundleLog.Error(err, "marshaling bundle YAML: %w", err)
os.Exit(1)
Expand Down

0 comments on commit 0e6ec78

Please sign in to comment.