Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: use keepalvied to add vip for virtual cluster #665

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions cmd/kubenest/node-agent/app/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var (
}
wg sync.WaitGroup

wsAddr []string // websocket client connect address list
WsAddr []string // websocket client connect address list
filePath string // the server path to save upload file
fileName string // local file to upload
params []string // New slice to hold multiple command parameters
Expand All @@ -79,7 +79,7 @@ func cmdCheckRun(cmd *cobra.Command, args []string) error {
headers := http.Header{
"Authorization": {"Basic " + auth},
}
for _, addr := range wsAddr {
for _, addr := range WsAddr {
wg.Add(1)
go func(addr string) {
defer wg.Done()
Expand All @@ -106,15 +106,11 @@ func init() {
// #nosec G402
dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

ClientCmd.PersistentFlags().StringSliceVarP(&wsAddr, "addr", "a", []string{}, "WebSocket address (e.g., host1:port1,host2:port2)")
err := ClientCmd.MarkPersistentFlagRequired("addr")
if err != nil {
return
}
ClientCmd.PersistentFlags().StringSliceVarP(&WsAddr, "addr", "a", []string{}, "WebSocket address (e.g., host1:port1,host2:port2)")

// PreRunE check param
ClientCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
for _, value := range wsAddr {
for _, value := range WsAddr {
if _, exists := uniqueValuesMap[value]; exists {
return errors.New("duplicate values are not allowed")
}
Expand All @@ -137,7 +133,7 @@ func init() {
_ = uploadCmd.MarkFlagRequired("path")

ttyCmd.Flags().StringVarP(&operation, "operation", "o", "", "Operation to perform")
err = ttyCmd.MarkFlagRequired("operation") // Ensure 'operation' flag is required for ttyCmd
err := ttyCmd.MarkFlagRequired("operation") // Ensure 'operation' flag is required for ttyCmd
if err != nil {
return
}
Expand All @@ -157,7 +153,7 @@ func cmdTtyRun(cmd *cobra.Command, args []string) error {
}
cmdStr := fmt.Sprintf("command=%s", operation)
// execute one every wsAddr
for _, addr := range wsAddr {
for _, addr := range WsAddr {
wsURL := fmt.Sprintf("wss://%s/tty/?%s", addr, cmdStr)
fmt.Println("Executing tty:", cmdStr, "on", addr)
err := connectTty(wsURL, headers)
Expand Down Expand Up @@ -294,7 +290,7 @@ func executeWebSocketCommand(auth string) error {
}

// execute one every wsAddr
for _, addr := range wsAddr {
for _, addr := range WsAddr {
wg.Add(1)
go func(addr string) {
defer wg.Done()
Expand All @@ -314,7 +310,7 @@ func uploadFile(filePath, fileName, auth string) error {
headers := http.Header{
"Authorization": {"Basic " + auth},
}
for _, addr := range wsAddr {
for _, addr := range WsAddr {
wg.Add(1)
go func(addr string) {
defer wg.Done()
Expand Down
23 changes: 22 additions & 1 deletion cmd/kubenest/node-agent/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -33,6 +34,7 @@ func initConfig() {
currentDir, _ := os.Getwd()
viper.AddConfigPath(currentDir)
viper.AddConfigPath("/srv/node-agent/agent.env")
viper.SetConfigType("toml")
// If a agent.env file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.Warnf("Load config file error, %s", err)
Expand All @@ -46,8 +48,27 @@ func initConfig() {
}
}

func initWebSocketAddr() {
err := viper.BindPFlag("ADDR", client.ClientCmd.PersistentFlags().Lookup("addr"))
if err != nil {
log.Fatalf("Failed to bind flag: %v", err)
return
}
err = viper.BindEnv("ADDR", "ADDR")
if err != nil {
log.Fatalf("Failed to bind env: %v", err)
return
}
// Initialize addr value from viper
log.Infof(strings.Join(viper.AllKeys(), ","))
if viper.Get("addr") != nil {
client.WsAddr = viper.GetStringSlice("addr")
log.Infof("addr: %v", client.WsAddr)
}
}

func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(initConfig, initWebSocketAddr)

RootCmd.PersistentFlags().StringVarP(&user, "user", "u", "", "Username for authentication")
RootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Password for authentication")
Expand Down
4 changes: 4 additions & 0 deletions deploy/crds/kosmos.io_virtualclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ spec:
updateTime:
format: date-time
type: string
vipMap:
additionalProperties:
type: string
type: object
type: object
required:
- spec
Expand Down
1 change: 1 addition & 0 deletions deploy/virtual-cluster-components-manifest-cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data:
[
{"name": "kube-proxy", "path": "/kosmos/manifest/kube-proxy/*.yaml"},
{"name": "calico", "path": "/kosmos/manifest/calico/*.yaml"},
{"name": "keepalived", "path": "/kosmos/manifest/keepalived/*.yaml"},
]
host-core-dns-components: |
[
Expand Down
11 changes: 11 additions & 0 deletions deploy/virtual-cluster-vip-pool-cm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: kosmos-vip-pool
namespace: kosmos-system
data:
vip-config.yaml: |
# can be use for vc, the ip formate is 192.168.0.1 and 192.168.0.2-192.168.0.10
vipPool:
- 192.168.0.1-192.168.0.10
4 changes: 2 additions & 2 deletions hack/node-agent/init.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

WEB_USER="$WEB_USER" sed -i 's/^WEB_USER=.*/WEB_USER='"$WEB_USER"'/' /app/agent.env
WEB_PASS="$WEB_PASS" sed -i 's/^WEB_PASS=.*/WEB_PASS='"$WEB_PASS"'/' /app/agent.env
WEB_USER="$WEB_USER" sed -i 's/^WEB_USER=.*/WEB_USER="'"$WEB_USER"'"/' /app/agent.env
WEB_PASS="$WEB_PASS" sed -i 's/^WEB_PASS=.*/WEB_PASS="'"$WEB_PASS"'"/' /app/agent.env
sha256sum /app/node-agent > /app/node-agent.sum
sha256sum /host-path/node-agent >> /app/node-agent.sum
rsync -avz /app/ /host-path/
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kosmos/v1alpha1/virtualcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ type VirtualClusterStatus struct {
Port int32 `json:"port,omitempty"`
// +optional
PortMap map[string]int32 `json:"portMap,omitempty"`
// +optional
VipMap map[string]string `json:"vipMap,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/kosmos/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/kubenest/constants/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ const (
ApiServerNetworkProxyAdminPortKey = "apiserver-network-proxy-admin-port"
VirtualClusterPortNum = 5

// vip
VipPoolConfigMapName = "kosmos-vip-pool"
VipPoolKey = "vip-config.yaml"
VcVipStatusKey = "vip-key"
VipKeepAlivedNodeLabelKey = "kosmos.io/keepalived-node"
VipKeepAlivedNodeLabelValue = "true"
VipKeepAlivedNodeRoleKey = "kosmos.io/keepalived-role"
VipKeepAlivedNodeRoleMaster = "master"
VipKeepalivedNodeRoleBackup = "backup"
VipKeepAlivedReplicas = 3

ManifestComponentsConfigMap = "components-manifest-cm"
NodePoolConfigmap = "node-pool"
NodeVirtualclusterState = "virtualcluster"
Expand Down
Loading
Loading