Skip to content

Commit d8d0043

Browse files
committed
Fix a bug in olm install
When installing OLM, retry creating resources, if the CRD is not ready yet. Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
1 parent 861f524 commit d8d0043

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: >
5+
Fix a bug where `olm install` command is failed fo "no-match" error.
6+
7+
kind: "bugfix"
8+
9+
# Is this a breaking change?
10+
breaking: false

internal/olm/client/client.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,44 @@ func (c Client) DoCreate(ctx context.Context, objs ...client.Object) error {
113113
for _, obj := range objs {
114114
kind := obj.GetObjectKind().GroupVersionKind().Kind
115115
log.Infof(" Creating %s %q", kind, getName(obj.GetNamespace(), obj.GetName()))
116-
err := c.KubeClient.Create(ctx, obj)
117-
if err != nil {
118-
if !apierrors.IsAlreadyExists(err) {
116+
117+
for i := 0; i < 5; i++ { // try to create 5 times before giving up
118+
retry, err := c.tryCreatingOneResource(ctx, obj, kind)
119+
if err != nil {
119120
return err
120121
}
121-
log.Infof(" %s %q already exists", kind, getName(obj.GetNamespace(), obj.GetName()))
122+
123+
if retry {
124+
log.Infof(" the CRD for %s %q is not ready yet. Retrying in one second", kind, getName(obj.GetNamespace(), obj.GetName()))
125+
time.Sleep(time.Second)
126+
continue
127+
}
128+
129+
log.Infof(" %s %q created", kind, getName(obj.GetNamespace(), obj.GetName()))
130+
break
122131
}
123132
}
124133
return nil
125134
}
126135

136+
func (c Client) tryCreatingOneResource(ctx context.Context, obj client.Object, kind string) (bool, error) {
137+
err := c.KubeClient.Create(ctx, obj)
138+
if err != nil {
139+
if apierrors.IsAlreadyExists(err) {
140+
log.Infof(" %s %q already exists", kind, getName(obj.GetNamespace(), obj.GetName()))
141+
return false, nil
142+
}
143+
144+
if meta.IsNoMatchError(err) {
145+
return true, nil
146+
}
147+
148+
return false, err
149+
}
150+
151+
return false, nil
152+
}
153+
127154
func (c Client) DoDelete(ctx context.Context, objs ...client.Object) error {
128155
for _, obj := range objs {
129156
kind := obj.GetObjectKind().GroupVersionKind().Kind

0 commit comments

Comments
 (0)