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

subscribe to secret change #486

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ea36ca8
subscribe to secret change
kerenlahav Nov 20, 2024
bf5a574
subscribe to secret change
kerenlahav Nov 20, 2024
2dc23cc
remove rate limit error from state
I065450 Nov 21, 2024
b61824c
remove rate limit error from state
I065450 Nov 21, 2024
400ffcb
remove rate limit error from state
I065450 Nov 21, 2024
58e7a7d
remove rate limit error from state
I065450 Nov 21, 2024
f3791d2
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 21, 2024
686158f
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 21, 2024
fe25616
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 21, 2024
a89b9bc
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 21, 2024
927f3c1
subscribe to secret change
kerenlahav Nov 25, 2024
5e055c3
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 25, 2024
26d64e5
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 25, 2024
d5f450d
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 26, 2024
4d63b68
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 26, 2024
6216fe4
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 27, 2024
23059fc
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 27, 2024
88d1854
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 28, 2024
27b271b
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 28, 2024
d977fec
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 28, 2024
3aeb027
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Nov 28, 2024
24bc634
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 2, 2024
27e5f87
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 4, 2024
b43ad4a
observed generation refactor
kerenlahav Dec 4, 2024
a9d8127
observed generation refactor
kerenlahav Dec 4, 2024
346991f
binding tests refactor
kerenlahav Dec 5, 2024
72d0e59
.
kerenlahav Dec 5, 2024
b416137
fix compile
kerenlahav Dec 5, 2024
343bd9b
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 5, 2024
7953510
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 5, 2024
ef63f3e
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 5, 2024
eb8cdc5
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 5, 2024
fad73ee
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 5, 2024
0bb2585
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 5, 2024
ee6c04f
.
kerenlahav Dec 8, 2024
62240de
add annotation to a secret created from template
kerenlahav Dec 8, 2024
b1e853c
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 8, 2024
4a6f287
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 8, 2024
4da933c
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 9, 2024
5aa3abb
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 9, 2024
684b491
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 9, 2024
78b9e1e
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 10, 2024
5d7025c
sharing instance update
kerenlahav Dec 10, 2024
567b95e
Merge remote-tracking branch 'origin/paramsFrom' into paramsFrom
kerenlahav Dec 10, 2024
1f7799c
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 10, 2024
165ab28
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 11, 2024
8e8b332
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 12, 2024
e8a3974
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 12, 2024
0eb41ce
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 12, 2024
9cb0f55
review
kerenlahav Dec 15, 2024
348fc74
[SAPBTPCFS-15469] Update Service Instance on change of 'parametersFro…
I065450 Dec 15, 2024
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
14 changes: 12 additions & 2 deletions api/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
import (
"fmt"

"k8s.io/apimachinery/pkg/api/meta"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -13,6 +15,7 @@ type ControllerName string
const (
ServiceInstanceController ControllerName = "ServiceInstance"
ServiceBindingController ControllerName = "ServiceBinding"
SecretController ControllerName = "Secret"
FinalizerName string = "services.cloud.sap.com/sap-btp-finalizer"
StaleBindingIDLabel string = "services.cloud.sap.com/stale"
StaleBindingRotationOfLabel string = "services.cloud.sap.com/rotationOf"
Expand Down Expand Up @@ -74,11 +77,18 @@ type SAPBTPResource interface {
GetParameters() *runtime.RawExtension
GetStatus() interface{}
SetStatus(status interface{})
GetObservedGeneration() int64
SetObservedGeneration(int64)
DeepClone() SAPBTPResource
SetReady(metav1.ConditionStatus)
GetReady() metav1.ConditionStatus
GetAnnotations() map[string]string
SetAnnotations(map[string]string)
}

func GetObservedGeneration(obj SAPBTPResource) int64 {
cond := meta.FindStatusCondition(obj.GetConditions(), ConditionSucceeded)
observedGen := int64(0)
if cond != nil {
observedGen = cond.ObservedGeneration
}
return observedGen
}
2 changes: 2 additions & 0 deletions api/common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
const (
ManagedByBTPOperatorLabel = "services.cloud.sap.com/managed-by-sap-btp-operator"
ClusterSecretLabel = "services.cloud.sap.com/cluster-secret"
InstanceSecretRefLabel = "services.cloud.sap.com/secret-ref_"
WatchSecretLabel = "services.cloud.sap.com/watch-secret"

NamespaceLabel = "_namespace"
K8sNameLabel = "_k8sname"
Expand Down
11 changes: 0 additions & 11 deletions api/v1/servicebinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ type ServiceBindingStatus struct {
// Service binding conditions
Conditions []metav1.Condition `json:"conditions"`

// Last generation that was acted on
ObservedGeneration int64 `json:"observedGeneration,omitempty"`

// Indicates whether binding is ready for usage
Ready metav1.ConditionStatus `json:"ready,omitempty"`

Expand Down Expand Up @@ -179,14 +176,6 @@ func (sb *ServiceBinding) SetStatus(status interface{}) {
sb.Status = status.(ServiceBindingStatus)
}

func (sb *ServiceBinding) GetObservedGeneration() int64 {
return sb.Status.ObservedGeneration
}

func (sb *ServiceBinding) SetObservedGeneration(newObserved int64) {
sb.Status.ObservedGeneration = newObserved
}

func (sb *ServiceBinding) DeepClone() common.SAPBTPResource {
return sb.DeepCopy()
}
Expand Down
12 changes: 0 additions & 12 deletions api/v1/servicebinding_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ var _ = Describe("Service Binding Type Test", func() {
Expect(binding.GetControllerName()).To(Equal(common.ServiceBindingController))
})

It("should update observed generation", func() {
Expect(binding.Status.ObservedGeneration).To(Equal(int64(0)))
binding.SetObservedGeneration(2)
Expect(binding.GetObservedGeneration()).To(Equal(int64(2)))
})

It("should update observed generation", func() {
Expect(binding.Status.ObservedGeneration).To(Equal(int64(0)))
binding.SetObservedGeneration(2)
Expect(binding.Status.ObservedGeneration).To(Equal(int64(2)))
})

It("should update ready", func() {
Expect(binding.Status.Ready).To(Equal(metav1.ConditionStatus("")))
binding.SetReady(metav1.ConditionTrue)
Expand Down
38 changes: 26 additions & 12 deletions api/v1/serviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ limitations under the License.
package v1

import (
"crypto/md5"
"encoding/hex"
"encoding/json"

"github.com/SAP/sap-btp-service-operator/api/common"
"github.com/SAP/sap-btp-service-operator/client/sm/types"
v1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -72,6 +77,10 @@ type ServiceInstanceSpec struct {
// +optional
ParametersFrom []ParametersFromSource `json:"parametersFrom,omitempty"`

// indicate instance will update on secrets from parametersFrom change
// +optional
SubscribeToSecretChanges *bool `json:"subscribeToSecretChanges,omitempty"`

// List of custom tags describing the ServiceInstance, will be copied to `ServiceBinding` secret in the key called `tags`.
// +optional
CustomTags []string `json:"customTags,omitempty"`
Expand Down Expand Up @@ -107,9 +116,6 @@ type ServiceInstanceStatus struct {
// Service instance conditions
Conditions []metav1.Condition `json:"conditions"`

// Last generation that was acted on
ObservedGeneration int64 `json:"observedGeneration,omitempty"`

// Indicates whether instance is ready for usage
Ready metav1.ConditionStatus `json:"ready,omitempty"`

Expand All @@ -118,6 +124,9 @@ type ServiceInstanceStatus struct {

// The subaccount id of the service instance
SubaccountID string `json:"subaccountID,omitempty"`

// if true need to update instance
ForceReconcile bool `json:"forceReconcile,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down Expand Up @@ -165,14 +174,6 @@ func (si *ServiceInstance) SetStatus(status interface{}) {
si.Status = status.(ServiceInstanceStatus)
}

func (si *ServiceInstance) GetObservedGeneration() int64 {
return si.Status.ObservedGeneration
}

func (si *ServiceInstance) SetObservedGeneration(newObserved int64) {
si.Status.ObservedGeneration = newObserved
}

func (si *ServiceInstance) DeepClone() common.SAPBTPResource {
return si.DeepCopy()
}
Expand Down Expand Up @@ -207,6 +208,19 @@ func init() {

func (si *ServiceInstance) Hub() {}

func (si *ServiceInstance) ShouldBeShared() bool {
func (si *ServiceInstance) GetShared() bool {
return si.Spec.Shared != nil && *si.Spec.Shared
}

func (si *ServiceInstance) IsSubscribedToSecretChange() bool {
return si.Spec.SubscribeToSecretChanges != nil && *si.Spec.SubscribeToSecretChanges
}

func (si *ServiceInstance) GetSpecHash() string {
spec := si.Spec
spec.Shared = ptr.To(false)
specBytes, _ := json.Marshal(spec)
s := string(specBytes)
hash := md5.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}
92 changes: 86 additions & 6 deletions api/v1/serviceinstance_types_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package v1

import (
"crypto/md5"
"encoding/hex"
"encoding/json"

"github.com/SAP/sap-btp-service-operator/api/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
)

var _ = Describe("Service Instance Type Test", func() {
Expand Down Expand Up @@ -96,12 +101,6 @@ var _ = Describe("Service Instance Type Test", func() {
Expect(instance.GetControllerName()).To(Equal(common.ServiceInstanceController))
})

It("should update observed generation", func() {
Expect(instance.Status.ObservedGeneration).To(Equal(int64(0)))
instance.SetObservedGeneration(2)
Expect(instance.GetObservedGeneration()).To(Equal(int64(2)))
})

It("should update ready", func() {
Expect(instance.Status.Ready).To(Equal(metav1.ConditionStatus("")))
instance.SetReady(metav1.ConditionTrue)
Expand Down Expand Up @@ -129,4 +128,85 @@ var _ = Describe("Service Instance Type Test", func() {
instance.SetAnnotations(annotation)
Expect(instance.GetAnnotations()).To(Equal(annotation))
})

It("should update SubscribeToSecretChanges", func() {
instance.Spec.SubscribeToSecretChanges = &[]bool{true}[0]
Expect(instance.IsSubscribedToSecretChange()).To(BeTrue())
})

It("should return correct spec hash", func() {
// Calculate expected hash
spec := instance.Spec
spec.Shared = ptr.To(false)
specBytes, _ := json.Marshal(spec)
hash := md5.Sum(specBytes)
expectedHash := hex.EncodeToString(hash[:])

// Get actual hash
actualHash := instance.GetSpecHash()

// Compare hashes
Expect(actualHash).To(Equal(expectedHash))
})
It("should update spec hash when spec changes", func() {
// Calculate initial hash
initialHash := instance.GetSpecHash()

// Modify the spec
instance.Spec.ServicePlanName = "new-plan"

// Calculate new hash
newHash := instance.GetSpecHash()

// Ensure the hash has changed
Expect(initialHash).NotTo(Equal(newHash))
})
It("should update spec hash when parametersFrom changes", func() {
// Calculate initial hash
initialHash := instance.GetSpecHash()

// Modify the parametersFrom field
instance.Spec.ParametersFrom = []ParametersFromSource{
{
SecretKeyRef: &SecretKeyReference{
Name: "new-param-secret",
Key: "new-secret-parameter",
},
},
}

// Calculate new hash
newHash := instance.GetSpecHash()

// Ensure the hash has changed
Expect(initialHash).NotTo(Equal(newHash))
})
It("should update spec hash when parametersFrom changes with initial object", func() {
// Initialize ParametersFrom with an object
instance.Spec.ParametersFrom = []ParametersFromSource{
{
SecretKeyRef: &SecretKeyReference{
Name: "initial-param-secret",
Key: "initial-secret-parameter",
},
},
}

// Calculate initial hash
initialHash := instance.GetSpecHash()

// Modify the parametersFrom field
instance.Spec.ParametersFrom = append(instance.Spec.ParametersFrom, ParametersFromSource{
SecretKeyRef: &SecretKeyReference{
Name: "new-param-secret",
Key: "new-secret-parameter",
},
})

// Calculate new hash
newHash := instance.GetSpecHash()

// Ensure the hash has changed
Expect(initialHash).NotTo(Equal(newHash))
})
})
1 change: 1 addition & 0 deletions api/v1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func getInstance() *ServiceInstance {
},
},
},
SubscribeToSecretChanges: &[]bool{true}[0],
UserInfo: &v1.UserInfo{
Username: "test-user",
Groups: []string{"test-group"},
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading