-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* prevent deletion init * prevent deletion init * prevent deletion init * prevent deletion init * prevent deletion init * prevent deletion init * prevent deletion init * prevent deletion init * remove prevent deletion from spec to annotation * test decription * fix test * fix test * fix test * fix lint * fix value of prevent to true * fix * fix * register webhook * daniel * fix
- Loading branch information
1 parent
08b1da4
commit c69d155
Showing
9 changed files
with
196 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/SAP/sap-btp-service-operator/api" | ||
) | ||
|
||
func (si *ServiceInstance) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(si). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=delete,path=/validate-services-cloud-sap-com-v1-serviceinstance,mutating=false,failurePolicy=fail,groups=services.cloud.sap.com,resources=serviceinstances,versions=v1,name=vserviceinstance.kb.io,sideEffects=None,admissionReviewVersions=v1beta1;v1 | ||
|
||
var _ webhook.Validator = &ServiceInstance{} | ||
|
||
func (si *ServiceInstance) ValidateCreate() (warnings admission.Warnings, err error) { | ||
return nil, nil | ||
} | ||
|
||
func (si *ServiceInstance) ValidateUpdate(old runtime.Object) (warnings admission.Warnings, err error) { | ||
return nil, nil | ||
} | ||
|
||
func (si *ServiceInstance) ValidateDelete() (warnings admission.Warnings, err error) { | ||
if si.Annotations != nil { | ||
preventDeletion, ok := si.Annotations[api.PreventDeletion] | ||
if ok && strings.ToLower(preventDeletion) == "true" { | ||
return nil, fmt.Errorf("service instance '%s' is marked with \"prevent deletion\"", si.Name) | ||
} | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package v1 | ||
|
||
import ( | ||
"github.com/SAP/sap-btp-service-operator/api" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Service Instance Webhook Test", func() { | ||
var instance *ServiceInstance | ||
BeforeEach(func() { | ||
instance = getInstance() | ||
}) | ||
|
||
Context("Validate Delete", func() { | ||
When("service instance is marked as prevent deletion", func() { | ||
It("should return error from webhook", func() { | ||
instance.Annotations = map[string]string{ | ||
api.PreventDeletion: "true", | ||
} | ||
_, err := instance.ValidateDelete() | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("service instance is not marked as prevent deletion", func() { | ||
It("should not return error from webhook", func() { | ||
_, err := instance.ValidateDelete() | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("service instance prevent deletion annotation is not set with true", func() { | ||
It("should not return error from webhook", func() { | ||
instance.Annotations = map[string]string{ | ||
api.PreventDeletion: "not-true", | ||
} | ||
_, err := instance.ValidateDelete() | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters