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

✨ Add configurable workers for individual controllers, defaults to 10 #3093

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024


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=") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

workers is the normal wording, not 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