Skip to content

Commit

Permalink
Add gcp script generator (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Sep 30, 2024
1 parent eaf9823 commit 6649564
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
130 changes: 130 additions & 0 deletions pkg/cmds/gcp_script.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 (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"strings"

"kmodules.xyz/go-containerregistry/name"

"github.com/spf13/cobra"
)

func NewCmdGenerateGCPScript() *cobra.Command {
var (
files []string
nondistro bool
insecure bool
outDir string
)
cmd := &cobra.Command{
Use: "generate-gcp-script",
Short: "Generate GCP Marketplace image syncer script",
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return GenerateGCPScript(files, outDir, nondistro, insecure)
},
}
cmd.Flags().StringSliceVar(&files, "src", files, "List of source files (http url or local file)")
cmd.Flags().BoolVar(&nondistro, "allow-nondistributable-artifacts", nondistro, "Allow pushing non-distributable (foreign) layers")
cmd.Flags().BoolVar(&insecure, "insecure", insecure, "Allow image references to be fetched without TLS")
cmd.Flags().StringVar(&outDir, "output-dir", "", "Output directory")

return cmd
}

var gcpImageMap = map[string]string{
"fluxcd/helm-controller": "flux-helm-controller",
"fluxcd/source-controller": "flux-source-controller",
"ingress-nginx/controller": "ingress-nginx-controller",
"ingress-nginx/kube-webhook-certgen": "ingress-nginx-kube-webhook-certgen",
"kedacore/http-add-on-interceptor": "keda-http-add-on-interceptor",
"kedacore/http-add-on-operator": "keda-http-add-on-operator",
"kedacore/http-add-on-scaler": "keda-http-add-on-scaler",
"sig-storage/livenessprobe": "csi-driver-livenessprobe",
}

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

var buf bytes.Buffer
buf.WriteString(`#!/bin/bash
set -x
if [ -z "${IMAGE_REGISTRY}" ]; then
echo "IMAGE_REGISTRY is not set"
exit 1
fi
if [ -z "${TAG}" ]; then
echo "TAG is not set"
exit 1
fi
`)

for _, img := range images {
// crane push images/cluster-ui.tar $IMAGE_REGISTRY/cluster-ui:0.4.16
ref, err := name.ParseReference(img)
if err != nil {
return err
}
if ref.Tag == "" {
return fmt.Errorf("image %s has no tag", img)
}

buf.WriteString("crane cp")
if nondistro {
buf.WriteString(" --allow-nondistributable-artifacts")
}
if insecure {
buf.WriteString(" --insecure")
}
buf.WriteString(" ")
buf.WriteString(img)
buf.WriteString(" ")
repo := ref.Repository
if repo == "prometheus-operator/prometheus-operator" {
continue
}
if strings.HasPrefix(ref.Repository, "library/") {
repo = ref.Repository[len("library/"):]
}
if v, found := gcpImageMap[repo]; found {
repo = v
}
_, bin := path.Split(repo)

buf.WriteString("$IMAGE_REGISTRY/" + bin + ":$TAG")
buf.WriteRune('\n')
}
err = os.WriteFile(filepath.Join(outdir, "sync-gcp-mp-images.sh"), buf.Bytes(), 0o755)
if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(NewCmdListEditorCharts())
rootCmd.AddCommand(NewCmdListFeatureCharts())
rootCmd.AddCommand(NewCmdGenerateScripts())
rootCmd.AddCommand(NewCmdGenerateGCPScript())
rootCmd.AddCommand(NewCmdCompletion())
rootCmd.AddCommand(v.NewCmdVersion())

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmds/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ set -x
mkdir -p images
OS=$(uname -o)
if [ "${OS}" = "GNU/Linix" ]; then
if [ "${OS}" = "GNU/Linux" ]; then
OS=Linux
fi
ARCH=$(uname -m)
Expand Down Expand Up @@ -221,7 +221,7 @@ if [ -z "${IMAGE_REGISTRY}" ]; then
fi
OS=$(uname -o)
if [ "${OS}" = "GNU/Linix" ]; then
if [ "${OS}" = "GNU/Linux" ]; then
OS=Linux
fi
ARCH=$(uname -m)
Expand Down

0 comments on commit 6649564

Please sign in to comment.