image: add version and debug field to lookup table (#682)

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2022-12-01 11:51:33 +01:00 committed by GitHub
parent e67f65709f
commit 8004edcc14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 20 deletions

View File

@ -637,11 +637,20 @@ jobs:
shell: bash shell: bash
run: | run: |
echo '{}' > intermediate.json echo '{}' > intermediate.json
jq '.version = "${{ needs.build-settings.outputs.imageVersionUid }}"' intermediate.json > lookup-table.json
cp lookup-table.json intermediate.json
jq '.debug = ${{ inputs.debug }}' intermediate.json > lookup-table.json
cp lookup-table.json intermediate.json
for lut in mkosi.output.*/*/image-upload*.json; do for lut in mkosi.output.*/*/image-upload*.json; do
jq -scS '.[0] * .[1]' intermediate.json "${lut}" > lookup-table.json jq -scS '.[0] * .[1]' intermediate.json "${lut}" > lookup-table.json
cp lookup-table.json intermediate.json cp lookup-table.json intermediate.json
done done
rm -f intermediate.json rm -f intermediate.json
mv lookup-table.json "${{ needs.build-settings.outputs.imageVersionUid }}.json" mv lookup-table.json "${{ needs.build-settings.outputs.imageVersionUid }}.json"
- name: Login to AWS - name: Login to AWS

View File

@ -24,7 +24,7 @@ import (
"github.com/spf13/afero" "github.com/spf13/afero"
) )
// imageLookupTable is a lookup table for image references. // imageInfo is a lookup table for image references.
// //
// Example: // Example:
// //
@ -40,22 +40,49 @@ import (
// }, // },
// "qemu": { // "qemu": {
// "default": "https://cdn.example.com/image.raw" // "default": "https://cdn.example.com/image.raw"
// } // },
// "version": "1.0.0",
// "debug": false
// } // }
type imageLookupTable map[string]map[string]string type imageInfo struct {
AWS map[string]string `json:"aws,omitempty"`
Azure map[string]string `json:"azure,omitempty"`
GCP map[string]string `json:"gcp,omitempty"`
QEMU map[string]string `json:"qemu,omitempty"`
Debug bool `json:"debug,omitempty"`
Version string `json:"version,omitempty"`
}
// getReference returns the image reference for a given CSP and image variant. // getReference returns the image reference for a given CSP and image variant.
func (l *imageLookupTable) getReference(csp, variant string) (string, error) { func (l *imageInfo) getReference(csp, variant string) (string, error) {
if l == nil { if l == nil {
return "", fmt.Errorf("image lookup table is nil") return "", fmt.Errorf("image info is nil")
} }
if _, ok := (*l)[csp]; !ok {
var cspList map[string]string
switch cloudprovider.FromString(csp) {
case cloudprovider.AWS:
cspList = l.AWS
case cloudprovider.Azure:
cspList = l.Azure
case cloudprovider.GCP:
cspList = l.GCP
case cloudprovider.QEMU:
cspList = l.QEMU
default:
return "", fmt.Errorf("image not available for CSP %q", csp) return "", fmt.Errorf("image not available for CSP %q", csp)
} }
if _, ok := (*l)[csp][variant]; !ok {
if cspList == nil {
return "", fmt.Errorf("image not available for CSP %q", csp)
}
ref, ok := cspList[variant]
if !ok {
return "", fmt.Errorf("image not available for variant %q", variant) return "", fmt.Errorf("image not available for variant %q", variant)
} }
return (*l)[csp][variant], nil
return ref, nil
} }
// Fetcher fetches image references using a lookup table. // Fetcher fetches image references using a lookup table.
@ -91,11 +118,11 @@ func (f *Fetcher) fetch(ctx context.Context, csp cloudprovider.Provider, version
if err != nil { if err != nil {
return "", fmt.Errorf("fetching image reference: %w", err) return "", fmt.Errorf("fetching image reference: %w", err)
} }
lut := make(imageLookupTable) var info imageInfo
if err := json.Unmarshal(raw, &lut); err != nil { if err := json.Unmarshal(raw, &info); err != nil {
return "", fmt.Errorf("decoding image reference: %w", err) return "", fmt.Errorf("decoding image reference: %w", err)
} }
return lut.getReference(strings.ToLower(csp.String()), variant) return info.getReference(strings.ToLower(csp.String()), variant)
} }
// variant returns the image variant for a given CSP and configuration. // variant returns the image variant for a given CSP and configuration.

View File

@ -29,25 +29,41 @@ func TestMain(m *testing.M) {
func TestGetReference(t *testing.T) { func TestGetReference(t *testing.T) {
testCases := map[string]struct { testCases := map[string]struct {
info *imageInfo
csp, variant string csp, variant string
wantReference string wantReference string
wantErr bool wantErr bool
}{ }{
"reference exists": { "reference exists": {
csp: "someCSP", info: &imageInfo{AWS: map[string]string{"someVariant": "someReference"}},
csp: "aws",
variant: "someVariant", variant: "someVariant",
wantReference: "someReference", wantReference: "someReference",
}, },
"csp does not exist": { "csp does not exist": {
info: &imageInfo{AWS: map[string]string{"someVariant": "someReference"}},
csp: "nonExistingCSP", csp: "nonExistingCSP",
variant: "someVariant", variant: "someVariant",
wantErr: true, wantErr: true,
}, },
"variant does not exist": { "variant does not exist": {
csp: "someCSP", info: &imageInfo{AWS: map[string]string{"someVariant": "someReference"}},
csp: "aws",
variant: "nonExistingVariant", variant: "nonExistingVariant",
wantErr: true, wantErr: true,
}, },
"info is nil": {
info: nil,
csp: "aws",
variant: "someVariant",
wantErr: true,
},
"csp is nil": {
info: &imageInfo{AWS: nil},
csp: "aws",
variant: "someVariant",
wantErr: true,
},
} }
for name, tc := range testCases { for name, tc := range testCases {
@ -55,12 +71,8 @@ func TestGetReference(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
lut := &imageLookupTable{ reference, err := tc.info.getReference(tc.csp, tc.variant)
"someCSP": {
"someVariant": "someReference",
},
}
reference, err := lut.getReference(tc.csp, tc.variant)
if tc.wantErr { if tc.wantErr {
assert.Error(err) assert.Error(err)
return return
@ -74,7 +86,7 @@ func TestGetReference(t *testing.T) {
func TestGetReferenceOnNil(t *testing.T) { func TestGetReferenceOnNil(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
var lut *imageLookupTable var lut *imageInfo
_, err := lut.getReference("someCSP", "someVariant") _, err := lut.getReference("someCSP", "someVariant")
assert.Error(err) assert.Error(err)
} }

View File

@ -92,6 +92,8 @@ s3://<BUCKET-NAME>/constellation/v1/images/<IMAGE-VERSION-UID>.json
```json ```json
{ {
"version": "<IMAGE-VERSION-UID>",
"debug": true,
"aws": { "aws": {
"us-east-1": "ami-123", "us-east-1": "ami-123",
"us-west-2": "ami-456", "us-west-2": "ami-456",