Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 4095668

Browse files
authored
Merge pull request #1013 from sttts/sttts-fix-crd-wait-loop
start: fix CRD wait loop
2 parents 87f9f97 + dc2fbe8 commit 4095668

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

pkg/bootkube/create.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
1919
"k8s.io/apimachinery/pkg/api/errors"
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/runtime/schema"
2122
"k8s.io/apimachinery/pkg/runtime/serializer"
2223
"k8s.io/apimachinery/pkg/util/wait"
2324
"k8s.io/apimachinery/pkg/util/yaml"
@@ -29,7 +30,7 @@ import (
2930
)
3031

3132
const (
32-
crdRolloutDuration = 5 * time.Second
33+
crdRolloutDuration = 1 * time.Second
3334
crdRolloutTimeout = 2 * time.Minute
3435
)
3536

@@ -222,9 +223,26 @@ func (c *creater) waitForCRD(m manifest) error {
222223
return fmt.Errorf("failed to unmarshal manifest: %v", err)
223224
}
224225

226+
// get first served version
227+
firstVer := ""
228+
if len(crd.Spec.Versions) > 0 {
229+
for _, v := range crd.Spec.Versions {
230+
if v.Served {
231+
firstVer = v.Name
232+
break
233+
}
234+
}
235+
} else {
236+
firstVer = crd.Spec.Version
237+
}
238+
if len(firstVer) == 0 {
239+
return fmt.Errorf("expected at least one served version")
240+
}
241+
225242
return wait.PollImmediate(crdRolloutDuration, crdRolloutTimeout, func() (bool, error) {
226-
uri := customResourceDefinitionKindURI(crd.Spec.Group, crd.Spec.Version, crd.GetNamespace(), crd.Spec.Names.Plural)
227-
res := c.client.Get().RequestURI(uri).Do()
243+
// get all resources, giving a 200 result with empty list on success, 404 before the CRD is active.
244+
namespaceLessURI := allCustomResourcesURI(schema.GroupVersionResource{Group: crd.Spec.Group, Version: firstVer, Resource: crd.Spec.Names.Plural})
245+
res := c.client.Get().RequestURI(namespaceLessURI).Do()
228246
if res.Error() != nil {
229247
if errors.IsNotFound(res.Error()) {
230248
return false, nil
@@ -235,21 +253,14 @@ func (c *creater) waitForCRD(m manifest) error {
235253
})
236254
}
237255

238-
// customResourceDefinitionKindURI returns the URI for the CRD kind.
239-
//
240-
// Example of apiGroup: "tco.coreos.com"
241-
// Example of version: "v1"
242-
// Example of namespace: "default"
243-
// Example of plural: "appversions"
244-
func customResourceDefinitionKindURI(apiGroup, version, namespace, plural string) string {
245-
if namespace == "" {
246-
namespace = metav1.NamespaceDefault
247-
}
248-
return fmt.Sprintf("/apis/%s/%s/namespaces/%s/%s",
249-
strings.ToLower(apiGroup),
250-
strings.ToLower(version),
251-
strings.ToLower(namespace),
252-
strings.ToLower(plural))
256+
// allCustomResourcesURI returns the URI for the CRD resource without a namespace, listing
257+
// all objects of that GroupVersionResource.
258+
func allCustomResourcesURI(gvr schema.GroupVersionResource) string {
259+
return fmt.Sprintf("/apis/%s/%s/%s",
260+
strings.ToLower(gvr.Group),
261+
strings.ToLower(gvr.Version),
262+
strings.ToLower(gvr.Resource),
263+
)
253264
}
254265

255266
func (c *creater) create(m manifest) error {

0 commit comments

Comments
 (0)