Skip to content

Commit

Permalink
chore: remove unneeded conversion webhooks + introduce unit tests for…
Browse files Browse the repository at this point in the history
… conversion functions (open-feature#385)

Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
Signed-off-by: odubajDT <93584209+odubajDT@users.noreply.github.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Co-authored-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
3 people authored Mar 16, 2023
1 parent c5a6a32 commit dd34801
Show file tree
Hide file tree
Showing 23 changed files with 1,010 additions and 218 deletions.
43 changes: 42 additions & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,70 @@ jobs:
exit 1
test:
name: Component Tests
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}

- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}

- name: Run tests
run: make test
run: make component-test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: component-tests

unit-test:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}

- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}

- name: Checkout repository
uses: actions/checkout@v3

- name: Unit Test
run: make unit-test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: unit-tests

docker-local:
permissions:
Expand Down
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
.PHONY: component-test
component-test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers/... -coverprofile cover-controllers.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./webhooks/... -coverprofile cover-webhooks.out
sed -i '/mode: set/d' "cover-controllers.out"
sed -i '/mode: set/d' "cover-webhooks.out"
echo "mode: set" > cover.out
cat cover-controllers.out cover-webhooks.out >> cover.out
rm cover-controllers.out cover-webhooks.out

.PHONY: unit-test
unit-test: manifests fmt vet generate envtest ## Run tests.
go test ./... -v -short -coverprofile cover.out

## Requires the operator to be deployed
.PHONY: e2e-test
Expand Down
8 changes: 0 additions & 8 deletions apis/core/v1alpha1/featureflagconfiguration_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,5 @@ limitations under the License.

package v1alpha1

import ctrl "sigs.k8s.io/controller-runtime"

// Hub marks this type as a conversion hub.
func (ffc *FeatureFlagConfiguration) Hub() {}

func (r *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
9 changes: 9 additions & 0 deletions apis/core/v1alpha1/featureflagconfiguration_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package v1alpha1

import ctrl "sigs.k8s.io/controller-runtime"

func (r *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
10 changes: 0 additions & 10 deletions apis/core/v1alpha1/flagsourceconfiguration_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,5 @@ limitations under the License.

package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
)

// Hub marks this type as a conversion hub.
func (ffc *FlagSourceConfiguration) Hub() {}

func (r *FlagSourceConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
11 changes: 11 additions & 0 deletions apis/core/v1alpha1/flagsourceconfiguration_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
)

func (r *FlagSourceConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
6 changes: 6 additions & 0 deletions apis/core/v1alpha2/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package common

import "errors"

var ErrCannotCastFlagSourceConfiguration = errors.New("cannot cast FlagSourceConfiguration to v1alpha2")
var ErrCannotCastFeatureFlagConfiguration = errors.New("cannot cast FeatureFlagConfiguration to v1alpha2")
26 changes: 17 additions & 9 deletions apis/core/v1alpha2/featureflagconfiguration_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ import (
"fmt"

"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha2/common"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

func (ffc *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(ffc).
Complete()
}

func (src *FeatureFlagConfiguration) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1alpha1.FeatureFlagConfiguration)
dst, ok := dstRaw.(*v1alpha1.FeatureFlagConfiguration)

if !ok {
return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastFeatureFlagConfiguration)
}

// Copy equal stuff to new object
// DO NOT COPY TypeMeta
dst.ObjectMeta = src.ObjectMeta

if src.Spec.ServiceProvider != nil {
dst.Spec.ServiceProvider = &v1alpha1.FeatureFlagServiceProvider{
Name: src.Spec.ServiceProvider.Name,
Expand Down Expand Up @@ -67,9 +68,16 @@ func (src *FeatureFlagConfiguration) ConvertTo(dstRaw conversion.Hub) error {
}

func (dst *FeatureFlagConfiguration) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1alpha1.FeatureFlagConfiguration)
src, ok := srcRaw.(*v1alpha1.FeatureFlagConfiguration)

if !ok {
return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastFeatureFlagConfiguration)
}

// Copy equal stuff to new object
// DO NOT COPY TypeMeta
dst.ObjectMeta = src.ObjectMeta

if src.Spec.ServiceProvider != nil {
dst.Spec.ServiceProvider = &FeatureFlagServiceProvider{
Name: src.Spec.ServiceProvider.Name,
Expand Down
Loading

0 comments on commit dd34801

Please sign in to comment.