@@ -17,13 +17,15 @@ limitations under the License.
1717package cmd
1818
1919import (
20+ "context"
2021 "encoding/json"
2122 "fmt"
2223 "os"
2324 "path/filepath"
2425
2526 "github.com/SovereignCloudStack/csctl/pkg/clusterstack"
2627 "github.com/SovereignCloudStack/csctl/pkg/github"
28+ "github.com/SovereignCloudStack/csctl/pkg/github/client"
2729 "github.com/SovereignCloudStack/csctl/pkg/hash"
2830 "github.com/SovereignCloudStack/csctl/pkg/template"
2931 "github.com/spf13/cobra"
5052var (
5153 mode string
5254 outputDirectory string
53- // TODO: remove this later.
54- githubReleasePath string
5555)
5656
5757// CreateOptions contains config for creating a release.
@@ -77,12 +77,10 @@ var createCmd = &cobra.Command{
7777func init () {
7878 createCmd .Flags ().StringVarP (& mode , "mode" , "m" , "stable" , "It defines the mode of the cluster stack manager" )
7979 createCmd .Flags ().StringVarP (& outputDirectory , "output" , "o" , "./releases" , "It defines the output directory in which the release artifacts will be generated" )
80- // TODO: remove this later
81- createCmd .Flags ().StringVar (& githubReleasePath , "github-release" , "github-release" , "It is used to get the path to local github release (for stable mode only)" )
8280}
8381
8482// GetCreateOptions create a Create Option for create command.
85- func GetCreateOptions (clusterStackPath string ) (* CreateOptions , error ) {
83+ func GetCreateOptions (ctx context. Context , clusterStackPath string ) (* CreateOptions , error ) {
8684 createOption := & CreateOptions {}
8785
8886 // ClusterAddon config
@@ -106,20 +104,35 @@ func GetCreateOptions(clusterStackPath string) (*CreateOptions, error) {
106104 return nil , fmt .Errorf ("failed to handle hash mode: %w" , err )
107105 }
108106 case stableMode :
109- createOption . Metadata , err = clusterstack . HandleStableMode ( githubReleasePath , createOption . CurrentReleaseHash , createOption . LatestReleaseHash )
107+ gc , err := client . NewFactory (). NewClient ( ctx )
110108 if err != nil {
111- return nil , fmt .Errorf ("failed to handle stable mode : %w" , err )
109+ return nil , fmt .Errorf ("failed to create new github client : %w" , err )
112110 }
113111
114112 // update the metadata kubernetes version with the csmctl.yaml config
115113 createOption .Metadata .Versions .Kubernetes = config .Config .KubernetesVersion
116114
117- // TODO: remove
118- latestReleaseInfoWithHash , err := github .GetLocalReleaseInfoWithHash (githubReleasePath )
115+ latestRepoRelease , err := github .GetLatestReleaseFromRemoteRepository (ctx , mode , & config , gc )
119116 if err != nil {
120- return nil , fmt .Errorf ("failed to get latest github local release: %w" , err )
117+ return nil , fmt .Errorf ("failed to get latest release form remote repository: %w" , err )
118+ }
119+ fmt .Printf ("latest release found: %q\n " , latestRepoRelease )
120+
121+ if latestRepoRelease == "" {
122+ createOption .Metadata .APIVersion = "metadata.clusterstack.x-k8s.io/v1alpha1"
123+ createOption .Metadata .Versions .Kubernetes = config .Config .KubernetesVersion
124+ createOption .Metadata .Versions .ClusterStack = "v1"
125+ createOption .Metadata .Versions .Components .ClusterAddon = "v1"
126+ } else {
127+ if err := github .DownloadReleaseAssets (ctx , latestRepoRelease , "./tmp/releases/" , gc ); err != nil {
128+ return nil , fmt .Errorf ("failed to download release asset: %w" , err )
129+ }
130+
131+ createOption .Metadata , err = clusterstack .HandleStableMode ("./tmp/releases/" , createOption .CurrentReleaseHash , createOption .LatestReleaseHash )
132+ if err != nil {
133+ return nil , fmt .Errorf ("failed to handle stable mode: %w" , err )
134+ }
121135 }
122- createOption .LatestReleaseHash = latestReleaseInfoWithHash .Hash
123136 }
124137
125138 releaseDirName , err := clusterstack .GetClusterStackReleaseDirectoryName (& createOption .Metadata , & createOption .Config )
@@ -132,7 +145,7 @@ func GetCreateOptions(clusterStackPath string) (*CreateOptions, error) {
132145 return createOption , nil
133146}
134147
135- func createAction (_ * cobra.Command , args []string ) error {
148+ func createAction (cmd * cobra.Command , args []string ) error {
136149 if len (args ) != 1 {
137150 return fmt .Errorf ("please provide a valid command, create only accept one argument to path to the cluster stacks" )
138151 }
@@ -143,7 +156,7 @@ func createAction(_ *cobra.Command, args []string) error {
143156 return fmt .Errorf ("mode is not supported please choose from - stable, hash" )
144157 }
145158
146- createOpts , err := GetCreateOptions (clusterStackPath )
159+ createOpts , err := GetCreateOptions (cmd . Context (), clusterStackPath )
147160 if err != nil {
148161 return fmt .Errorf ("failed to create create options: %w" , err )
149162 }
0 commit comments