Skip to content

Commit

Permalink
Merge pull request #663 from makhov/fix-provider-id-crash
Browse files Browse the repository at this point in the history
Check for CAPI crds to be installed before setup
  • Loading branch information
makhov authored Aug 8, 2024
2 parents 4fc4f01 + 865565c commit 4d610bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ func main() {
os.Exit(1)
}
if err = (&bootstrap.ProviderIDController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Bootstrap")
os.Exit(1)
Expand Down
13 changes: 12 additions & 1 deletion internal/controller/bootstrap/providerid_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capiutil "sigs.k8s.io/cluster-api/util"
Expand All @@ -20,7 +21,8 @@ import (

type ProviderIDController struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
ClientSet *kubernetes.Clientset
}

func (p *ProviderIDController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -88,6 +90,15 @@ func (p *ProviderIDController) Reconcile(ctx context.Context, req ctrl.Request)
}

func (p *ProviderIDController) SetupWithManager(mgr ctrl.Manager) error {
apiResources, err := p.ClientSet.Discovery().ServerResourcesForGroupVersion(clusterv1.GroupVersion.String())
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if apiResources == nil {
log.Log.Info("CAPI crds are not installed yet, skipping initializing providerID controller")
return nil
}

return ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.Machine{}).
Complete(p)
Expand Down

0 comments on commit 4d610bd

Please sign in to comment.