-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from kubesphere/cloud
Integrate with KubeSphere Cloud
- Loading branch information
Showing
11 changed files
with
575 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/kubesphere/ksbuilder/pkg/cloud" | ||
"github.com/kubesphere/ksbuilder/pkg/config" | ||
) | ||
|
||
type loginOptions struct { | ||
token string | ||
server string | ||
} | ||
|
||
func loginCmd() *cobra.Command { | ||
o := loginOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "login", | ||
Short: "Login to KubeSphere Cloud", | ||
Args: cobra.NoArgs, | ||
RunE: o.login, | ||
} | ||
cmd.Flags().StringVarP(&o.token, "token", "t", "", "API access token") | ||
cmd.Flags().StringVar(&o.server, "server", "https://apis.kubesphere.cloud", "API server address") | ||
return cmd | ||
} | ||
|
||
func (o *loginOptions) login(_ *cobra.Command, _ []string) error { | ||
if o.token == "" { | ||
prompt := promptui.Prompt{ | ||
Label: "Enter API token", | ||
Mask: '*', | ||
Validate: func(input string) error { | ||
if len(strings.TrimSpace(input)) <= 0 { | ||
return errors.New("token can't be empty") | ||
} | ||
return nil | ||
}, | ||
} | ||
result, err := prompt.Run() | ||
if err != nil { | ||
return err | ||
} | ||
o.token = result | ||
} | ||
|
||
if _, err := cloud.NewClient(cloud.WithToken(o.token), cloud.WithServer(o.server)); err != nil { | ||
return fmt.Errorf("login failed: %v", err) | ||
} | ||
if err := config.Write([]byte(o.token), o.server); err != nil { | ||
return err | ||
} | ||
fmt.Println("Login Succeeded") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/kubesphere/ksbuilder/pkg/config" | ||
) | ||
|
||
type logoutOptions struct{} | ||
|
||
func logoutCmd() *cobra.Command { | ||
o := logoutOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "logout", | ||
Short: "Log out from KubeSphere Cloud", | ||
Args: cobra.NoArgs, | ||
RunE: o.logout, | ||
} | ||
return cmd | ||
} | ||
|
||
func (o *logoutOptions) logout(_ *cobra.Command, _ []string) error { | ||
if err := config.Remove(); err != nil { | ||
return err | ||
} | ||
fmt.Println("Logout Succeeded") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"helm.sh/helm/v3/pkg/chart/loader" | ||
"helm.sh/helm/v3/pkg/chartutil" | ||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/kubesphere/ksbuilder/pkg/cloud" | ||
"github.com/kubesphere/ksbuilder/pkg/extension" | ||
) | ||
|
||
type pushOptions struct{} | ||
|
||
func pushCmd() *cobra.Command { | ||
o := pushOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "push", | ||
Short: "Push and submit extensions to KubeSphere Cloud for review", | ||
Long: `NOTE: We will upload static files such as icons and screenshots in the extension to the KubeSphere Cloud separately | ||
and delete the static file directory in the original package to reduce the size of the entire chart`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: o.push, | ||
} | ||
return cmd | ||
} | ||
|
||
func (o *pushOptions) push(_ *cobra.Command, args []string) error { | ||
fmt.Printf("push extension %s\n", args[0]) | ||
|
||
client, err := cloud.NewClient() | ||
if err != nil { | ||
return fmt.Errorf("login failed: %v", err) | ||
} | ||
|
||
tempDir, err := os.MkdirTemp("", "chart") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(tempDir) | ||
if err = extension.WriteFilesToTempDir(args[0], tempDir); err != nil { | ||
return err | ||
} | ||
|
||
metadata, err := extension.LoadMetadata(tempDir) | ||
if err != nil { | ||
return err | ||
} | ||
// upload images to cloud | ||
if needUpload(metadata.Icon) { | ||
resp, err := client.UploadFiles(metadata.Name, metadata.Version, tempDir, metadata.Icon) | ||
if err != nil { | ||
return err | ||
} | ||
metadata.Icon = resp.Files[0].URL | ||
} | ||
screenshots := make([]string, 0) | ||
localScreenshots := make([]string, 0) | ||
for _, p := range metadata.Screenshots { | ||
if needUpload(p) { | ||
localScreenshots = append(localScreenshots, p) | ||
} else { | ||
screenshots = append(screenshots, p) | ||
} | ||
} | ||
if len(localScreenshots) > 0 { | ||
resp, err := client.UploadFiles(metadata.Name, metadata.Version, tempDir, localScreenshots...) | ||
if err != nil { | ||
return err | ||
} | ||
for _, f := range resp.Files { | ||
screenshots = append(screenshots, f.URL) | ||
} | ||
metadata.Screenshots = screenshots | ||
} | ||
// delete static directory to reduce chart package size | ||
if metadata.StaticFileDirectory != "" { | ||
if err = os.RemoveAll(filepath.Join(tempDir, metadata.StaticFileDirectory)); err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
} | ||
|
||
data, err := yaml.Marshal(metadata) | ||
if err != nil { | ||
return err | ||
} | ||
if err = os.WriteFile(tempDir+"/"+extension.MetadataFilename, data, 0644); err != nil { | ||
return err | ||
} | ||
|
||
chartYaml, err := metadata.ToChartYaml() | ||
if err != nil { | ||
return err | ||
} | ||
chartMetadata, err := yaml.Marshal(chartYaml) | ||
if err != nil { | ||
return err | ||
} | ||
if err = os.WriteFile(tempDir+"/Chart.yaml", chartMetadata, 0644); err != nil { | ||
return err | ||
} | ||
|
||
ch, err := loader.LoadDir(tempDir) | ||
if err != nil { | ||
return err | ||
} | ||
chartFilename, err := chartutil.Save(ch, tempDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
uploadExtensionResp, err := client.UploadExtension(metadata.Name, chartFilename) | ||
if err != nil { | ||
return err | ||
} | ||
if err = client.SubmitExtension(uploadExtensionResp.Snapshot.SnapshotID); err != nil { | ||
return err | ||
} | ||
fmt.Println("Pushed to KubeSphere Cloud") | ||
return nil | ||
} | ||
|
||
func needUpload(path string) bool { | ||
if strings.HasPrefix(path, "http://") || | ||
strings.HasPrefix(path, "https://") || | ||
strings.HasPrefix(path, "data:image") { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.