Skip to content

Commit

Permalink
Merge pull request #3 from openshift-splat-team/SPLAT-1430
Browse files Browse the repository at this point in the history
SPLAT-1430: Fixed controller-runtime usage to match newer version of go.
  • Loading branch information
vr4manta authored Feb 5, 2024
2 parents 8f84b4c + c2ed38b commit dfad9f3
Show file tree
Hide file tree
Showing 46 changed files with 2,847 additions and 198 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
testbin/*

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~

.vscode/
7 changes: 0 additions & 7 deletions .idea/vcs.xml

This file was deleted.

Binary file removed bin/mapi-static-ip-controller
Binary file not shown.
88 changes: 68 additions & 20 deletions cmd/mapi-static-ip-controller/mapi-static-ip-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
"strings"
"sync"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"

osclientset "github.com/openshift/client-go/config/clientset/versioned"
mapiclientset "github.com/openshift/client-go/machine/clientset/versioned"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2/klogr"
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/builder"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand All @@ -33,6 +36,8 @@ var (
)

func main() {
logger := klogr.New()
ctrl.SetLogger(logger)

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
if err != nil {
Expand All @@ -47,21 +52,15 @@ func main() {
ipamv1.AddToScheme(mgr.GetScheme())
ipamcontrollerv1.AddToScheme(mgr.GetScheme())

err = builder.
ControllerManagedBy(mgr). // Create the ControllerManagedBy
For(&ipamv1.IPAddressClaim{}).
Complete(&IPPoolClaimProcessor{})
if err != nil {
log.Error(err, "could not create claim processor")
if err := (&IPPoolClaimProcessor{}).
SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "IPPoolClaimController")
os.Exit(1)
}

err = builder.
ControllerManagedBy(mgr). // Create the ControllerManagedBy
For(&ipamcontrollerv1.IPPool{}).
Complete(&IPPoolController{})
if err != nil {
log.Error(err, "could not create controller")
if err := (&IPPoolController{}).
SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "IPPoolController")
os.Exit(1)
}

Expand All @@ -73,10 +72,18 @@ func main() {

type IPPoolClaimProcessor struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
RESTMapper meta.RESTMapper
UncachedClient client.Client
}

type IPPoolController struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
RESTMapper meta.RESTMapper
UncachedClient client.Client
}

func (a *IPPoolClaimProcessor) BindClaim(ctx context.Context, ipAddressClaim *ipamv1.IPAddressClaim) error {
Expand Down Expand Up @@ -128,14 +135,34 @@ func (a *IPPoolClaimProcessor) ReleaseClaim(ctx context.Context, namespacedName
return err
}

// SetupWithManager sets up the controller with the Manager.
func (a *IPPoolClaimProcessor) SetupWithManager(mgr ctrl.Manager) error {
if err := ctrl.NewControllerManagedBy(mgr).
For(&ipamv1.IPAddressClaim{}).
Complete(a); err != nil {
return fmt.Errorf("could not set up controller for ip pool claim: %w", err)
}

// Set up API helpers from the manager.
a.Client = mgr.GetClient()
a.Scheme = mgr.GetScheme()
a.Recorder = mgr.GetEventRecorderFor("ip-pool-claim-controller")
a.RESTMapper = mgr.GetRESTMapper()

return nil
}

func (a *IPPoolClaimProcessor) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
mu.Lock()
defer mu.Unlock()

log.Infof("Received request %v", req)
log.Infof("Reconciling request %v", req)
defer log.Infof("Finished reconciling request %v", req)

ipAddressClaim := &ipamv1.IPAddressClaim{}
if err := a.Get(ctx, req.NamespacedName, ipAddressClaim); err != nil {
claimKey := client.ObjectKey{Namespace: req.Namespace, Name: req.Name}

if err := a.Get(ctx, claimKey, ipAddressClaim); err != nil {
log.Warnf("Got error: %v", err)
if strings.Contains(fmt.Sprintf("%v%", err), "not found") {
log.Info("Handling remove of claim")
Expand All @@ -149,6 +176,7 @@ func (a *IPPoolClaimProcessor) Reconcile(ctx context.Context, req reconcile.Requ
// Check claim to see if it needs IP from a pool that we own.
poolRef := ipAddressClaim.Spec.PoolRef
log.Debugf("Kind(%v) Group(%v) Name(%v)", poolRef.Kind, *poolRef.APIGroup, poolRef.Name)

if poolRef.Kind == ipamcontrollerv1.IPPoolKind && *poolRef.APIGroup == ipamcontrollerv1.APIGroupName {
log.Debugf("Found a claim for an IP from this provider. Status: %v", ipAddressClaim.Status)
if ipAddressClaim.Status.AddressRef.Name == "" {
Expand Down Expand Up @@ -229,14 +257,34 @@ func (a *IPPoolController) RemovePool(ctx context.Context, pool string) error {
return err
}

// SetupWithManager sets up the controller with the Manager.
func (a *IPPoolController) SetupWithManager(mgr ctrl.Manager) error {
if err := ctrl.NewControllerManagedBy(mgr).
For(&ipamcontrollerv1.IPPool{}).
Complete(a); err != nil {
return fmt.Errorf("could not set up controller for ip pool: %w", err)
}

// Set up API helpers from the manager.
a.Client = mgr.GetClient()
a.Scheme = mgr.GetScheme()
a.Recorder = mgr.GetEventRecorderFor("ip-pool-controller")
a.RESTMapper = mgr.GetRESTMapper()

return nil
}

func (a *IPPoolController) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
mu.Lock()
defer mu.Unlock()

log.Infof("Received request %v", req)
log.Infof("Reconciling request %v", req)
defer log.Infof("Finished reconciling request %v", req)

pool := &ipamcontrollerv1.IPPool{}
if err := a.Get(ctx, req.NamespacedName, pool); err != nil {
poolKey := client.ObjectKey{Namespace: req.Namespace, Name: req.Name}

if err := a.Get(ctx, poolKey, pool); err != nil {
log.Warnf("Got error: %v", err)
switch t := err.(type) {
default:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
k8s.io/code-generator v0.28.4
k8s.io/klog/v2 v2.110.1
sigs.k8s.io/cluster-api v1.6.0
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/controller-tools v0.10.0
Expand Down Expand Up @@ -240,7 +241,6 @@ require (
k8s.io/apiextensions-apiserver v0.28.4 // indirect
k8s.io/component-base v0.28.4 // indirect
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
mvdan.cc/gofumpt v0.4.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/zapr v1.2.4 h1:QHVo+6stLbfJmYGkQ7uGHUCu5hnAFAj6mDe6Ea0SeOo=
Expand Down Expand Up @@ -1152,8 +1151,8 @@ k8s.io/component-base v0.28.4/go.mod h1:m9hR0uvqXDybiGL2nf/3Lf0MerAfQXzkfWhUY58J
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d h1:U9tB195lKdzwqicbJvyJeOXV7Klv+wNAWENRnXEGi08=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
Expand Down
2 changes: 1 addition & 1 deletion hack/go-fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if [ "$IS_CONTAINER" != "" ]; then
gci_repo=github.com/daixiang0/gci
for TARGET in "${@}"; do
find "${TARGET}" -name '*.go' ! -path '*/vendor/*' ! -path '*/.build/*' -exec gofmt -s -w {} \+
find "${TARGET}" -name '*.go' ! -path '*/vendor/*' ! -path '*/.build/*' -exec go run "$gci_repo" write -s standard -s default -s "prefix(github.com/rvanderp3)" -s blank --skip-generated {} \+
find "${TARGET}" -name '*.go' ! -path '*/vendor/*' ! -path '*/.build/*' -exec go run "$gci_repo" write -s standard -s default -s "prefix(github.com/openshift-splat-team)" -s blank --skip-generated {} \+
done
git diff --exit-code
else
Expand Down
14 changes: 7 additions & 7 deletions pkg/generated/clientset/versioned/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/generated/clientset/versioned/scheme/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions pkg/generated/informers/externalversions/factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/mgmt/ipmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"fmt"
"net/netip"

ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"

goipam "github.com/metal-stack/go-ipam"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"

v1 "github.com/openshift-splat-team/machine-ipam-controller/pkg/apis/ipamcontroller.openshift.io/v1"
)
Expand Down
Loading

0 comments on commit dfad9f3

Please sign in to comment.