Skip to content

Commit 33819f5

Browse files
committed
Add support for experimental features at build time
Signed-off-by: jose.vazquez <[email protected]>
1 parent f7206b0 commit 33819f5

File tree

8 files changed

+110
-14
lines changed

8 files changed

+110
-14
lines changed

.licenses-gomod.sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
100644 cc82b2933dc41b1ba0560cbc6b1709c13678dcf7 go.mod
1+
100644 b68045f891e8dcb43981fbcd757bf32e36a699fd go.mod

Makefile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ DOCKER_SBOM_PLUGIN_VERSION=0.6.1
1313
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
1414
VERSION ?= $(shell git describe --always --tags --dirty --broken | cut -c 2-)
1515

16+
# LD_FLAGS
17+
LD_FLAG_SET_VERSION = -X github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/version.Version=$(VERSION)
18+
LD_FLAGS_SET_EXPERIMENTAL = -X github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/version.Experimental=$(EXPERIMENTAL)
19+
ifdef EXPERIMENTAL
20+
LD_FLAGS = $(LD_FLAGS_SET_EXPERIMENTAL) $(LD_FLAG_SET_VERSION)
21+
else
22+
LD_FLAGS = $(LD_FLAG_SET_VERSION)
23+
endif
24+
1625
# NEXT_VERSION represents a version that is higher than anything released
1726
# VERSION default value does not play well with the run target which might end up failing
1827
# with errors such as:
@@ -219,7 +228,7 @@ bin/$(TARGET_OS)/$(TARGET_ARCH):
219228

220229
bin/$(TARGET_OS)/$(TARGET_ARCH)/manager: $(GO_SOURCES) bin/$(TARGET_OS)/$(TARGET_ARCH)
221230
@echo "Building operator with version $(VERSION); $(TARGET_OS) - $(TARGET_ARCH)"
222-
CGO_ENABLED=0 GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) go build -o $@ -ldflags="-X github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/version.Version=$(VERSION)" cmd/main.go
231+
CGO_ENABLED=0 GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH) go build -o $@ -ldflags="$(LD_FLAGS)" cmd/main.go
223232
@touch $@
224233

225234
bin/manager: bin/$(TARGET_OS)/$(TARGET_ARCH)/manager
@@ -246,7 +255,9 @@ manifests: CRD_OPTIONS ?= "crd:crdVersions=v1,ignoreUnexportedFields=true"
246255
manifests: fmt ## Generate manifests e.g. CRD, RBAC etc.
247256
controller-gen $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./api/..." paths="./internal/controller/..." output:crd:artifacts:config=config/crd/bases
248257
@./scripts/split_roles_yaml.sh
249-
258+
ifdef EXPERIMENTAL
259+
controller-gen crd paths="./internal/nextapi/v1" output:crd:artifacts:config=internal/next-crds
260+
endif
250261

251262
.PHONY: lint
252263
lint: ## Run the lint against the code
@@ -275,6 +286,9 @@ vet: $(TIMESTAMPS_DIR)/vet ## Run go vet against code
275286
.PHONY: generate
276287
generate: ${GO_SOURCES} ## Generate code
277288
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./api/..." paths="./internal/controller/..."
289+
ifdef EXPERIMENTAL
290+
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./internal/nextapi/v1/..."
291+
endif
278292
$(MAKE) fmt
279293

