Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEVOPS-FIX] Add wait for CRD to be created #18

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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