Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Co-authored-by: Georgi Sabev <[email protected]>
  • Loading branch information
danail-branekov and georgethebeatle committed Sep 2, 2024
1 parent 68acde9 commit 2abc7bc
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 82 deletions.
22 changes: 0 additions & 22 deletions controllers/controllers/shared/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
Expand Down Expand Up @@ -113,26 +111,6 @@ func serviceBindingServiceInstanceGUIDIndexFn(rawObj client.Object) []string {
return []string{serviceBinding.Spec.Service.Name}
}

// GetConditionOrSetAsUnknown is a helper function that retrieves the value of the provided conditionType, like
// "Succeeded" and returns the value: "True", "False", or "Unknown". If the value is not present, the pointer to the
// list of conditions provided to the function is used to add an entry to the list of Conditions with a value of
// "Unknown" and "Unknown" is returned
func GetConditionOrSetAsUnknown(conditions *[]metav1.Condition, conditionType string, generation int64) metav1.ConditionStatus {
if conditionStatus := meta.FindStatusCondition(*conditions, conditionType); conditionStatus != nil {
return conditionStatus.Status
}

meta.SetStatusCondition(conditions, metav1.Condition{
Type: conditionType,
Status: metav1.ConditionUnknown,
Reason: "Unknown",
Message: "Unknown",
ObservedGeneration: generation,
})

return metav1.ConditionUnknown
}

