Skip to content

Commit

Permalink
feature: enhance vip and resolve conflict when merge release-0.4.0 to…
Browse files Browse the repository at this point in the history
… main

Signed-off-by: wangdepeng <[email protected]>
  • Loading branch information
village-way committed Aug 22, 2024
1 parent f263d4f commit 6d9e4c5
Show file tree
Hide file tree
Showing 20 changed files with 633 additions and 52 deletions.
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
10 changes: 10 additions & 0 deletions deploy/crds/kosmos.io_virtualclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ spec:
items:
type: string
type: array
externalVips:
description: ExternalVips is the external vips of the virtual kubernetes's
control plane
items:
type: string
type: array
kubeconfig:
description: Kubeconfig is the kubeconfig of the virtual kubernetes's
control plane
Expand Down Expand Up @@ -208,6 +214,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
6 changes: 6 additions & 0 deletions pkg/apis/kosmos/v1alpha1/virtualcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type VirtualClusterSpec struct {
// +optional
ExternalIps []string `json:"externalIps,omitempty"`

// ExternalVips is the external vips of the virtual kubernetes's control plane
// +optional
ExternalVips []string `json:"externalVips,omitempty"`

// PromotePolicies definites the policies for promote to the kubernetes's control plane
// +required
PromotePolicies []PromotePolicy `json:"promotePolicies,omitempty"`
Expand Down Expand Up @@ -139,6 +143,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
12 changes: 12 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.

30 changes: 30 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.

12 changes: 12 additions & 0 deletions pkg/kubenest/constants/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ 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
VipKeepalivedComponentName = "keepalived"

ManifestComponentsConfigMap = "components-manifest-cm"

WaitAllPodsRunningTimeoutSeconds = 1800
Expand Down
Loading

0 comments on commit 6d9e4c5

Please sign in to comment.