Skip to content

Commit 326005f

Browse files
committed
feat(ctrl): implement MeshFederation reconciliation
This commit introduces the initial implementation of the MeshFederation controller. The controller is responsible for: - Managing the MeshFederation server lifecycle (openshift-service-mesh#152) - Configuring MeshFederation resources, including: - IngressGateway - PeerAuthentication - EnvoyFilter (for OpenShift Router) - Routes (for OpenShift Router) - Watching Kubernetes services to: - Push SotW updates to all connected peers (openshift-service-mesh#153) - Update MeshFederation cluster configuration - Support both label selectors and expressions (openshift-service-mesh#52 openshift-service-mesh#143) Basic EnvTest tests are included to verify the setup. Fixes openshift-service-mesh#152 openshift-service-mesh#52 openshift-service-mesh#143 openshift-service-mesh#153
1 parent 45f2077 commit 326005f

34 files changed

+17662
-58
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ bin/
55
.idea/
66
.vscode/
77

8+
# Test artifacts
9+
cover.out
10+
ginkgo-test-results.xml
11+
812
# demo output
913
Makefile.selfsigned.mk
1014
common.mk

Makefile

+23-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export ISTIO_VERSION ?= 1.23.0
1010
LOCALBIN := $(PROJECT_DIR)/bin
1111

1212
include Makefile.tooling.mk
13+
include Makefile.func.mk
1314

1415
PROTOBUF_API_DIR := $(PROJECT_DIR)/api/proto/federation
1516
PROTOBUF_API_SRC := $(shell find $(PROTOBUF_API_DIR) -type f -name "*.proto")
@@ -40,15 +41,34 @@ build: deps $(PROTOBUF_GEN) $(CRD_GEN) ## Builds the project
4041
##@ Development
4142

4243
.PHONY: test
43-
test: build ## Runs tests
44-
go test $(PROJECT_DIR)/...
44+
test: test-unit test-ctrl ## Runs unit and controller tests
45+
46+
.PHONY: test-unit
47+
test-unit: build ## Runs unit tests
48+
go test $(PROJECT_DIR)/internal/pkg/...
49+
50+
ENVTEST_K8S_VERSION = 1.31 # refers to the version of kubebuilder assets to be downloaded by envtest binary.
51+
test-ctrl: fetch-test-crds
52+
test-ctrl: $(ENVTEST) $(GINKGO) ## Runs controller tests using k8s envtest
53+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" \
54+
$(GINKGO) $(PROJECT_DIR)/internal/controller/... -r \
55+
-vet=off \
56+
-coverprofile cover.out \
57+
--junit-report=ginkgo-test-results.xml ${args}
58+
59+
.PHONY: fetch-test-crds
60+
fetch-test-crds: $(CONTROLLER_GEN)
61+
$(eval crd_folder = "$(PROJECT_DIR)/test/testdata/crds/external")
62+
@$(call fetch-external-crds,github.com/openshift/api,route/v1,$(crd_folder))
63+
@curl -s https://raw.githubusercontent.com/istio/istio/$(ISTIO_VERSION)/manifests/charts/base/crds/crd-all.gen.yaml > $(crd_folder)/istio.yaml
64+
4565

4666
define local_tag
4767
$(TAG)$(shell [ "$(USE_LOCAL_IMAGE)" = "true" ] && echo "-local")
4868
endef
4969

50-
.PHONY: e2e
5170
TEST_SUITES ?= remote_ip remote_dns_name spire
71+
.PHONY: e2e
5272
e2e: kind-clusters ## Runs end-to-end tests against KinD clusters
5373
@local_tag=$(call local_tag); \
5474
$(foreach suite, $(TEST_SUITES), \

Makefile.func.mk

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
define go-mod-version
2+
$(shell go mod graph | grep $(1) | head -n 1 | cut -d'@' -f 2)
3+
endef
4+
5+
# Using controller-gen to fetch external CRDs and put them in defined folder folder
6+
# They can be used e.g. in testing using EnvTest where controller under test
7+
# requires additional resources to manage.
8+
#
9+
# $(1) - repository to fetch CRDs from, e.g. github.com/openshift/api
10+
# $(2) - location, e.g. route/v1
11+
# $(3) - target folder
12+
#
13+
# Example use in Makefile target: $(call fetch-external-crds,github.com/openshift/api,route/v1,test/testdata/crds)
14+
define fetch-external-crds
15+
GOFLAGS="-mod=readonly" $(CONTROLLER_GEN) crd \
16+
paths=$(shell go env GOPATH)/pkg/mod/$(1)@$(call go-mod-version,$(1))/$(2)/... \
17+
output:crd:artifacts:config=$(3)
18+
endef

Makefile.tooling.mk

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Versions
22
CONTROLLER_TOOLS_VERSION ?= v0.16.4
3+
ENVTEST_PACKAGE_VERSION = release-0.20
34

45
## Binaries
56
KIND := $(LOCALBIN)/kind
@@ -10,6 +11,8 @@ PROTOC_GEN_GRPC := $(LOCALBIN)/protoc-gen-go-grpc
1011
PROTOC_GEN_DEEPCOPY := $(LOCALBIN)/protoc-gen-golang-deepcopy
1112
CONTROLLER_GEN := $(LOCALBIN)/controller-gen
1213
GCI := $(LOCALBIN)/gci
14+
GINKGO := $(LOCALBIN)/ginkgo
15+
ENVTEST := $(LOCALBIN)/setup-envtest
1316

1417
$(shell mkdir -p $(LOCALBIN))
1518

@@ -46,3 +49,10 @@ $(KIND):
4649
$(CONTROLLER_GEN):
4750
GOBIN=$(LOCALBIN) go install -mod=readonly sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
4851

52+
$(GINKGO):
53+
@GOBIN=$(LOCALBIN) go install -mod=readonly github.com/onsi/ginkgo/v2/ginkgo
54+
55+
$(ENVTEST):
56+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(ENVTEST_PACKAGE_VERSION)
57+
58+

api/v1alpha1/meshfederation_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ type MeshFederationStatus struct {
7676
// Conditions describes the state of the MeshFederation resource.
7777
// +optional
7878
Conditions []metav1.Condition `json:"conditions,omitempty"`
79+
80+
// +optional
81+
ExportedServices []string `json:"exportedServices,omitempty"`
7982
}
8083

8184
type PortConfig struct {

api/v1alpha1/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chart/crds/federation.openshift-service-mesh.io_meshfederations.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ spec:
227227
- type
228228
type: object
229229
type: array
230+
exportedServices:
231+
items:
232+
type: string
233+
type: array
230234
type: object
231235
type: object
232236
served: true

cmd/federation-controller/main.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import (
3030
"istio.io/istio/pkg/slices"
3131
corev1 "k8s.io/api/core/v1"
3232
"k8s.io/apimachinery/pkg/runtime"
33-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3433
"k8s.io/client-go/informers"
35-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3634
v1 "k8s.io/client-go/listers/core/v1"
3735
// +kubebuilder:scaffold:imports
3836
"k8s.io/client-go/rest"
@@ -41,7 +39,7 @@ import (
4139
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4240
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
4341

44-
"github.com/openshift-service-mesh/federation/api/v1alpha1"
42+
"github.com/openshift-service-mesh/federation/internal/controller"
4543
"github.com/openshift-service-mesh/federation/internal/controller/federatedservice"
4644
"github.com/openshift-service-mesh/federation/internal/controller/meshfederation"
4745
"github.com/openshift-service-mesh/federation/internal/pkg/config"
@@ -78,10 +76,7 @@ var (
7876
)
7977

8078
func init() {
81-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
82-
utilruntime.Must(v1alpha1.AddToScheme(scheme))
83-
utilruntime.Must(v1alpha1.AddToScheme(scheme))
84-
// +kubebuilder:scaffold:scheme
79+
controller.MustAddToScheme(scheme)
8580
}
8681

8782
const reconnectDelay = time.Second * 5
@@ -158,7 +153,7 @@ func runCtrls(ctx context.Context, cancel context.CancelFunc) {
158153
}
159154

160155
if err = meshfederation.NewReconciler(mgr.GetClient()).SetupWithManager(mgr); err != nil {
161-
log.Errorf("unable to create controller for MeshFederation custom resource: %s", err)
156+
log.Errorf("unable to create MeshFederation controller: %s", err)
162157
os.Exit(1)
163158
}
164159
if err = federatedservice.NewReconciler(mgr.GetClient()).SetupWithManager(mgr); err != nil {
@@ -329,7 +324,7 @@ func startFDSClient(ctx context.Context, remote config.Remote, meshConfigPushReq
329324
DiscoveryAddr: discoveryAddr,
330325
Authority: remote.ServiceFQDN(),
331326
Handlers: map[string]adsc.ResponseHandler{
332-
xds.ExportedServiceTypeUrl: fds.NewImportedServiceHandler(importedServiceStore, meshConfigPushRequests),
327+
xds.FederatedServiceTypeUrl: fds.NewImportedServiceHandler(importedServiceStore, meshConfigPushRequests),
333328
},
334329
ReconnectDelay: reconnectDelay,
335330
})

0 commit comments

Comments
 (0)