@@ -150,27 +150,39 @@ 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
161+ bindingsCreatedByClient := []apisv1alpha1.APIBinding {}
154162 for _ , binding := range apiBindings {
155- found , err := bindingAlreadyExists (ctx , binding , kcpClient , b .Out )
163+ found , err := bindingAlreadyExists (binding , existingBindingList , b .Out )
156164 if err != nil {
157165 allErrors = append (allErrors , err )
158166 }
159167
160- if ! found {
161- err := kcpClient .Create (ctx , & binding )
162- if err != nil {
163- allErrors = append (allErrors , err )
164- }
168+ // if the binding exists continue, if not create the binding
169+ if found {
170+ continue
165171 }
166172
173+ err = kcpClient .Create (ctx , & binding )
174+ if err != nil {
175+ allErrors = append (allErrors , err )
176+ }
177+
178+ bindingsCreatedByClient = append (bindingsCreatedByClient , binding )
167179 }
168180
169181 if err := wait .PollImmediate (time .Millisecond * 500 , b .BindWaitTimeout , func () (done bool , err error ) {
170182 availableBindings := []apisv1alpha1.APIBinding {}
171- for _ , binding := range apiBindings {
183+ for _ , binding := range bindingsCreatedByClient {
172184 createdBinding := apisv1alpha1.APIBinding {}
173- err = kcpClient .Get (ctx , types.NamespacedName {Name : binding .Name }, & createdBinding )
185+ err = kcpClient .Get (ctx , types.NamespacedName {Name : binding .GetName () }, & createdBinding )
174186 if err != nil {
175187 return false , err
176188 }
@@ -181,7 +193,7 @@ func (b *BindOptions) Run(ctx context.Context) error {
181193 return fmt .Errorf ("bindings for catalog entry %s could not be created successfully: %v" , entryName , err )
182194 }
183195
184- if _ , err := fmt .Fprintf (b .Out , "%s created and bound to catalog entry.\n " , entryName ); err != nil {
196+ if _ , err := fmt .Fprintf (b .Out , "Apibinding created and bound to catalog entry %s .\n " , entryName ); err != nil {
185197 allErrors = append (allErrors , err )
186198 }
187199 return utilerrors .NewAggregate (allErrors )
@@ -214,27 +226,18 @@ func newClient(cfg *rest.Config, clusterName logicalcluster.Name) (client.Client
214226
215227// bindingAlreadyExists lists out the existing bindings in a workspace, checks if the export reference is the same. If so,
216228// 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 ) {
229+ func bindingAlreadyExists (expectedBinding apisv1alpha1.APIBinding , existingBindingList apisv1alpha1. APIBindingList , wr io.Writer ) (bool , error ) {
218230 found := false
219- bindingList := apisv1alpha1.APIBindingList {}
220- err := kcpclient .List (ctx , & bindingList )
221- if err != nil {
222- return found , err
223- }
224231
225- for _ , b := range bindingList .Items {
226- if b .Spec .Reference == expectedBinding .Spec .Reference {
232+ for _ , b := range existingBindingList .Items {
233+ if reflect . DeepEqual ( & b .Spec .Reference , & expectedBinding .Spec .Reference ) {
227234 found = true
228235 // if the specified export reference matches the expected export reference, then check if permission
229236 // claims also match.
230237 if ! reflect .DeepEqual (b .Spec .PermissionClaims , expectedBinding .Spec .PermissionClaims ) {
231- // if the permission claims are not equal then update the apibinding
232- 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 {
238+ // if the permission claims are not equal then print the message.
239+ // TODO: Add a command to print the differences and print the bindings.
240+ if _ , err := fmt .Fprintf (wr , "Binding for %s already exists, but the permission claims are different. Skipping any action.\n " , b .Name ); err != nil {
238241 return found , err
239242 }
240243 }
@@ -243,6 +246,7 @@ func bindingAlreadyExists(ctx context.Context, expectedBinding apisv1alpha1.APIB
243246 if _ , err := fmt .Fprintf (wr , "Found an existing APIExport %s pointing to the same export reference.\n " , b .Name ); err != nil {
244247 return found , err
245248 }
249+ break
246250 }
247251 }
248252 return found , nil
0 commit comments