Skip to content

Commit

Permalink
Add ace-up command (#15)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Nov 20, 2024
1 parent 6fa84e7 commit 949c0d8
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 20 deletions.
130 changes: 130 additions & 0 deletions pkg/cmds/ace_up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmds

import (
"fmt"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
)

func NewCmdAceUp() *cobra.Command {
var dir string
cmd := &cobra.Command{
Use: "ace-up",
Short: "Update ace.yaml",
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
aceImageMap := map[string]string{}
err := LoadLatestImageMap(filepath.Join(dir, "imagelist.yaml"), aceImageMap)
if err != nil {
return err
}

tagB3, found := aceImageMap["ghcr.io/appscode/b3"]
if !found {
return fmt.Errorf("no b3 image found in imagelist.yaml")
}

featureMap := map[string]string{}
err = LoadLatestImageMap(filepath.Join(dir, "feature-charts.yaml"), featureMap)
if err != nil {
return err
}
files := []string{
filepath.Join(dir, "imagelist.yaml"),
fmt.Sprintf("https://github.com/kluster-manager/installer/raw/%s/catalog/imagelist.yaml", tagB3),
}
if tag, ok := featureMap["ghcr.io/appscode-charts/kubedb"]; ok {
files = append(files, fmt.Sprintf("https://github.com/kubedb/installer/raw/%s/catalog/imagelist.yaml", tag))
}
if tag, ok := featureMap["ghcr.io/appscode-charts/kubestash"]; ok {
files = append(files, fmt.Sprintf("https://github.com/kubestash/installer/raw/%s/catalog/imagelist.yaml", tag))
}
if tag, ok := featureMap["ghcr.io/appscode-charts/kubevault"]; ok {
files = append(files, fmt.Sprintf("https://github.com/kubestash/installer/raw/%s/catalog/imagelist.yaml", tag))
}
if tag, ok := featureMap["ghcr.io/appscode-charts/capi-catalog"]; ok {
files = append(files, fmt.Sprintf("https://github.com/kluster-api/installer/raw/%s/catalog/imagelist.yaml", tag))
}

var images map[string]string
if imageList, err := GenerateImageList(files, true); err != nil {
return err
} else {
images = ToImageMap(imageList)
}

aceMap, err := LoadImageMap(filepath.Join(dir, "ace.yaml"))
if err != nil {
return err
}

// update ace with images
for img, tag := range images {
tags := aceMap[img]
switch len(tags) {
case 0, 1:
aceMap[img] = []string{tag}
default:
aceMap[img] = sets.List(sets.New[string](aceMap[img]...).Insert(tag))
}
}

return write(ToImageList2(aceMap), filepath.Join(dir, "ace.yaml"))
},
}

cmd.Flags().StringVar(&dir, "dir", "", "Directory containing ace.yaml and imagelist.yaml files")
_ = cobra.MarkFlagRequired(cmd.Flags(), "dir")

return cmd
}

func ToImageList(in map[string]string) []string {
images := make([]string, 0, len(in))
for img, tag := range in {
images = append(images, img+":"+tag)
}
return images
}

func ToImageList2(in map[string][]string) []string {
images := make([]string, 0, len(in))
for img, tags := range in {
for _, tag := range tags {
images = append(images, img+":"+tag)
}
}
sort.Strings(images)
return images
}

func ToImageMap(in []string) map[string]string {
images := make(map[string]string, len(in))
for _, repo := range in {
if img, tag, ok := strings.Cut(repo, ":"); ok {
images[img] = tag
}
}
return images
}
2 changes: 1 addition & 1 deletion pkg/cmds/cve_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (r CVEReport) Strings() []string {

// "Class": "os-pkgs",
func GatherReports(files []string) ([]CVEReport, error) {
images, err := generateImageList(files, false)
images, err := GenerateImageList(files, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmds/gcp_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var gcpImageMap = map[string]string{
}

func GenerateGCPScript(files []string, outdir string, nondistro, insecure bool) error {
images, err := generateImageList(files, true)
images, err := GenerateImageList(files, true)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewRootCmd() *cobra.Command {

rootCmd.AddCommand(NewCmdParseImage())
rootCmd.AddCommand(NewCmdListImages())
rootCmd.AddCommand(NewCmdAceUp())
rootCmd.AddCommand(NewCmdListEditorCharts())
rootCmd.AddCommand(NewCmdListFeatureCharts())
rootCmd.AddCommand(NewCmdGenerateScripts())
Expand Down
65 changes: 47 additions & 18 deletions pkg/cmds/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -59,14 +60,15 @@ func NewCmdGenerateScripts() *cobra.Command {
return cmd
}

func generateImageList(files []string, uniqueTag bool) ([]string, error) {
func GenerateImageList(files []string, uniqueTag bool) ([]string, error) {
if !uniqueTag {
images := sets.Set[string]{}

for _, file := range files {
list, err := readImageList(file)
list, err := LoadImageList(file)
if err != nil {
return nil, fmt.Errorf("failed to read image list from %s: %w", file, err)
klog.ErrorS(err, fmt.Sprintf("failed to read image list from %s", file))
continue
}
images.Insert(list...)
}
Expand All @@ -76,19 +78,8 @@ func generateImageList(files []string, uniqueTag bool) ([]string, error) {
images := map[string]string{}

for _, file := range files {
list, err := readImageList(file)
if err != nil {
return nil, fmt.Errorf("failed to read image list from %s: %w", file, err)
}

for _, entry := range list {
img, tag, ok := strings.Cut(entry, ":")
if !ok {
images[entry] = ""
}
if existing, ok := images[img]; !ok || GreaterThan(tag, existing) {
images[img] = tag
}
if err := LoadLatestImageMap(file, images); err != nil {
return nil, err
}
}

Expand All @@ -104,7 +95,45 @@ func generateImageList(files []string, uniqueTag bool) ([]string, error) {
return result, nil
}

func readImageList(file string) ([]string, error) {
func LoadLatestImageMap(file string, images map[string]string) error {
list, err := LoadImageList(file)
if err != nil {
klog.ErrorS(err, fmt.Sprintf("failed to read image list from %s", file))
return nil
}

for _, entry := range list {
img, tag, ok := strings.Cut(entry, ":")
if !ok {
images[entry] = ""
}
if existing, ok := images[img]; !ok || GreaterThan(tag, existing) {
images[img] = tag
}
}
return nil
}

func LoadImageMap(file string) (map[string][]string, error) {
images := map[string][]string{}

list, err := LoadImageList(file)
if err != nil {
klog.ErrorS(err, fmt.Sprintf("failed to read image list from %s", file))
return images, nil
}

for _, entry := range list {
img, tag, ok := strings.Cut(entry, ":")
if !ok {
continue
}
images[img] = append(images[img], tag)
}
return images, nil
}

func LoadImageList(file string) ([]string, error) {
if u, err := url.Parse(file); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
resp, err := http.Get(file)
if err != nil || resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -137,7 +166,7 @@ func readImageList(file string) ([]string, error) {
}

func GenerateScripts(files []string, outdir string, nondistro, insecure bool) error {
images, err := generateImageList(files, false)
images, err := GenerateImageList(files, false)
if err != nil {
return err
}
Expand Down

0 comments on commit 949c0d8

Please sign in to comment.