Skip to content

Commit

Permalink
Split domain and route controllers into own packages
Browse files Browse the repository at this point in the history
issue: #3304
  • Loading branch information
danail-branekov authored and tcdowney committed May 29, 2024
1 parent 7a499b7 commit 802eadd
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 357 deletions.
125 changes: 0 additions & 125 deletions controllers/controllers/networking/cfdomain_controller_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package networking
package domains

import (
"context"
Expand All @@ -26,30 +26,29 @@ import (

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

type CFDomainReconciler struct {
type Reconciler struct {
client client.Client
scheme *runtime.Scheme
log logr.Logger
}

func NewCFDomainReconciler(
func NewReconciler(
client client.Client,
scheme *runtime.Scheme,
log logr.Logger,
) *k8s.PatchingReconciler[korifiv1alpha1.CFDomain, *korifiv1alpha1.CFDomain] {
routeReconciler := CFDomainReconciler{client: client, scheme: scheme, log: log}
routeReconciler := Reconciler{client: client, scheme: scheme, log: log}
return k8s.NewPatchingReconciler[korifiv1alpha1.CFDomain, *korifiv1alpha1.CFDomain](log, client, &routeReconciler)
}

func (r *CFDomainReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder {
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder {
return ctrl.NewControllerManagedBy(mgr).
For(&korifiv1alpha1.CFDomain{})
}
Expand All @@ -58,28 +57,27 @@ func (r *CFDomainReconciler) SetupWithManager(mgr ctrl.Manager) *builder.Builder
//+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=cfdomains/status,verbs=patch
//+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=cfdomains/finalizers,verbs=update

func (r *CFDomainReconciler) ReconcileResource(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) (ctrl.Result, error) {
func (r *Reconciler) ReconcileResource(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) (ctrl.Result, error) {
log := logr.FromContextOrDiscard(ctx)

var err error
readyConditionBuilder := k8s.NewReadyConditionBuilder(cfDomain)
defer func() {
meta.SetStatusCondition(&cfDomain.Status.Conditions, readyConditionBuilder.WithError(err).Build())
}()

if !cfDomain.GetDeletionTimestamp().IsZero() {
return r.finalizeCFDomain(ctx, cfDomain)
}

cfDomain.Status.ObservedGeneration = cfDomain.Generation
log.V(1).Info("set observed generation", "generation", cfDomain.Status.ObservedGeneration)

meta.SetStatusCondition(&cfDomain.Status.Conditions, metav1.Condition{
Type: "Valid",
Status: metav1.ConditionTrue,
Reason: "Valid",
Message: "Valid Domain",
ObservedGeneration: cfDomain.Generation,
})

readyConditionBuilder.Ready()
return ctrl.Result{}, nil
}

func (r *CFDomainReconciler) finalizeCFDomain(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) (ctrl.Result, error) {
func (r *Reconciler) finalizeCFDomain(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) (ctrl.Result, error) {
log := logr.FromContextOrDiscard(ctx).WithName("finalizeCFDomain")

if !controllerutil.ContainsFinalizer(cfDomain, korifiv1alpha1.CFDomainFinalizerName) {
Expand All @@ -92,6 +90,8 @@ func (r *CFDomainReconciler) finalizeCFDomain(ctx context.Context, cfDomain *kor
return ctrl.Result{}, err
}

log.Info("routes", "len", len(domainRoutes))

if len(domainRoutes) == 0 {
if controllerutil.RemoveFinalizer(cfDomain, korifiv1alpha1.CFDomainFinalizerName) {
log.V(1).Info("finalizer removed")
Expand All @@ -111,7 +111,7 @@ func (r *CFDomainReconciler) finalizeCFDomain(ctx context.Context, cfDomain *kor
return ctrl.Result{RequeueAfter: time.Second}, nil
}

func (r *CFDomainReconciler) listRoutesForDomain(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) ([]korifiv1alpha1.CFRoute, error) {
func (r *Reconciler) listRoutesForDomain(ctx context.Context, cfDomain *korifiv1alpha1.CFDomain) ([]korifiv1alpha1.CFRoute, error) {
routesList := korifiv1alpha1.CFRouteList{}
err := r.client.List(ctx, &routesList, client.MatchingFields{shared.IndexRouteDomainQualifiedName: cfDomain.Namespace + "." + cfDomain.Name})
if err != nil {
Expand Down
111 changes: 111 additions & 0 deletions controllers/controllers/networking/domains/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package domains_test

import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("CFDomainReconciler Integration Tests", func() {
var cfDomain *korifiv1alpha1.CFDomain

BeforeEach(func() {
domainNamespace := uuid.NewString()
Expect(adminClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: domainNamespace,
},
})).To(Succeed())

cfDomain = &korifiv1alpha1.CFDomain{
ObjectMeta: metav1.ObjectMeta{
Name: uuid.NewString(),
Namespace: domainNamespace,
Finalizers: []string{
korifiv1alpha1.CFDomainFinalizerName,
},
},
Spec: korifiv1alpha1.CFDomainSpec{
Name: "a" + uuid.NewString() + ".com",
},
}
Expect(adminClient.Create(ctx, cfDomain)).To(Succeed())
})

It("sets the domain Ready status", func() {
Eventually(func(g Gomega) {
g.Expect(adminClient.Get(ctx, client.ObjectKeyFromObject(cfDomain), cfDomain)).To(Succeed())
g.Expect(meta.IsStatusConditionTrue(cfDomain.Status.Conditions, korifiv1alpha1.StatusConditionReady)).To(BeTrue())
}).Should(Succeed())
})

Describe("finalization", func() {
var (
route1Namespace string
route2Namespace string
)
BeforeEach(func() {
route1Namespace = uuid.NewString()
Expect(adminClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: route1Namespace,
},
})).To(Succeed())

route2Namespace = uuid.NewString()
Expect(adminClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: route2Namespace,
},
})).To(Succeed())

Expect(adminClient.Create(ctx, &korifiv1alpha1.CFRoute{
ObjectMeta: metav1.ObjectMeta{
Name: uuid.NewString(),
Namespace: route1Namespace,
},
Spec: korifiv1alpha1.CFRouteSpec{
DomainRef: corev1.ObjectReference{
Name: cfDomain.Name,
Namespace: cfDomain.Namespace,
},
},
})).To(Succeed())

Expect(adminClient.Create(ctx, &korifiv1alpha1.CFRoute{
ObjectMeta: metav1.ObjectMeta{
Name: uuid.NewString(),
Namespace: route2Namespace,
},
Spec: korifiv1alpha1.CFRouteSpec{
DomainRef: corev1.ObjectReference{
Name: cfDomain.Name,
Namespace: cfDomain.Namespace,
},
},
})).To(Succeed())
})

JustBeforeEach(func() {
Expect(adminClient.Delete(ctx, cfDomain)).To(Succeed())
})

It("deletes the domain routes", func() {
Eventually(func(g Gomega) {
routes := &korifiv1alpha1.CFRouteList{}

g.Expect(adminClient.List(ctx, routes, client.InNamespace(route1Namespace))).To(Succeed())
g.Expect(routes.Items).To(BeEmpty())

g.Expect(adminClient.List(ctx, routes, client.InNamespace(route2Namespace))).To(Succeed())
g.Expect(routes.Items).To(BeEmpty())
}).Should(Succeed())
})
})
})
Loading

0 comments on commit 802eadd

Please sign in to comment.