-
Notifications
You must be signed in to change notification settings - Fork 341
Add a new "chaosduck" e2e test tool for leaderelection. #1333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| Copyright 2020 The Knative 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. | ||
| */ | ||
|
|
||
| // The chaosduck binary is an e2e testing tool for leader election, which loads | ||
| // the leader election configuration within the system namespace and | ||
| // periodically kills one of the leader pods for each HA component. | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "log" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "golang.org/x/sync/errgroup" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/client-go/kubernetes" | ||
| kubeclient "knative.dev/pkg/client/injection/kube/client" | ||
| "knative.dev/pkg/controller" | ||
| "knative.dev/pkg/injection" | ||
| "knative.dev/pkg/injection/sharedmain" | ||
| "knative.dev/pkg/signals" | ||
| "knative.dev/pkg/system" | ||
| "knative.dev/pkg/test/ha" | ||
| ) | ||
|
|
||
| // components is a mapping from component name to the collection of leader pod names. | ||
| type components map[string]sets.String | ||
|
|
||
| // buildComponents crawls the list of leases and builds a mapping from component names | ||
| // to the set pod names that hold one or more leases. | ||
| func buildComponents(kc kubernetes.Interface) (components, error) { | ||
| leases, err := kc.CoordinationV1().Leases(system.Namespace()).List(metav1.ListOptions{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cs := components{} | ||
| for _, lease := range leases.Items { | ||
| if lease.Spec.HolderIdentity == nil { | ||
| log.Printf("Found lease %q held by nobody!", lease.Name) | ||
| continue | ||
| } | ||
| pod := strings.SplitN(*lease.Spec.HolderIdentity, "_", 2)[0] | ||
| deploymentName := ha.ExtractDeployment(pod) | ||
| if deploymentName == "" { | ||
| continue | ||
| } | ||
|
|
||
| set, ok := cs[deploymentName] | ||
| if !ok { | ||
| set = sets.NewString() | ||
| cs[deploymentName] = set | ||
| } | ||
| set.Insert(pod) | ||
| } | ||
| return cs, nil | ||
| } | ||
|
|
||
| // quack will kill one of the components leader pods. | ||
| func quack(ctx context.Context, kc kubernetes.Interface, component string, leaders sets.String) error { | ||
| tribute, ok := leaders.PopAny() | ||
|
mattmoor marked this conversation as resolved.
|
||
| if !ok { | ||
| return errors.New("this should not be possible, since components are only created when they have components") | ||
| } | ||
| log.Printf("Quacking at %q leader %q", component, tribute) | ||
|
|
||
| return kc.CoreV1().Pods(system.Namespace()).Delete(tribute, &metav1.DeleteOptions{}) | ||
| } | ||
|
|
||
| // frequency is the frequency with which we kill off leaders. | ||
| const frequency = 30 * time.Second | ||
|
|
||
| func main() { | ||
| ctx := signals.NewContext() | ||
|
|
||
| // We don't expect informers to be set up, but we do expect the client to get attached to ctx. | ||
| ctx, informers := injection.Default.SetupInformers(ctx, sharedmain.ParseAndGetConfigOrDie()) | ||
| if err := controller.StartInformers(ctx.Done(), informers...); err != nil { | ||
| log.Fatalf("Failed to start informers %v", err) | ||
| } | ||
| kc := kubeclient.Get(ctx) | ||
|
|
||
| // Until we are shutdown, build up an index of components and kill | ||
| // of a leader at the specified frequency. | ||
| for { | ||
| select { | ||
| case <-time.After(frequency): | ||
| components, err := buildComponents(kc) | ||
| if err != nil { | ||
| log.Printf("Error building components: %v", err) | ||
| } | ||
| log.Printf("Got components: %#v", components) | ||
|
|
||
| eg, ctx := errgroup.WithContext(ctx) | ||
| for name, leaders := range components { | ||
| name, leaders := name, leaders | ||
| eg.Go(func() error { | ||
| return quack(ctx, kc, name, leaders) | ||
| }) | ||
| } | ||
| if err := eg.Wait(); err != nil { | ||
| log.Printf("Ended iteration with err: %v", err) | ||
| } | ||
|
|
||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.