Skip to content

Commit

Permalink
CFServiceBinding and CFServiceInstance controllers pass logger in the…
Browse files Browse the repository at this point in the history
… ctx

[#1935]
  • Loading branch information
davewalter committed Jun 23, 2023
1 parent 5914feb commit a8ebe9d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"time"

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"
servicebindingv1beta1 "github.com/servicebinding/runtime/apis/v1beta1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -71,7 +73,8 @@ func (r *CFServiceBindingReconciler) SetupWithManager(mgr ctrl.Manager) *builder
//+kubebuilder:rbac:groups=servicebinding.io,resources=servicebindings,verbs=get;list;create;update;patch;watch

func (r *CFServiceBindingReconciler) ReconcileResource(ctx context.Context, cfServiceBinding *korifiv1alpha1.CFServiceBinding) (ctrl.Result, error) {
log := r.log.WithValues("namespace", cfServiceBinding.Namespace, "name", cfServiceBinding.Name)
log := shared.ObjectLogger(r.log, cfServiceBinding)
ctx = logr.NewContext(ctx, log)

cfServiceBinding.Status.ObservedGeneration = cfServiceBinding.Generation
log.V(1).Info("set observed generation", "generation", cfServiceBinding.Status.ObservedGeneration)
Expand Down Expand Up @@ -224,13 +227,16 @@ func generateDesiredServiceBinding(actualServiceBinding *servicebindingv1beta1.S
Name: cfServiceBinding.Name,
},
}

secretType, ok := secret.Data["type"]
if ok && len(secretType) > 0 {
desiredServiceBinding.Spec.Type = string(secretType)
}

secretProvider, ok := secret.Data["provider"]
if ok {
desiredServiceBinding.Spec.Provider = string(secretProvider)
}

return &desiredServiceBinding
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gstruct"
servicebindingv1beta1 "github.com/servicebinding/runtime/apis/v1beta1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -191,6 +192,10 @@ var _ = Describe("CFServiceBinding", func() {
}).Should(Succeed())
})

It("writes a log message", func() {
Eventually(logOutput).Should(gbytes.Say("set observed generation"))
})

When("the CFServiceBinding has a displayName set", func() {
var bindingName string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

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 @@ -61,8 +62,11 @@ func (r *CFServiceInstanceReconciler) SetupWithManager(mgr ctrl.Manager) *builde
//+kubebuilder:rbac:groups=korifi.cloudfoundry.org,resources=cfserviceinstances/finalizers,verbs=update

func (r *CFServiceInstanceReconciler) ReconcileResource(ctx context.Context, cfServiceInstance *korifiv1alpha1.CFServiceInstance) (ctrl.Result, error) {
log := shared.ObjectLogger(r.log, cfServiceInstance)
ctx = logr.NewContext(ctx, log)

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

secret := new(corev1.Secret)
err := r.k8sClient.Get(ctx, types.NamespacedName{Name: cfServiceInstance.Spec.SecretName, Namespace: cfServiceInstance.Namespace}, secret)
Expand All @@ -72,6 +76,7 @@ func (r *CFServiceInstanceReconciler) ReconcileResource(ctx context.Context, cfS
return ctrl.Result{RequeueAfter: 2 * time.Second}, nil
}

log.Info("failed to get secret", "reason", err)
cfServiceInstance.Status = bindSecretUnavailableStatus(cfServiceInstance, "UnknownError", "Error occurred while fetching secret: "+err.Error())
return ctrl.Result{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services_test
import (
"context"

"github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gstruct"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -84,6 +85,10 @@ var _ = Describe("CFServiceInstance", func() {
}).Should(Succeed())
})

It("writes a log message", func() {
Eventually(logOutput).Should(gbytes.Say("set observed generation"))
})

When("the referenced secret does not exist", func() {
BeforeEach(func() {
cfServiceInstance.Spec.SecretName = "other-secret-name"
Expand Down
4 changes: 4 additions & 0 deletions controllers/controllers/services/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
servicebindingv1beta1 "github.com/servicebinding/runtime/apis/v1beta1"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -47,6 +48,7 @@ var (
cancel context.CancelFunc
testEnv *envtest.Environment
k8sClient client.Client
logOutput *gbytes.Buffer
)

func TestAPIs(t *testing.T) {
Expand All @@ -58,6 +60,8 @@ func TestAPIs(t *testing.T) {
}

var _ = BeforeSuite(func() {
logOutput = gbytes.NewBuffer()
GinkgoWriter.TeeTo(logOutput)
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

ctx, cancel = context.WithCancel(context.TODO())
Expand Down

0 comments on commit a8ebe9d

Please sign in to comment.