Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 9 additions & 3 deletions apis/v1beta1/clusterworkloadresourcemapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,19 @@ func TestClusterWorkloadResourceMappingValidate(t *testing.T) {
}

expectedErr := c.expected.ToAggregate()
if diff := cmp.Diff(expectedErr, c.seed.ValidateCreate()); diff != "" {

_, actualCreateErr := c.seed.ValidateCreate()
if diff := cmp.Diff(expectedErr, actualCreateErr); diff != "" {
t.Errorf("ValidateCreate (-expected, +actual): %s", diff)
}
if diff := cmp.Diff(expectedErr, c.seed.ValidateUpdate(c.seed.DeepCopy())); diff != "" {

_, actualUpdateErr := c.seed.ValidateUpdate(c.seed.DeepCopy())
if diff := cmp.Diff(expectedErr, actualUpdateErr); diff != "" {
t.Errorf("ValidateCreate (-expected, +actual): %s", diff)
}
if diff := cmp.Diff(nil, c.seed.ValidateDelete()); diff != "" {

_, actualDeleteErr := c.seed.ValidateDelete()
if diff := cmp.Diff(nil, actualDeleteErr); diff != "" {
t.Errorf("ValidateDelete (-expected, +actual): %s", diff)
}
})
Expand Down
13 changes: 7 additions & 6 deletions apis/v1beta1/clusterworkloadresourcemapping_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/client-go/util/jsonpath"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (r *ClusterWorkloadResourceMapping) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -77,21 +78,21 @@ func (r *ClusterWorkloadResourceMappingTemplate) Default() {
var _ webhook.Validator = &ClusterWorkloadResourceMapping{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *ClusterWorkloadResourceMapping) ValidateCreate() error {
func (r *ClusterWorkloadResourceMapping) ValidateCreate() (admission.Warnings, error) {
r.Default()
return r.validate().ToAggregate()
return nil, r.validate().ToAggregate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *ClusterWorkloadResourceMapping) ValidateUpdate(old runtime.Object) error {
func (r *ClusterWorkloadResourceMapping) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
r.Default()
// TODO(user): check for immutable fields, if any
return r.validate().ToAggregate()
return nil, r.validate().ToAggregate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *ClusterWorkloadResourceMapping) ValidateDelete() error {
return nil
func (r *ClusterWorkloadResourceMapping) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (r *ClusterWorkloadResourceMapping) validate() field.ErrorList {
Expand Down
12 changes: 9 additions & 3 deletions apis/v1beta1/servicebinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,19 @@ func TestServiceBindingValidate(t *testing.T) {
}

expectedErr := c.expected.ToAggregate()
if diff := cmp.Diff(expectedErr, c.seed.ValidateCreate()); diff != "" {

_, actualCreateErr := c.seed.ValidateCreate()
if diff := cmp.Diff(expectedErr, actualCreateErr); diff != "" {
t.Errorf("ValidateCreate (-expected, +actual): %s", diff)
}
if diff := cmp.Diff(expectedErr, c.seed.ValidateUpdate(c.seed.DeepCopy())); diff != "" {

_, actualUpdateErr := c.seed.ValidateUpdate(c.seed.DeepCopy())
if diff := cmp.Diff(expectedErr, actualUpdateErr); diff != "" {
t.Errorf("ValidateCreate (-expected, +actual): %s", diff)
}
if diff := cmp.Diff(nil, c.seed.ValidateDelete()); diff != "" {

_, actualDeleteErr := c.seed.ValidateDelete()
if diff := cmp.Diff(nil, actualDeleteErr); diff != "" {
t.Errorf("ValidateDelete (-expected, +actual): %s", diff)
}
})
Expand Down
13 changes: 7 additions & 6 deletions apis/v1beta1/servicebinding_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (r *ServiceBinding) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -44,21 +45,21 @@ func (r *ServiceBinding) Default() {
var _ webhook.Validator = &ServiceBinding{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *ServiceBinding) ValidateCreate() error {
func (r *ServiceBinding) ValidateCreate() (admission.Warnings, error) {
r.Default()
return r.validate().ToAggregate()
return nil, r.validate().ToAggregate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *ServiceBinding) ValidateUpdate(old runtime.Object) error {
func (r *ServiceBinding) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
// TODO(user): check for immutable fields, if any
r.Default()
return r.validate().ToAggregate()
return nil, r.validate().ToAggregate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *ServiceBinding) ValidateDelete() error {
return nil
func (r *ServiceBinding) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (r *ServiceBinding) validate() field.ErrorList {
Expand Down
38 changes: 17 additions & 21 deletions controllers/servicebinding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctlr "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

servicebindingv1beta1 "github.com/servicebinding/runtime/apis/v1beta1"
"github.com/servicebinding/runtime/projector"
Expand All @@ -44,12 +42,11 @@ import (
//+kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch;delete

// ServiceBindingReconciler reconciles a ServiceBinding object
func ServiceBindingReconciler(c reconcilers.Config) *reconcilers.ResourceReconciler {
return &reconcilers.ResourceReconciler{
Type: &servicebindingv1beta1.ServiceBinding{},
Reconciler: &reconcilers.WithFinalizer{
func ServiceBindingReconciler(c reconcilers.Config) *reconcilers.ResourceReconciler[*servicebindingv1beta1.ServiceBinding] {
return &reconcilers.ResourceReconciler[*servicebindingv1beta1.ServiceBinding]{
Reconciler: &reconcilers.WithFinalizer[*servicebindingv1beta1.ServiceBinding]{
Finalizer: servicebindingv1beta1.GroupVersion.Group + "/finalizer",
Reconciler: reconcilers.Sequence{
Reconciler: reconcilers.Sequence[*servicebindingv1beta1.ServiceBinding]{
ResolveBindingSecret(),
ResolveWorkloads(),
ProjectBinding(),
Expand All @@ -61,8 +58,8 @@ func ServiceBindingReconciler(c reconcilers.Config) *reconcilers.ResourceReconci
}
}

func ResolveBindingSecret() reconcilers.SubReconciler {
return &reconcilers.SyncReconciler{
func ResolveBindingSecret() reconcilers.SubReconciler[*servicebindingv1beta1.ServiceBinding] {
return &reconcilers.SyncReconciler[*servicebindingv1beta1.ServiceBinding]{
Name: "ResolveBindingSecret",
Sync: func(ctx context.Context, resource *servicebindingv1beta1.ServiceBinding) error {
c := reconcilers.RetrieveConfigOrDie(ctx)
Expand Down Expand Up @@ -119,11 +116,11 @@ func ResolveBindingSecret() reconcilers.SubReconciler {
}
}

func ResolveWorkloads() reconcilers.SubReconciler {
return &reconcilers.SyncReconciler{
func ResolveWorkloads() reconcilers.SubReconciler[*servicebindingv1beta1.ServiceBinding] {
return &reconcilers.SyncReconciler[*servicebindingv1beta1.ServiceBinding]{
Name: "ResolveWorkloads",
SyncDuringFinalization: true,
Sync: func(ctx context.Context, resource *servicebindingv1beta1.ServiceBinding) (reconcile.Result, error) {
SyncWithResult: func(ctx context.Context, resource *servicebindingv1beta1.ServiceBinding) (reconcile.Result, error) {
c := reconcilers.RetrieveConfigOrDie(ctx)

ref := corev1.ObjectReference{
Expand Down Expand Up @@ -164,8 +161,8 @@ func ResolveWorkloads() reconcilers.SubReconciler {

//+kubebuilder:rbac:groups=servicebinding.io,resources=clusterworkloadresourcemappings,verbs=get;list;watch

func ProjectBinding() reconcilers.SubReconciler {
return &reconcilers.SyncReconciler{
func ProjectBinding() reconcilers.SubReconciler[*servicebindingv1beta1.ServiceBinding] {
return &reconcilers.SyncReconciler[*servicebindingv1beta1.ServiceBinding]{
Name: "ProjectBinding",
SyncDuringFinalization: true,
Sync: func(ctx context.Context, resource *servicebindingv1beta1.ServiceBinding) error {
Expand Down Expand Up @@ -195,22 +192,21 @@ func ProjectBinding() reconcilers.SubReconciler {
},

Setup: func(ctx context.Context, mgr ctlr.Manager, bldr *builder.Builder) error {
bldr.Watches(&source.Kind{Type: &servicebindingv1beta1.ClusterWorkloadResourceMapping{}}, handler.Funcs{})
bldr.Watches(&servicebindingv1beta1.ClusterWorkloadResourceMapping{}, handler.Funcs{})
return nil
},
}
}

func PatchWorkloads() reconcilers.SubReconciler {
workloadManager := &reconcilers.ResourceManager{
func PatchWorkloads() reconcilers.SubReconciler[*servicebindingv1beta1.ServiceBinding] {
workloadManager := &reconcilers.ResourceManager[*unstructured.Unstructured]{
Name: "PatchWorkloads",
Type: &unstructured.Unstructured{},
MergeBeforeUpdate: func(current, desired *unstructured.Unstructured) {
current.SetUnstructuredContent(desired.UnstructuredContent())
},
}

return &reconcilers.SyncReconciler{
return &reconcilers.SyncReconciler[*servicebindingv1beta1.ServiceBinding]{
Name: "PatchWorkloads",
SyncDuringFinalization: true,
Sync: func(ctx context.Context, resource *servicebindingv1beta1.ServiceBinding) error {
Expand All @@ -222,8 +218,8 @@ func PatchWorkloads() reconcilers.SubReconciler {
}

for i := range workloads {
workload := workloads[i].(client.Object)
projectedWorkload := projectedWorkloads[i].(client.Object)
workload := workloads[i].(*unstructured.Unstructured)
projectedWorkload := projectedWorkloads[i].(*unstructured.Unstructured)
if workload.GetUID() != projectedWorkload.GetUID() || workload.GetResourceVersion() != projectedWorkload.GetResourceVersion() {
panic(fmt.Errorf("workload and projectedWorkload must have the same uid and resourceVersion"))
}
Expand Down
Loading