Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ podman run --rm -ti \
1. Use CVO `render` to render all the manifests from release-payload to a directory. [here](#using-cvo-to-render-the-release-payload-locally)

2. Create the operators from the manifests by using `oc create -f <directory when CVO rendered manfiests>`.

## Running CVO tests

```sh
# Run all unit tests
go test ./...

# Run integration tests against a cluster (creates content in a given namespace)
# Requires the CVO CRD to be installed.
export KUBECONFIG=<admin kubeconfig>
TEST_INTEGRATION=1 go test ./... -test.run=^TestIntegration
```
59 changes: 35 additions & 24 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"math/rand"
"net/http"
"os"
Expand All @@ -16,12 +17,10 @@ import (
"github.com/openshift/cluster-version-operator/pkg/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apiextinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -47,6 +46,11 @@ var (
}

startOpts struct {
// name is provided for testing only to allow multiple CVO's to be running at once
name string
// namespace is provided for testing only
namespace string

kubeconfig string
nodeName string
listenAddr string
Expand Down Expand Up @@ -78,6 +82,16 @@ func runStartCmd(cmd *cobra.Command, args []string) {
startOpts.nodeName = name
}

// exposed for end-to-end testing only
startOpts.name = os.Getenv("CVO_NAME")
if len(startOpts.name) == 0 {
startOpts.name = componentName
}
startOpts.namespace = os.Getenv("CVO_NAMESPACE")
if len(startOpts.name) == 0 {
startOpts.namespace = componentNamespace
}

if rootOpts.releaseImage == "" {
glog.Fatalf("missing --release-image flag, it is required")
}
Expand All @@ -99,14 +113,13 @@ func runStartCmd(cmd *cobra.Command, args []string) {
stopCh := make(chan struct{})
run := func(stop <-chan struct{}) {

ctx := createControllerContext(cb, stopCh)
ctx := createControllerContext(cb, startOpts.name, stopCh)
if err := startControllers(ctx); err != nil {
glog.Fatalf("error starting controllers: %v", err)
}

ctx.CVInformerFactory.Start(ctx.Stop)
ctx.InformerFactory.Start(ctx.Stop)
ctx.KubeInformerFactory.Start(ctx.Stop)
ctx.APIExtInformerFactory.Start(ctx.Stop)
close(ctx.InformersStarted)

select {}
Expand Down Expand Up @@ -209,9 +222,8 @@ func newClientBuilder(kubeconfig string) (*clientBuilder, error) {
type controllerContext struct {
ClientBuilder *clientBuilder

InformerFactory informers.SharedInformerFactory
KubeInformerFactory kubeinformers.SharedInformerFactory
APIExtInformerFactory apiextinformers.SharedInformerFactory
CVInformerFactory informers.SharedInformerFactory
InformerFactory informers.SharedInformerFactory

Stop <-chan struct{}

Expand All @@ -220,23 +232,21 @@ type controllerContext struct {
ResyncPeriod func() time.Duration
}

func createControllerContext(cb *clientBuilder, stop <-chan struct{}) *controllerContext {
func createControllerContext(cb *clientBuilder, name string, stop <-chan struct{}) *controllerContext {
client := cb.ClientOrDie("shared-informer")
kubeClient := cb.KubeClientOrDie("kube-shared-informer")
apiExtClient := cb.APIExtClientOrDie("apiext-shared-informer")

cvInformer := informers.NewFilteredSharedInformerFactory(client, resyncPeriod()(), "", func(opts *metav1.ListOptions) {
opts.FieldSelector = fmt.Sprintf("metadata.name=%s", name)
})
sharedInformers := informers.NewSharedInformerFactory(client, resyncPeriod()())
kubeSharedInformer := kubeinformers.NewSharedInformerFactory(kubeClient, resyncPeriod()())
apiExtSharedInformer := apiextinformers.NewSharedInformerFactory(apiExtClient, resyncPeriod()())

return &controllerContext{
ClientBuilder: cb,
InformerFactory: sharedInformers,
KubeInformerFactory: kubeSharedInformer,
APIExtInformerFactory: apiExtSharedInformer,
Stop: stop,
InformersStarted: make(chan struct{}),
ResyncPeriod: resyncPeriod(),
ClientBuilder: cb,
CVInformerFactory: cvInformer,
InformerFactory: sharedInformers,
Stop: stop,
InformersStarted: make(chan struct{}),
ResyncPeriod: resyncPeriod(),
}
}

Expand All @@ -248,22 +258,23 @@ func startControllers(ctx *controllerContext) error {

go cvo.New(
startOpts.nodeName,
componentNamespace, componentName,
startOpts.namespace, startOpts.name,
rootOpts.releaseImage,
overrideDirectory,
ctx.ResyncPeriod(),
ctx.InformerFactory.Config().V1().ClusterVersions(),
ctx.CVInformerFactory.Config().V1().ClusterVersions(),
ctx.InformerFactory.Config().V1().ClusterOperators(),
ctx.ClientBuilder.RestConfig(),
ctx.ClientBuilder.ClientOrDie(componentName),
ctx.ClientBuilder.KubeClientOrDie(componentName),
ctx.ClientBuilder.APIExtClientOrDie(componentName),
true,
).Run(2, ctx.Stop)

if startOpts.enableAutoUpdate {
go autoupdate.New(
componentNamespace, componentName,
ctx.InformerFactory.Config().V1().ClusterVersions(),
ctx.CVInformerFactory.Config().V1().ClusterVersions(),
ctx.InformerFactory.Config().V1().ClusterOperators(),
ctx.ClientBuilder.ClientOrDie(componentName),
ctx.ClientBuilder.KubeClientOrDie(componentName),
Expand Down
8 changes: 8 additions & 0 deletions hack/test-integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -euo pipefail

base=$( dirname "${BASH_SOURCE[0]}")

go run "${base}/test-prerequisites.go"
TEST_INTEGRATION=1 go test ./... -test.run=^TestIntegration -args -alsologtostderr -v=5
55 changes: 55 additions & 0 deletions hack/test-prerequisites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"io/ioutil"
"log"
"time"

"github.com/ghodss/yaml"
v1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/clientcmd"
)

// main installs the CV CRD to a cluster for integration testing.
func main() {
log.SetFlags(0)
kcfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{})
cfg, err := kcfg.ClientConfig()
if err != nil {
log.Fatalf("cannot load config: %v", err)
}

client := apiext.NewForConfigOrDie(cfg)
for _, path := range []string{
"install/0000_00_cluster-version-operator_01_clusterversion.crd.yaml",
"install/0000_00_cluster-version-operator_01_clusteroperator.crd.yaml",
} {
var name string
err := wait.PollImmediate(time.Second, 30*time.Second, func() (bool, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Unable to read %s: %v", path, err)
}
var crd v1beta1.CustomResourceDefinition
if err := yaml.Unmarshal(data, &crd); err != nil {
log.Fatalf("Unable to parse CRD %s: %v", path, err)
}
name = crd.Name
_, err = client.Apiextensions().CustomResourceDefinitions().Create(&crd)
if errors.IsAlreadyExists(err) {
return true, nil
}
if err != nil {
return false, err
}
log.Printf("Installed %s CRD", crd.Name)
return true, nil
})
if err != nil {
log.Fatalf("Could not install %s CRD: %v", name, err)
}
}
}
Loading