Skip to content

Commit

Permalink
RHTAPBUGS-937 use env var for image SPI
Browse files Browse the repository at this point in the history
Instead of detecting if the image controller SPI is present it now
requires an explicit environment variable, to prevent race conditions.
  • Loading branch information
stuartwdouglas committed Nov 2, 2023
1 parent c2f9d38 commit b401357
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/redhat-appstudio/jvm-build-service/pkg/reconciler/util"
"github.com/tektoncd/cli/pkg/cli"
"os"
"time"

"github.com/redhat-appstudio/jvm-build-service/pkg/metrics"
Expand Down Expand Up @@ -43,6 +44,7 @@ func NewManager(cfg *rest.Config, options ctrl.Options) (ctrl.Manager, error) {
// and controller-runtime does not retry on missing CRDs.
// so we are going to wait on the CRDs existing before moving forward.
apiextensionsClient := apiextensionsclient.NewForConfigOrDie(cfg)

if err := wait.PollUntilContextTimeout(context.TODO(), time.Second*5, time.Minute*5, true, func(ctx context.Context) (done bool, err error) {
_, err = apiextensionsClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), "taskruns.tekton.dev", metav1.GetOptions{})
if err != nil {
Expand All @@ -55,14 +57,25 @@ func NewManager(cfg *rest.Config, options ctrl.Options) (ctrl.Manager, error) {
controllerLog.Error(err, "timed out waiting for taskrun CRD to be created")
return nil, err
}

//check for the SPI to be present
imageSpiPresent := true
_, err := apiextensionsClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), "imagerepositories.appstudio.redhat.com", metav1.GetOptions{})
if err != nil {
imageSpiPresent = false
imageSpiPresent := false

if os.Getenv("USE_IMAGE_SPI") == "true" {
//check for the SPI to be present
imageSpiPresent = true
if err := wait.PollUntilContextTimeout(context.TODO(), time.Second*5, time.Minute*5, true, func(ctx context.Context) (done bool, err error) {
_, err = apiextensionsClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), "imagerepositories.appstudio.redhat.com", metav1.GetOptions{})
if err != nil {
controllerLog.Info(fmt.Sprintf("get of imagerepositories CRD failed with: %s", err.Error()))
return false, nil
}
controllerLog.Info("get of imagerepositories CRD returned successfully")
return true, nil
}); err != nil {
controllerLog.Error(err, "timed out waiting for imagerepositories CRD to be created")
return nil, err
}
controllerLog.Info("ImageRepositories SPI Present")
}
controllerLog.Info(fmt.Sprintf("ImageRepositories SPI Present: %t", imageSpiPresent))

options.Scheme = runtime.NewScheme()

Expand Down Expand Up @@ -124,7 +137,7 @@ func NewManager(cfg *rest.Config, options ctrl.Options) (ctrl.Manager, error) {
&v1.Pod{}: cacheSelector,
}})

mgr, err = ctrl.NewManager(cfg, options)
mgr, err := ctrl.NewManager(cfg, options)

if err != nil {
return nil, err
Expand Down

0 comments on commit b401357

Please sign in to comment.