From d2daa4e2aca57a5c3ff15a6384c9d647b934b738 Mon Sep 17 00:00:00 2001 From: "Song.Jin" Date: Fri, 2 Nov 2018 20:24:40 +1100 Subject: [PATCH 1/3] allow it to disable CRD creation --- storage/kubernetes/storage.go | 56 ++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/storage/kubernetes/storage.go b/storage/kubernetes/storage.go index fa72e717..4c9399a0 100644 --- a/storage/kubernetes/storage.go +++ b/storage/kubernetes/storage.go @@ -40,6 +40,7 @@ type Config struct { InCluster bool `json:"inCluster"` KubeConfigFile string `json:"kubeConfigFile"` UseTPR bool `json:"useTPR"` // Flag option to use TPRs instead of CRDs + NoCrdCreation bool `json:"noCrdCreation"` // Flag to disable creation of CRDs at cluster level } // Open returns a storage using Kubernetes third party resource. @@ -85,37 +86,38 @@ func (c *Config) open(logger logrus.FieldLogger, waitForResources bool) (*client } ctx, cancel := context.WithCancel(context.Background()) + if !c.NoCrdCreation { + logger.Info("creating custom Kubernetes resources") + if !cli.registerCustomResources(c.UseTPR) { + if waitForResources { + cancel() + return nil, fmt.Errorf("failed creating custom resources") + } - logger.Info("creating custom Kubernetes resources") - if !cli.registerCustomResources(c.UseTPR) { - if waitForResources { - cancel() - return nil, fmt.Errorf("failed creating custom resources") + // Try to synchronously create the custom resources once. This doesn't mean + // they'll immediately be available, but ensures that the client will actually try + // once. + logger.Errorf("failed creating custom resources: %v", err) + go func() { + for { + if cli.registerCustomResources(c.UseTPR) { + return + } + + select { + case <-ctx.Done(): + return + case <-time.After(30 * time.Second): + } + } + }() } - // Try to synchronously create the custom resources once. This doesn't mean - // they'll immediately be available, but ensures that the client will actually try - // once. - logger.Errorf("failed creating custom resources: %v", err) - go func() { - for { - if cli.registerCustomResources(c.UseTPR) { - return - } - - select { - case <-ctx.Done(): - return - case <-time.After(30 * time.Second): - } + if waitForResources { + if err := cli.waitForCRDs(ctx); err != nil { + cancel() + return nil, err } - }() - } - - if waitForResources { - if err := cli.waitForCRDs(ctx); err != nil { - cancel() - return nil, err } } From 9b5bec1ddf7c00abd38584dd7952f6d0c4514ce9 Mon Sep 17 00:00:00 2001 From: "Song.Jin" Date: Sun, 4 Nov 2018 11:23:34 +1100 Subject: [PATCH 2/3] check if crd exist before try creating them --- storage/kubernetes/storage.go | 62 +++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/storage/kubernetes/storage.go b/storage/kubernetes/storage.go index 4c9399a0..211c736a 100644 --- a/storage/kubernetes/storage.go +++ b/storage/kubernetes/storage.go @@ -40,7 +40,6 @@ type Config struct { InCluster bool `json:"inCluster"` KubeConfigFile string `json:"kubeConfigFile"` UseTPR bool `json:"useTPR"` // Flag option to use TPRs instead of CRDs - NoCrdCreation bool `json:"noCrdCreation"` // Flag to disable creation of CRDs at cluster level } // Open returns a storage using Kubernetes third party resource. @@ -86,38 +85,37 @@ func (c *Config) open(logger logrus.FieldLogger, waitForResources bool) (*client } ctx, cancel := context.WithCancel(context.Background()) - if !c.NoCrdCreation { - logger.Info("creating custom Kubernetes resources") - if !cli.registerCustomResources(c.UseTPR) { - if waitForResources { - cancel() - return nil, fmt.Errorf("failed creating custom resources") - } - // Try to synchronously create the custom resources once. This doesn't mean - // they'll immediately be available, but ensures that the client will actually try - // once. - logger.Errorf("failed creating custom resources: %v", err) - go func() { - for { - if cli.registerCustomResources(c.UseTPR) { - return - } - - select { - case <-ctx.Done(): - return - case <-time.After(30 * time.Second): - } - } - }() + logger.Info("creating custom Kubernetes resources") + if !cli.registerCustomResources(c.UseTPR) { + if waitForResources { + cancel() + return nil, fmt.Errorf("failed creating custom resources") } - if waitForResources { - if err := cli.waitForCRDs(ctx); err != nil { - cancel() - return nil, err + // Try to synchronously create the custom resources once. This doesn't mean + // they'll immediately be available, but ensures that the client will actually try + // once. + logger.Errorf("failed creating custom resources: %v", err) + go func() { + for { + if cli.registerCustomResources(c.UseTPR) { + return + } + + select { + case <-ctx.Done(): + return + case <-time.After(30 * time.Second): + } } + }() + } + + if waitForResources { + if err := cli.waitForCRDs(ctx); err != nil { + cancel() + return nil, err } } @@ -150,6 +148,12 @@ func (cli *client) registerCustomResources(useTPR bool) (ok bool) { resourceName = r.ObjectMeta.Name } else { r := customResourceDefinitions[i] + var i interface{} + cli.logger.Infof("checking if resource %s has been created already...", r.ObjectMeta.Name) + if err := cli.list(r.Spec.Names.Plural, &i); err == nil { + cli.logger.Infof("The custom resource already created %s, hence continue", r.ObjectMeta.Name) + continue + } err = cli.postResource("apiextensions.k8s.io/v1beta1", "", "customresourcedefinitions", r) resourceName = r.ObjectMeta.Name } From 5f0a03a06b3c67540f93fe95a95105aedc5ec3f7 Mon Sep 17 00:00:00 2001 From: "Song.Jin" Date: Tue, 6 Nov 2018 11:05:36 +1100 Subject: [PATCH 3/3] modify log msg as per suggested --- storage/kubernetes/storage.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/storage/kubernetes/storage.go b/storage/kubernetes/storage.go index 211c736a..86f3f209 100644 --- a/storage/kubernetes/storage.go +++ b/storage/kubernetes/storage.go @@ -149,10 +149,12 @@ func (cli *client) registerCustomResources(useTPR bool) (ok bool) { } else { r := customResourceDefinitions[i] var i interface{} - cli.logger.Infof("checking if resource %s has been created already...", r.ObjectMeta.Name) + cli.logger.Infof("checking if custom resource %s has been created already...", r.ObjectMeta.Name) if err := cli.list(r.Spec.Names.Plural, &i); err == nil { - cli.logger.Infof("The custom resource already created %s, hence continue", r.ObjectMeta.Name) + cli.logger.Infof("The custom resource %s already available, skipping create", r.ObjectMeta.Name) continue + } else { + cli.logger.Infof("failed to list custom resource %s, attempting to create: %v", r.ObjectMeta.Name, err) } err = cli.postResource("apiextensions.k8s.io/v1beta1", "", "customresourcedefinitions", r) resourceName = r.ObjectMeta.Name