Skip to content

Commit

Permalink
Userinfo (#57)
Browse files Browse the repository at this point in the history
* don't update user info during reconciliations that caused by operator
  • Loading branch information
pavelmaliy authored Jun 18, 2021
1 parent 4bb8798 commit 399dff6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
29 changes: 2 additions & 27 deletions api/v1alpha1/servicebinding_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -60,33 +61,7 @@ func (sb *ServiceBinding) ValidateUpdate(old runtime.Object) error {

func (sb *ServiceBinding) specChanged(old runtime.Object) bool {
oldBinding := old.(*ServiceBinding)

if changed := sb.paramsFromChanged(oldBinding); changed {
return true
}

return sb.Spec.ExternalName != oldBinding.Spec.ExternalName ||
sb.Spec.ServiceInstanceName != oldBinding.Spec.ServiceInstanceName ||
// TODO + labels
//r.Spec.Labels != oldBinding.Spec.Labels ||
sb.Spec.Parameters.String() != oldBinding.Spec.Parameters.String() ||
sb.Spec.SecretName != oldBinding.Spec.SecretName
}

func (sb *ServiceBinding) paramsFromChanged(oldBinding *ServiceBinding) bool {
if len(sb.Spec.ParametersFrom) != len(oldBinding.Spec.ParametersFrom) {
return true
}
for i, paramFrom := range sb.Spec.ParametersFrom {
if paramFrom.SecretKeyRef != nil && oldBinding.Spec.ParametersFrom[i].SecretKeyRef != nil {
if *paramFrom.SecretKeyRef != *oldBinding.Spec.ParametersFrom[i].SecretKeyRef {
return true
}
} else if paramFrom.SecretKeyRef != oldBinding.Spec.ParametersFrom[i].SecretKeyRef {
return true
}
}
return false
return !reflect.DeepEqual(oldBinding.Spec, sb.Spec)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
Expand Down
13 changes: 8 additions & 5 deletions api/v1alpha1/webhooks/servicebinding_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/SAP/sap-btp-service-operator/api/v1alpha1"
v1admission "k8s.io/api/admission/v1"
v1 "k8s.io/api/authentication/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -47,11 +48,13 @@ func (s *ServiceBindingDefaulter) Handle(_ context.Context, req admission.Reques
binding.Spec.SecretName = binding.Name
}

binding.Spec.UserInfo = &v1.UserInfo{
Username: req.UserInfo.Username,
UID: req.UserInfo.UID,
Groups: req.UserInfo.Groups,
Extra: req.UserInfo.Extra,
if req.Operation == v1admission.Create || req.Operation == v1admission.Delete {
binding.Spec.UserInfo = &v1.UserInfo{
Username: req.UserInfo.Username,
UID: req.UserInfo.UID,
Groups: req.UserInfo.Groups,
Extra: req.UserInfo.Extra,
}
}

marshaledInstance, err := json.Marshal(binding)
Expand Down
36 changes: 30 additions & 6 deletions api/v1alpha1/webhooks/serviceinstance_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"

v1admission "k8s.io/api/admission/v1"
v1 "k8s.io/api/authentication/v1"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/SAP/sap-btp-service-operator/api/v1alpha1"
v1 "k8s.io/api/authentication/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand Down Expand Up @@ -42,11 +45,10 @@ func (s *ServiceInstanceDefaulter) Handle(_ context.Context, req admission.Reque
instancelog.Info("externalName not provided, defaulting to k8s name", "name", instance.Name)
instance.Spec.ExternalName = instance.Name
}
instance.Spec.UserInfo = &v1.UserInfo{
Username: req.UserInfo.Username,
UID: req.UserInfo.UID,
Groups: req.UserInfo.Groups,
Extra: req.UserInfo.Extra,

err = s.setServiceInstanceUserInfo(req, instance)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}

marshaledInstance, err := json.Marshal(instance)
Expand All @@ -56,6 +58,28 @@ func (s *ServiceInstanceDefaulter) Handle(_ context.Context, req admission.Reque
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledInstance)
}

func (s *ServiceInstanceDefaulter) setServiceInstanceUserInfo(req admission.Request, instance *v1alpha1.ServiceInstance) error {
userInfo := &v1.UserInfo{
Username: req.UserInfo.Username,
UID: req.UserInfo.UID,
Groups: req.UserInfo.Groups,
Extra: req.UserInfo.Extra,
}
if req.Operation == v1admission.Create || req.Operation == v1admission.Delete {
instance.Spec.UserInfo = userInfo
} else if req.Operation == v1admission.Update {
oldInstance := &v1alpha1.ServiceInstance{}
err := s.decoder.DecodeRaw(req.OldObject, oldInstance)
if err != nil {
return err
}
if !reflect.DeepEqual(oldInstance.Spec, instance.Spec) {
instance.Spec.UserInfo = userInfo
}
}
return nil
}

func (s *ServiceInstanceDefaulter) InjectDecoder(d *admission.Decoder) error {
s.decoder = d
return nil
Expand Down

0 comments on commit 399dff6

Please sign in to comment.