Skip to content

Commit

Permalink
Merge pull request #4 from jakub-dzon/sample-operator
Browse files Browse the repository at this point in the history
Functional tests for the reference operator
  • Loading branch information
pkliczewski authored Sep 9, 2020
2 parents da986e7 + 3436d83 commit d37e7ee
Show file tree
Hide file tree
Showing 45 changed files with 1,978 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ vet
whitespace
whitespace-check

_output
_output
_kubevirtci
_out
42 changes: 39 additions & 3 deletions examples/sample-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
all: fmt check

VERSION ?= v0.0.3
export VERSION := $(VERSION)

# Image registry variables
QUAY_USER ?= $(USER)
IMAGE_REGISTRY ?= quay.io/$(QUAY_USER)
IMAGE_TAG ?= latest
OPERATOR_IMAGE ?= sample-operator
HTTP_SERVER_IMAGE ?= sample-http-server
DEPLOY_DIR ?= manifests

TARGETS = \
gen-k8s \
Expand Down Expand Up @@ -45,7 +49,7 @@ whitespace: $(all_sources)
touch $@


check: whitespace-check vet goimports-check test/unit
check: whitespace-check vet goimports-check test-unit

whitespace-check: $(all_sources)
./hack/whitespace.sh
Expand All @@ -63,11 +67,17 @@ $(OPERATOR_SDK): go.mod

gen-k8s: $(OPERATOR_SDK) $(apis_sources)
$(OPERATOR_SDK) generate k8s
GOFLAGS=-mod= ./hack/update-codegen.sh
touch $@

test/unit: $(GINKGO) $(KUBEBUILDER_DIR)
test-unit: $(GINKGO) $(KUBEBUILDER_DIR)
$(GINKGO) $(GINKGO_ARGS) ./pkg/ ./cmd/

test-functional:
automation/test.sh

test: test-unit test-functional

operator-build:
docker build -f build/operator/Dockerfile -t $(IMAGE_REGISTRY)/$(OPERATOR_IMAGE):$(IMAGE_TAG) .

Expand All @@ -92,11 +102,37 @@ vet: $(cmd_sources) $(pkg_sources)
go vet ./pkg/... ./cmd/...
touch $@

gen-manifests:
DEPLOY_DIR=$(DEPLOY_DIR) \
CONTAINER_PREFIX=$(IMAGE_REGISTRY) \
IMAGE_TAG=$(IMAGE_TAG) \
OPERATOR_IMAGE=$(OPERATOR_IMAGE) \
SERVER_IMAGE=$(HTTP_SERVER_IMAGE) \
./hack/generate-manifests.sh

cluster-up:
./cluster/up.sh

cluster-down:
./cluster/down.sh

cluster-sync: cluster-operator-push cluster-operator-install

cluster-operator-push:
./cluster/operator-push.sh

cluster-operator-install:
./cluster/operator-install.sh

cluster-clean:
./cluster/clean.sh

.PHONY: \
all \
check \
docker-build \
docker-push \
test/unit \
test-unit \
test-functional \
gen-k8s \
vendor
12 changes: 7 additions & 5 deletions examples/sample-operator/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# Operator reference implementation

`sample-operator` controls deployment of a simple, not customized nginx HTTP server.

## Deployment
`sample-operator` is deployed in `kubevirt` namespace and deployment manifests will create it, if it's not present.

To deploy the operator execute following command:
```shell script
kubectl apply -f deploy/operator.yaml
kubectl apply -f manifests/v0.0.3/operator.yaml
```

Deployment of the HTTP server is controlled by the presence of a `sampleconfig` CR; to deploy it, execute:
```shell script
kubectl apply -f deploy/crd/sampleconfig_types.go
kubectl apply -f manifests/v0.0.3/sampleconfig_types.go
```

The HTTP service listens on port 8081, which can be forwarded locally:
The HTTP service listens on port `8081`, which in turn is exposed as a node port `30080`.

In case of deployments like CRC or kubevirtci, port forwarding might be required:
```shell script
kubectl -n kubevirt port-forward service/http-server 8081:8081
```
Expand Down
27 changes: 27 additions & 0 deletions examples/sample-operator/automation/execute-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# Copyright 2018-2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Run functional tests
export KUBECONFIG=$(./cluster/kubeconfig.sh)
export KUBECTL=${KUBECTL:-'./cluster/kubectl.sh'}
NAMESPACE=${NAMESPACE:-'kubevirt'}


echo "Using: "
echo " KUBECTL: $KUBECTL"
echo " KUBECONFIG: $KUBECONFIG"