func RemovePackageManagerKeys(src map[string]string, log logr.Logger) map[string]string {
if src == nil {
return src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (r *buildpackBuildReconciler) ReconcileBuild(
) (ctrl.Result, error) {
log := logr.FromContextOrDiscard(ctx)

stagingStatus := shared.GetConditionOrSetAsUnknown(&cfBuild.Status.Conditions, korifiv1alpha1.StagingConditionType, cfBuild.Generation)
if stagingStatus == metav1.ConditionUnknown {
stagingStatus := meta.FindStatusCondition(cfBuild.Status.Conditions, korifiv1alpha1.StagingConditionType)
if stagingStatus == nil {
err := r.createBuildWorkload(ctx, cfBuild, cfApp, cfPackage)
if err != nil {
log.Info("failed to create BuildWorkload", "reason", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ var _ = Describe("CFBuildpackBuildReconciler Integration Tests", func() {
g.Expect(stagingCondition.Status).To(Equal(metav1.ConditionTrue))
g.Expect(stagingCondition.Reason).To(Equal("BuildRunning"))
g.Expect(stagingCondition.ObservedGeneration).To(Equal(cfBuild.Generation))

succeededCondition := meta.FindStatusCondition(cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType)
g.Expect(succeededCondition).NotTo(BeNil())
g.Expect(succeededCondition.Status).To(Equal(metav1.ConditionUnknown))
g.Expect(succeededCondition.ObservedGeneration).To(Equal(cfBuild.Generation))
}).Should(Succeed())
})

Expand Down Expand Up @@ -274,7 +269,7 @@ var _ = Describe("CFBuildpackBuildReconciler Integration Tests", func() {
Expect(adminClient.Create(ctx, existingBuildWorkload)).To(Succeed())
})

It("sets the status conditions on the CFBuild to running", func() {
It("sets the staging status condition on the CFBuild to running", func() {
Eventually(func(g Gomega) {
g.Expect(adminClient.Get(context.Background(), client.ObjectKeyFromObject(cfBuild), cfBuild)).To(Succeed())

Expand All @@ -283,11 +278,6 @@ var _ = Describe("CFBuildpackBuildReconciler Integration Tests", func() {
g.Expect(stagingCondition.Status).To(Equal(metav1.ConditionTrue))
g.Expect(stagingCondition.Reason).To(Equal("BuildRunning"))
g.Expect(stagingCondition.ObservedGeneration).To(Equal(cfBuild.Generation))

succeededCondition := meta.FindStatusCondition(cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType)
g.Expect(succeededCondition).NotTo(BeNil())
g.Expect(succeededCondition.Status).To(Equal(metav1.ConditionUnknown))
g.Expect(succeededCondition.ObservedGeneration).To(Equal(cfBuild.Generation))
}).Should(Succeed())
})
})
Expand Down
5 changes: 2 additions & 3 deletions controllers/controllers/workloads/build/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -82,8 +81,8 @@ func (r *Reconciler) ReconcileResource(ctx context.Context, cfBuild *korifiv1alp
log.Info("unable to clean up old builds", "reason", err)
}

succeededStatus := shared.GetConditionOrSetAsUnknown(&cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType, cfBuild.Generation)
if succeededStatus != metav1.ConditionUnknown {
succeededStatus := meta.FindStatusCondition(cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType)
if succeededStatus != nil {
log.Info("build status indicates completion", "status", succeededStatus)
return ctrl.Result{}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions controllers/controllers/workloads/build/docker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"
"code.cloudfoundry.org/korifi/controllers/controllers/workloads/build"
"code.cloudfoundry.org/korifi/tools/image"
"code.cloudfoundry.org/korifi/tools/k8s"
Expand Down Expand Up @@ -93,8 +92,9 @@ func (r *dockerBuildReconciler) ReconcileBuild(
cfPackage *korifiv1alpha1.CFPackage,
) (ctrl.Result, error) {
log := logr.FromContextOrDiscard(ctx)
succeededStatus := shared.GetConditionOrSetAsUnknown(&cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType, cfBuild.Generation)
if succeededStatus != metav1.ConditionUnknown {

succeededStatus := meta.FindStatusCondition(cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType)
if succeededStatus != nil {
log.Info("build status indicates completion", "status", succeededStatus)
return ctrl.Result{}, nil
}
Expand Down
26 changes: 5 additions & 21 deletions controllers/controllers/workloads/k8sns/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/api/v1alpha1/status"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -80,28 +80,26 @@ func (r *Reconciler[T, NS]) ReconcileResource(ctx context.Context, obj NS) (ctrl
return r.finalizer.Finalize(ctx, obj)
}

shared.GetConditionOrSetAsUnknown(obj.GetStatus().GetConditions(), korifiv1alpha1.StatusConditionReady, obj.GetGeneration())

obj.GetStatus().SetGUID(obj.GetName())

err := r.createOrPatchNamespace(ctx, obj)
if err != nil {
return r.setNotReady(log, obj, fmt.Errorf("error creating namespace: %w", err), "NamespaceCreation")
return ctrl.Result{}, fmt.Errorf("error creating namespace: %w", err)
}

err = r.getNamespace(ctx, obj.GetName())
if err != nil {
return ctrl.Result{RequeueAfter: 100 * time.Millisecond}, nil
return ctrl.Result{}, k8s.NewNotReadyError().WithCause(err).WithRequeueAfter(time.Second)
}

err = r.propagateSecrets(ctx, obj, r.containerRegistrySecretNames)
if err != nil {
return r.setNotReady(log, obj, fmt.Errorf("error propagating secrets: %w", err), "RegistrySecretPropagation")
return ctrl.Result{}, fmt.Errorf("error propagating secrets: %w", err)
}

err = r.reconcileRoleBindings(ctx, obj)
if err != nil {
return r.setNotReady(log, obj, fmt.Errorf("error propagating role-bindings: %w", err), "RoleBindingPropagation")
return ctrl.Result{}, fmt.Errorf("error propagating role-bindings: %w", err)
}

return ctrl.Result{}, nil
Expand Down Expand Up @@ -139,20 +137,6 @@ func updateMap(dest *map[string]string, values map[string]string) {
}
}

func (r *Reconciler[T, NS]) setNotReady(log logr.Logger, obj NS, err error, reason string) (ctrl.Result, error) {
log.Info("not ready yet", "reason", reason, "error", err)

meta.SetStatusCondition(obj.GetStatus().GetConditions(), metav1.Condition{
Type: korifiv1alpha1.StatusConditionReady,
Status: metav1.ConditionFalse,
Reason: reason,
Message: err.Error(),
ObservedGeneration: obj.GetGeneration(),
})

return ctrl.Result{}, err
}

func (r *Reconciler[T, NS]) propagateSecrets(ctx context.Context, obj NS, secretNames []string) error {
if len(secretNames) == 0 {
// we are operating in service account role association mode for registry permissions.
Expand Down
17 changes: 2 additions & 15 deletions controllers/controllers/workloads/k8sns/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package k8sns_test
import (
"context"
"errors"
"fmt"
"maps"
"slices"

Expand All @@ -18,7 +17,6 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -172,22 +170,11 @@ var _ = Describe("K8S NS Reconciler Integration Tests", func() {

When("the image-registry-credentials secret does not exist in the root-ns", Serial, func() {
BeforeEach(func() {
reconciler = k8sns.NewReconciler[korifiv1alpha1.CFOrg, *korifiv1alpha1.CFOrg](controllersClient, finalizer, metadataCompiler, []string{"i-do-not-exist"})
reconciler = k8sns.NewReconciler(controllersClient, finalizer, metadataCompiler, []string{"i-do-not-exist"})
})

It("sets the NSObj's Ready condition to 'False'", func() {
It("returns an error", func() {
Expect(reconcileErr).To(MatchError(ContainSubstring("error fetching secret")))

readyCondition := meta.FindStatusCondition(nsObj.Status.Conditions, korifiv1alpha1.StatusConditionReady)
Expect(readyCondition).NotTo(BeNil())
Expect(readyCondition.Status).To(Equal(metav1.ConditionFalse))
Expect(readyCondition.Message).To(ContainSubstring(fmt.Sprintf(
"error fetching secret %q from namespace %q",
"i-do-not-exist",
rootNamespace,
)))
Expect(readyCondition.Reason).To(Equal("RegistrySecretPropagation"))
Expect(readyCondition.ObservedGeneration).To(Equal(nsObj.Generation))
})
})
})
Expand Down
2 changes: 0 additions & 2 deletions controllers/controllers/workloads/processes/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ func (r *Reconciler) ReconcileResource(ctx context.Context, cfProcess *korifiv1a
cfProcess.Status.ObservedGeneration = cfProcess.Generation
log.V(1).Info("set observed generation", "generation", cfProcess.Status.ObservedGeneration)

shared.GetConditionOrSetAsUnknown(&cfProcess.Status.Conditions, korifiv1alpha1.StatusConditionReady, cfProcess.Generation)

cfApp := new(korifiv1alpha1.CFApp)
err := r.k8sClient.Get(ctx, types.NamespacedName{Name: cfProcess.Spec.AppRef.Name, Namespace: cfProcess.Namespace}, cfApp)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions statefulset-runner/controllers/appworkload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -153,8 +152,6 @@ func (r *AppWorkloadReconciler) ReconcileResource(ctx context.Context, appWorklo
appWorkload.Status.ObservedGeneration = appWorkload.Generation
log.V(1).Info("set observed generation", "generation", appWorkload.Status.ObservedGeneration)

shared.GetConditionOrSetAsUnknown(&appWorkload.Status.Conditions, korifiv1alpha1.StatusConditionReady, appWorkload.Generation)

statefulSet, err := r.workloadsToStSet.Convert(appWorkload)
// Not clear what errors this would produce, but we may use it later
if err != nil {
Expand Down

0 comments on commit 2abc7bc

Please sign in to comment.