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 #2007 from shijilin0116/console
feat: Kubekey Web Console
- Loading branch information
Showing
117 changed files
with
5,957 additions
and
86 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
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
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,65 @@ | ||
package controllers | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/gorilla/websocket" | ||
kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Cluster struct { | ||
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"` | ||
ApiVersion string `yaml:"apiVersion,omitempty" json:"apiVersion,omitempty"` | ||
metav1.ObjectMeta `yaml:"metadata,omitempty" json:"metadata,omitempty"` | ||
|
||
Spec kubekeyapiv1alpha2.ClusterSpec `yaml:"spec,omitempty" json:"spec,omitempty"` | ||
} | ||
|
||
var Upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true | ||
}, | ||
} | ||
|
||
type WebSocketWriter struct { | ||
WsConn *websocket.Conn | ||
} | ||
|
||
//var clientConn *websocket.Conn | ||
|
||
func SetupCaptureBuffer() *WebSocketWriter { | ||
captureBuffer := &WebSocketWriter{} | ||
readerOut, writerOut, _ := os.Pipe() | ||
os.Stdout = writerOut | ||
outReader := bufio.NewReader(readerOut) | ||
// 新开线程,监听管道信息,并输出到websocket | ||
go func() { | ||
for { | ||
line, _, err := outReader.ReadLine() | ||
if err != nil { | ||
break | ||
} | ||
if captureBuffer.WsConn != nil { | ||
captureBuffer.WsConn.WriteMessage(websocket.TextMessage, line) | ||
} | ||
} | ||
}() | ||
return captureBuffer | ||
} | ||
|
||
func FormatErrorMessage(err error) string { | ||
msg := err.Error() | ||
if !strings.HasPrefix(msg, "error: ") { | ||
msg = fmt.Sprintf("error: %s", msg) | ||
} | ||
if !strings.HasSuffix(msg, "\n") { | ||
msg += "\n" | ||
} | ||
return msg | ||
} |
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,28 @@ | ||
package console | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/kubesphere/kubekey/v3/cmd/kk/cmd/options" | ||
) | ||
|
||
type ConsoleOptions struct { | ||
CommonOptions *options.CommonOptions | ||
} | ||
|
||
func NewConsoleOptions() *ConsoleOptions { | ||
return &ConsoleOptions{ | ||
CommonOptions: options.NewCommonOptions(), | ||
} | ||
} | ||
func NewCmdConsole() *cobra.Command { | ||
o := NewConsoleOptions() | ||
cmd := &cobra.Command{ | ||
Use: "console", | ||
Short: "Start a web console of kubekey", | ||
} | ||
|
||
o.CommonOptions.AddCommonFlag(cmd) | ||
cmd.AddCommand(NewCmdConsoleStart()) | ||
return cmd | ||
} |
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,65 @@ | ||
package console_common | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/gorilla/websocket" | ||
kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Cluster struct { | ||
Kind string `yaml:"kind,omitempty" json:"kind,omitempty"` | ||
ApiVersion string `yaml:"apiVersion,omitempty" json:"apiVersion,omitempty"` | ||
metav1.ObjectMeta `yaml:"metadata,omitempty" json:"metadata,omitempty"` | ||
|
||
Spec kubekeyapiv1alpha2.ClusterSpec `yaml:"spec,omitempty" json:"spec,omitempty"` | ||
} | ||
|
||
var Upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true | ||
}, | ||
} | ||
|
||
type WebSocketWriter struct { | ||
WsConn *websocket.Conn | ||
} | ||
|
||
//var clientConn *websocket.Conn | ||
|
||
func SetupCaptureBuffer() *WebSocketWriter { | ||
captureBuffer := &WebSocketWriter{} | ||
readerOut, writerOut, _ := os.Pipe() | ||
os.Stdout = writerOut | ||
outReader := bufio.NewReader(readerOut) | ||
// 新开线程,监听管道信息,并输出到websocket | ||
go func() { | ||
for { | ||
line, _, err := outReader.ReadLine() | ||
if err != nil { | ||
break | ||
} | ||
if captureBuffer.WsConn != nil { | ||
captureBuffer.WsConn.WriteMessage(websocket.TextMessage, line) | ||
} | ||
} | ||
}() | ||
return captureBuffer | ||
} | ||
|
||
func FormatErrorMessage(err error) string { | ||
msg := err.Error() | ||
if !strings.HasPrefix(msg, "error: ") { | ||
msg = fmt.Sprintf("error: %s", msg) | ||
} | ||
if !strings.HasSuffix(msg, "\n") { | ||
msg += "\n" | ||
} | ||
return msg | ||
} |
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,89 @@ | ||
package operators | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2" | ||
"github.com/kubesphere/kubekey/v3/cmd/kk/cmd/console/console_common" | ||
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common" | ||
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/pipelines" | ||
"gopkg.in/yaml.v3" | ||
"log" | ||
"os" | ||
) | ||
|
||
func AddNode(c *gin.Context, targetDir string, tmpDir string) { | ||
// 升级连接 | ||
clusterName := c.DefaultQuery("clusterName", "") | ||
clientConn, err := console_common.Upgrader.Upgrade(c.Writer, c.Request, nil) | ||
if err != nil { | ||
log.Fatalf("Failed to set websocket upgrade: %v", err) | ||
return | ||
} | ||
// 建立管道stdout->websocket,监听信息 | ||
captureBuffer := console_common.SetupCaptureBuffer() | ||
captureBuffer.WsConn = clientConn | ||
for { | ||
_, readMsg, readErr := clientConn.ReadMessage() | ||
|
||
if readErr != nil { | ||
fmt.Println("websocket后台读取消息出错:", err) | ||
fmt.Println("添加节点失败") | ||
break | ||
} | ||
go func(message []byte) { | ||
// 写入文件 | ||
mkdirErr := os.MkdirAll(fmt.Sprintf("./%s/%s", tmpDir, clusterName), 0755) | ||
if mkdirErr != nil { | ||
fmt.Println("创建目录时出错:", err) | ||
fmt.Println("添加节点失败") | ||
return | ||
} | ||
filePath := fmt.Sprintf("./%s/%s/Cluster-addNode.yaml", tmpDir, clusterName) | ||
err := os.WriteFile(filePath, readMsg, 0644) | ||
if err != nil { | ||
fmt.Println("写入文件时出错:", err) | ||
fmt.Println("添加节点失败") | ||
return | ||
} | ||
// 解析yaml数据到data | ||
var data kubekeyapiv1alpha2.Cluster | ||
unmarshalErr := yaml.Unmarshal(readMsg, &data) | ||
if unmarshalErr != nil { | ||
fmt.Println("websocket解析yaml出错:", unmarshalErr) | ||
fmt.Println("添加节点失败") | ||
return | ||
} | ||
arg := common.Argument{ | ||
FilePath: filePath, | ||
Debug: false, | ||
IgnoreErr: false, | ||
SkipConfirmCheck: true, | ||
SkipPullImages: false, | ||
ContainerManager: data.Spec.Kubernetes.ContainerManager, | ||
Artifact: "", | ||
InstallPackages: false, | ||
} | ||
actionErr := pipelines.AddNodes(arg, "") | ||
if actionErr != nil { | ||
msg := console_common.FormatErrorMessage(actionErr) | ||
fmt.Println(msg) | ||
fmt.Println("添加节点失败") | ||
} else { | ||
fmt.Println("添加节点成功") | ||
mkdirErr := os.MkdirAll(fmt.Sprintf("./%s/%s", targetDir, clusterName), 0755) | ||
if mkdirErr != nil { | ||
fmt.Println("创建目录时出错:", err) | ||
return | ||
} | ||
filePath := fmt.Sprintf("./%s/%s/Cluster.yaml", targetDir, clusterName) | ||
err := os.WriteFile(filePath, readMsg, 0644) | ||
if err != nil { | ||
fmt.Println("写入文件时出错:", err) | ||
return | ||
} | ||
} | ||
}(readMsg) | ||
} | ||
|
||
} |
Oops, something went wrong.