Skip to content

Commit dd34801

Browse files
odubajDTbeeme1mrskyerus
authored
chore: remove unneeded conversion webhooks + introduce unit tests for conversion functions (#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>
1 parent c5a6a32 commit dd34801

23 files changed

+1010
-218
lines changed

.github/workflows/pr-checks.yml

+42-1
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,70 @@ jobs:
4848
exit 1
4949
5050
test:
51+
name: Component Tests
5152
runs-on: ubuntu-latest
5253
steps:
5354
- name: Install Go
5455
uses: actions/setup-go@v3
5556
with:
5657
go-version: ${{ env.DEFAULT_GO_VERSION }}
58+
5759
- name: Checkout repository
5860
uses: actions/checkout@v3
61+
5962
- name: Setup Environment
6063
run: |
6164
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
6265
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
66+
6367
- name: Module cache
6468
uses: actions/cache@v3
6569
env:
6670
cache-name: go-mod-cache
6771
with:
6872
path: ~/go/pkg/mod
6973
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
74+
7075
- name: Run tests
71-
run: make test
76+
run: make component-test
77+
7278
- name: Upload coverage to Codecov
7379
uses: codecov/codecov-action@v3
80+
with:
81+
flags: component-tests
82+
83+
unit-test:
84+
name: Unit Tests
85+
runs-on: ubuntu-latest
86+
steps:
87+
- name: Install Go
88+
uses: actions/setup-go@v3
89+
with:
90+
go-version: ${{ env.DEFAULT_GO_VERSION }}
91+
92+
- name: Setup Environment
93+
run: |
94+
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
95+
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
96+
97+
- name: Module cache
98+
uses: actions/cache@v3
99+
env:
100+
cache-name: go-mod-cache
101+
with:
102+
path: ~/go/pkg/mod
103+
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
104+
105+
- name: Checkout repository
106+
uses: actions/checkout@v3
107+
108+
- name: Unit Test
109+
run: make unit-test
110+
111+
- name: Upload coverage to Codecov
112+
uses: codecov/codecov-action@v3
113+
with:
114+
flags: unit-tests
74115

75116
docker-local:
76117
permissions:

Makefile

+13-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@ fmt: ## Run go fmt against code.
6565
vet: ## Run go vet against code.
6666
go vet ./...
6767

68-
.PHONY: test
69-
test: manifests generate fmt vet envtest ## Run tests.
70-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
68+
.PHONY: component-test
69+
component-test: manifests generate fmt vet envtest ## Run tests.
70+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers/... -coverprofile cover-controllers.out
71+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./webhooks/... -coverprofile cover-webhooks.out
72+
sed -i '/mode: set/d' "cover-controllers.out"
73+
sed -i '/mode: set/d' "cover-webhooks.out"
74+
echo "mode: set" > cover.out
75+
cat cover-controllers.out cover-webhooks.out >> cover.out
76+
rm cover-controllers.out cover-webhooks.out
77+
78+
.PHONY: unit-test
79+
unit-test: manifests fmt vet generate envtest ## Run tests.
80+
go test ./... -v -short -coverprofile cover.out
7181

7282
## Requires the operator to be deployed
7383
.PHONY: e2e-test

apis/core/v1alpha1/featureflagconfiguration_conversion.go

-8
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,5 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19-
import ctrl "sigs.k8s.io/controller-runtime"
20-
2119
// Hub marks this type as a conversion hub.
2220
func (ffc *FeatureFlagConfiguration) Hub() {}
23-
24-
func (r *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
25-
return ctrl.NewWebhookManagedBy(mgr).
26-
For(r).
27-
Complete()
28-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package v1alpha1
2+
3+
import ctrl "sigs.k8s.io/controller-runtime"
4+
5+
func (r *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
6+
return ctrl.NewWebhookManagedBy(mgr).
7+
For(r).
8+
Complete()
9+
}

apis/core/v1alpha1/flagsourceconfiguration_conversion.go

-10
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,5 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19-
import (
20-
ctrl "sigs.k8s.io/controller-runtime"
21-
)
22-
2319
// Hub marks this type as a conversion hub.
2420
func (ffc *FlagSourceConfiguration) Hub() {}
25-
26-
func (r *FlagSourceConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
27-
return ctrl.NewWebhookManagedBy(mgr).
28-
For(r).
29-
Complete()
30-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package v1alpha1
2+
3+
import (
4+
ctrl "sigs.k8s.io/controller-runtime"
5+
)
6+
7+
func (r *FlagSourceConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
8+
return ctrl.NewWebhookManagedBy(mgr).
9+
For(r).
10+
Complete()
11+
}

apis/core/v1alpha2/common/common.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package common
2+
3+
import "errors"
4+
5+
var ErrCannotCastFlagSourceConfiguration = errors.New("cannot cast FlagSourceConfiguration to v1alpha2")
6+
var ErrCannotCastFeatureFlagConfiguration = errors.New("cannot cast FeatureFlagConfiguration to v1alpha2")

apis/core/v1alpha2/featureflagconfiguration_conversion.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@ import (
2121
"fmt"
2222

2323
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
24-
ctrl "sigs.k8s.io/controller-runtime"
24+
"github.com/open-feature/open-feature-operator/apis/core/v1alpha2/common"
2525
"sigs.k8s.io/controller-runtime/pkg/conversion"
2626
)
2727

28-
func (ffc *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
29-
return ctrl.NewWebhookManagedBy(mgr).
30-
For(ffc).
31-
Complete()
32-
}
33-
3428
func (src *FeatureFlagConfiguration) ConvertTo(dstRaw conversion.Hub) error {
35-
dst := dstRaw.(*v1alpha1.FeatureFlagConfiguration)
29+
dst, ok := dstRaw.(*v1alpha1.FeatureFlagConfiguration)
30+
31+
if !ok {
32+
return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastFeatureFlagConfiguration)
33+
}
3634

35+
// Copy equal stuff to new object
36+
// DO NOT COPY TypeMeta
3737
dst.ObjectMeta = src.ObjectMeta
38+
3839
if src.Spec.ServiceProvider != nil {
3940
dst.Spec.ServiceProvider = &v1alpha1.FeatureFlagServiceProvider{
4041
Name: src.Spec.ServiceProvider.Name,
@@ -67,9 +68,16 @@ func (src *FeatureFlagConfiguration) ConvertTo(dstRaw conversion.Hub) error {
6768
}
6869

6970
func (dst *FeatureFlagConfiguration) ConvertFrom(srcRaw conversion.Hub) error {
70-
src := srcRaw.(*v1alpha1.FeatureFlagConfiguration)
71+
src, ok := srcRaw.(*v1alpha1.FeatureFlagConfiguration)
72+
73+
if !ok {
74+
return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastFeatureFlagConfiguration)
75+
}
7176

77+
// Copy equal stuff to new object
78+
// DO NOT COPY TypeMeta
7279
dst.ObjectMeta = src.ObjectMeta
80+
7381
if src.Spec.ServiceProvider != nil {
7482
dst.Spec.ServiceProvider = &FeatureFlagServiceProvider{
7583
Name: src.Spec.ServiceProvider.Name,

0 commit comments

Comments
 (0)