Skip to content
This repository was archived by the owner on Jun 22, 2023. It is now read-only.

Commit dbfa3b6

Browse files
Specify if an existing binding exists
1 parent 4acf817 commit dbfa3b6

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

cmd/kcp-catalog/bind/catalogentry/bind.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

config/samples/catalog.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ metadata:
66
spec:
77
exports:
88
- workspace:
9-
path: root:cert-manager
9+
path: root:cert-manager-stable
1010
exportName: cert-manager-stable

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ require (
4343
github.com/davecgh/go-spew v1.1.1 // indirect
4444
github.com/docker/distribution v2.8.1+incompatible // indirect
4545
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
46-
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
4746
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
4847
github.com/felixge/httpsnoop v1.0.1 // indirect
4948
github.com/fsnotify/fsnotify v1.5.4 // indirect
@@ -142,7 +141,7 @@ require (
142141
k8s.io/cluster-bootstrap v0.0.0 // indirect
143142
k8s.io/component-helpers v0.0.0 // indirect
144143
k8s.io/kube-aggregator v0.0.0 // indirect
145-
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
144+
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
146145
k8s.io/kubelet v0.0.0 // indirect
147146
k8s.io/kubernetes v1.24.3 // indirect
148147
k8s.io/mount-utils v0.0.0 // indirect

go.sum

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkg
194194
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
195195
github.com/emicklei/go-restful v2.9.5+incompatible h1:spTtZBk5DYEvbxMVutUuTyh1Ao2r4iyvLdACqsl/Ljk=
196196
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
197-
github.com/emicklei/go-restful/v3 v3.8.0 h1:eCZ8ulSerjdAiaNpF7GxXIE7ZCMo1moN1qX+S609eVw=
198-
github.com/emicklei/go-restful/v3 v3.8.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
199197
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
200198
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
201199
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -1285,9 +1283,8 @@ k8s.io/klog/v2 v2.60.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
12851283
k8s.io/klog/v2 v2.70.1 h1:7aaoSdahviPmR+XkS7FyxlkkXs6tHISSG03RxleQAVQ=
12861284
k8s.io/klog/v2 v2.70.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
12871285
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
1286+
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 h1:Gii5eqf+GmIEwGNKQYQClCayuJCe2/4fZUvF7VG99sU=
12881287
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42/go.mod h1:Z/45zLw8lUo4wdiUkI+v/ImEGAvu3WatcZl3lPMR4Rk=
1289-
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 h1:MQ8BAZPZlWk3S9K4a9NCkIFQtZShWqoha7snGixVgEA=
1290-
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1/go.mod h1:C/N6wCaBHeBHkHUesQOQy2/MZqGgMAFPqGsGQLdbZBU=
12911288
k8s.io/system-validators v1.7.0/go.mod h1:gP1Ky+R9wtrSiFbrpEPwWMeYz9yqyy1S/KOh0Vci7WI=
12921289
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
12931290
k8s.io/utils v0.0.0-20211116205334-6203023598ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=

0 commit comments

Comments
 (0)