Skip to content

Commit

Permalink
Add go.mod to import private capi with required changes
Browse files Browse the repository at this point in the history
This also includes the following changes to make this work:
- envtest has changed, so duplicate the makefile
- internal/envtest was not picking up the crds (different go mod), so duplicate it
  so we can add the path to the crds (and remove the operator from the top level envtest).
- fix some lint errors
  • Loading branch information
asalkeld committed Sep 16, 2021
1 parent 24fdb70 commit cff74b0
Show file tree
Hide file tree
Showing 17 changed files with 1,586 additions and 62 deletions.
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -123,6 +123,7 @@ ARTIFACTS ?= ${ROOT_DIR}/_artifacts

.PHONY: test
test: ## Run tests.
$(MAKE) -C exp/operator test
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; go test ./... $(TEST_ARGS)

.PHONY: test-verbose
Expand Down Expand Up @@ -222,13 +223,15 @@ lint: $(GOLANGCI_LINT) ## Lint codebase
$(MAKE) -j8 lint-all

.PHONY: lint-all lint-core lint-e2e lint-capd
lint-all: lint-core lint-e2e lint-capd
lint-all: lint-core lint-e2e lint-capd lint-operator
lint-core:
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
lint-e2e:
cd $(E2E_FRAMEWORK_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
lint-capd:
cd $(CAPD_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
lint-operator:
$(MAKE) -C exp/operator lint

.PHONY: lint-fix
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter.
Expand Down
24 changes: 18 additions & 6 deletions exp/operator/Makefile
Expand Up @@ -43,10 +43,17 @@ BIN_DIR := bin

export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)

#
# Kubebuilder.
#
export KUBEBUILDER_ENVTEST_KUBERNETES_VERSION ?= 1.21.2


# Binaries.
# Need to use abspath so we can invoke these from subdirectories
SETUP_ENVTEST := $(abspath hack/tools/bin/setup-envtest)
CONTROLLER_GEN := $(abspath $(TOOLS_BIN_DIR)/controller-gen)
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint
GOLANGCI_LINT := $(abspath $(TOOLS_BIN_DIR)/golangci-lint)
KUSTOMIZE := $(abspath $(TOOLS_BIN_DIR)/kustomize)

# Define Docker related variables. Releases should modify and double check these vars.
Expand Down Expand Up @@ -77,9 +84,11 @@ help: ## Display this help
## Testing
## --------------------------------------

KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION))

.PHONY: test
test: ## Run tests.
source $(ROOT)/scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; go test ./... $(TEST_ARGS)
test: $(SETUP_ENVTEST) ## Run tests.
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)

.PHONY: test-verbose
test-verbose: ## Run tests with verbose settings.
Expand All @@ -93,11 +102,14 @@ test-verbose: ## Run tests with verbose settings.
operator: ## Build operator binary
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/operator sigs.k8s.io/cluster-api/exp/operator

$(SETUP_ENVTEST): hack/tools/go.mod # Build setup-envtest from tools folder.
cd hack/tools; go build -tags=tools -o $(BIN_DIR)/setup-envtest sigs.k8s.io/controller-runtime/tools/setup-envtest

