Skip to content

Commit

Permalink
Merge pull request #793 from alicerum/check-taskruns
Browse files Browse the repository at this point in the history
Check for Tekton installed before running the manager
  • Loading branch information
openshift-merge-robot authored Jun 7, 2021
2 parents 2903c72 + 7817249 commit 570e931
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/shipwright-build-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)

"github.com/spf13/pflag"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"

"k8s.io/apimachinery/pkg/api/meta"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -95,6 +99,25 @@ func main() {
os.Exit(1)
}

// check for pipelines to exist and be available
installed, err := checkForPipelinesInstalled(mgr)
if err != nil {
ctxlog.Error(ctx, err, "Error while checking for TaskRuns")
os.Exit(1)
}
if !installed {
msg := "Cannot start manager: Tekton Pipelines are not installed on the cluster"

// we also want to put this into the termination log
// so that user can see this message as the reason pod failed
if err := ioutil.WriteFile(buildCfg.TerminationLogPath, []byte(msg), 0644); err != nil {
ctxlog.Error(ctx, err, "Error while trying to write to termination log")
}

ctxlog.Error(ctx, nil, msg)
os.Exit(1)
}

buildMetrics.InitPrometheus(buildCfg)

// Add optionally configured extra handlers to metrics endpoint
Expand All @@ -113,3 +136,21 @@ func main() {
os.Exit(1)
}
}

// checkForPipelinesInstalled tries to find the "TaskRun" resource on the api
// returns (true, nil) if resource is found, (false, nil) otherwise
// and error as second return parameter in case of failed request
func checkForPipelinesInstalled(mgr manager.Manager) (bool, error) {
rm := mgr.GetRESTMapper()

_, err := rm.KindFor(pipeline.TaskRunResource.WithVersion(pipelinev1beta1.SchemeGroupVersion.Version))
if err != nil {
if meta.IsNoMatchError(err) {
return false, nil
}

return false, err
}

return true, nil
}
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ The following environment variables are available:
| `CLUSTERBUILDSTRATEGY_MAX_CONCURRENT_RECONCILES` | The number of concurrent reconciles by the clusterbuildstrategy controller. A value of 0 or lower will use the default from the [controller-runtime controller Options](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/controller#Options). Default is 0. |
| `KUBE_API_BURST` | Burst to use for the Kubernetes API client. See [Config.Burst](https://pkg.go.dev/k8s.io/client-go/rest#Config.Burst). A value of 0 or lower will use the default from client-go, which currently is 10. Default is 0. |
| `KUBE_API_QPS` | QPS to use for the Kubernetes API client. See [Config.QPS](https://pkg.go.dev/k8s.io/client-go/rest#Config.QPS). A value of 0 or lower will use the default from client-go, which currently is 5. Default is 0. |
| `TERMINATION_LOG_PATH` | Path of the termination log. This is where controller application will write the reason of its termination. Default value is `/dev/termination-log`. |
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const (
// environment variables for the kube API
kubeAPIBurst = "KUBE_API_BURST"
kubeAPIQPS = "KUBE_API_QPS"

terminationLogPathDefault = "/dev/termination-log"
terminationLogPathEnvVar = "TERMINATION_LOG_PATH"
)

var (
Expand All @@ -77,6 +80,7 @@ type Config struct {
GitContainerTemplate corev1.Container
KanikoContainerImage string
RemoteArtifactsContainerImage string
TerminationLogPath string
Prometheus PrometheusConfig
ManagerOptions ManagerOptions
Controllers Controllers
Expand Down Expand Up @@ -160,6 +164,7 @@ func NewDefaultConfig() *Config {
QPS: 0,
Burst: 0,
},
TerminationLogPath: terminationLogPathDefault,
}
}

Expand Down Expand Up @@ -248,6 +253,10 @@ func (c *Config) SetConfigFromEnv() error {
return err
}

if terminationLogPath := os.Getenv(terminationLogPathEnvVar); terminationLogPath != "" {
c.TerminationLogPath = terminationLogPath
}

return nil
}

Expand Down

0 comments on commit 570e931

Please sign in to comment.