Skip to content

Commit 399dff6

Browse files
authored
Userinfo (#57)
* don't update user info during reconciliations that caused by operator
1 parent 4bb8798 commit 399dff6

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

api/v1alpha1/servicebinding_validating_webhook.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha1
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122

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

6162
func (sb *ServiceBinding) specChanged(old runtime.Object) bool {
6263
oldBinding := old.(*ServiceBinding)
63-
64-
if changed := sb.paramsFromChanged(oldBinding); changed {
65-
return true
66-
}
67-
68-
return sb.Spec.ExternalName != oldBinding.Spec.ExternalName ||
69-
sb.Spec.ServiceInstanceName != oldBinding.Spec.ServiceInstanceName ||
70-
// TODO + labels
71-
//r.Spec.Labels != oldBinding.Spec.Labels ||
72-
sb.Spec.Parameters.String() != oldBinding.Spec.Parameters.String() ||
73-
sb.Spec.SecretName != oldBinding.Spec.SecretName
74-
}
75-
76-
func (sb *ServiceBinding) paramsFromChanged(oldBinding *ServiceBinding) bool {
77-
if len(sb.Spec.ParametersFrom) != len(oldBinding.Spec.ParametersFrom) {
78-
return true
79-
}
80-
for i, paramFrom := range sb.Spec.ParametersFrom {
81-
if paramFrom.SecretKeyRef != nil && oldBinding.Spec.ParametersFrom[i].SecretKeyRef != nil {
82-
if *paramFrom.SecretKeyRef != *oldBinding.Spec.ParametersFrom[i].SecretKeyRef {
83-
return true
84-
}
85-
} else if paramFrom.SecretKeyRef != oldBinding.Spec.ParametersFrom[i].SecretKeyRef {
86-
return true
87-
}
88-
}
89-
return false
64+
return !reflect.DeepEqual(oldBinding.Spec, sb.Spec)
9065
}
9166

9267
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type

api/v1alpha1/webhooks/servicebinding_mutating_webhook.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1010

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

50-
binding.Spec.UserInfo = &v1.UserInfo{
51-
Username: req.UserInfo.Username,
52-
UID: req.UserInfo.UID,
53-
Groups: req.UserInfo.Groups,
54-
Extra: req.UserInfo.Extra,
51+
if req.Operation == v1admission.Create || req.Operation == v1admission.Delete {
52+
binding.Spec.UserInfo = &v1.UserInfo{
53+
Username: req.UserInfo.Username,
54+
UID: req.UserInfo.UID,
55+
Groups: req.UserInfo.Groups,
56+
Extra: req.UserInfo.Extra,
57+
}
5558
}
5659

5760
marshaledInstance, err := json.Marshal(binding)

api/v1alpha1/webhooks/serviceinstance_mutating_webhook.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"reflect"
9+
10+
v1admission "k8s.io/api/admission/v1"
11+
v1 "k8s.io/api/authentication/v1"
812

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

1115
"github.com/SAP/sap-btp-service-operator/api/v1alpha1"
12-
v1 "k8s.io/api/authentication/v1"
1316
"sigs.k8s.io/controller-runtime/pkg/client"
1417
logf "sigs.k8s.io/controller-runtime/pkg/log"
1518
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -42,11 +45,10 @@ func (s *ServiceInstanceDefaulter) Handle(_ context.Context, req admission.Reque
4245
instancelog.Info("externalName not provided, defaulting to k8s name", "name", instance.Name)
4346
instance.Spec.ExternalName = instance.Name
4447
}
45-
instance.Spec.UserInfo = &v1.UserInfo{
46-
Username: req.UserInfo.Username,
47-
UID: req.UserInfo.UID,
48-
Groups: req.UserInfo.Groups,
49-
Extra: req.UserInfo.Extra,
48+
49+
err = s.setServiceInstanceUserInfo(req, instance)
50+
if err != nil {
51+
return admission.Errored(http.StatusInternalServerError, err)
5052
}
5153

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

61+
func (s *ServiceInstanceDefaulter) setServiceInstanceUserInfo(req admission.Request, instance *v1alpha1.ServiceInstance) error {
62+
userInfo := &v1.UserInfo{
63+
Username: req.UserInfo.Username,
64+
UID: req.UserInfo.UID,
65+
Groups: req.UserInfo.Groups,
66+
Extra: req.UserInfo.Extra,
67+
}
68+
if req.Operation == v1admission.Create || req.Operation == v1admission.Delete {
69+
instance.Spec.UserInfo = userInfo
70+
} else if req.Operation == v1admission.Update {
71+
oldInstance := &v1alpha1.ServiceInstance{}
72+
err := s.decoder.DecodeRaw(req.OldObject, oldInstance)
73+
if err != nil {
74+
return err
75+
}
76+
if !reflect.DeepEqual(oldInstance.Spec, instance.Spec) {
77+
instance.Spec.UserInfo = userInfo
78+
}
79+
}
80+
return nil
81+
}
82+
5983
func (s *ServiceInstanceDefaulter) InjectDecoder(d *admission.Decoder) error {
6084
s.decoder = d
6185
return nil

0 commit comments

Comments
 (0)