Skip to content

Commit

Permalink
enhance unit testing and overall project structures (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Ng <[email protected]>
  • Loading branch information
mikeshng authored Oct 19, 2021
1 parent 6ef4eb4 commit f906eb7
Show file tree
Hide file tree
Showing 73 changed files with 1,656 additions and 251 deletions.
2 changes: 1 addition & 1 deletion COMPONENT_NAME
Original file line number Diff line number Diff line change
@@ -1 +1 @@
multicloud-operators-channel
multicluster-operators-channel
2 changes: 1 addition & 1 deletion COMPONENT_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
latest
2.4.0
37 changes: 15 additions & 22 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ contribution. See the [DCO](DCO) file for details.
## Contributing A Patch

1. Submit an issue describing your proposed change to the repo in question.
2. The [repo owners](OWNERS) will respond to your issue promptly.
3. Fork the desired repo, develop and test your code changes.
4. Submit a pull request.
1. The [repo owners](OWNERS) will respond to your issue promptly.
1. Fork the desired repo, develop and test your code changes.
1. Submit a pull request.

## Issue and Pull Request Management

Expand All @@ -45,22 +45,15 @@ Repo maintainers can assign you an issue or pull request by leaving a

After your PR is ready to commit, please run following commands to check your code.

- verify your code
```shell
make lint
```

- build your code
```shell
make build
```

- build a local image
```shell
make build-images
```

- run the test
```shell
make test
```
```shell
make build
make test
```

## Build images

Make sure your code build passed.

```shell
make
```
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019 The Kubernetes Authors.
# Copyright 2021 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,10 +27,9 @@ FINDFILES=find . \( -path ./.git -o -path ./.github \) -prune -o -type f
XARGS = xargs -0 ${XARGS_FLAGS}
CLEANXARGS = xargs ${XARGS_FLAGS}

IMG ?= $(shell cat COMPONENT_NAME 2> /dev/null)
REGISTRY = quay.io/open-cluster-management
VERSION ?= $(shell cat COMPONENT_VERSION 2> /dev/null)
IMAGE_NAME_AND_VERSION ?= $(REGISTRY)/$(IMG):$(VERSION)
VERSION = latest
IMAGE_NAME_AND_VERSION ?= $(REGISTRY)/multicloud-operators-channel:$(VERSION)
export GOPACKAGES = $(shell go list ./... | grep -v /manager | grep -v /bindata | grep -v /vendor | grep -v /internal | grep -v /build | grep -v /test | grep -v /e2e )

TEST_TMP :=/tmp
Expand All @@ -44,13 +43,12 @@ KB_TOOLS_ARCHIVE_PATH := $(TEST_TMP)/$(KB_TOOLS_ARCHIVE_NAME)
.PHONY: build

build:
@common/scripts/gobuild.sh build/_output/bin/$(IMG) ./cmd/manager
@common/scripts/gobuild.sh build/_output/bin/multicluster-operators-channel ./cmd/manager

.PHONY: build-images

build-images: build
@docker build -t ${IMAGE_NAME_AND_VERSION} -f build/Dockerfile .
@docker tag ${IMAGE_NAME_AND_VERSION} $(REGISTRY)/$(IMG):latest

.PHONY: lint

Expand Down
78 changes: 78 additions & 0 deletions Makefile.prow
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2021 The Kubernetes Authors.
#
# 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.

-include /opt/build-harness/Makefile.prow

LOCAL_OS := $(shell uname)
ifeq ($(LOCAL_OS),Linux)
TARGET_OS ?= linux
XARGS_FLAGS="-r"
else ifeq ($(LOCAL_OS),Darwin)
TARGET_OS ?= darwin
XARGS_FLAGS=
else
$(error "This system's OS $(LOCAL_OS) isn't recognized/supported")
endif

FINDFILES=find . \( -path ./.git -o -path ./.github \) -prune -o -type f
XARGS = xargs -0 ${XARGS_FLAGS}
CLEANXARGS = xargs ${XARGS_FLAGS}

IMG ?= $(shell cat COMPONENT_NAME 2> /dev/null)
REGISTRY = quay.io/open-cluster-management
VERSION ?= $(shell cat COMPONENT_VERSION 2> /dev/null)
IMAGE_NAME_AND_VERSION ?= $(REGISTRY)/$(IMG):$(VERSION)
export GOPACKAGES = $(shell go list ./... | grep -v /manager | grep -v /bindata | grep -v /vendor | grep -v /internal | grep -v /build | grep -v /test | grep -v /e2e )

.PHONY: build

build:
@common/scripts/gobuild.sh build/_output/bin/$(IMG) ./cmd/manager

.PHONY: build-images

build-images: build
@docker build -t ${IMAGE_NAME_AND_VERSION} -f build/Dockerfile .
@docker tag ${IMAGE_NAME_AND_VERSION} $(REGISTRY)/$(IMG):latest

.PHONY: lint

lint:
@build/run-code-lint.sh

.PHONY: lint-all

lint-all: lint-yaml lint-go

.PHONY: lint-yaml

lint-yaml:
@${FINDFILES} \( -name '*.yml' -o -name '*.yaml' \) -print0 | ${XARGS} grep -L -e "{{" | ${CLEANXARGS} yamllint -c ./common/config/.yamllint.yml

.PHONY: lint-go

lint-go:
@${FINDFILES} -name '*.go' \( ! \( -name '*.gen.go' -o -name '*.pb.go' \) \) -print0 | ${XARGS} common/scripts/lint_go.sh

.PHONY: test

test:
@build/setup-tests.sh
@build/run-unit-tests.sh

.PHONY: e2e

export CONTAINER_NAME=e2e
e2e: build build-images
build/run-e2e-tests.sh
1 change: 1 addition & 0 deletions RELEASE_MAIN_BRANCH
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main
6 changes: 3 additions & 3 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
RUN microdnf update && \
microdnf clean all

ENV OPERATOR=/usr/local/bin/multicloud-operators-channel \
ENV OPERATOR=/usr/local/bin/multicluster-operators-channel \
USER_UID=1001 \
USER_NAME=multicloud-operators-channel
USER_NAME=multicluster-operators-channel

# install operator binary
COPY build/_output/bin/multicloud-operators-channel ${OPERATOR}
COPY build/_output/bin/multicluster-operators-channel ${OPERATOR}

COPY build/bin /usr/local/bin
RUN /usr/local/bin/user_setup
Expand Down
24 changes: 24 additions & 0 deletions build/Dockerfile.prow
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM registry.ci.openshift.org/open-cluster-management/builder:go1.16-linux AS builder

WORKDIR /go/src/github.com/open-cluster-management/multicluster-operators-channel
COPY . .
RUN make -f Makefile.prow build

FROM registry.access.redhat.com/ubi8/ubi-minimal:latest

RUN microdnf update && \
microdnf clean all

ENV OPERATOR=/usr/local/bin/multicluster-operators-channel \
USER_UID=1001 \
USER_NAME=multicluster-operators-channel

# install operator binary
COPY --from=builder /go/src/github.com/open-cluster-management/multicluster-operators-channel/build/_output/bin/multicluster-operators-channel ${OPERATOR}

COPY build/bin /usr/local/bin
RUN /usr/local/bin/user_setup

ENTRYPOINT ["/usr/local/bin/entrypoint"]

USER ${USER_UID}
26 changes: 26 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

#
# Copyright 2021 The Kubernetes Authors.
#
# 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.


echo "BUILD GOES HERE!"

echo "<repo>/<component>:<tag> : $1"

# Run our build target and set IMAGE_NAME_AND_VERSION
export IMAGE_NAME_AND_VERSION=${1}
make build
make build-images
19 changes: 19 additions & 0 deletions build/deploy-to-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
#
# Copyright 2021 The Kubernetes Authors.
#
# 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.

echo "DEPLOY TO CLUSTER GOES HERE! IT SHOULD DO NOTHING RIGHT NOW!"

exit 0;
29 changes: 29 additions & 0 deletions build/install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
#
# Copyright 2021 The Kubernetes Authors.
#
# 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.

echo "INSTALL DEPENDENCIES GOES HERE!"

_OPERATOR_SDK_VERSION=v0.12.0

if ! [ -x "$(command -v operator-sdk)" ]; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then
curl -L https://github.com/operator-framework/operator-sdk/releases/download/${_OPERATOR_SDK_VERSION}/operator-sdk-${_OPERATOR_SDK_VERSION}-x86_64-linux-gnu -o operator-sdk
elif [[ "$OSTYPE" == "darwin"* ]]; then
curl -L https://github.com/operator-framework/operator-sdk/releases/download/${_OPERATOR_SDK_VERSION}/operator-sdk-${_OPERATOR_SDK_VERSION}-x86_64-apple-darwin -o operator-sdk
fi
chmod +x operator-sdk
sudo mv operator-sdk /usr/local/bin/operator-sdk
fi
43 changes: 43 additions & 0 deletions build/run-code-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

#
# Copyright 2021 The Kubernetes Authors.
#
# 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 -o errexi
set -o nounset
set -o pipefail
set -o xtrace

# Prepare lint tools
# Install hadolint
HADOLINT_PATH="${HOME}"/hadolint
mkdir -p "${HADOLINT_PATH}"
wget -P "${HADOLINT_PATH}" https://github.com/hadolint/hadolint/releases/download/v1.17.5/hadolint-Linux-x86_64
mv "${HADOLINT_PATH}"/hadolint-Linux-x86_64 "${HADOLINT_PATH}"/hadolint
chmod +x "${HADOLINT_PATH}"/hadolint
export PATH="${HADOLINT_PATH}":"${PATH}"

# Install yamllint
pip install --user yamllint

# Install markdown lint
gem install mdl
gem install awesome_bot

# Install golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)"/bin v1.24.0

# Start lint task
make -f Makefile.prow lint-all
Loading

0 comments on commit f906eb7

Please sign in to comment.