-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Controller v2 libs review #48
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad0c1eb
Do not review. Already reviewed
pwittrock b9b5807
Controller Event package
pwittrock 08d6ea0
Controller handler package
pwittrock 044e754
Controller Reconcile pkg
pwittrock 66f3905
Controller source package
pwittrock 0358f59
Controller predicate package
pwittrock 973ead5
Controller manager package
pwittrock fbd8207
Controller controller package
pwittrock 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,92 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 controller | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/client-go/util/workqueue" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/internal/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// Options are the arguments for creating a new Controller | ||
type Options struct { | ||
// MaxConcurrentReconciles is the maximum number of concurrent Reconciles which can be run. Defaults to 1. | ||
MaxConcurrentReconciles int | ||
|
||
// Reconcile reconciles an object | ||
Reconcile reconcile.Reconcile | ||
} | ||
|
||
// Controller is a work queue that watches for changes to objects (i.e. Create / Update / Delete events) and | ||
// then reconciles an object (i.e. make changes to ensure the system state matches what is specified in the object). | ||
type Controller interface { | ||
// Reconcile is called to Reconcile an object by Namespace/Name | ||
reconcile.Reconcile | ||
|
||
// Watch takes events provided by a Source and uses the EventHandler to enqueue reconcile.Requests in | ||
// response to the events. | ||
// | ||
// Watch may be provided one or more Predicates to filter events before they are given to the EventHandler. | ||
// Events will be passed to the EventHandler iff all provided Predicates evaluate to true. | ||
Watch(src source.Source, evthdler handler.EventHandler, prct ...predicate.Predicate) error | ||
|
||
// Start starts the controller. Start blocks until stop is closed or a controller has an error starting. | ||
Start(stop <-chan struct{}) error | ||
} | ||
|
||
// New returns a new Controller registered with the Manager. The Manager will ensure that shared Caches have | ||
// been synced before the Controller is Started. | ||
func New(name string, mrg manager.Manager, options Options) (Controller, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if options.Reconcile == nil { | ||
return nil, fmt.Errorf("must specify Reconcile") | ||
} | ||
|
||
if len(name) == 0 { | ||
return nil, fmt.Errorf("must specify Name for Controller") | ||
} | ||
|
||
if options.MaxConcurrentReconciles <= 0 { | ||
options.MaxConcurrentReconciles = 1 | ||
} | ||
|
||
// Inject dependencies into Reconcile | ||
if err := mrg.SetFields(options.Reconcile); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create controller with dependencies set | ||
c := &controller.Controller{ | ||
Do: options.Reconcile, | ||
Cache: mrg.GetCache(), | ||
Config: mrg.GetConfig(), | ||
Scheme: mrg.GetScheme(), | ||
Client: mrg.GetClient(), | ||
Recorder: mrg.GetRecorder(name), | ||
Queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name), | ||
MaxConcurrentReconciles: options.MaxConcurrentReconciles, | ||
Name: name, | ||
} | ||
|
||
// Add the controller as a Manager components | ||
return c, mrg.Add(c) | ||
} |
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,162 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 controller_test | ||
|
||
import ( | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
var _ = Describe("controller", func() { | ||
var reconciled chan reconcile.Request | ||
var stop chan struct{} | ||
|
||
BeforeEach(func() { | ||
stop = make(chan struct{}) | ||
reconciled = make(chan reconcile.Request) | ||
Expect(cfg).NotTo(BeNil()) | ||
}) | ||
|
||
AfterEach(func() { | ||
close(stop) | ||
}) | ||
|
||
Describe("controller", func() { | ||
// TODO(directxman12): write a whole suite of controller-client interaction tests | ||
|
||
It("should reconcile", func(done Done) { | ||
By("Creating the Manager") | ||
cm, err := manager.New(cfg, manager.Options{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Creating the Controller") | ||
instance, err := controller.New("foo-controller", cm, controller.Options{ | ||
Reconcile: reconcile.Func( | ||
func(request reconcile.Request) (reconcile.Result, error) { | ||
reconciled <- request | ||
return reconcile.Result{}, nil | ||
}), | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Watching Resources") | ||
err = instance.Watch(&source.Kind{Type: &appsv1.ReplicaSet{}}, &handler.EnqueueOwner{ | ||
OwnerType: &appsv1.Deployment{}, | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = instance.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.Enqueue{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Starting the Manager") | ||
go func() { | ||
defer GinkgoRecover() | ||
Expect(cm.Start(stop)).NotTo(HaveOccurred()) | ||
}() | ||
|
||
deployment := &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "deployment-name"}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"foo": "bar"}, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "nginx", | ||
Image: "nginx", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
expectedReconcileRequest := reconcile.Request{NamespacedName: types.NamespacedName{ | ||
Namespace: "default", | ||
Name: "deployment-name", | ||
}} | ||
|
||
By("Invoking Reconciling for Create") | ||
deployment, err = clientset.AppsV1().Deployments("default").Create(deployment) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(<-reconciled).To(Equal(expectedReconcileRequest)) | ||
|
||
By("Invoking Reconciling for Update") | ||
newDeployment := deployment.DeepCopy() | ||
newDeployment.Labels = map[string]string{"foo": "bar"} | ||
newDeployment, err = clientset.AppsV1().Deployments("default").Update(newDeployment) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(<-reconciled).To(Equal(expectedReconcileRequest)) | ||
|
||
By("Invoking Reconciling for an OwnedObject when it is created") | ||
replicaset := &appsv1.ReplicaSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "rs-name", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
*metav1.NewControllerRef(deployment, schema.GroupVersionKind{ | ||
Group: "apps", | ||
Version: "v1", | ||
Kind: "Deployment", | ||
}), | ||
}, | ||
}, | ||
Spec: appsv1.ReplicaSetSpec{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"foo": "bar"}, | ||
}, | ||
Template: deployment.Spec.Template, | ||
}, | ||
} | ||
replicaset, err = clientset.AppsV1().ReplicaSets("default").Create(replicaset) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(<-reconciled).To(Equal(expectedReconcileRequest)) | ||
|
||
By("Invoking Reconciling for an OwnedObject when it is updated") | ||
newReplicaset := replicaset.DeepCopy() | ||
newReplicaset.Labels = map[string]string{"foo": "bar"} | ||
newReplicaset, err = clientset.AppsV1().ReplicaSets("default").Update(newReplicaset) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(<-reconciled).To(Equal(expectedReconcileRequest)) | ||
|
||
By("Invoking Reconciling for an OwnedObject when it is deleted") | ||
err = clientset.AppsV1().ReplicaSets("default").Delete(replicaset.Name, &metav1.DeleteOptions{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(<-reconciled).To(Equal(expectedReconcileRequest)) | ||
|
||
By("Invoking Reconciling for Delete") | ||
err = clientset.AppsV1().Deployments("default"). | ||
Delete("deployment-name", &metav1.DeleteOptions{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(<-reconciled).To(Equal(expectedReconcileRequest)) | ||
|
||
close(done) | ||
}, 5) | ||
}) | ||
}) |
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,59 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 controller_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
"sigs.k8s.io/controller-runtime/pkg/test" | ||
) | ||
|
||
func TestSource(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecsWithDefaultAndCustomReporters(t, "Controller Integration Suite", []Reporter{test.NewlineReporter{}}) | ||
} | ||
|
||
var testenv *test.Environment | ||
var cfg *rest.Config | ||
var clientset *kubernetes.Clientset | ||
|
||
var _ = BeforeSuite(func(done Done) { | ||
logf.SetLogger(logf.ZapLogger(false)) | ||
|
||
testenv = &test.Environment{} | ||
|
||
var err error | ||
cfg, err = testenv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
clientset, err = kubernetes.NewForConfig(cfg) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
close(done) | ||
}, 60) | ||
|
||
var _ = AfterSuite(func() { | ||
testenv.Stop() | ||
}) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't abbreviate so much, especially in the interface definition, where people will be looking for documentation.
eventHandler
andpredicates
are fine.