Skip to content

Commit

Permalink
Support contextual logging
Browse files Browse the repository at this point in the history
  • Loading branch information
haijianyang committed Apr 12, 2024
1 parent 718a3ae commit 5e48de6
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 220 deletions.
182 changes: 91 additions & 91 deletions controllers/elfmachine_controller.go

Large diffs are not rendered by default.

180 changes: 90 additions & 90 deletions controllers/elfmachine_controller_test.go

Large diffs are not rendered by default.

30 changes: 8 additions & 22 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ import (
capecontext "github.com/smartxworks/cluster-api-provider-elf/pkg/context"
"k8s.io/klog/v2"
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/context"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam"
ipamutil "github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam/util"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/test/fake"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/test/helpers"
)

Expand All @@ -47,6 +45,7 @@ func TestControllers(t *testing.T) {

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

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -74,20 +73,20 @@ func setup() {
_ = fmt.Errorf("Error setting alsologtostderr flag")
}

testEnv = helpers.NewTestEnvironment()
testEnv = helpers.NewTestEnvironment(ctx)

// Set kubeconfig.
os.Setenv("KUBECONFIG", testEnv.Kubeconfig)

controllerOpts := controller.Options{MaxConcurrentReconciles: 10}

if err := AddMachineControllerToManager(testEnv.GetContext(), testEnv.Manager, controllerOpts); err != nil {
if err := AddMachineControllerToManager(ctx, testEnv.GetControllerManagerContext(), testEnv.Manager, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup ElfMachine controller: %v", err))
}

go func() {
fmt.Println("Starting the manager")
if err := testEnv.StartManager(testEnv.GetContext()); err != nil {
if err := testEnv.StartManager(ctx); err != nil {
panic(fmt.Sprintf("failed to start the envtest manager: %v", err))
}
}()
Expand All @@ -101,33 +100,20 @@ func teardown() {
}
}

func newCtrlContexts(objs ...client.Object) *capecontext.ControllerContext {
ctrlMgrContext := fake.NewControllerManagerContext(objs...)
ctrlContext := &capecontext.ControllerContext{
ControllerManagerContext: ctrlMgrContext,
Logger: ctrllog.Log,
}

return ctrlContext
}

func setMetal3IPForClaim(ipClaim *ipamv1.IPClaim, ip *ipamv1.IPAddress) {
ref := ipamutil.GetObjRef(ip)
ipClaim.Status.Address = &ref
}

func newMachineContext(
ctrlContext *capecontext.ControllerContext,
ipamService ipam.IPAddressManager,
cluster *capiv1.Cluster, machine *capiv1.Machine, elfMachine *capev1.ElfMachine) *context.MachineContext {
return &context.MachineContext{
IPAMService: ipamService,
MachineContext: &capecontext.MachineContext{
ControllerContext: ctrlContext,
Cluster: cluster,
Machine: machine,
ElfMachine: elfMachine,
Logger: ctrlContext.Logger,
Cluster: cluster,
Machine: machine,
ElfMachine: elfMachine,
},
}
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/onsi/ginkgo/v2 v2.13.1
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
github.com/smartxworks/cluster-api-provider-elf v1.3.3-rc.0
github.com/smartxworks/cluster-api-provider-elf v1.3.3-rc.0.0.20240412070309-2f0e706ff1ee
github.com/spf13/pflag v1.0.5
golang.org/x/mod v0.13.0
k8s.io/apiextensions-apiserver v0.28.4
Expand All @@ -27,6 +27,7 @@ require (
)

require (
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
Expand Down Expand Up @@ -78,6 +79,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3I
github.com/openlyinc/pointy v1.2.0/go.mod h1:JodZOTJoBNaAQHeU0F/SwA4PL0lg4pKF7fYFpX291P0=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -348,6 +350,10 @@ github.com/smartxworks/cloudtower-go-sdk/v2 v2.13.1-0.20231116110941-d411454388a
github.com/smartxworks/cloudtower-go-sdk/v2 v2.13.1-0.20231116110941-d411454388af/go.mod h1:X6R9+L438SMnLJXykSCV3fJ+AZul0hlyjITsZgrSRtM=
github.com/smartxworks/cluster-api-provider-elf v1.3.3-rc.0 h1:+1JTJ3q677eO7Bl3nEHWrbHhBh7yG1G5Kz1z+bJKrDI=
github.com/smartxworks/cluster-api-provider-elf v1.3.3-rc.0/go.mod h1:AvzRbdE+tP4R5E3dx5yxoIxIiYeOOowjQ+sKw6ETyVM=
github.com/smartxworks/cluster-api-provider-elf v1.3.3-rc.0.0.20240412070309-2f0e706ff1ee h1:aUjheAw1Z24PruYrE+UxLkElvw4zT1ZyUBjsOiFgAZ0=
github.com/smartxworks/cluster-api-provider-elf v1.3.3-rc.0.0.20240412070309-2f0e706ff1ee/go.mod h1:AvzRbdE+tP4R5E3dx5yxoIxIiYeOOowjQ+sKw6ETyVM=
github.com/smartxworks/cluster-api-provider-elf v1.4.0-beta.0 h1:aSVakeKqVzEOzSwn86J7hUVJibpnfSDq9IQdsqrfHn8=
github.com/smartxworks/cluster-api-provider-elf v1.4.0-beta.0/go.mod h1:7IFFlC0lBnaDS1t8RQp4SXzJ0PzPRsS403/WyPm4KK8=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
goctx "context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -48,7 +49,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
ctrlsig "sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/smartxworks/cluster-api-provider-elf-static-ip/controllers"
Expand Down Expand Up @@ -207,8 +207,8 @@ func main() {
utilruntime.Must(ipamv1.AddToScheme(managerOpts.Scheme))

// Create a function that adds all of the controllers and webhooks to the manager.
addToManager := func(ctx *context.ControllerManagerContext, mgr ctrlmgr.Manager) error {
if err := controllers.AddMachineControllerToManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: elfMachineConcurrency}); err != nil {
addToManager := func(ctx goctx.Context, ctrlMgrCtx *context.ControllerManagerContext, mgr ctrlmgr.Manager) error {
if err := controllers.AddMachineControllerToManager(ctx, ctrlMgrCtx, mgr, controller.Options{MaxConcurrentReconciles: elfMachineConcurrency}); err != nil {
return err
}

Expand All @@ -226,17 +226,18 @@ func main() {
managerOpts.Metrics = capiflags.GetDiagnosticsOptions(diagnosticsOptions)

setupLog.Info("creating controller manager", "version", version.Get().String())
mgr, err := capemanager.New(managerOpts)
// Set up the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()
mgr, err := capemanager.New(ctx, managerOpts)
if err != nil {
setupLog.Error(err, "problem creating controller manager")
os.Exit(1)
}

setupChecks(mgr)

sigHandler := ctrlsig.SetupSignalHandler()
setupLog.Info("starting controller manager")
if err := mgr.Start(sigHandler); err != nil {
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running controller manager")
os.Exit(1)
}
Expand Down
6 changes: 4 additions & 2 deletions test/fake/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package fake

import (
goctx "context"

capev1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
"github.com/smartxworks/cluster-api-provider-elf/pkg/context"
capefake "github.com/smartxworks/cluster-api-provider-elf/test/fake"
Expand Down Expand Up @@ -45,8 +47,8 @@ func NewClusterAndMachineObjects() (*capev1.ElfCluster, *capiv1.Cluster, *capev1
}

func InitOwnerReferences(
ctrlContext *context.ControllerContext,
ctx goctx.Context, ctrlMgrCtx *context.ControllerManagerContext,
elfCluster *capev1.ElfCluster, cluster *capiv1.Cluster,
elfMachine *capev1.ElfMachine, machine *capiv1.Machine) {
capefake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
capefake.InitOwnerReferences(ctx, ctrlMgrCtx, elfCluster, cluster, elfMachine, machine)
}
5 changes: 0 additions & 5 deletions test/fake/controller_manager_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package fake

import (
goctx "context"

ipamv1 "github.com/metal3-io/ip-address-manager/api/v1alpha1"
capev1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
capecontext "github.com/smartxworks/cluster-api-provider-elf/pkg/context"
Expand All @@ -33,7 +31,6 @@ import (
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
)

// NewControllerManagerContext returns a fake ControllerManagerContext for unit
Expand All @@ -56,9 +53,7 @@ func NewControllerManagerContext(initObjects ...client.Object) *capecontext.Cont
).WithObjects(initObjects...).Build()

return &capecontext.ControllerManagerContext{
Context: goctx.Background(),
Client: clientWithObjects,
Logger: ctrllog.Log.WithName(capefake.ControllerManagerName),
Scheme: scheme,
Name: capefake.ControllerManagerName,
LeaderElectionNamespace: capefake.LeaderElectionNamespace,
Expand Down
6 changes: 3 additions & 3 deletions test/helpers/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type (
)

// NewTestEnvironment creates a new environment spinning up a local api-server.
func NewTestEnvironment() *TestEnvironment {
func NewTestEnvironment(ctx goctx.Context) *TestEnvironment {
// Create the test environment.
env := &envtest.Environment{
ErrorIfCRDPathMissing: true,
Expand Down Expand Up @@ -157,11 +157,11 @@ func NewTestEnvironment() *TestEnvironment {
},
KubeConfig: env.Config,
}
managerOpts.AddToManager = func(ctx *context.ControllerManagerContext, mgr ctrlmgr.Manager) error {
managerOpts.AddToManager = func(ctx goctx.Context, ctrlMgrCtx *context.ControllerManagerContext, mgr ctrlmgr.Manager) error {
return nil
}

mgr, err := manager.New(managerOpts)
mgr, err := manager.New(ctx, managerOpts)
if err != nil {
klog.Fatalf("failed to create the SKS controller manager: %v", err)
}
Expand Down

0 comments on commit 5e48de6

Please sign in to comment.