Skip to content

Commit

Permalink
Merge pull request #8 from takeshi-yoshimura/add-operator-test
Browse files Browse the repository at this point in the history
Add operator test
  • Loading branch information
takeshi-yoshimura authored Oct 10, 2023
2 parents 55b6a27 + f494d7f commit 0a94901
Show file tree
Hide file tree
Showing 7 changed files with 813 additions and 17 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ vet: ## Run go vet against code.
.PHONY: operator-test
operator-test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./controllers/... -coverprofile operator-cover.out
go tool cover -html=operator-cover.out -o operator-cover.html

##@ Build

Expand Down Expand Up @@ -279,6 +280,6 @@ uploader-push: uploader-cbuild
docker push $(UPLOADER_IMAGE_TAG_BASE):$(VERSION)

.PHONY: uploader-test
uploader-test: fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./cmd/... -coverprofile uploader-cover.out
uploader-test: fmt vet ## Run tests.
go test ./cmd/... -coverprofile uploader-cover.out
go tool cover -html=uploader-cover.out -o uploader-cover.html
141 changes: 141 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/crd/bases/charts.ibm.com_coredumphandlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ spec:
type: object
type: array
uploaderImage:
default: ghcr.io/ibm/core-dump-operator/core-dump-uploader:0.0.1
default: ghcr.io/ibm/core-dump-operator/core-dump-uploader:v0.0.1
description: UploaderImage is the image for core-dump-uploader to
upload zip files generated by handlerImage containers
type: string
Expand Down
24 changes: 12 additions & 12 deletions controllers/coredumphandler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
corev1apply "k8s.io/client-go/applyconfigurations/core/v1"
metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
"k8s.io/utils/pointer"
ccharts "sigs.k8s.io/controller-runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -61,35 +61,35 @@ type CoreDumpHandlerReconciler struct {
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *CoreDumpHandlerReconciler) Reconcile(ctx context.Context, req ccharts.Request) (ccharts.Result, error) {
func (r *CoreDumpHandlerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
l := logger.WithValues("CoreDumpHandler", req.NamespacedName)
cdu := &chartsv1alpha1.CoreDumpHandler{}
err := r.Get(ctx, req.NamespacedName, cdu)
var requeue = false
if err != nil {
if errors.IsNotFound(err) {
return ccharts.Result{}, nil
return ctrl.Result{}, nil
}
l.Error(err, "Failed: Reconcile, Get", "namespace", req.Namespace, "name", req.Name)
return ccharts.Result{RequeueAfter: 100 * time.Millisecond}, err
return ctrl.Result{RequeueAfter: 100 * time.Millisecond}, err
}

// Add finalizer to instance
if !controllerutil.ContainsFinalizer(cdu, coredumpHandlerFinalizer) {
controllerutil.AddFinalizer(cdu, coredumpHandlerFinalizer)
err = r.Update(ctx, cdu)
if err != nil {
return ccharts.Result{RequeueAfter: 100 * time.Millisecond}, err
return ctrl.Result{RequeueAfter: 100 * time.Millisecond}, err
}
}
if cdu.GetDeletionTimestamp() != nil {
if controllerutil.ContainsFinalizer(cdu, coredumpHandlerFinalizer) {
if requeue, err = r.DeleteCluster(ctx, cdu, l); requeue || err != nil {
return ccharts.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
return ctrl.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
}
if requeue, err = r.DeleteScc(ctx, cdu, l); requeue || err != nil {
return ccharts.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
return ctrl.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
}
controllerutil.RemoveFinalizer(cdu, coredumpHandlerFinalizer)
err = r.Update(ctx, cdu)
Expand All @@ -99,18 +99,18 @@ func (r *CoreDumpHandlerReconciler) Reconcile(ctx context.Context, req ccharts.R
}
} else {
if requeue, err = r.UpdateScc(ctx, cdu, l); requeue || err != nil {
return ccharts.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
return ctrl.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
}
if requeue, err = r.UpdateCluster(ctx, cdu, l); requeue || err != nil {
return ccharts.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
return ctrl.Result{Requeue: requeue, RequeueAfter: 100 * time.Millisecond}, err
}
}
return ccharts.Result{Requeue: requeue}, err
return ctrl.Result{Requeue: requeue}, err
}

// SetupWithManager sets up the controller with the Manager.
func (r *CoreDumpHandlerReconciler) SetupWithManager(mgr ccharts.Manager) error {
return ccharts.NewControllerManagedBy(mgr).
func (r *CoreDumpHandlerReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&chartsv1alpha1.CoreDumpHandler{}).
Owns(&appsv1.DaemonSet{}).
Complete(r)
Expand Down
Loading

0 comments on commit 0a94901

Please sign in to comment.