@@ -150,9 +150,16 @@ func (b *BindOptions) Run(ctx context.Context) error {
150150 apiBindings = append (apiBindings , * apiBinding )
151151 }
152152
153+ // fetch a list of existing binding in the current workspace.
154+ existingBindingList := apisv1alpha1.APIBindingList {}
155+ err = kcpClient .List (ctx , & existingBindingList )
156+ if err != nil {
157+ allErrors = append (allErrors , err )
158+ }
159+
153160 // Create bindings to the target workspace
154161 for _ , binding := range apiBindings {
155- found , err := bindingAlreadyExists (ctx , binding , kcpClient , b .Out )
162+ found , err := bindingAlreadyExists (ctx , binding , existingBindingList , kcpClient , b .Out )
156163 if err != nil {
157164 allErrors = append (allErrors , err )
158165 }
@@ -214,27 +221,18 @@ func newClient(cfg *rest.Config, clusterName logicalcluster.Name) (client.Client
214221
215222// bindingAlreadyExists lists out the existing bindings in a workspace, checks if the export reference is the same. If so,
216223// it further checks the permission claims and updates the existing binding's claims.
217- func bindingAlreadyExists (ctx context.Context , expectedBinding apisv1alpha1.APIBinding , kcpclient client.Client , wr io.Writer ) (bool , error ) {
224+ func bindingAlreadyExists (ctx context.Context , expectedBinding apisv1alpha1.APIBinding , existingBindingList apisv1alpha1. APIBindingList , kcpclient client.Client , wr io.Writer ) (bool , error ) {
218225 found := false
219- bindingList := apisv1alpha1.APIBindingList {}
220- err := kcpclient .List (ctx , & bindingList )
221- if err != nil {
222- return found , err
223- }
224226
225- for _ , b := range bindingList .Items {
227+ for _ , b := range existingBindingList .Items {
226228 if b .Spec .Reference == expectedBinding .Spec .Reference {
227229 found = true
228230 // if the specified export reference matches the expected export reference, then check if permission
229231 // claims also match.
230232 if ! reflect .DeepEqual (b .Spec .PermissionClaims , expectedBinding .Spec .PermissionClaims ) {
231233 // if the permission claims are not equal then update the apibinding
232234 b .Spec = expectedBinding .Spec
233- err := kcpclient .Update (ctx , & b )
234- if err != nil {
235- return found , err
236- }
237- if _ , err := fmt .Fprintf (wr , "Updating an existing binding %s pointing to the same export reference.\n " , b .Name ); err != nil {
235+ if _ , err := fmt .Fprintf (wr , "Binding for %s already exists, but the permission claims are different. Skipping any action.\n " , b .Name ); err != nil {
238236 return found , err
239237 }
240238 }
0 commit comments