diff --git a/cmd/latestversion-get.go b/cmd/latestversion-get.go new file mode 100644 index 0000000..06a3031 --- /dev/null +++ b/cmd/latestversion-get.go @@ -0,0 +1,103 @@ +package cmd + +import ( + "context" + "fmt" + + metadata_resolver "github.com/krafton-hq/version-helper/pkg/modules/metadata-resolver" + meta_resolver_service "github.com/krafton-hq/version-helper/pkg/services/meta-resolver-service" + version_object_service "github.com/krafton-hq/version-helper/pkg/services/version-object-service" + "github.com/spf13/cobra" + "github.com/thediveo/enumflag" + "go.uber.org/zap" +) + +func latestVersionGetCommand() *cobra.Command { + var ( + ciHint meta_resolver_service.CiFlag + + namePrefix string + repo string + branch string + namespace string + ) + + cmd := &cobra.Command{ + Use: "get {namePrefix}", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Short: "최신 버전 조회 명령어", + Example: "latestversion get MapDLC", + } + + cmd.Flags().VarP(enumflag.New(&ciHint, "CI 이름", meta_resolver_service.CiFlags, enumflag.EnumCaseInsensitive), + "ci-hint", "c", "CI 힌트") + cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "[REQUIRED] namespace name (ex: mapdlc-metadata)") + cmd.Flags().StringVarP(&repo, "repo", "r", "", "repository name") + cmd.Flags().StringVarP(&branch, "branch", "b", "", "branch name") + + cmd.Run = func(cmd *cobra.Command, args []string) { + namePrefix = args[0] + + var metadata *metadata_resolver.BuildMetadata + if repo == "" && branch == "" { + resolver, err := GetMetaResolver(ciHint) + if err != nil { + zap.S().Infof("Get Resolver Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + zap.S().Debugf("Use Git Metadata Resolver: %s", resolver.String()) + + if !resolver.CheckResolveTarget() { + zap.S().Infof("Check Resolver Target Failed, Please check --ci-hint or Env") + SetExitCode(ExitCodeError) + return + } + + metadata, err = resolver.ResolveBuildMetadata() + if err != nil { + zap.S().Infof("Resolve Build Metadata Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + } + + if repo == "" { + repo = metadata.Repository + } + + if repo == "" { + zap.S().Infof("Repository is empty, Please check --repo or Env") + SetExitCode(ExitCodeError) + return + } + + if branch == "" { + branch = metadata.Branch + } + + if branch == "" { + zap.S().Infof("Branch is empty, Please check --branch or Env") + SetExitCode(ExitCodeError) + return + } + + objName := namePrefix + "-" + repo + "-" + branch + + ctx := context.Background() + + result, err := version_object_service.GetLatestVersion(ctx, objName, namespace) + if err != nil { + zap.S().Infof("Get LatestVersion Object Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + + fmt.Println(result.Status.VersionRef.Name) + } + return cmd +} + +func init() { + latestversionCmd.AddCommand(latestVersionGetCommand()) +} diff --git a/cmd/latestversion-root.go b/cmd/latestversion-root.go new file mode 100644 index 0000000..ef5fa82 --- /dev/null +++ b/cmd/latestversion-root.go @@ -0,0 +1,15 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var latestversionCmd = &cobra.Command{ + Use: "latestversion", + Aliases: []string{"lv"}, + Short: "latestversion 조회 및 업데이트 명령어", +} + +func init() { + rootCmd.AddCommand(latestversionCmd) +} diff --git a/cmd/latestversion-update.go b/cmd/latestversion-update.go new file mode 100644 index 0000000..ee3b52f --- /dev/null +++ b/cmd/latestversion-update.go @@ -0,0 +1,142 @@ +package cmd + +import ( + "context" + "strings" + + redfoxV1alpha1 "github.com/krafton-hq/redfox/pkg/apis/redfox/v1alpha1" + metadata_resolver "github.com/krafton-hq/version-helper/pkg/modules/metadata-resolver" + meta_resolver_service "github.com/krafton-hq/version-helper/pkg/services/meta-resolver-service" + version_object_service "github.com/krafton-hq/version-helper/pkg/services/version-object-service" + "github.com/spf13/cobra" + "github.com/thediveo/enumflag" + "go.uber.org/zap" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +func latestVersionUpdateCommand() *cobra.Command { + var ( + ciHint meta_resolver_service.CiFlag + + namePrefix string + versionRef string + repo string + branch string + namespace string + labels map[string]string + ) + + cmd := &cobra.Command{ + Use: "update {namePrefix} {version}", + Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs), + Short: "최신 버전 추가 또는 업데이트 명령어", + Example: "latestversion update MapDLC abc123", + } + + cmd.Flags().VarP(enumflag.New(&ciHint, "CI 이름", meta_resolver_service.CiFlags, enumflag.EnumCaseInsensitive), + "ci-hint", "c", "CI 힌트") + cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "[REQUIRED] namespace name (ex: mapdlc-metadata)") + cmd.Flags().StringVarP(&repo, "repo", "r", "", "repository name") + cmd.Flags().StringVarP(&branch, "branch", "b", "", "branch name") + cmd.Flags().StringToStringVarP(&labels, "label", "l", map[string]string{}, "latest version label (ex: hello=world)") + + cmd.Run = func(cmd *cobra.Command, args []string) { + namePrefix = args[0] + versionRef = args[1] + + var metadata *metadata_resolver.BuildMetadata + if repo == "" && branch == "" { + resolver, err := GetMetaResolver(ciHint) + if err != nil { + zap.S().Infof("Get Resolver Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + zap.S().Debugf("Use Git Metadata Resolver: %s", resolver.String()) + + if !resolver.CheckResolveTarget() { + zap.S().Infof("Check Resolver Target Failed, Please check --ci-hint or Env") + SetExitCode(ExitCodeError) + return + } + + metadata, err = resolver.ResolveBuildMetadata() + if err != nil { + zap.S().Infof("Resolve Build Metadata Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + } + + if repo == "" { + repo = metadata.Repository + } + + if repo == "" { + zap.S().Infof("Repository is empty, Please check --repo or Env") + SetExitCode(ExitCodeError) + return + } + + if branch == "" { + branch = metadata.Branch + } + + if branch == "" { + zap.S().Infof("Branch is empty, Please check --branch or Env") + SetExitCode(ExitCodeError) + return + } + + objName := namePrefix + "-" + repo + "-" + branch + + ctx := context.Background() + + labels["branch"] = strings.ReplaceAll(branch, "/", "-") + labels["repository"] = repo + + lv := &redfoxV1alpha1.LatestVersion{ + TypeMeta: metav1.TypeMeta{ + Kind: "LatestVersion", + APIVersion: "metadata.sbx-central.io/v1alpha1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: objName, + Labels: labels, + }, + Spec: redfoxV1alpha1.LatestVersionSpec{ + GitRef: redfoxV1alpha1.LatestVersionGitRef{ + Branch: branch, + Repository: repo, + }, + }, + Status: redfoxV1alpha1.LatestVersionStatus{ + VersionRef: redfoxV1alpha1.LatestVersionVersionRef{ + Name: versionRef, + }, + }, + } + + result, err := version_object_service.UploadLatestVersion(ctx, lv, namespace) + if err != nil { + zap.S().Infof("Upload LatestVersion Object Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + + resultYaml, err := yaml.Marshal(result) + if err != nil { + zap.S().Infof("Marshal result LatestVersion Object Failed, error: %s", err.Error()) + SetExitCode(ExitCodeError) + return + } + + zap.S().Infof("Upload LatestVersion Object Success, name: %s", resultYaml) + } + return cmd +} + +func init() { + latestversionCmd.AddCommand(latestVersionUpdateCommand()) +} diff --git a/pkg/modules/git/git.go b/pkg/modules/git/git.go index c7f28ca..7213311 100644 --- a/pkg/modules/git/git.go +++ b/pkg/modules/git/git.go @@ -42,6 +42,11 @@ func GetTag() (string, error) { if err != nil { return "", err } + + if trim(stdout) == "" { + return "v0.0.0", nil + } + return trim(stdout), nil } diff --git a/pkg/modules/metadata-resolver/local.go b/pkg/modules/metadata-resolver/local.go index 63ac9d2..4aa67dc 100644 --- a/pkg/modules/metadata-resolver/local.go +++ b/pkg/modules/metadata-resolver/local.go @@ -35,10 +35,6 @@ func (r *LocalResolver) ResolveBuildMetadata() (*BuildMetadata, error) { return nil, errors.New(fmt.Sprintf("TagParseError: %s", err.Error())) } - if tag == "" { - tag = "v0.0.0" - } - lastVersion, err := semver.NewVersion(tag) if err != nil { return nil, errors.New(fmt.Sprintf("SemverParseError: %s", err.Error())) diff --git a/pkg/modules/version-object/uploader.go b/pkg/modules/version-object/uploader.go index 02b2911..63e7cbe 100644 --- a/pkg/modules/version-object/uploader.go +++ b/pkg/modules/version-object/uploader.go @@ -31,7 +31,7 @@ var versionKind = schema.GroupVersionKind{Group: "metadata.sbx-central.io", Vers func (u *Uploader) Upload(ctx context.Context, version *redfoxV1alpha1.Version) error { latestVersion := u.generateLatestVersion(version) - latestVersion, err := u.applyLatestVersion(ctx, latestVersion) + latestVersion, err := u.ApplyLatestVersion(ctx, latestVersion) if err != nil { return err } @@ -112,7 +112,7 @@ func (u *Uploader) applyVersion(ctx context.Context, version *redfoxV1alpha1.Ver return obj, nil } -func (u *Uploader) applyLatestVersion(ctx context.Context, latestVersion *redfoxV1alpha1.LatestVersion) (*redfoxV1alpha1.LatestVersion, error) { +func (u *Uploader) ApplyLatestVersion(ctx context.Context, latestVersion *redfoxV1alpha1.LatestVersion) (*redfoxV1alpha1.LatestVersion, error) { buf, err := json.Marshal(latestVersion) if err != nil { zap.S().Debugw("Failed to marshal LatestVersion object", "error", err, "name", latestVersion.Name) diff --git a/pkg/services/version-object-service/version-obj.go b/pkg/services/version-object-service/version-obj.go index 5cacffb..42b713e 100644 --- a/pkg/services/version-object-service/version-obj.go +++ b/pkg/services/version-object-service/version-obj.go @@ -3,8 +3,11 @@ package version_object_service import ( "bytes" "context" + "errors" "io/ioutil" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + redfoxV1alpha1 "github.com/krafton-hq/redfox/pkg/apis/redfox/v1alpha1" redfoxScheme "github.com/krafton-hq/redfox/pkg/generated/clientset/versioned/scheme" fox_utils "github.com/krafton-hq/version-helper/pkg/modules/fox-utils" @@ -70,3 +73,35 @@ func UploadVersionObj(ctx context.Context, obj *redfoxV1alpha1.Version) error { uploader := version_object.NewUploader(redFoxClient, redfoxNamespace) return uploader.Upload(ctx, obj) } + +func UploadLatestVersion(ctx context.Context, obj *redfoxV1alpha1.LatestVersion, namespace string) (*redfoxV1alpha1.LatestVersion, error) { + redFoxClient, err := fox_utils.NewRedFoxClient() + if err != nil { + return nil, err + } + + if namespace == "" { + return nil, errors.New("namespace is empty") + } + + uploader := version_object.NewUploader(redFoxClient, namespace) + return uploader.ApplyLatestVersion(ctx, obj) +} + +func GetLatestVersion(ctx context.Context, name, namespace string) (*redfoxV1alpha1.LatestVersion, error) { + redFoxClient, err := fox_utils.NewRedFoxClient() + if err != nil { + return nil, err + } + + if namespace == "" { + return nil, errors.New("namespace is empty") + } + + lv, err := redFoxClient.MetadataV1alpha1().LatestVersions(namespace).Get(ctx, name, metav1.GetOptions{}) + if err != nil { + return nil, err + } + + return lv, nil +}