go test ./tests/operator --v -timeout 30m -kubeconfig "$KUBECONFIG" -namespace "$NAMESPACE"
35 changes: 35 additions & 0 deletions examples/sample-operator/automation/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
#
# This file is part of the KubeVirt project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright 2017 Red Hat, Inc.
#

# CI considerations: $TARGET is used by the jenkins build, to distinguish what to test
# Currently considered $TARGET values:
# kubernetes-release: Runs all functional tests on a release kubernetes setup
# openshift-release: Runs all functional tests on a release openshift setup

set -ex

# Extend path to use also local installation of the golang
export PATH=$PATH:/usr/local/go/bin
export KUBEVIRT_PROVIDER=$TARGET

make cluster-down
make cluster-up
make cluster-sync

./automation/execute-tests.sh
22 changes: 22 additions & 0 deletions examples/sample-operator/cluster/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
#
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

echo 'Cleaning up ...'

./cluster/kubectl.sh delete --ignore-not-found -f _out/sample-operator/${VERSION}/sampleconfig_cr.yaml || true
./cluster/kubectl.sh delete --ignore-not-found -f _out/sample-operator/${VERSION}/operator.yaml || true
20 changes: 20 additions & 0 deletions examples/sample-operator/cluster/cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source ./cluster/kubevirtci.sh
kubevirtci::install

$(kubevirtci::path)/cluster-up/cli.sh "$@"
9 changes: 9 additions & 0 deletions examples/sample-operator/cluster/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -e

function debug {
./cluster/kubectl.sh get events
./cluster/kubectl.sh get all -n $1
./cluster/kubectl.sh get pod -n $1 | awk 'NR>1 {print $1}' | xargs ./cluster/kubectl.sh logs -n $1 --tail=50
}
22 changes: 22 additions & 0 deletions examples/sample-operator/cluster/down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
#
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

source ./cluster/kubevirtci.sh
kubevirtci::install

$(kubevirtci::path)/cluster-up/down.sh
20 changes: 20 additions & 0 deletions examples/sample-operator/cluster/kubeconfig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source ./cluster/kubevirtci.sh
kubevirtci::install

$(kubevirtci::path)/cluster-up/kubeconfig.sh "$@"
20 changes: 20 additions & 0 deletions examples/sample-operator/cluster/kubectl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source ./cluster/kubevirtci.sh
kubevirtci::install

$(kubevirtci::path)/cluster-up/kubectl.sh "$@"
36 changes: 36 additions & 0 deletions examples/sample-operator/cluster/kubevirtci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export KUBEVIRT_PROVIDER=${KUBEVIRT_PROVIDER:-'k8s-1.18'}
export KUBEVIRTCI_VERSION=${KUBEVIRTCI_VERSION:-'463da29b9cbdcaa22daed3a8ef5c5e864f582b0b'}

KUBEVIRTCI_PATH="${PWD}/_kubevirtci"

function kubevirtci::install() {
if [ ! -d ${KUBEVIRTCI_PATH} ]; then
git clone https://github.com/kubevirt/kubevirtci.git ${KUBEVIRTCI_PATH}
(
cd ${KUBEVIRTCI_PATH}
git checkout ${KUBEVIRTCI_VERSION}
)
fi
}

function kubevirtci::path() {
echo -n ${KUBEVIRTCI_PATH}
}

function kubevirtci::kubeconfig() {
echo -n ${KUBEVIRTCI_PATH}/_ci-configs/${KUBEVIRT_PROVIDER}/.kubeconfig
}
37 changes: 37 additions & 0 deletions examples/sample-operator/cluster/operator-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
#
# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

export VERSION=v0.0.0
export SAMPLE_CONFIG_STATUS=${SAMPLE_CONFIG_STATUS:-''}

./cluster/kubectl.sh create -f _out/sample-operator/${VERSION}/operator.yaml
./cluster/kubectl.sh wait deploy/sample-operator -n kubevirt --for=condition=Available --timeout=600s

# Create and wait for the controller
./cluster/kubectl.sh create -f _out/sample-operator/${VERSION}/sampleconfig_cr.yaml

# When `kubectl wait` will support `--ignore-not` found parameter this `if` can be removed.
if [[ "$(./cluster/kubectl.sh get deploy/http-server -n kubevirt 2>&1)" =~ "not found" ]]; then
sleep 10
fi
./cluster/kubectl.sh wait deploy/http-server -n kubevirt --for=condition=Available --timeout=600s

if [ ! -z "$SAMPLE_CONFIG_STATUS" ]
then
./cluster/kubectl.sh wait sampleconfig/example-sampleconfig -n kubevirt --timeout=600s --for=condition=$SAMPLE_CONFIG_STATUS
fi
Loading

0 comments on commit d37e7ee

Please sign in to comment.