Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Introduce generics to providers #475

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/v1alpha1/provider_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"

apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
ctrlconfigv1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
Expand Down Expand Up @@ -525,5 +525,5 @@ func fromImageMeta(im *ImageMeta) *string {
result = result + ":" + im.Tag
}

return pointer.String(result)
return ptr.To(result)
}
68 changes: 30 additions & 38 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
"k8s.io/klog/v2/textlogger"
"sigs.k8s.io/cluster-api-operator/internal/webhook"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
Expand All @@ -47,6 +47,8 @@ import (
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
providercontroller "sigs.k8s.io/cluster-api-operator/internal/controller"
healtchcheckcontroller "sigs.k8s.io/cluster-api-operator/internal/controller/healthcheck"
"sigs.k8s.io/cluster-api-operator/internal/controller/phases"
"sigs.k8s.io/cluster-api-operator/internal/controller/providers"
)

var (
Expand Down Expand Up @@ -129,7 +131,9 @@ func main() {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

ctrl.SetLogger(klogr.New())
loggerConfig := textlogger.NewConfig([]textlogger.ConfigOption{}...)
ctrl.SetLogger(textlogger.NewLogger(loggerConfig))

restConfig := ctrl.GetConfigOrDie()

diagnosticsOpts := flags.GetDiagnosticsOptions(diagnosticsOptions)
Expand Down Expand Up @@ -210,62 +214,50 @@ func setupChecks(mgr ctrl.Manager) {
}

func setupReconcilers(mgr ctrl.Manager) {
if err := (&providercontroller.GenericProviderReconciler{
Provider: &operatorv1.CoreProvider{},
ProviderList: &operatorv1.CoreProviderList{},
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
if err := providercontroller.NewProviderControllerWrapper(
providers.NewCoreProviderReconciler(mgr),
phases.NewPhase,
).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CoreProvider")
os.Exit(1)
}

if err := (&providercontroller.GenericProviderReconciler{
Provider: &operatorv1.InfrastructureProvider{},
ProviderList: &operatorv1.InfrastructureProviderList{},
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
if err := providercontroller.NewProviderControllerWrapper(
providers.NewInfrastructureProviderReconciler(mgr),
phases.NewPhase,
).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "InfrastructureProvider")
os.Exit(1)
}

if err := (&providercontroller.GenericProviderReconciler{
Provider: &operatorv1.BootstrapProvider{},
ProviderList: &operatorv1.BootstrapProviderList{},
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
if err := providercontroller.NewProviderControllerWrapper(
providers.NewBootstrapProviderReconciler(mgr),
phases.NewPhase,
).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "BootstrapProvider")
os.Exit(1)
}

if err := (&providercontroller.GenericProviderReconciler{
Provider: &operatorv1.ControlPlaneProvider{},
ProviderList: &operatorv1.ControlPlaneProviderList{},
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
if err := providercontroller.NewProviderControllerWrapper(
providers.NewControlPlaneProviderReconciler(mgr),
phases.NewPhase,
).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ControlPlaneProvider")
os.Exit(1)
}

if err := (&providercontroller.GenericProviderReconciler{
Provider: &operatorv1.AddonProvider{},
ProviderList: &operatorv1.AddonProviderList{},
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
if err := providercontroller.NewProviderControllerWrapper(
providers.NewAddonProviderReconciler(mgr),
phases.NewPhase,
).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AddonProvider")
os.Exit(1)
}

if err := (&providercontroller.GenericProviderReconciler{
Provider: &operatorv1.IPAMProvider{},
ProviderList: &operatorv1.IPAMProviderList{},
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
if err := providercontroller.NewProviderControllerWrapper(
providers.NewIPAMProviderReconciler(mgr),
phases.NewPhase,
).SetupWithManager(mgr, concurrency(concurrencyNumber)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IPAMProvider")
os.Exit(1)
}
Expand Down
24 changes: 10 additions & 14 deletions cmd/plugin/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2/klogr"
"k8s.io/klog/v2/textlogger"

kerrors "k8s.io/apimachinery/pkg/util/errors"

Expand All @@ -40,6 +40,7 @@ import (
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
"sigs.k8s.io/cluster-api-operator/internal/controller/generic"
)

type deleteOptions struct {
Expand Down Expand Up @@ -141,7 +142,8 @@ func init() {
func runDelete() error {
ctx := context.Background()

ctrl.SetLogger(klogr.New())
loggerConfig := textlogger.NewConfig([]textlogger.ConfigOption{}...)
ctrl.SetLogger(textlogger.NewLogger(loggerConfig))

hasProviderNames := deleteOpts.coreProvider ||
(len(deleteOpts.bootstrapProviders) > 0) ||
Expand Down Expand Up @@ -170,7 +172,7 @@ func runDelete() error {

group := &DeleteGroup{
selectors: []fields.Set{},
providers: []genericProviderList{},
providers: []generic.ProviderList{},
}
errors := append([]error{},
group.delete(&operatorv1.BootstrapProviderList{}, deleteOpts.bootstrapProviders...),
Expand All @@ -196,10 +198,10 @@ func runDelete() error {

type DeleteGroup struct {
selectors []fields.Set
providers []genericProviderList
providers []generic.ProviderList
}

func (d *DeleteGroup) delete(providerType genericProviderList, names ...string) error {
func (d *DeleteGroup) delete(providerType generic.ProviderList, names ...string) error {
for _, provider := range names {
selector, err := selectorFromProvider(provider)
if err != nil {
Expand All @@ -215,7 +217,7 @@ func (d *DeleteGroup) delete(providerType genericProviderList, names ...string)

func (d *DeleteGroup) deleteAll() {
for _, list := range operatorv1.ProviderLists {
providerList, ok := list.(genericProviderList)
providerList, ok := list.(generic.ProviderList)
if !ok {
log.V(5).Info("Expected to get GenericProviderList")
continue
Expand Down Expand Up @@ -283,9 +285,9 @@ func selectorFromProvider(provider string) (fields.Set, error) {
return selector, nil
}

func deleteProviders(ctx context.Context, client ctrlclient.Client, providerList genericProviderList, selector ctrlclient.MatchingFieldsSelector) (bool, error) {
func deleteProviders(ctx context.Context, client ctrlclient.Client, providerList generic.ProviderList, selector ctrlclient.MatchingFieldsSelector) (bool, error) {
//nolint:forcetypeassert
providerList = providerList.DeepCopyObject().(genericProviderList)
providerList = providerList.DeepCopyObject().(generic.ProviderList)
ready := true

gvks, _, err := scheme.ObjectKinds(providerList)
Expand All @@ -306,12 +308,6 @@ func deleteProviders(ctx context.Context, client ctrlclient.Client, providerList
for _, provider := range providerList.GetItems() {
log.Info(fmt.Sprintf("Deleting %s %s/%s", provider.GetType(), provider.GetName(), provider.GetNamespace()))

provider, ok := provider.(genericProvider)
if !ok {
log.Info(fmt.Sprintf("Expected to get GenericProvider for %s", gvk))
continue
}

if err := client.DeleteAllOf(ctx, provider, ctrlclient.InNamespace(provider.GetNamespace())); err != nil {
return false, fmt.Errorf("unable to issue delete for %s: %w", gvk, err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/plugin/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/fields"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
"sigs.k8s.io/cluster-api-operator/internal/controller/generic"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -78,13 +79,13 @@ func TestSelectorFromProvider(t *testing.T) {
func TestDeleteProviders(t *testing.T) {
tests := []struct {
name string
list genericProviderList
providers []genericProvider
list generic.ProviderList
providers []generic.Provider
selector fields.Set
}{{
name: "Delete providers",
list: &operatorv1.AddonProviderList{},
providers: []genericProvider{&operatorv1.AddonProvider{
providers: []generic.Provider{&operatorv1.AddonProvider{
ObjectMeta: metav1.ObjectMeta{
Name: "addon",
Namespace: "default",
Expand Down
9 changes: 5 additions & 4 deletions cmd/plugin/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
"sigs.k8s.io/cluster-api-operator/internal/controller/generic"
"sigs.k8s.io/cluster-api-operator/util"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
"sigs.k8s.io/cluster-api-operator/util"
)

type initOptions struct {
Expand Down Expand Up @@ -471,7 +471,8 @@ func createGenericProvider(ctx context.Context, client ctrlclient.Client, provid
return nil, fmt.Errorf("provider name can't be empty")
}

provider := NewGenericProvider(providerType)
rec := generic.ProviderReconcilers[providerType]
provider := rec.GenericProvider()

// Set name and namespace
provider.SetName(name)
Expand Down
Loading
Loading