|
7 | 7 | package main |
8 | 8 |
|
9 | 9 | import ( |
| 10 | + "encoding/json" |
10 | 11 | "fmt" |
11 | 12 | "os" |
12 | 13 | "path/filepath" |
13 | 14 | "strings" |
14 | 15 |
|
| 16 | + "github.com/coreos/stream-metadata-go/stream" |
| 17 | + "github.com/coreos/stream-metadata-go/stream/rhcos" |
15 | 18 | corev1 "k8s.io/api/core/v1" |
16 | 19 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
17 | 20 | "sigs.k8s.io/yaml" |
18 | 21 | ) |
19 | 22 |
|
20 | 23 | const ( |
21 | | - streamRHCOSJSON = "data/data/coreos/rhcos.json" |
22 | | - streamFCOSJSON = "data/data/coreos/fcos.json" |
23 | | - streamSCOSJSON = "data/data/coreos/scos.json" |
24 | | - fcosTAG = "okd" |
25 | | - scosTAG = "scos" |
26 | | - dest = "bin/manifests/coreos-bootimages.yaml" |
| 24 | + streamRHCOSJSON = "data/data/coreos/rhcos.json" |
| 25 | + streamFCOSJSON = "data/data/coreos/fcos.json" |
| 26 | + streamSCOSJSON = "data/data/coreos/scos.json" |
| 27 | + streamMarketplaceRHCOSJSON = "data/data/coreos/marketplace-rhcos.json" |
| 28 | + fcosTAG = "okd" |
| 29 | + scosTAG = "scos" |
| 30 | + dest = "bin/manifests/coreos-bootimages.yaml" |
27 | 31 | ) |
28 | 32 |
|
29 | 33 | func run() error { |
30 | | - var streamJSON string |
31 | | - tags, _ := os.LookupEnv("TAGS") |
32 | | - switch { |
33 | | - case strings.Contains(tags, fcosTAG): |
34 | | - streamJSON = streamFCOSJSON |
35 | | - case strings.Contains(tags, scosTAG): |
36 | | - streamJSON = streamSCOSJSON |
37 | | - default: |
38 | | - streamJSON = streamRHCOSJSON |
39 | | - } |
40 | | - bootimages, err := os.ReadFile(streamJSON) |
| 34 | + bootimages, err := getBootImages() |
41 | 35 | if err != nil { |
42 | 36 | return err |
43 | 37 | } |
@@ -79,6 +73,67 @@ func run() error { |
79 | 73 | return nil |
80 | 74 | } |
81 | 75 |
|
| 76 | +func getBootImages() ([]byte, error) { |
| 77 | + var okd bool |
| 78 | + var streamJSON string |
| 79 | + tags, _ := os.LookupEnv("TAGS") |
| 80 | + switch { |
| 81 | + case strings.Contains(tags, fcosTAG): |
| 82 | + streamJSON = streamFCOSJSON |
| 83 | + okd = true |
| 84 | + case strings.Contains(tags, scosTAG): |
| 85 | + streamJSON = streamSCOSJSON |
| 86 | + okd = true |
| 87 | + default: |
| 88 | + streamJSON = streamRHCOSJSON |
| 89 | + } |
| 90 | + |
| 91 | + bootimages, err := os.ReadFile(streamJSON) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + if okd { |
| 97 | + // okd does not yet have marketplace images, so we are done |
| 98 | + return bootimages, nil |
| 99 | + } |
| 100 | + |
| 101 | + return mergeMarketplaceStream(bootimages) |
| 102 | +} |
| 103 | + |
| 104 | +type marketplaceStream map[string]*rhcos.Marketplace |
| 105 | + |
| 106 | +func mergeMarketplaceStream(streamJSON []byte) ([]byte, error) { |
| 107 | + mktStream := marketplaceStream{} |
| 108 | + mktJSON, err := os.ReadFile(streamMarketplaceRHCOSJSON) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("failed to open marketplace file: %w", err) |
| 111 | + } |
| 112 | + if err := json.Unmarshal(mktJSON, &mktStream); err != nil { |
| 113 | + return nil, fmt.Errorf("failed to unmarshal market stream: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + stream := stream.Stream{} |
| 117 | + if err := json.Unmarshal(streamJSON, &stream); err != nil { |
| 118 | + return nil, fmt.Errorf("failed to unmarshal boot image stream: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + for name, arch := range stream.Architectures { |
| 122 | + if mkt, ok := mktStream[name]; ok { |
| 123 | + if arch.RHELCoreOSExtensions == nil { |
| 124 | + arch.RHELCoreOSExtensions = &rhcos.Extensions{} |
| 125 | + } |
| 126 | + arch.RHELCoreOSExtensions.Marketplace = mkt |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + bootImgs, err := json.Marshal(stream) |
| 131 | + if err != nil { |
| 132 | + return nil, fmt.Errorf("failed to marshal merged boot image stream: %w", err) |
| 133 | + } |
| 134 | + return bootImgs, nil |
| 135 | +} |
| 136 | + |
82 | 137 | func main() { |
83 | 138 | if err := run(); err != nil { |
84 | 139 | fmt.Fprintf(os.Stderr, "%s\n", err) |
|
0 commit comments