-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Framework for watch/job unittest #137
Merged
Merged
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 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 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,69 @@ | ||
package watch | ||
|
||
import ( | ||
kubeapi "k8s.io/client-go/pkg/api" | ||
batchv1 "k8s.io/client-go/pkg/apis/batch/v1" | ||
"k8s.io/client-go/pkg/fields" | ||
"k8s.io/client-go/pkg/labels" | ||
"k8s.io/client-go/pkg/util/workqueue" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/tools/record" | ||
kvirtv1 "kubevirt.io/kubevirt/pkg/api/v1" | ||
"kubevirt.io/kubevirt/pkg/kubecli" | ||
"kubevirt.io/kubevirt/pkg/virt-controller/services" | ||
) | ||
|
||
func migrationJobSelector() kubeapi.ListOptions { | ||
fieldSelector := fields.Everything() | ||
labelSelector, err := labels.Parse(kvirtv1.DomainLabel) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return kubeapi.ListOptions{FieldSelector: fieldSelector, LabelSelector: labelSelector} | ||
} | ||
|
||
func NewJobController(vmService services.VMService, recorder record.EventRecorder, restClient *rest.RESTClient) (cache.Store, *kubecli.Controller) { | ||
selector := migrationJobSelector() | ||
lw := kubecli.NewListWatchFromClient(restClient, "jobs", kubeapi.NamespaceDefault, selector.FieldSelector, selector.LabelSelector) | ||
return NewJobControllerWithListWatch(vmService, recorder, lw, restClient) | ||
} | ||
|
||
func NewJobControllerWithListWatch(vmService services.VMService, _ record.EventRecorder, lw cache.ListerWatcher, restClient *rest.RESTClient) (cache.Store, *kubecli.Controller) { | ||
|
||
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()) | ||
return kubecli.NewController(lw, queue, &batchv1.Job{}, func(store cache.Store, queue workqueue.RateLimitingInterface) bool { | ||
key, quit := queue.Get() | ||
if quit { | ||
return false | ||
} | ||
defer queue.Done(key) | ||
|
||
// Fetch the latest Job state from cache | ||
obj, exists, err := store.GetByKey(key.(string)) | ||
|
||
if err != nil { | ||
queue.AddRateLimited(key) | ||
return true | ||
} | ||
if exists { | ||
var job *batchv1.Job = obj.(*batchv1.Job) | ||
|
||
if job.Status.Succeeded < 1 { | ||
//Job did not succeed, do not update the vm | ||
return true | ||
} | ||
|
||
name := job.ObjectMeta.Labels["vmname"] | ||
vm, err := vmService.FetchVM(name) | ||
if err != nil { | ||
//TODO proper error handling | ||
queue.AddRateLimited(key) | ||
return true | ||
} | ||
vm.Status.Phase = kvirtv1.Running | ||
putVm(vm, restClient, nil) | ||
} | ||
return true | ||
}) | ||
} |
This file contains 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,170 @@ | ||
package watch | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/facebookgo/inject" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/ghttp" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/tools/cache/testing" | ||
"kubevirt.io/kubevirt/pkg/kubecli" | ||
"kubevirt.io/kubevirt/pkg/logging" | ||
"kubevirt.io/kubevirt/pkg/virt-controller/services" | ||
|
||
corev1 "k8s.io/client-go/pkg/api/v1" | ||
batchv1 "k8s.io/client-go/pkg/apis/batch/v1" | ||
kvirtv1 "kubevirt.io/kubevirt/pkg/api/v1" | ||
) | ||
|
||
var _ = Describe("Migration", func() { | ||
var server *ghttp.Server | ||
var stopChan chan struct{} | ||
var jobController *kubecli.Controller | ||
var lw *framework.FakeControllerSource | ||
var jobCache cache.Store | ||
|
||
var vmService services.VMService | ||
var restClient *rest.RESTClient | ||
var vm *kvirtv1.VM | ||
|
||
logging.DefaultLogger().SetIOWriter(GinkgoWriter) | ||
|
||
BeforeEach(func() { | ||
var g inject.Graph | ||
vmService = services.NewVMService() | ||
server = ghttp.NewServer() | ||
config := rest.Config{} | ||
config.Host = server.URL() | ||
clientSet, _ := kubernetes.NewForConfig(&config) | ||
templateService, _ := services.NewTemplateService("kubevirt/virt-launcher") | ||
restClient, _ = kubecli.GetRESTClientFromFlags(server.URL(), "") | ||
|
||
g.Provide( | ||
&inject.Object{Value: restClient}, | ||
&inject.Object{Value: clientSet}, | ||
&inject.Object{Value: vmService}, | ||
&inject.Object{Value: templateService}, | ||
) | ||
g.Populate() | ||
|
||
stopChan = make(chan struct{}) | ||
lw = framework.NewFakeControllerSource() | ||
jobCache = cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, nil) | ||
|
||
_, jobController = NewJobControllerWithListWatch(vmService, nil, lw, restClient) | ||
|
||
vm = kvirtv1.NewMinimalVM("test-vm") | ||
|
||
// Start the controller | ||
jobController.StartInformer(stopChan) | ||
go jobController.Run(1, stopChan) | ||
}) | ||
|
||
Context("Running job with out migration labels", func() { | ||
It("should not attempt to update the VM", func(done Done) { | ||
|
||
job := &batchv1.Job{} | ||
|
||
// Register the expected REST call | ||
//server.AppendHandlers() | ||
|
||
// Tell the controller that there is a new Migration | ||
lw.Add(job) | ||
|
||
// Wait until we have processed the added item | ||
finishController(jobController, stopChan) | ||
|
||
Expect(len(server.ReceivedRequests())).To(Equal(0)) | ||
close(done) | ||
}, 10) | ||
}) | ||
|
||
Context("Running job with migration labels but no success", func() { | ||
It("should ignore the the VM ", func(done Done) { | ||
|
||
job := &batchv1.Job{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
kvirtv1.DomainLabel: "something", | ||
"vmname": vm.ObjectMeta.Name, | ||
}, | ||
}, | ||
} | ||
|
||
// No registered REST calls | ||
//server.AppendHandlers() | ||
|
||
// Tell the controller that there is a new Job | ||
lw.Add(job) | ||
|
||
// Wait until we have processed the added item | ||
finishController(jobController, stopChan) | ||
|
||
Expect(len(server.ReceivedRequests())).To(Equal(0)) | ||
close(done) | ||
}, 10) | ||
}) | ||
|
||
Context("Running job with migration labels and one success", func() { | ||
It("should update the VM to Running", func(done Done) { | ||
|
||
job := &batchv1.Job{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
kvirtv1.DomainLabel: "something", | ||
"vmname": vm.ObjectMeta.Name, | ||
}, | ||
}, | ||
Status: batchv1.JobStatus{ | ||
Succeeded: 1, | ||
Failed: 0, | ||
Active: 0, | ||
}, | ||
} | ||
|
||
// Register the expected REST call | ||
server.AppendHandlers( | ||
handlerToFetchTestVM(vm), | ||
handlerToUpdateTestVM(vm), | ||
) | ||
|
||
// Tell the controller that there is a new Job | ||
lw.Add(job) | ||
finishController(jobController, stopChan) | ||
Expect(len(server.ReceivedRequests())).To(Equal(2)) | ||
close(done) | ||
}, 10) | ||
}) | ||
|
||
AfterEach(func() { | ||
close(stopChan) | ||
server.Close() | ||
}) | ||
}) | ||
|
||
func handlerToFetchTestVM(vm *kvirtv1.VM) http.HandlerFunc { | ||
return ghttp.CombineHandlers( | ||
ghttp.VerifyRequest("GET", "/apis/kubevirt.io/v1alpha1/namespaces/default/vms/"+vm.ObjectMeta.Name), | ||
ghttp.RespondWithJSONEncoded(http.StatusOK, vm), | ||
) | ||
} | ||
|
||
func handlerToUpdateTestVM(vm *kvirtv1.VM) http.HandlerFunc { | ||
return ghttp.CombineHandlers( | ||
ghttp.VerifyRequest("PUT", "/apis/kubevirt.io/v1alpha1/namespaces/default/vms/"+vm.ObjectMeta.Name), | ||
ghttp.RespondWithJSONEncoded(http.StatusOK, vm), | ||
) | ||
} | ||
|
||
func finishController(jobController *kubecli.Controller, stopChan chan struct{}) { | ||
// Wait until we have processed the added item | ||
|
||
jobController.WaitForSync(stopChan) | ||
jobController.ShutDownQueue() | ||
jobController.WaitUntilDone() | ||
} |
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.
We probably don't want to panic here. make this function return
(kubeapi.ListOptions, error)
and handle the error higher in the call chain.
Also, we definitely want to log this error.
logger := logging.DefaultLogger()
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.
I would say panic is fine, since this really never should fail ...