Skip to content

Commit

Permalink
Merge pull request #18 from spotahome/devops-fix-add-wait-for-crd-cre…
Browse files Browse the repository at this point in the history
…ation

[DEVOPS-FIX] Add wait for CRD to be created
  • Loading branch information
jchanam authored Jan 18, 2018
2 parents fdd117d + 264b5ef commit 831efd6
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pkg/crd/crd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package crd

import (
"errors"
"fmt"
"time"

apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand Down Expand Up @@ -70,9 +72,10 @@ func NewCRD(config rest.Config, signalChan chan int, kind string, domain string,

// Create creates the crd on k8s
func (t *CRD) Create() error {
crdName := fmt.Sprintf("%s.%s", t.apiName, t.domain)
crd := &apiextensionsv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", t.apiName, t.domain),
Name: crdName,
},
Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
Group: t.domain,
Expand All @@ -85,7 +88,37 @@ func (t *CRD) Create() error {
}

_, err := t.clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
return err
if err != nil {
return err
}
return t.waitForCRD(crdName)
}

func (t *CRD) waitForCRD(crdName string) error {
finished := make(chan struct{})
timeout := make(chan struct{})
defer close(timeout)

go func() {
for range time.NewTicker(10 * time.Millisecond).C {
_, err := t.clientset.Apiextensions().CustomResourceDefinitions().Get(crdName, metav1.GetOptions{})
if err == nil {
finished <- struct{}{}
return
}
select {
case <-timeout:
return
default:
}
}
}()
select {
case <-time.After(5 * time.Second):
return errors.New("timeout waiting for CRD creation")
case <-finished:
return nil
}
}

func (t *CRD) createClient() (*rest.RESTClient, error) {
Expand Down

0 comments on commit 831efd6

Please sign in to comment.