280294
.PHONY: check-missing-files
@@ -530,6 +544,9 @@ clear-e2e-leftovers: ## Clear the e2e test leftovers quickly
530544
.PHONY: install-crds
531545
install-crds: ## Install CRDs in Kubernetes
532546
kubectl apply -k config/crd
547+
ifdef EXPERIMENTAL
548+
kubectl apply -f internal/next-crds/*.yaml
549+
endif
533550

534551
.PHONY: set-namespace
535552
set-namespace:
@@ -553,7 +570,7 @@ prepare-run: generate vet manifests run-kind install-crds install-credentials
553570
.PHONY: run
554571
run: prepare-run ## Run a freshly compiled manager against kind
555572
ifdef RUN_YAML
556-
kubectl apply -f $(RUN_YAML)
573+
kubectl apply -n $(OPERATOR_NAMESPACE) -f $(RUN_YAML)
557574
endif
558575
VERSION=$(NEXT_VERSION) \
559576
OPERATOR_POD_NAME=$(OPERATOR_POD_NAME) \

cmd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func main() {
7373
ctrl.SetLogger(logrLogger.WithName("ctrl"))
7474
klog.SetLogger(logrLogger.WithName("klog"))
7575
setupLog := logger.Named("setup").Sugar()
76+
if version.IsExperimental() {
77+
setupLog.Warn("Experimental features enabled!")
78+
}
7679
setupLog.Info("starting with configuration", zap.Any("config", config), zap.Any("version", version.Version))
7780

7881
runnable, err := operator.NewBuilder(operator.ManagerProviderFunc(ctrl.NewManager), akoScheme, time.Duration(minimumIndependentSyncPeriod)*time.Minute).
@@ -87,6 +90,7 @@ func main() {
8790
WithDeletionProtection(config.ObjectDeletionProtection).
8891
WithIndependentSyncPeriod(time.Duration(config.IndependentSyncPeriod) * time.Minute).
8992
WithDryRun(config.DryRun).
93+
WithExperimentalReconcilers(version.IsExperimental()).
9094
Build(ctx)
9195
if err != nil {
9296
setupLog.Error(err, "unable to start operator")

internal/controller/registry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/watch"
4444
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/dryrun"
4545
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/featureflags"
46+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/version"
4647
)
4748

4849
type Reconciler interface {
@@ -114,6 +115,9 @@ func (r *Registry) registerControllers(c cluster.Cluster, ap atlas.Provider) {
114115
reconcilers = append(reconcilers, atlasipaccesslist.NewAtlasIPAccessListReconciler(c, r.defaultPredicates(), ap, r.deletionProtection, r.independentSyncPeriod, r.logger, r.globalSecretRef))
115116
reconcilers = append(reconcilers, atlasnetworkcontainer.NewAtlasNetworkContainerReconciler(c, r.defaultPredicates(), ap, r.deletionProtection, r.logger, r.independentSyncPeriod, r.globalSecretRef))
116117
reconcilers = append(reconcilers, atlasnetworkpeering.NewAtlasNetworkPeeringsReconciler(c, r.defaultPredicates(), ap, r.deletionProtection, r.logger, r.independentSyncPeriod, r.globalSecretRef))
118+
if version.IsExperimental() {
119+
// Experimental reconciler initializations go here
120+
}
117121
r.reconcilers = reconcilers
118122
}
119123

internal/operator/builder.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ type Builder struct {
8181
leaderElection bool
8282
leaderElectionID string
8383

84-
atlasDomain string
85-
predicates []predicate.Predicate
86-
apiSecret client.ObjectKey
87-
atlasProvider atlas.Provider
88-
featureFlags *featureflags.FeatureFlags
89-
deletionProtection bool
90-
skipNameValidation bool
91-
dryRun bool
84+
atlasDomain string
85+
predicates []predicate.Predicate
86+
apiSecret client.ObjectKey
87+
atlasProvider atlas.Provider
88+
featureFlags *featureflags.FeatureFlags
89+
deletionProtection bool
90+
skipNameValidation bool
91+
dryRun bool
92+
experimentalReconcilers bool
9293
}
9394

9495
func (b *Builder) WithConfig(config *rest.Config) *Builder {
@@ -175,6 +176,11 @@ func (b *Builder) WithDryRun(dryRun bool) *Builder {
175176
return b
176177
}
177178

179+
func (b *Builder) WithExperimentalReconcilers(experimentalReconcilers bool) *Builder {
180+
b.experimentalReconcilers = experimentalReconcilers
181+
return b
182+
}
183+
178184
// Build builds the cluster object and configures operator controllers
179185
func (b *Builder) Build(ctx context.Context) (cluster.Cluster, error) {
180186
mergeDefaults(b)
@@ -202,7 +208,14 @@ func (b *Builder) Build(ctx context.Context) (cluster.Cluster, error) {
202208
}
203209
}
204210

205-
controllerRegistry := controller.NewRegistry(b.predicates, b.deletionProtection, b.logger, b.independentSyncPeriod, b.featureFlags, b.apiSecret)
211+
controllerRegistry := controller.NewRegistry(
212+
b.predicates,
213+
b.deletionProtection,
214+
b.logger,
215+
b.independentSyncPeriod,
216+
b.featureFlags,
217+
b.apiSecret,
218+
)
206219

207220
var akoCluster cluster.Cluster
208221
if b.dryRun {

internal/version/experimental_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestIsExperimental(t *testing.T) {
24+
testCases := []struct {
25+
title string
26+
set string
27+
want bool
28+
}{
29+
{
30+
title: "default is not experimental",
31+
},
32+
{
33+
title: "setting true makes it experimental",
34+
set: "true",
35+
want: true,
36+
},
37+
{
38+
title: "setting other value is not experimental",
39+
set: "1",
40+
want: false,
41+
},
42+
}
43+
for _, tc := range testCases {
44+
t.Run(tc.title, func(t *testing.T) {
45+
if tc.set != "" {
46+
Experimental = tc.set
47+
}
48+
assert.Equal(t, tc.want, IsExperimental())
49+
})
50+
}
51+
}

internal/version/version.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ const DefaultVersion = "unknown"
2424
// Version set by the linker during link time.
2525
var Version = DefaultVersion
2626

27+
// Experimental enables unreleased features
28+
var Experimental = "false"
29+
2730
func IsRelease(v string) bool {
2831
return v != DefaultVersion &&
2932
regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+[-certified]*$`).Match([]byte(strings.TrimSpace(v)))
3033
}
34+
35+
func IsExperimental() bool {
36+
return Experimental == "true"
37+
}

licenses.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ golang.org/x/exp,https://cs.opensource.google/go/x/exp/+/8a7402ab:LICENSE,BSD-3-
113113
golang.org/x/net,https://cs.opensource.google/go/x/net/+/v0.40.0:LICENSE,BSD-3-Clause
114114
golang.org/x/oauth2,https://cs.opensource.google/go/x/oauth2/+/v0.30.0:LICENSE,BSD-3-Clause
115115
golang.org/x/sync,https://cs.opensource.google/go/x/sync/+/v0.14.0:LICENSE,BSD-3-Clause
116-
golang.org/x/sys,https://cs.opensource.google/go/x/sys/+/v0.33.0:LICENSE,BSD-3-Clause
116+
golang.org/x/sys/unix,https://cs.opensource.google/go/x/sys/+/v0.33.0:LICENSE,BSD-3-Clause
117117
golang.org/x/term,https://cs.opensource.google/go/x/term/+/v0.32.0:LICENSE,BSD-3-Clause
118118
golang.org/x/text,https://cs.opensource.google/go/x/text/+/v0.25.0:LICENSE,BSD-3-Clause
119119
golang.org/x/time/rate,https://cs.opensource.google/go/x/time/+/v0.11.0:LICENSE,BSD-3-Clause

0 commit comments

Comments
 (0)