Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczyns committed Jun 16, 2020
2 parents dd7b437 + 2788adb commit 8a10094
Show file tree
Hide file tree
Showing 78 changed files with 5,517 additions and 4,626 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:

# Install operator-sdk
env:
- RELEASE_VERSION=v0.16.0
- RELEASE_VERSION=v0.17.1

before_install:
- curl -OJL https://github.com/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu
Expand Down
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ IMAGE_SHA = $(lastword $(subst @, ,$(IMAGE_REPO_DIGEST)))
REGISTRY_IMAGE_SHA = $(lastword $(subst @, ,$(REGISTRY_IMAGE_REPO_DIGEST)))

# Current release (used for CSV management)
CURRENT_RELEASE=0.9.0
CURRENT_RELEASE=0.10.0

# OS detection
ifeq ($(OS),Windows_NT)
Expand All @@ -66,33 +66,34 @@ endif
endif


.PHONY: build deploy deploy-olm build-image build-registry-image push-image push-registry-image push-manifest int-test-install int-test-collections int-test-uninstall int-test-lifecycle
.PHONY: build deploy deploy-olm build-image build-registry-image push-image push-registry-image push-manifest int-test-install int-test-stacks int-test-uninstall int-test-lifecycle

build: generate
GO111MODULE=on go install ./cmd/manager
GO111MODULE=on go install ./cmd/manager/collection
GO111MODULE=on go install ./cmd/manager/stack
GO111MODULE=on go install ./cmd/admission-webhook
GO111MODULE=on go install ./cmd/devfile-registry-controller

build-image: generate
# These commands were taken from operator-sdk 0.8.1. The sdk did not let us
# pass the ldflags option. The advice from operator-sdk was to run the
# commands separately here.
# operator-sdk build ${IMAGE}
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o build/_output/bin/kabanero-operator -gcflags "all=-trimpath=$(GOPATH)" -asmflags "all=-trimpath=$(GOPATH)" -ldflags "-X main.GitTag=$(TRAVIS_TAG) -X main.GitCommit=$(TRAVIS_COMMIT) -X main.GitRepoSlug=$(TRAVIS_REPO_SLUG) -X main.BuildDate=`date -u +%Y%m%d.%H%M%S`" github.com/kabanero-io/kabanero-operator/cmd/manager
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o build/_output/bin/kabanero-operator-collection-controller -gcflags "all=-trimpath=$(GOPATH)" -asmflags "all=-trimpath=$(GOPATH)" -ldflags "-X main.GitTag=$(TRAVIS_TAG) -X main.GitCommit=$(TRAVIS_COMMIT) -X main.GitRepoSlug=$(TRAVIS_REPO_SLUG) -X main.BuildDate=`date -u +%Y%m%d.%H%M%S`" github.com/kabanero-io/kabanero-operator/cmd/manager/collection
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o build/_output/bin/kabanero-operator-stack-controller -gcflags "all=-trimpath=$(GOPATH)" -asmflags "all=-trimpath=$(GOPATH)" -ldflags "-X main.GitTag=$(TRAVIS_TAG) -X main.GitCommit=$(TRAVIS_COMMIT) -X main.GitRepoSlug=$(TRAVIS_REPO_SLUG) -X main.BuildDate=`date -u +%Y%m%d.%H%M%S`" github.com/kabanero-io/kabanero-operator/cmd/manager/stack
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o build/_output/bin/admission-webhook -gcflags "all=-trimpath=$(GOPATH)" -asmflags "all=-trimpath=$(GOPATH)" -ldflags "-X main.GitTag=$(TRAVIS_TAG) -X main.GitCommit=$(TRAVIS_COMMIT) -X main.GitRepoSlug=$(TRAVIS_REPO_SLUG) -X main.BuildDate=`date -u +%Y%m%d.%H%M%S`" github.com/kabanero-io/kabanero-operator/cmd/admission-webhook
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -o build/_output/bin/devfile-registry-controller -gcflags "all=-trimpath=$(GOPATH)" -asmflags "all=-trimpath=$(GOPATH)" -ldflags "-X main.GitTag=$(TRAVIS_TAG) -X main.GitCommit=$(TRAVIS_COMMIT) -X main.GitRepoSlug=$(TRAVIS_REPO_SLUG) -X main.BuildDate=`date -u +%Y%m%d.%H%M%S`" github.com/kabanero-io/kabanero-operator/cmd/devfile-registry-controller

