Skip to content

Commit

Permalink
Add configurable workers for each controller, defaults to 10
Browse files Browse the repository at this point in the history
Signed-off-by: Dilip Tadepalli <[email protected]>
  • Loading branch information
dilipmighty245 committed Mar 16, 2024
1 parent f8f0a65 commit c401669
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 33 deletions.
30 changes: 30 additions & 0 deletions cmd/kcp/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package main
import (
"fmt"
"io"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -67,3 +69,31 @@ func printMostImportantFlags(w io.Writer, fss cliflag.NamedFlagSets, cols int, v

cliflag.PrintSections(w, filteredFFS, cols)
}

func parseControllerSettings(flagString string) map[string]int {
settingsMap := make(map[string]int)

// Split the flag string based on commas
settings := strings.Split(flagString, ",")

// Iterate over each setting
for _, setting := range settings {
// Split the setting into key and value based on '='
parts := strings.Split(setting, "=")
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
num, err := strconv.Atoi(value)
if err != nil {
fmt.Println("Error:", err)
continue
}
settingsMap[key] = num
} else {
// Handle invalid format
fmt.Printf("Invalid format for setting: %s\n", setting)
}
}

return settingsMap
}
74 changes: 74 additions & 0 deletions cmd/kcp/help_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2022 The KCP Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"reflect"
"testing"
)

func Test_parseControllerSettings(t *testing.T) {
type args struct {
flagString string
}

expected := map[string]int{
"kcp-workspace": 2,
}

expected2 := map[string]int{
"kcp-workspace": 2,
"kcp-logicalcluster-deletion": 3,
}

emptyMap := map[string]int{}

tests := []struct {
name string
args args
want map[string]int
}{
{
name: "Single Valid flag",
args: args{
flagString: "kcp-workspace=2",
},
want: expected,
},
{
name: "Invalid argument",
args: args{
flagString: "kcp-workspace",
},
want: emptyMap,
},
{
name: "Multiple Valid arguments",
args: args{
flagString: "kcp-workspace=2,kcp-logicalcluster-deletion=3",
},
want: expected2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseControllerSettings(tt.args.flagString); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseControllerSettings() = %v, want %v", got, tt.want)
}
})
}
}
8 changes: 7 additions & 1 deletion cmd/kcp/kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func main() {

// manually extract root directory from flags first as it influence all other flags
rootDir := ".kcp"
controllerThreadsStr := ""
for i, f := range os.Args {
if f == "--root-directory" {
if i < len(os.Args)-1 {
Expand All @@ -76,6 +77,10 @@ func main() {
} else if strings.HasPrefix(f, "--root-directory=") {
rootDir = strings.TrimPrefix(f, "--root-directory=")
}

if strings.HasPrefix(f, "--controller-threads=") {
controllerThreadsStr = strings.TrimPrefix(f, "--controller-threads=")
}
}

serverOptions := options.NewOptions(rootDir)
Expand Down Expand Up @@ -136,7 +141,8 @@ func main() {
}
}

s, err := server.NewServer(completedConfig)
controllerThreads := parseControllerSettings(controllerThreadsStr)
s, err := server.NewServer(completedConfig, server.WithControllerThreads(controllerThreads))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c401669

Please sign in to comment.