Skip to content

Commit

Permalink
Default CFServiceInstance.Spec.ServiceLabel to the offering name
Browse files Browse the repository at this point in the history
By doing that, bindings to that instance would be under the offering
name key in `VCAP_SERVICES`

If the service label has been set, its value is not overridden

Co-authored-by: Danail Branekov <danailster@gmail.com>
  • Loading branch information
zabanov-lab and danail-branekov committed Oct 25, 2024
1 parent 4a17e11 commit 1af41fa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion controllers/api/v1alpha1/cfserviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ type CFServiceInstanceSpec struct {
Type InstanceType `json:"type"`

// Service label to use when adding this instance to VCAP_SERVICES. If not
// set, the service instance Type would be used.
// set, the service instance Type would be used. For managed services the
// value is defaulted to the offering name
// +optional
ServiceLabel *string `json:"serviceLabel,omitempty"`

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

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

"github.com/go-logr/logr"
Expand Down Expand Up @@ -121,6 +122,10 @@ func (r *Reconciler) ReconcileResource(ctx context.Context, serviceInstance *kor
return ctrl.Result{}, err
}

if serviceInstance.Spec.ServiceLabel == nil {
serviceInstance.Spec.ServiceLabel = tools.PtrTo(serviceOffering.Spec.Name)
}

osbapiClient, err := r.osbapiClientFactory.CreateClient(ctx, serviceBroker)
if err != nil {
log.Error(err, "failed to create broker client", "broker", serviceBroker.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"code.cloudfoundry.org/korifi/controllers/controllers/services/osbapi/fake"
"code.cloudfoundry.org/korifi/model/services"
. "code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -79,6 +81,7 @@ var _ = Describe("CFServiceInstance", func() {
},
Spec: korifiv1alpha1.CFServiceOfferingSpec{
ServiceOffering: services.ServiceOffering{
Name: "service-offering-name",
BrokerCatalog: services.ServiceBrokerCatalog{
ID: "service-offering-id",
},
Expand Down Expand Up @@ -152,6 +155,28 @@ var _ = Describe("CFServiceInstance", func() {
}).Should(Succeed())
})

It("defaults the service label to the service offering name", func() {
Eventually(func(g Gomega) {
g.Expect(adminClient.Get(ctx, client.ObjectKeyFromObject(instance), instance)).To(Succeed())
g.Expect(instance.Spec.ServiceLabel).To(PointTo(Equal("service-offering-name")))
}).Should(Succeed())
})

When("the service label is set", func() {
BeforeEach(func() {
Expect(k8s.Patch(ctx, adminClient, instance, func() {
instance.Spec.ServiceLabel = tools.PtrTo("custom-service-label")
})).To(Succeed())
})

It("does not override it", func() {
Consistently(func(g Gomega) {
g.Expect(adminClient.Get(ctx, client.ObjectKeyFromObject(instance), instance)).To(Succeed())
g.Expect(instance.Spec.ServiceLabel).To(PointTo(Equal("custom-service-label")))
}).Should(Succeed())
})
})

It("provisions the service", func() {
Eventually(func(g Gomega) {
g.Expect(brokerClient.ProvisionCallCount()).To(Equal(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var (
func TestAPIs(t *testing.T) {
SetDefaultEventuallyTimeout(30 * time.Second)
SetDefaultEventuallyPollingInterval(250 * time.Millisecond)
SetDefaultConsistentlyDuration(5 * time.Second)
SetDefaultConsistentlyPollingInterval(250 * time.Millisecond)

RegisterFailHandler(Fail)
RunSpecs(t, "Services Instance Controller Integration Suite")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ spec:
serviceLabel:
description: |-
Service label to use when adding this instance to VCAP_SERVICES. If not
set, the service instance Type would be used.
set, the service instance Type would be used. For managed services the
value is defaulted to the offering name
type: string
tags:
description: Tags are used by apps to identify service instances
Expand Down

0 comments on commit 1af41fa

Please sign in to comment.