$(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen

$(GOLANGCI_LINT): $(TOOLS_DIR)/go.mod # Build golangci-lint from tools folder.
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint
$(GOLANGCI_LINT):
$(MAKE) -C $(ROOT) $(GOLANGCI_LINT)

$(KUSTOMIZE): # Build kustomize from tools folder.
$(ROOT)/hack/ensure-kustomize.sh
Expand Down Expand Up @@ -264,7 +276,7 @@ clean: ## Remove all generated files
.PHONY: clean-bin
clean-bin: ## Remove all generated binaries
rm -rf bin
rm -rf $(TOOLS_BIN_DIR)
rm -rf $(TOOLS_BIN_DIR) $(PWD)/hack/tools/bin

.PHONY: clean-release
clean-release: ## Remove the release folder
Expand Down
2 changes: 1 addition & 1 deletion exp/operator/api/v1alpha1/conditions_consts.go
Expand Up @@ -23,6 +23,6 @@ const (
PreflightCheckCondition clusterv1.ConditionType = "PreflightCheckPassed"

// MoreThanOneProviderInstanceExistsReason (Severity=Info) documents that more than one instance of provider
// exists in the cluster
// exists in the cluster.
MoreThanOneProviderInstanceExistsReason = "MoreThanOneExists"
)
Expand Up @@ -65,8 +65,9 @@ type BootstrapProviderListWrapper struct {

func (b *BootstrapProviderListWrapper) GetItems() []GenericProvider {
providers := []GenericProvider{}
for _, provider := range b.Items {
providers = append(providers, &BootstrapProviderWrapper{&provider})
for i := range b.Items {
p := b.Items[i]
providers = append(providers, &BootstrapProviderWrapper{&p})
}

return providers
Expand Down
Expand Up @@ -65,8 +65,9 @@ type ControlPlaneProviderListWrapper struct {

func (c *ControlPlaneProviderListWrapper) GetItems() []GenericProvider {
providers := []GenericProvider{}
for _, provider := range c.Items {
providers = append(providers, &ControlPlaneProviderWrapper{&provider})
for i := range c.Items {
p := c.Items[i]
providers = append(providers, &ControlPlaneProviderWrapper{&p})
}

return providers
Expand Down
Expand Up @@ -65,8 +65,9 @@ type CoreProviderListWrapper struct {

func (c *CoreProviderListWrapper) GetItems() []GenericProvider {
providers := []GenericProvider{}
for _, provider := range c.Items {
providers = append(providers, &CoreProviderWrapper{&provider})
for i := range c.Items {
p := c.Items[i]
providers = append(providers, &CoreProviderWrapper{&p})
}

return providers
Expand Down
Expand Up @@ -65,8 +65,9 @@ type InfrastructureProviderListWrapper struct {

func (i *InfrastructureProviderListWrapper) GetItems() []GenericProvider {
providers := []GenericProvider{}
for _, provider := range i.Items {
providers = append(providers, &InfrastructureProviderWrapper{&provider})
for x := range i.Items {
p := i.Items[x]
providers = append(providers, &InfrastructureProviderWrapper{&p})
}

return providers
Expand Down
30 changes: 10 additions & 20 deletions exp/operator/controllers/genericprovider_controller_test.go
Expand Up @@ -33,10 +33,10 @@ func TestReconcilerPreflightConditions(t *testing.T) {

namespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "provider-test"}}

g.Expect(testEnv.Create(ctx, namespace)).To(Succeed())
g.Expect(env.Create(ctx, namespace)).To(Succeed())

defer func() {
g.Expect(testEnv.Delete(ctx, namespace)).To(Succeed())
g.Expect(env.Delete(ctx, namespace)).To(Succeed())
}()

testCases := []struct {
Expand Down Expand Up @@ -71,31 +71,23 @@ func TestReconcilerPreflightConditions(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
gs := NewWithT(t)

gs.Expect(testEnv.Create(ctx, tc.provider.GetObject())).To(Succeed())
gs.Expect(env.Create(ctx, tc.provider.GetObject())).To(Succeed())

g.Eventually(func() bool {
if err := testEnv.Get(ctx, client.ObjectKeyFromObject(tc.provider.GetObject()), tc.provider.GetObject()); err != nil {
if err := env.Get(ctx, client.ObjectKeyFromObject(tc.provider.GetObject()), tc.provider.GetObject()); err != nil {
return false
}

conditions := tc.provider.GetStatus().Conditions

if len(conditions) == 0 {
return false
}

if conditions[0].Type != operatorv1.PreflightCheckCondition {
return false
for _, cond := range tc.provider.GetStatus().Conditions {
if cond.Type == operatorv1.PreflightCheckCondition && cond.Status == corev1.ConditionTrue {
return true
}
}

if conditions[0].Status != corev1.ConditionTrue {
return false
}

return true
return false
}, timeout).Should(BeEquivalentTo(true))

gs.Expect(testEnv.Delete(ctx, tc.provider.GetObject())).To(Succeed())
gs.Expect(env.Delete(ctx, tc.provider.GetObject())).To(Succeed())
})
}
}
Expand Down Expand Up @@ -136,7 +128,6 @@ func TestNewGenericProvider(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

g := NewWithT(t)
r := GenericProviderReconciler{
Provider: tc.provider,
Expand Down Expand Up @@ -190,7 +181,6 @@ func TestNewGenericProviderList(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

g := NewWithT(t)
r := GenericProviderReconciler{
ProviderList: tc.providerList,
Expand Down
8 changes: 2 additions & 6 deletions exp/operator/controllers/preflight_checks_test.go
Expand Up @@ -25,12 +25,10 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
operatorv1 "sigs.k8s.io/cluster-api/exp/operator/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/operator/controllers/genericprovider"

"sigs.k8s.io/cluster-api/test/helpers"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestPreflightChecks(t *testing.T) {
Expand Down Expand Up @@ -286,9 +284,7 @@ func TestPreflightChecks(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
gs := NewWithT(t)

fakeclient := helpers.NewFakeClientWithScheme(
scheme.Scheme,
)
fakeclient := fake.NewClientBuilder().WithObjects().Build()

for _, c := range tc.providers {
gs.Expect(fakeclient.Create(ctx, c.GetObject())).To(Succeed())
Expand Down
35 changes: 18 additions & 17 deletions exp/operator/controllers/suite_test.go
Expand Up @@ -24,11 +24,13 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
operatorv1 "sigs.k8s.io/cluster-api/exp/operator/api/v1alpha1"
"sigs.k8s.io/cluster-api/test/helpers"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"

operatorv1 "sigs.k8s.io/cluster-api/exp/operator/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/operator/internal/envtest"
// +kubebuilder:scaffold:imports
)

Expand All @@ -37,8 +39,8 @@ const (
)

var (
testEnv *helpers.TestEnvironment
ctx = ctrl.SetupSignalHandler()
env *envtest.Environment
ctx = ctrl.SetupSignalHandler()
)

func TestAPIs(t *testing.T) {
Expand All @@ -52,52 +54,51 @@ func TestAPIs(t *testing.T) {
func TestMain(m *testing.M) {
fmt.Println("Creating new test environment")

testEnv = helpers.NewTestEnvironment()
env = envtest.New()

if err := (&GenericProviderReconciler{
Provider: &operatorv1.CoreProvider{},
ProviderList: &operatorv1.CoreProviderList{},
Client: testEnv,
}).SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
Client: env,
}).SetupWithManager(env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start CoreProviderReconciler: %v", err))
}

if err := (&GenericProviderReconciler{
Provider: &operatorv1.InfrastructureProvider{},
ProviderList: &operatorv1.InfrastructureProviderList{},
Client: testEnv,
}).SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
Client: env,
}).SetupWithManager(env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start InfrastructureProviderReconciler: %v", err))
}

if err := (&GenericProviderReconciler{
Provider: &operatorv1.BootstrapProvider{},
ProviderList: &operatorv1.BootstrapProviderList{},
Client: testEnv,
}).SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
Client: env,
}).SetupWithManager(env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start BootstrapProviderReconciler: %v", err))
}

if err := (&GenericProviderReconciler{
Provider: &operatorv1.ControlPlaneProvider{},
ProviderList: &operatorv1.ControlPlaneProviderList{},
Client: testEnv,
}).SetupWithManager(testEnv.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
Client: env,
}).SetupWithManager(env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
panic(fmt.Sprintf("Failed to start ControlPlaneProviderReconciler: %v", err))
}

go func() {
if err := testEnv.StartManager(ctx); err != nil {
if err := env.Start(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the envtest manager: %v", err))
}
}()
<-testEnv.Manager.Elected()
testEnv.WaitForWebhooks()
<-env.Manager.Elected()

// Run tests
code := m.Run()
// Tearing down the test environment
if err := testEnv.Stop(); err != nil {
if err := env.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop the envtest: %v", err))
}

Expand Down
20 changes: 20 additions & 0 deletions exp/operator/go.mod
@@ -0,0 +1,20 @@
module sigs.k8s.io/cluster-api/exp/operator

go 1.16

require (
github.com/google/uuid v1.2.0 // indirect
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.15.0
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
k8s.io/api v0.21.4
k8s.io/apiextensions-apiserver v0.21.4
k8s.io/apimachinery v0.21.4
k8s.io/client-go v0.21.4
k8s.io/klog/v2 v2.9.0
sigs.k8s.io/cluster-api v0.0.0-00010101000000-000000000000
sigs.k8s.io/controller-runtime v0.9.7
)

replace sigs.k8s.io/cluster-api => github.com/asalkeld/cluster-api v0.4.1-0.20210815234132-fb9f42d35cb1

0 comments on commit cff74b0

Please sign in to comment.