docker build -f build/Dockerfile -t $(IMAGE) .

build-registry-image:
# Build an OLM private registry for Kabanero. Should be run after push-image so the IMAGE SHA is generated
rm -Rf build/registry
mkdir -p build/registry
cp LICENSE build/registry/LICENSE
cp -R registry/manifests build/registry/
cp registry/Dockerfile build/registry/Dockerfile
cp deploy/crds/kabanero.io_kabaneros_crd.yaml deploy/crds/kabanero.io_collections_crd.yaml deploy/crds/kabanero.io_stacks_crd.yaml build/registry/manifests/kabanero-operator/$(CURRENT_RELEASE)/
cp deploy/crds/kabanero.io_kabaneros_crd.yaml deploy/crds/kabanero.io_stacks_crd.yaml build/registry/manifests/kabanero-operator/$(CURRENT_RELEASE)/

# Use the internal service address in the CSV
ifdef INTERNAL_REGISTRY
Expand Down Expand Up @@ -156,7 +157,6 @@ generate:
install:
kubectl config set-context $$(kubectl config current-context) --namespace=kabanero
kubectl apply -f deploy/crds/kabanero.io_kabaneros_crd.yaml
kubectl apply -f deploy/crds/kabanero.io_collections_crd.yaml
kubectl apply -f deploy/crds/kabanero.io_stacks_crd.yaml

deploy:
Expand Down Expand Up @@ -197,7 +197,7 @@ ifndef GITHUB_TOKEN
endif
mkdir -p build/bin
curl -L https://github.com/mitchellh/golicense/releases/download/v0.2.0/golicense_0.2.0_$(detected_OS)_x86_64.tar.gz | tar -C build/bin -xzf - golicense
build/bin/golicense -plain ./license-rules.json build/_output/bin/admission-webhook build/_output/bin/kabanero-operator build/_output/bin/kabanero-operator-collection-controller build/_output/bin/kabanero-operator-stack-controller | sort > 3RD_PARTY || true
build/bin/golicense -plain ./license-rules.json build/_output/bin/admission-webhook build/_output/bin/kabanero-operator build/_output/bin/kabanero-operator-stack-controller build/_output/bin/devfile-registry-controller | sort > 3RD_PARTY || true
rm build/bin/golicense

