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

code refactor #384

Merged
merged 15 commits into from
Jan 14, 2024
2 changes: 1 addition & 1 deletion api/common.go → api/common/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package common

import (
"fmt"
Expand Down
36 changes: 36 additions & 0 deletions api/common/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package common

const (
NamespaceLabel = "_namespace"
K8sNameLabel = "_k8sname"
ClusterIDLabel = "_clusterid"

Created = "Created"
Updated = "Updated"
Deleted = "Deleted"
Provisioned = "Provisioned"
NotProvisioned = "NotProvisioned"

CreateInProgress = "CreateInProgress"
UpdateInProgress = "UpdateInProgress"
DeleteInProgress = "DeleteInProgress"
InProgress = "InProgress"
Finished = "Finished"

CreateFailed = "CreateFailed"
UpdateFailed = "UpdateFailed"
DeleteFailed = "DeleteFailed"
Failed = "Failed"
ShareFailed = "ShareFailed"
ShareSucceeded = "ShareSucceeded"
ShareNotSupported = "ShareNotSupported"
UnShareFailed = "UnShareFailed"
UnShareSucceeded = "UnShareSucceeded"

Blocked = "Blocked"
Unknown = "Unknown"

// Cred Rotation
CredPreparing = "Preparing"
CredRotating = "Rotating"
)
8 changes: 4 additions & 4 deletions api/v1/servicebinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1

import (
"github.com/SAP/sap-btp-service-operator/api"
"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"
Expand Down Expand Up @@ -152,8 +152,8 @@ func (sb *ServiceBinding) SetConditions(conditions []metav1.Condition) {
sb.Status.Conditions = conditions
}

func (sb *ServiceBinding) GetControllerName() api.ControllerName {
return api.ServiceBindingController
func (sb *ServiceBinding) GetControllerName() common.ControllerName {
return common.ServiceBindingController
}

func (sb *ServiceBinding) GetParameters() *runtime.RawExtension {
Expand All @@ -176,7 +176,7 @@ func (sb *ServiceBinding) SetObservedGeneration(newObserved int64) {
sb.Status.ObservedGeneration = newObserved
}

func (sb *ServiceBinding) DeepClone() api.SAPBTPResource {
func (sb *ServiceBinding) DeepClone() common.SAPBTPResource {
return sb.DeepCopy()
}

Expand Down
8 changes: 4 additions & 4 deletions api/v1/servicebinding_types_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1

import (
"github.com/SAP/sap-btp-service-operator/api"
"github.com/SAP/sap-btp-service-operator/api/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -14,7 +14,7 @@ var _ = Describe("Service Binding Type Test", func() {
BeforeEach(func() {
binding = getBinding()
conditions := binding.GetConditions()
lastOpCondition := metav1.Condition{Type: api.ConditionSucceeded, Status: metav1.ConditionTrue, Reason: "reason", Message: "message"}
lastOpCondition := metav1.Condition{Type: common.ConditionSucceeded, Status: metav1.ConditionTrue, Reason: "reason", Message: "message"}
meta.SetStatusCondition(&conditions, lastOpCondition)
binding.SetConditions(conditions)
})
Expand Down Expand Up @@ -63,7 +63,7 @@ var _ = Describe("Service Binding Type Test", func() {
})

It("should return controller name", func() {
Expect(binding.GetControllerName()).To(Equal(api.ServiceBindingController))
Expect(binding.GetControllerName()).To(Equal(common.ServiceBindingController))
})

It("should update observed generation", func() {
Expand Down Expand Up @@ -100,7 +100,7 @@ var _ = Describe("Service Binding Type Test", func() {

It("should update annotation", func() {
annotation := map[string]string{
api.IgnoreNonTransientErrorAnnotation: "true",
common.IgnoreNonTransientErrorAnnotation: "true",
}
binding.SetAnnotations(annotation)
Expect(binding.GetAnnotations()).To(Equal(annotation))
Expand Down
14 changes: 7 additions & 7 deletions api/v1/servicebinding_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"reflect"
"time"

"github.com/SAP/sap-btp-service-operator/api/common"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/SAP/sap-btp-service-operator/api"
)

// log is for logging in this package.
Expand Down Expand Up @@ -76,7 +76,7 @@ func (sb *ServiceBinding) ValidateUpdate(old runtime.Object) (admission.Warnings
oldBinding := old.(*ServiceBinding)
isStale := false
if oldBinding.Labels != nil {
if _, ok := oldBinding.Labels[api.StaleBindingIDLabel]; ok {
if _, ok := oldBinding.Labels[common.StaleBindingIDLabel]; ok {
if sb.Spec.CredRotationPolicy.Enabled {
return nil, fmt.Errorf("enabling cred rotation for rotated binding is not allowed")
}
Expand All @@ -95,10 +95,10 @@ func (sb *ServiceBinding) ValidateUpdate(old runtime.Object) (admission.Warnings
}

func (sb *ServiceBinding) validateRotationLabels(old *ServiceBinding) bool {
if sb.Labels[api.StaleBindingIDLabel] != old.Labels[api.StaleBindingIDLabel] {
if sb.Labels[common.StaleBindingIDLabel] != old.Labels[common.StaleBindingIDLabel] {
return false
}
return sb.Labels[api.StaleBindingRotationOfLabel] == old.Labels[api.StaleBindingRotationOfLabel]
return sb.Labels[common.StaleBindingRotationOfLabel] == old.Labels[common.StaleBindingRotationOfLabel]
}

func (sb *ServiceBinding) specChanged(oldBinding *ServiceBinding) bool {
Expand Down Expand Up @@ -134,8 +134,8 @@ func (sb *ServiceBinding) validateCredRotatingConfig() error {

func (sb *ServiceBinding) validateAnnotations() error {
if sb.Annotations != nil {
if _, ok := sb.Annotations[api.IgnoreNonTransientErrorAnnotation]; ok {
return fmt.Errorf(AnnotationNotSupportedError, api.IgnoreNonTransientErrorAnnotation)
if _, ok := sb.Annotations[common.IgnoreNonTransientErrorAnnotation]; ok {
return fmt.Errorf(AnnotationNotSupportedError, common.IgnoreNonTransientErrorAnnotation)
}
}
return nil
Expand Down
14 changes: 7 additions & 7 deletions api/v1/servicebinding_validating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package v1

import (
"fmt"
"github.com/SAP/sap-btp-service-operator/api"
"github.com/SAP/sap-btp-service-operator/api/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -22,11 +22,11 @@ var _ = Describe("Service Binding Webhook Test", func() {
})
It("should failed", func() {
binding.Annotations = map[string]string{
api.IgnoreNonTransientErrorAnnotation: "false",
common.IgnoreNonTransientErrorAnnotation: "false",
}
_, err := binding.ValidateCreate()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(AnnotationNotSupportedError, api.IgnoreNonTransientErrorAnnotation)))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(AnnotationNotSupportedError, common.IgnoreNonTransientErrorAnnotation)))

})
})
Expand Down Expand Up @@ -103,9 +103,9 @@ var _ = Describe("Service Binding Webhook Test", func() {
})

It("should fail on update with stale label", func() {
binding.Labels = map[string]string{api.StaleBindingIDLabel: "true"}
binding.Labels = map[string]string{common.StaleBindingIDLabel: "true"}
newBinding.Spec.ParametersFrom[0].SecretKeyRef.Name = "newName"
newBinding.Labels = map[string]string{api.StaleBindingIDLabel: "true"}
newBinding.Labels = map[string]string{common.StaleBindingIDLabel: "true"}
_, err := newBinding.ValidateUpdate(binding)
Expect(err).To(HaveOccurred())
})
Expand All @@ -122,11 +122,11 @@ var _ = Describe("Service Binding Webhook Test", func() {
When("Annotation changed", func() {
It("should fail", func() {
newBinding.Annotations = map[string]string{
api.IgnoreNonTransientErrorAnnotation: "false",
common.IgnoreNonTransientErrorAnnotation: "false",
}
_, err := newBinding.ValidateUpdate(binding)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(AnnotationNotSupportedError, api.IgnoreNonTransientErrorAnnotation)))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(AnnotationNotSupportedError, common.IgnoreNonTransientErrorAnnotation)))

})
})
Expand Down
8 changes: 4 additions & 4 deletions api/v1/serviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1

import (
"github.com/SAP/sap-btp-service-operator/api"
"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"
Expand Down Expand Up @@ -150,8 +150,8 @@ func (si *ServiceInstance) SetConditions(conditions []metav1.Condition) {
si.Status.Conditions = conditions
}

func (si *ServiceInstance) GetControllerName() api.ControllerName {
return api.ServiceInstanceController
func (si *ServiceInstance) GetControllerName() common.ControllerName {
return common.ServiceInstanceController
}

func (si *ServiceInstance) GetParameters() *runtime.RawExtension {
Expand All @@ -174,7 +174,7 @@ func (si *ServiceInstance) SetObservedGeneration(newObserved int64) {
si.Status.ObservedGeneration = newObserved
}

func (si *ServiceInstance) DeepClone() api.SAPBTPResource {
func (si *ServiceInstance) DeepClone() common.SAPBTPResource {
return si.DeepCopy()
}

Expand Down
8 changes: 4 additions & 4 deletions api/v1/serviceinstance_types_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1

import (
"github.com/SAP/sap-btp-service-operator/api"
"github.com/SAP/sap-btp-service-operator/api/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -14,7 +14,7 @@ var _ = Describe("Service Instance Type Test", func() {
BeforeEach(func() {
instance = getInstance()
conditions := instance.GetConditions()
lastOpCondition := metav1.Condition{Type: api.ConditionSucceeded, Status: metav1.ConditionTrue, Reason: "reason", Message: "message"}
lastOpCondition := metav1.Condition{Type: common.ConditionSucceeded, Status: metav1.ConditionTrue, Reason: "reason", Message: "message"}
meta.SetStatusCondition(&conditions, lastOpCondition)
instance.SetConditions(conditions)
})
Expand Down Expand Up @@ -93,7 +93,7 @@ var _ = Describe("Service Instance Type Test", func() {
})

It("should return controller name", func() {
Expect(instance.GetControllerName()).To(Equal(api.ServiceInstanceController))
Expect(instance.GetControllerName()).To(Equal(common.ServiceInstanceController))
})

It("should update observed generation", func() {
Expand Down Expand Up @@ -124,7 +124,7 @@ var _ = Describe("Service Instance Type Test", func() {

It("should update annotation", func() {
annotation := map[string]string{
api.IgnoreNonTransientErrorAnnotation: "true",
common.IgnoreNonTransientErrorAnnotation: "true",
}
instance.SetAnnotations(annotation)
Expect(instance.GetAnnotations()).To(Equal(annotation))
Expand Down
18 changes: 9 additions & 9 deletions api/v1/serviceinstance_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"strings"
"time"

"github.com/SAP/sap-btp-service-operator/api/common"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/SAP/sap-btp-service-operator/api"
)

const (
Expand Down Expand Up @@ -65,7 +65,7 @@ func (si *ServiceInstance) ValidateUpdate(old runtime.Object) (warnings admissio
func (si *ServiceInstance) ValidateDelete() (warnings admission.Warnings, err error) {
serviceinstancelog.Info("validate delete", "name", si.Name)
if si.Annotations != nil {
preventDeletion, ok := si.Annotations[api.PreventDeletion]
preventDeletion, ok := si.Annotations[common.PreventDeletion]
if ok && strings.ToLower(preventDeletion) == "true" {
return nil, fmt.Errorf("service instance '%s' is marked with \"prevent deletion\"", si.Name)
}
Expand All @@ -74,16 +74,16 @@ func (si *ServiceInstance) ValidateDelete() (warnings admission.Warnings, err er
}

func (si *ServiceInstance) validateNonTransientTimestampAnnotation() error {
if len(si.Annotations) > 0 && len(si.Annotations[api.IgnoreNonTransientErrorAnnotation]) > 0 {
serviceinstancelog.Info(fmt.Sprintf("%s annotation exist, validating %s annotation", api.IgnoreNonTransientErrorAnnotation, api.IgnoreNonTransientErrorTimestampAnnotation))
annotationTime, err := time.Parse(time.RFC3339, si.Annotations[api.IgnoreNonTransientErrorTimestampAnnotation])
if len(si.Annotations) > 0 && len(si.Annotations[common.IgnoreNonTransientErrorAnnotation]) > 0 {
serviceinstancelog.Info(fmt.Sprintf("%s annotation exist, validating %s annotation", common.IgnoreNonTransientErrorAnnotation, common.IgnoreNonTransientErrorTimestampAnnotation))
annotationTime, err := time.Parse(time.RFC3339, si.Annotations[common.IgnoreNonTransientErrorTimestampAnnotation])
if err != nil {
serviceinstancelog.Error(err, fmt.Sprintf("failed to parse %s", api.IgnoreNonTransientErrorTimestampAnnotation))
return fmt.Errorf(annotationNotValidTimestampError, api.IgnoreNonTransientErrorTimestampAnnotation)
serviceinstancelog.Error(err, fmt.Sprintf("failed to parse %s", common.IgnoreNonTransientErrorTimestampAnnotation))
return fmt.Errorf(annotationNotValidTimestampError, common.IgnoreNonTransientErrorTimestampAnnotation)
}

if time.Since(annotationTime) < 0 {
return fmt.Errorf(annotationInFutureError, api.IgnoreNonTransientErrorTimestampAnnotation)
return fmt.Errorf(annotationInFutureError, common.IgnoreNonTransientErrorTimestampAnnotation)
}
}

Expand Down
Loading
Loading