Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(konnect): add SDK interface types and theirs mocks #507

Merged
merged 8 commits into from
Sep 5, 2024
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/zz_generated*.go linguist-generated=true
pkg/clientset/** linguist-generated=true
controller/konnect/ops/*_mock_test.go linguist-generated=true
7 changes: 7 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ linters-settings:

- pkg: github.com/kong/gateway-operator/internal/types
alias: gwtypes

- pkg: github.com/Kong/sdk-konnect-go/models/components
alias: sdkkonnectcomp
- pkg: github.com/Kong/sdk-konnect-go/models/operations
alias: sdkkonnectops
- pkg: github.com/Kong/sdk-konnect-go/models/sdkerrors
alias: sdkkonnecterrs
revive:
rules:
- name: errorf
Expand Down
15 changes: 15 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
quiet: False
inpackage: True
disable-version-string: True
with-expecter: True

filename: "{{ trimSuffix .InterfaceFile \".go\" | base | lower }}_mock_test.go"
dir: "{{ .InterfaceDir }}"
mockname: "Mock{{ .InterfaceName }}"
outpkg: "{{ .PackageName }}"

packages:
github.com/kong/gateway-operator/controller/konnect/ops:
interfaces:
ControlPlaneSDK:
ServicesSDK:
randmonkey marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions .tools_versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ dlv: "1.23.0"
gotestsum: "1.12.0"
# renovate: datasource=github-releases depName=elastic/crd-ref-docs
crd-ref-docs: "0.1.0"
# renovate: datasource=github-releases depName=vektra/mockery
mockery: "2.44.2"
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ skaffold: mise yq ## Download skaffold locally if necessary.
@$(MISE) plugin install --yes -q skaffold
@$(MISE) install -q skaffold@$(SKAFFOLD_VERSION)

MOCKERY_VERSION = $(shell $(YQ) -r '.mockery' < $(TOOLS_VERSIONS_FILE))
MOCKERY = $(PROJECT_DIR)/bin/installs/mockery/$(MOCKERY_VERSION)/bin/mockery
.PHONY: mockery
mockery: mise yq ## Download mockery locally if necessary.
@$(MISE) plugin install --yes -q mockery https://github.com/cabify/asdf-mockery.git
@$(MISE) install -q mockery@$(MOCKERY_VERSION)

# ------------------------------------------------------------------------------
# Build
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -191,7 +198,7 @@ verify.generators: verify.repo generate verify.diff
API_DIR ?= api

.PHONY: generate
generate: generate.api generate.clientsets generate.rbacs generate.gateway-api-urls generate.docs generate.k8sio-gomod-replace generate.testcases-registration generate.kic-webhook-config
generate: generate.api generate.clientsets generate.rbacs generate.gateway-api-urls generate.docs generate.k8sio-gomod-replace generate.testcases-registration generate.kic-webhook-config generate.mocks

.PHONY: generate.api
generate.api: controller-gen
Expand Down Expand Up @@ -388,6 +395,11 @@ test.conformance:
test.samples: kustomize
find ./config/samples -not -name "kustomization.*" -type f | sort | xargs -I{} bash -c "kubectl apply -f {}; kubectl delete -f {}"

# https://github.com/vektra/mockery/issues/803#issuecomment-2287198024
.PHONY: generate.mocks
generate.mocks: mockery
GODEBUG=gotypesalias=0 $(MOCKERY)

# ------------------------------------------------------------------------------
# Gateway API
# ------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package konnect
package conditions

// TODO(pmalek): move this to Konnect API directory so that it's part of the API contract.
// https://github.com/Kong/kubernetes-configuration/issues/14
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package konnect
package constraints

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -25,9 +25,9 @@ type SupportedKonnectEntityType interface {
}

// EntityType is an interface that all Konnect entity types must implement.
// Separating this from SupportedKonnectEntityType allows us to use EntityType
// Separating this from constraints.SupportedKonnectEntityType allows us to use EntityType
// where client.Object is required, since it embeds client.Object and uses pointer
// to refer to the SupportedKonnectEntityType.
// to refer to the constraints.SupportedKonnectEntityType.
type EntityType[T SupportedKonnectEntityType] interface {
*T
// Kubernetes Object methods
Expand Down
7 changes: 7 additions & 0 deletions controller/konnect/constraints/entitytypename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package constraints

// EntityTypeName returns the name of the entity type.
func EntityTypeName[T SupportedKonnectEntityType]() string {
var e T
return e.GetTypeName()
}
6 changes: 0 additions & 6 deletions controller/konnect/entitytypename.go

This file was deleted.

19 changes: 0 additions & 19 deletions controller/konnect/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import (
"k8s.io/apimachinery/pkg/types"
)

// FailedKonnectOpError is an error type that is returned when an operation against
// Konnect API fails.
type FailedKonnectOpError[T SupportedKonnectEntityType] struct {
Op Op
Err error
}

// Error implements the error interface.
func (e FailedKonnectOpError[T]) Error() string {
return fmt.Sprintf("failed to %s %s on Konnect: %v",
e.Op, entityTypeName[T](), e.Err,
)
}

// Unwrap returns the underlying error.
func (e FailedKonnectOpError[T]) Unwrap() error {
return e.Err
}

// ReferencedControlPlaneDoesNotExistError is an error type that is returned when
// a Konnect entity references a KonnectGatewayControlPlane that does not exist.
type ReferencedControlPlaneDoesNotExistError struct {
Expand Down
6 changes: 4 additions & 2 deletions controller/konnect/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package konnect
import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kong/gateway-operator/controller/konnect/constraints"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

Expand All @@ -14,8 +16,8 @@ type ReconciliationIndexOption struct {
}

// ReconciliationIndexOptionsForEntity returns required index options for controller reconciliing the entity.
func ReconciliationIndexOptionsForEntity[T SupportedKonnectEntityType,
TEnt EntityType[T]](ent TEnt) []ReconciliationIndexOption {
func ReconciliationIndexOptionsForEntity[T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T]](ent TEnt) []ReconciliationIndexOption {
switch any(ent).(type) { //nolint:gocritic // TODO: add index options required for other entities
case *configurationv1alpha1.KongPluginBinding:
return IndexOptionsForKongPluginBinding()
Expand Down
15 changes: 15 additions & 0 deletions controller/konnect/ops/controlplane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ops

import (
"context"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
)

// ControlPlaneSDK is the interface for the Konnect ControlPlaneSDK SDK.
type ControlPlaneSDK interface {
CreateControlPlane(ctx context.Context, req sdkkonnectcomp.CreateControlPlaneRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateControlPlaneResponse, error)
DeleteControlPlane(ctx context.Context, id string, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteControlPlaneResponse, error)
UpdateControlPlane(ctx context.Context, id string, req sdkkonnectcomp.UpdateControlPlaneRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpdateControlPlaneResponse, error)
}
Loading
Loading