@@ -3,6 +3,7 @@ package organization
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
6
7
7
8
"k8s.io/apiserver/pkg/authentication/user"
8
9
"k8s.io/apiserver/pkg/endpoints/request"
@@ -45,29 +46,38 @@ func (c impersonatorFromRestconf) Impersonate(u user.Info) (client.Client, error
45
46
// billingEntityValidator validates that the billing entity exists and the requesting user has access to it.
46
47
// it does so by impersonating the user and trying to get the billing entity.
47
48
func (s * organizationStorage ) billingEntityValidator (ctx context.Context , org , oldOrg * orgv1.Organization ) error {
48
- // check if changed
49
- if oldOrg != nil && oldOrg .Spec .BillingEntityRef == org .Spec .BillingEntityRef {
50
- return nil
51
- }
52
- // check if we allow empty billing entities
53
- if org .Spec .BillingEntityRef == "" && s .allowEmptyBillingEntity {
54
- return nil
55
- }
49
+ validate := func () error {
50
+ // check if changed
51
+ if oldOrg != nil && oldOrg .Spec .BillingEntityRef == org .Spec .BillingEntityRef {
52
+ return nil
53
+ }
54
+ // check if we allow empty billing entities
55
+ if org .Spec .BillingEntityRef == "" && s .allowEmptyBillingEntity {
56
+ return nil
57
+ }
56
58
57
- user , ok := request .UserFrom (ctx )
58
- if ! ok {
59
- return fmt .Errorf ("no user in context" )
60
- }
59
+ user , ok := request .UserFrom (ctx )
60
+ if ! ok {
61
+ return fmt .Errorf ("no user in context" )
62
+ }
61
63
62
- var be billingv1.BillingEntity
63
- c , err := s .impersonator .Impersonate (user )
64
- if err != nil {
65
- return fmt .Errorf ("failed to impersonate user: %w" , err )
66
- }
64
+ var be billingv1.BillingEntity
65
+ c , err := s .impersonator .Impersonate (user )
66
+ if err != nil {
67
+ return fmt .Errorf ("failed to impersonate user: %w" , err )
68
+ }
69
+
70
+ if err := c .Get (ctx , client.ObjectKey {Name : org .Spec .BillingEntityRef }, & be ); err != nil {
71
+ return err
72
+ }
67
73
68
- if err := c .Get (ctx , client.ObjectKey {Name : org .Spec .BillingEntityRef }, & be ); err != nil {
69
- return err
74
+ return nil
70
75
}
71
76
72
- return nil
77
+ err := validate ()
78
+ if err != nil && s .skipBillingEntityValidation {
79
+ fmt .Fprintf (os .Stderr , "Warning: billing entity validation for %q (ref: %q) would have failed: %v\n " , org .Name , org .Spec .BillingEntityRef , err )
80
+ return nil
81
+ }
82
+ return err
73
83
}
0 commit comments