diff --git a/controllers/api/v1alpha1/cfserviceinstance_types.go b/controllers/api/v1alpha1/cfserviceinstance_types.go index 529f7d076..f1b330207 100644 --- a/controllers/api/v1alpha1/cfserviceinstance_types.go +++ b/controllers/api/v1alpha1/cfserviceinstance_types.go @@ -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"` diff --git a/controllers/controllers/services/instances/managed/controller.go b/controllers/controllers/services/instances/managed/controller.go index 8a4c7a19a..33e5180ef 100644 --- a/controllers/controllers/services/instances/managed/controller.go +++ b/controllers/controllers/services/instances/managed/controller.go @@ -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" @@ -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) diff --git a/controllers/controllers/services/instances/managed/controller_test.go b/controllers/controllers/services/instances/managed/controller_test.go index b9774763a..fd352791a 100644 --- a/controllers/controllers/services/instances/managed/controller_test.go +++ b/controllers/controllers/services/instances/managed/controller_test.go @@ -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" @@ -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", }, @@ -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)) diff --git a/controllers/controllers/services/instances/managed/suite_test.go b/controllers/controllers/services/instances/managed/suite_test.go index b3a626302..4d0ad2303 100644 --- a/controllers/controllers/services/instances/managed/suite_test.go +++ b/controllers/controllers/services/instances/managed/suite_test.go @@ -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") diff --git a/helm/korifi/controllers/crds/korifi.cloudfoundry.org_cfserviceinstances.yaml b/helm/korifi/controllers/crds/korifi.cloudfoundry.org_cfserviceinstances.yaml index 7750bc9c5..b0c48f3c9 100644 --- a/helm/korifi/controllers/crds/korifi.cloudfoundry.org_cfserviceinstances.yaml +++ b/helm/korifi/controllers/crds/korifi.cloudfoundry.org_cfserviceinstances.yaml @@ -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