Skip to content

Commit

Permalink
feat: parse module name and version from the kcl.mod in mod push
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Apr 28, 2024
1 parent 2166015 commit 3cd2b46
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/go-chi/render v1.0.3
github.com/go-git/go-git/v5 v5.11.0
github.com/go-sql-driver/mysql v1.7.0
github.com/go-test/deep v1.0.3
github.com/go-test/deep v1.0.8
github.com/goccy/go-yaml v1.11.3
github.com/gonvenience/bunt v1.1.1
github.com/gonvenience/neat v1.3.0
Expand Down Expand Up @@ -79,7 +79,7 @@ require (
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
kcl-lang.io/kcl-go v0.8.2
kcl-lang.io/kcl-plugin v0.5.0
kcl-lang.io/kpm v0.8.3-0.20240321122901-fd054837ea5c
kcl-lang.io/kpm v0.8.6-0.20240428063742-f3e75d94c905
kusionstack.io/kube-api v0.1.1
sigs.k8s.io/controller-runtime v0.15.1
)
Expand Down Expand Up @@ -121,7 +121,7 @@ require (
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/go-getter v1.7.3 // indirect
github.com/hashicorp/go-getter v1.7.4 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
Expand Down Expand Up @@ -269,7 +269,7 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/otiai10/copy v1.12.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -279,7 +279,7 @@ require (
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
Expand Down Expand Up @@ -323,5 +323,3 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace kcl-lang.io/kpm => github.com/KusionStack/kpm v0.8.4
15 changes: 9 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I=
github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
Expand Down Expand Up @@ -709,8 +709,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.7.3 h1:bN2+Fw9XPFvOCjB0UOevFIMICZ7G2XSQHzfvLUyOM5E=
github.com/hashicorp/go-getter v1.7.3/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0=
github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs=
github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
Expand Down Expand Up @@ -911,6 +911,7 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc h1:Ak86L+yDSOzKFa7WM5bf5itSOo1e3Xh8bm5YCMUXIjQ=
github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
github.com/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY=
Expand Down Expand Up @@ -968,8 +969,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down Expand Up @@ -1754,6 +1755,8 @@ kcl-lang.io/kcl-go v0.8.2 h1:XbF7NJ2dHXREmZx/dJSvEdMdaOM9NA8ePZj3LdcEZ34=
kcl-lang.io/kcl-go v0.8.2/go.mod h1:KLPIalP4jr19KUqnXWbe2sVNEMEH7EhnsXH+ihnTUQw=
kcl-lang.io/kcl-plugin v0.5.0 h1:eoh6y4l81rwA8yhJXU4hN7YmJeTUNB1nfYCP9OffSxc=
kcl-lang.io/kcl-plugin v0.5.0/go.mod h1:QnZ5OLcyBw5nOnHpChRHtvBq8wvjwiHu/ZZ8j1dfz48=
kcl-lang.io/kpm v0.8.6-0.20240428063742-f3e75d94c905 h1:c3QhaGQG/c6jbX7n1ZRQBe+UbeobTqvsl3ffJ0A0+RM=
kcl-lang.io/kpm v0.8.6-0.20240428063742-f3e75d94c905/go.mod h1:ws42LNStszL2UJ/fA6Zmof+XTAPbrJRTOlThDqB5jsA=
kcl-lang.io/lib v0.8.2 h1:G8Fm+eHSMpVOSNJhd5T41XgoKZjysHRU290q6Xotepk=
kcl-lang.io/lib v0.8.2/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
kusionstack.io/kube-api v0.1.1 h1:ieoZhaUfK78hsyQ7GsU6ZuxBAcVU+ZuKs7vedGkO8sI=
Expand Down
46 changes: 29 additions & 17 deletions pkg/cmd/mod/mod_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand All @@ -15,6 +16,7 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"kcl-lang.io/kpm/pkg/api"

cmdutil "kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/oci"
Expand All @@ -34,24 +36,24 @@ var (

pushExample = i18n.T(`
# Push a module of current OS arch to an OCI Registry using a token
kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 --creds <YOUR_TOKEN>
kusion mod push /path/to/my-module oci://ghcr.io/org --creds <YOUR_TOKEN>
# Push a module of specific OS arch to an OCI Registry using a token
kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --os-arch==darwin/arm64 --version=1.0.0 --creds <YOUR_TOKEN>
kusion mod push /path/to/my-module oci://ghcr.io/org --os-arch==darwin/arm64 --creds <YOUR_TOKEN>
# Push a module to an OCI Registry using a credentials in <YOUR_USERNAME>:<YOUR_TOKEN> format.
kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 --creds <YOUR_USERNAME>:<YOUR_TOKEN>
kusion mod push /path/to/my-module oci://ghcr.io/org --creds <YOUR_USERNAME>:<YOUR_TOKEN>
# Push a release candidate without marking it as the latest stable
kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0-rc.1 --latest=false
kusion mod push /path/to/my-module oci://ghcr.io/org --latest=false
# Push a module with custom OCI annotations
kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 \
kusion mod push /path/to/my-module oci://ghcr.io/org \
--annotation='org.opencontainers.image.documentation=https://app.org/docs'
# Push and sign a module with Cosign (the cosign binary must be present in PATH)
export COSIGN_PASSWORD=password
kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 \
kusion mod push /path/to/my-module oci://ghcr.io/org \
--sign=cosign --cosign-key=/path/to/cosign.key`)
)

Expand All @@ -64,7 +66,6 @@ const LatestVersion = "latest"
//
// This structure reduces the transformation to wiring and makes the logic itself easy to unit test.
type PushModFlags struct {
Version string
Latest bool
OSArch string
Annotations []string
Expand All @@ -83,6 +84,7 @@ type PushModOptions struct {
OCIUrl string
Latest bool
OSArch string
Name string
Version string
Sign string
CosignKey string
Expand Down Expand Up @@ -129,7 +131,6 @@ func NewCmdPush(ioStreams genericiooptions.IOStreams) *cobra.Command {

// AddFlags registers flags for a cli.
func (flags *PushModFlags) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&flags.Version, "version", "v", "", "The version of the module e.g. '1.0.0' or '1.0.0-rc.1'.")
cmd.Flags().StringVar(&flags.OSArch, "os-arch", "", "The os arch of the module e.g. 'darwin/arm64', 'linux/amd64'.")
cmd.Flags().BoolVar(&flags.Latest, "latest", true, "Tags the current version as the latest stable module version.")
cmd.Flags().StringVar(&flags.Credentials, "creds", flags.Credentials,
Expand All @@ -148,11 +149,22 @@ func (flags *PushModFlags) ToOptions(args []string, ioStreams genericiooptions.I
}

// version
version := flags.Version
if _, err := semver.StrictNewVersion(version); err != nil {
kclPkg, err := api.GetKclPackage(args[0])
if err != nil {
return nil, err
}
version := kclPkg.GetVersion()
if _, err = semver.StrictNewVersion(version); err != nil {
return nil, fmt.Errorf("version is not in semver format: %w", err)
}

name := kclPkg.GetPkgName()
if name == "" {
return nil, fmt.Errorf("module name is empty")
}

ociURL := strings.TrimRight(args[1], "/") + "/" + name

// If creds in <token> format, creds must be base64 encoded
if len(flags.Credentials) != 0 && !strings.Contains(flags.Credentials, ":") {
flags.Credentials = base64.StdEncoding.EncodeToString([]byte(flags.Credentials))
Expand Down Expand Up @@ -205,12 +217,13 @@ func (flags *PushModFlags) ToOptions(args []string, ioStreams genericiooptions.I

opt := &PushModOptions{
ModulePath: args[0],
OCIUrl: args[1],
OCIUrl: ociURL,
Latest: flags.Latest,
Sign: flags.Sign,
CosignKey: flags.CosignKey,
Client: client,
Metadata: meta,
Name: name,
Version: version,
IOStreams: ioStreams,
}
Expand All @@ -224,7 +237,10 @@ func (o *PushModOptions) Validate() error {
return fmt.Errorf("no module found at path %s", o.ModulePath)
}

// TODO: add oci url validation
_, err := url.Parse(o.OCIUrl)
if err != nil {
return fmt.Errorf("illegal oci url:%s, %w", o.OCIUrl, err)
}
return nil
}

Expand Down Expand Up @@ -322,10 +338,6 @@ func (o *PushModOptions) buildModule() (string, error) {
pOS := o.Metadata.Platform.OS
pArch := o.Metadata.Platform.Architecture

// OCIUrl example: oci://ghcr.io/org/my-module
split := strings.Split(o.OCIUrl, "/")
name := split[len(split)-1]

if matches, err := filepath.Glob(goFileSearchPattern); err != nil || len(matches) == 0 {
return "", fmt.Errorf("no go source code files found for 'go build' matching %s", goFileSearchPattern)
}
Expand All @@ -335,7 +347,7 @@ func (o *PushModOptions) buildModule() (string, error) {
return "", fmt.Errorf("unable to find executable 'go' binary: %w", err)
}

output := filepath.Join(targetDir, "_dist", pOS, pArch, "kusion-module-"+name+"_"+o.Version)
output := filepath.Join(targetDir, "_dist", pOS, pArch, "kusion-module-"+o.Name+"_"+o.Version)
if strings.Contains(o.OSArch, "windows") {
output += ".exe"
}
Expand Down

0 comments on commit 3cd2b46

Please sign in to comment.