Skip to content
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

Replace CFRoute Valid/Invalid with the Ready condition #3305

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions api/repositories/route_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ var _ = Describe("RouteRepository", func() {
BeforeEach(func() {
Expect(k8s.Patch(ctx, k8sClient, cfRoute, func() {
cfRoute.Status = korifiv1alpha1.CFRouteStatus{
CurrentStatus: "valid",
Description: "ok",
Destinations: []korifiv1alpha1.Destination{{
GUID: "destination-guid",
AppRef: corev1.LocalObjectReference{
Expand Down
9 changes: 0 additions & 9 deletions controllers/api/v1alpha1/cfroute_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
const (
// Deprecated. Used for removing leftover finalizers
CFRouteFinalizerName = "cfRoute.korifi.cloudfoundry.org"

ValidStatus CurrentStatus = "valid"
InvalidStatus CurrentStatus = "invalid"
)

// Destination defines a target for a CFRoute, does not carry meaning outside of a CF context
Expand Down Expand Up @@ -72,8 +69,6 @@ type CFRouteSpec struct {

// CFRouteStatus defines the observed state of CFRoute
type CFRouteStatus struct {
CurrentStatus CurrentStatus `json:"currentStatus"`
Description string `json:"description"`
// The fully-qualified domain name for the route
FQDN string `json:"fqdn,omitempty"`

Expand All @@ -90,10 +85,6 @@ type CFRouteStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

// CurrentStatus declares whether the CFRoute is currently valid or invalid
// +kubebuilder:validation:Enum=valid;invalid
type CurrentStatus string

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="URI",type=string,JSONPath=`.status.uri`
Expand Down
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
Loading