-
Notifications
You must be signed in to change notification settings - Fork 7k
feat: ApplicationSet controller sharding (#9002) #9568
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
Closed
hcelaloner
wants to merge
1
commit into
argoproj:master
from
hcelaloner:feat/9002-implement-sharding-on-applicationset-controller
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,84 @@ | ||
| package sharding | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/argoproj/argo-cd/v2/common" | ||
| argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" | ||
| "github.com/argoproj/argo-cd/v2/util/env" | ||
| log "github.com/sirupsen/logrus" | ||
| "hash/fnv" | ||
| "math" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ApplicationSetFilter the function used by the controller to filter ApplicationSets that belongs to its shard | ||
| type ApplicationSetFilter func(appset *argoprojiov1alpha1.ApplicationSet) bool | ||
|
|
||
| var noFilter ApplicationSetFilter = func(appset *argoprojiov1alpha1.ApplicationSet) bool { | ||
| return true | ||
| } | ||
|
|
||
| // InferShardFromHostname tries to detect the shard which controller instance manages by its hostname | ||
| // For instance, applicationset-controller-0 manages the shard 0 | ||
| // For instance, applicationset-controller-1 manages the shard 1 | ||
| func InferShardFromHostname(hostnameDetector func() (string, error)) (int, error) { | ||
| hostname, err := hostnameDetector() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| parts := strings.Split(hostname, "-") | ||
| if len(parts) == 1 { | ||
| return 0, fmt.Errorf("hostname should ends with shard number separated by '-' but got: %s", hostname) | ||
| } | ||
| shard, err := strconv.Atoi(parts[len(parts)-1]) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("hostname should ends with shard number separated by '-' but got: %s", hostname) | ||
| } | ||
| return shard, nil | ||
| } | ||
|
|
||
| // InferShard initially tries to detect the shard which controller instance manages by environment variable | ||
| // If not specified, it fallbacks to InferShardFromHostname | ||
| func InferShard(hostnameDetector func() (string, error)) (int, error) { | ||
| shard := env.ParseNumFromEnv(common.EnvApplicationSetControllerShard, -1, -math.MaxInt32, math.MaxInt32) | ||
| if shard < 0 { | ||
| return InferShardFromHostname(hostnameDetector) | ||
| } | ||
| return shard, nil | ||
| } | ||
|
|
||
| func GenerateApplicationSetFilterForStatefulSet(hostnameDetector func() (string, error)) (ApplicationSetFilter, error) { | ||
| replicas := env.ParseNumFromEnv(common.EnvApplicationSetControllerReplicas, 0, 0, math.MaxInt32) | ||
| if replicas <= 1 { | ||
| return noFilter, nil | ||
| } | ||
|
|
||
| shard, err := InferShard(hostnameDetector) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if shard >= replicas { | ||
| return nil, fmt.Errorf("illegal status detected while generating applicastionset filter we have %d replicas but controller assigned to %d shard", replicas, shard) | ||
| } | ||
| log.Debugf("Generating applicationset filter with replicas: %d, shard:%d", replicas, shard) | ||
|
|
||
| return func(appset *argoprojiov1alpha1.ApplicationSet) bool { | ||
| shardOfAppset := 0 | ||
| if appset != nil { | ||
| shardOfAppset = getShardByID(string(appset.UID), replicas) | ||
| } | ||
| return shardOfAppset == shard | ||
| }, nil | ||
| } | ||
|
|
||
| // getShardByID calculates the shard as `id % replicas count` | ||
| func getShardByID(id string, replicas int) int { | ||
| if id == "" { | ||
| return 0 | ||
| } else { | ||
| h := fnv.New32a() | ||
| _, _ = h.Write([]byte(id)) | ||
| return int(h.Sum32() % uint32(replicas)) | ||
| } | ||
| } | ||
This file contains hidden or 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,188 @@ | ||
| package sharding | ||
|
|
||
| import ( | ||
| "errors" | ||
| "github.com/argoproj/argo-cd/v2/common" | ||
| argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" | ||
| "github.com/stretchr/testify/assert" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestInferShardFromHostname(t *testing.T) { | ||
| type args struct { | ||
| hostnameDetector func() (string, error) | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| expectedShard int | ||
| expectingErr bool | ||
| }{ | ||
| { | ||
| name: "Should return error when detector returns an error", | ||
| args: args{hostnameDetector: func() (string, error) { | ||
| return "", errors.New("fake-error") | ||
| }}, | ||
| expectedShard: 0, | ||
| expectingErr: true, | ||
| }, | ||
| { | ||
| name: "should return err when hostname does contain -", | ||
| args: args{hostnameDetector: func() (string, error) { | ||
| return "fakehostname", nil | ||
| }}, | ||
| expectedShard: 0, | ||
| expectingErr: true, | ||
| }, | ||
| { | ||
| name: "Should return error when hostname does not end with -<number>", | ||
| args: args{hostnameDetector: func() (string, error) { | ||
| return "fake-hostname", nil | ||
| }}, | ||
| expectedShard: 0, | ||
| expectingErr: true, | ||
| }, | ||
| { | ||
| name: "Should return shard number successfully", | ||
| args: args{hostnameDetector: func() (string, error) { | ||
| return "fake-hostname-12", nil | ||
| }}, | ||
| expectedShard: 12, | ||
| expectingErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := InferShardFromHostname(tt.args.hostnameDetector) | ||
| assert.Equal(t, tt.expectingErr, err != nil) | ||
| assert.Equalf(t, tt.expectedShard, got, "InferShardFromHostname(%v)", tt.args.hostnameDetector) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestInferShard(t *testing.T) { | ||
| type args struct { | ||
| hostnameDetector func() (string, error) | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| envVars map[string]string | ||
| args args | ||
| expectedShard int | ||
| expectingErr bool | ||
| }{ | ||
| { | ||
| name: "should detect shard number from env successfully", | ||
| envVars: map[string]string{ | ||
| common.EnvApplicationSetControllerShard: "6", | ||
| }, | ||
| args: args{hostnameDetector: func() (string, error) { | ||
| return "fake-hostname-12", nil | ||
| }}, | ||
| expectedShard: 6, | ||
| expectingErr: false, | ||
| }, | ||
| { | ||
| name: "should fallback to hostname based detection when the given shard number is less than zero", | ||
| envVars: map[string]string{ | ||
| common.EnvApplicationSetControllerShard: "-6", | ||
| }, | ||
| args: args{hostnameDetector: func() (string, error) { | ||
| return "fake-hostname-12", nil | ||
| }}, | ||
| expectedShard: 12, | ||
| expectingErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| for k, v := range tt.envVars { | ||
| t.Setenv(k, v) | ||
| } | ||
| got, err := InferShard(tt.args.hostnameDetector) | ||
| assert.Equal(t, tt.expectingErr, err != nil) | ||
| assert.Equalf(t, tt.expectedShard, got, "InferShard(%v)", tt.args.hostnameDetector) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGenerateApplicationSetFilterForStatefulSetShouldReturnNoFilterIfNoReplicaSpecified(t *testing.T) { | ||
| // Given | ||
| mockHostnameDetector := func() (string, error) { | ||
| return "fake-hostname-12", nil | ||
| } | ||
|
|
||
| // When | ||
| filter, err := GenerateApplicationSetFilterForStatefulSet(mockHostnameDetector) | ||
|
|
||
| // Then | ||
| assert.NoError(t, err) | ||
| assert.True(t, reflect.ValueOf(noFilter).Pointer() == reflect.ValueOf(filter).Pointer()) | ||
| } | ||
|
|
||
| func TestGenerateApplicationSetFilterForStatefulSetShouldReturnErrorWhenCouldNotInferShard(t *testing.T) { | ||
| // Given | ||
| t.Setenv(common.EnvApplicationSetControllerReplicas, "10") | ||
| mockHostnameDetector := func() (string, error) { | ||
| return "invalidhostname", nil | ||
| } | ||
|
|
||
| // When | ||
| filter, err := GenerateApplicationSetFilterForStatefulSet(mockHostnameDetector) | ||
|
|
||
| // Then | ||
| assert.Error(t, err) | ||
| assert.Nil(t, filter) | ||
| } | ||
|
|
||
| func TestGenerateApplicationSetFilterForStatefulSetShouldReturnErrorWhenInferredShardGreaterThanReplica(t *testing.T) { | ||
| // Given | ||
| t.Setenv(common.EnvApplicationSetControllerReplicas, "10") | ||
| t.Setenv(common.EnvApplicationSetControllerShard, "11") | ||
| mockHostnameDetector := func() (string, error) { | ||
| return "invalidhostname", nil | ||
| } | ||
|
|
||
| // When | ||
| filter, err := GenerateApplicationSetFilterForStatefulSet(mockHostnameDetector) | ||
|
|
||
| // Then | ||
| assert.Error(t, err) | ||
| assert.Nil(t, filter) | ||
| } | ||
|
|
||
| func TestGenerateApplicationSetFilterForStatefulSetShouldReturnFilterSuccessfully(t *testing.T) { | ||
| // Given | ||
| t.Setenv(common.EnvApplicationSetControllerReplicas, "10") | ||
| mockHostnameDetector := func() (string, error) { | ||
| return "hostname-8", nil | ||
| } | ||
|
|
||
| // When | ||
| filter, err := GenerateApplicationSetFilterForStatefulSet(mockHostnameDetector) | ||
|
|
||
| // Then | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, filter) | ||
|
|
||
| firstAppset := &argoprojiov1alpha1.ApplicationSet{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| UID: types.UID("5"), | ||
| }, | ||
| } | ||
| assert.True(t, filter(firstAppset)) | ||
|
|
||
| secondAppset := &argoprojiov1alpha1.ApplicationSet{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| UID: types.UID("8"), | ||
| }, | ||
| } | ||
| assert.False(t, filter(secondAppset)) | ||
| } | ||
| func TestGetShardByID(t *testing.T) { | ||
| assert.Equal(t, 0, getShardByID("", 10)) | ||
| assert.Equal(t, 8, getShardByID("5", 10)) | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Incorrect conversion between integer types