# Integration Tests
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ We suggest you install from a [release](https://github.com/kabanero-io/kabanero-
[![Build Status](https://travis-ci.org/kabanero-io/kabanero-operator.svg?branch=master)](https://travis-ci.org/kabanero-io/kabanero-operator)

The Kabanero operator is developed using:
* `operator-sdk` version 0.16.0.
* `operator-sdk` version 0.17.1.
* `go` version 1.13.x

## Clone the Kabanero operator
Expand All @@ -17,7 +17,7 @@ git clone https://github.com/kabanero-io/kabanero-operator
cd kabanero-operator
```

# Quickstart - OpenShift Container Platform (OCP) 4.3
# Quickstart - OpenShift Container Platform (OCP) 4.4

We recommend you follow the install instructions referenced above to set up your cluster for the first time. If you would rather set it up manually, please continue with the following steps:

Expand All @@ -31,7 +31,7 @@ oc login -u admin -p admin https://openshift.my.com:8443/
## Deploy Prerequisite operators:

The following operators need to be installed at the cluster scope:
* [OpenShift Serverless Operator](https://docs.openshift.com/container-platform/4.3/serverless/installing-openshift-serverless.html)
* [OpenShift Serverless Operator](https://docs.openshift.com/container-platform/4.4/serverless/installing-openshift-serverless.html)
* OpenShift Pipelines Operator (from community-operators)
* Appsody Operator (from certified-operators)
* Open Liberty Operator (from certified-operators)
Expand Down
6 changes: 4 additions & 2 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM registry.access.redhat.com/ubi7/ubi-minimal:latest
LABEL vendor="Kabanero" \
name="Kabanero Operator" \
summary="Image for Kabanero Operator" \
description="This image contains the controller for the Kabanero Foundation and Collection. See https://github.com/kabanero-io/kabanero-operator/"
description="This image contains the controller for the Kabanero Foundation. See https://github.com/kabanero-io/kabanero-operator/"

# The license must be here for Redhat container certification
COPY LICENSE /licenses/
Expand All @@ -16,8 +16,10 @@ ENV OPERATOR=/usr/local/bin/kabanero-operator \
# install operator binary and supporting controllers.
COPY build/_output/bin/kabanero-operator ${OPERATOR}
COPY build/_output/bin/kabanero-operator-stack-controller /usr/local/bin/kabanero-operator-stack-controller
COPY build/_output/bin/kabanero-operator-collection-controller /usr/local/bin/kabanero-operator-collection-controller
COPY build/_output/bin/admission-webhook /usr/local/bin/admission-webhook
COPY build/_output/bin/devfile-registry-controller /usr/local/bin/devfile-registry-controller

RUN mkdir /devfiles && chmod +777 /devfiles

COPY build/bin /usr/local/bin
RUN /usr/local/bin/user_setup
Expand Down
6 changes: 0 additions & 6 deletions cmd/admission-webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/kabanero-io/kabanero-operator/pkg/apis"
collectionwebhook "github.com/kabanero-io/kabanero-operator/pkg/webhook/collection"
kabanerowebhookv1alpha1 "github.com/kabanero-io/kabanero-operator/pkg/webhook/kabanero/v1alpha1"
kabanerowebhookv1alpha2 "github.com/kabanero-io/kabanero-operator/pkg/webhook/kabanero/v1alpha2"
stackwebhook "github.com/kabanero-io/kabanero-operator/pkg/webhook/stack"

Expand Down Expand Up @@ -98,10 +96,6 @@ func main() {
// Setup the webhook server
hookServer := mgr.GetWebhookServer()
hookServer.Port = 9443
hookServer.Register("/validate-collections", collectionwebhook.BuildValidatingWebhook(&mgr))
hookServer.Register("/mutate-collections", collectionwebhook.BuildMutatingWebhook(&mgr))
hookServer.Register("/validate-kabaneros", kabanerowebhookv1alpha1.BuildValidatingWebhook(&mgr))
hookServer.Register("/mutate-kabaneros", kabanerowebhookv1alpha1.BuildMutatingWebhook(&mgr))
hookServer.Register("/validate-kabaneros/v1alpha2", kabanerowebhookv1alpha2.BuildValidatingWebhook(&mgr))
hookServer.Register("/validate-stacks", stackwebhook.BuildValidatingWebhook(&mgr))
hookServer.Register("/mutate-stacks", stackwebhook.BuildMutatingWebhook(&mgr))
Expand Down
232 changes: 232 additions & 0 deletions cmd/devfile-registry-controller/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"os"
// "runtime"
"strings"
"net/http"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"

"github.com/kabanero-io/kabanero-operator/pkg/apis"
"github.com/kabanero-io/kabanero-operator/pkg/controller/devfileregistry"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/log/zap"
"github.com/operator-framework/operator-sdk/pkg/metrics"
// sdkVersion "github.com/operator-framework/operator-sdk/version"
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)

// Change below variables to serve metrics on different host or port.
var (
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
operatorMetricsPort int32 = 8686
)
var log = logf.Log.WithName("cmd")

func main() {
// Add the zap logger flag set to the CLI. The flag set must
// be added before calling pflag.Parse().
pflag.CommandLine.AddFlagSet(zap.FlagSet())

// Add flags registered by imported packages (e.g. glog and
// controller-runtime)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

pflag.Parse()

// Use a zap logr.Logger implementation. If none of the zap
// flags are configured (or if the zap flag set is not being
// used), this defaults to a production zap logger.
//
// The logger instantiated here can be changed to any logger
// implementing the logr.Logger interface. This logger will
// be propagated through the whole operator, generating
// uniform and structured logs.
logf.SetLogger(zap.Logger())

namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
log.Error(err, "Failed to get watch namespace")
os.Exit(1)
}

// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "")
os.Exit(1)
}

ctx := context.TODO()
// Become the leader before proceeding
err = leader.Become(ctx, "devfile-registry-lock")
if err != nil {
log.Error(err, "")
os.Exit(1)
}

// Set default manager options
options := manager.Options{
Namespace: namespace,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
}

// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
// Note that this is not intended to be used for excluding namespaces, this is better done via a Predicate
// Also note that you may face performance issues when using this with a high number of namespaces.
// More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
if strings.Contains(namespace, ",") {
options.Namespace = ""
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
}

// Create a new manager to provide shared dependencies and start components
mgr, err := manager.New(cfg, options)
if err != nil {
log.Error(err, "")
os.Exit(1)
}

log.Info("Registering Components.")

// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Error(err, "")
os.Exit(1)
}

// Setup all Controllers
if err := devfileregistry.AddToManager(mgr); err != nil {
log.Error(err, "")
os.Exit(1)
}

// Add the Metrics Service
addMetrics(ctx, cfg)

//Start HTTTPS & Cmd
errs := Run(mgr)

// Run until channel receives error
select {
case err := <-errs:
log.Error(err, "Manager exited non-zero")
os.Exit(1)
}

}

func Run(mgr manager.Manager) chan error {

errs := make(chan error)

// Start serving devfiles index
go func() {
fs := http.FileServer(http.Dir("/devfiles"))
http.Handle("/", fs)
log.Info("Starting Devfile registry on port :8443...")
if err := http.ListenAndServeTLS(":8443","/tmp/serving-certs/tls.crt","/tmp/serving-certs/tls.key",nil); err != nil {
errs <- err
}
}()

// Start the Cmd
go func() {
log.Info("Starting the Cmd.")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
errs <- err
}
}()

return errs
}



// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
// the Prometheus operator
func addMetrics(ctx context.Context, cfg *rest.Config) {
// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
if errors.Is(err, k8sutil.ErrRunLocal) {
log.Info("Skipping CR metrics server creation; not running in a cluster.")
return
}
}

if err := serveCRMetrics(cfg, operatorNs); err != nil {
log.Info("Could not generate and serve custom resource metrics", "error", err.Error())
}

// Add to the below struct any other metrics ports you want to expose.
servicePorts := []v1.ServicePort{
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
}

// Create Service object to expose the metrics port(s).
service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts)
if err != nil {
log.Info("Could not create metrics Service", "error", err.Error())
}

// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
// necessary to configure Prometheus to scrape metrics from this operator.
services := []*v1.Service{service}

// The ServiceMonitor is created in the same namespace where the operator is deployed
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
if err != nil {
log.Info("Could not create ServiceMonitor object", "error", err.Error())
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
// ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation.
if err == metrics.ErrServiceMonitorNotPresent {
log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error())
}
}
}

// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
func serveCRMetrics(cfg *rest.Config, operatorNs string) error {
// The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below
// with your own custom logic. Note that if you are adding third party API schemas, probably you will need to
// customize this implementation to avoid permissions issues.
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
if err != nil {
return err
}

// The metrics will be generated from the namespaces which are returned here.
// NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error.
ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs)
if err != nil {
return err
}

// Generate and serve custom resource specific metrics.
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 8a10094

Please sign in to comment.