Skip to content

Commit

Permalink
Proper registration of scheme for hub client (stolostron#1397)
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas Camata <[email protected]>
  • Loading branch information
douglascamata authored Apr 8, 2024
1 parent 29821c7 commit 1cb5bad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (r *ObservabilityAddonReconciler) Reconcile(ctx context.Context, req ctrl.R
// Fetch the ObservabilityAddon instance in hub cluster
err := r.HubClient.Get(ctx, types.NamespacedName{Name: obAddonName, Namespace: hubNamespace}, hubObsAddon)
if err != nil {
hubClient, obsAddon, err := util.RenewAndRetry(ctx)
hubClient, obsAddon, err := util.RenewAndRetry(ctx, r.Scheme)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get observabilityaddon: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *StatusReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
hubObsAddon := &oav1beta1.ObservabilityAddon{}
err := r.HubClient.Get(ctx, types.NamespacedName{Name: obAddonName, Namespace: hubNamespace}, hubObsAddon)
if err != nil {
hubClient, obsAddon, err := util.RenewAndRetry(ctx)
hubClient, obsAddon, err := util.RenewAndRetry(ctx, r.Scheme)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion operators/endpointmetrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func main() {
os.Exit(1)
}

hubClient, err := util.GetOrCreateHubClient(false)
hubClient, err := util.GetOrCreateHubClient(false, scheme)
if err != nil {
setupLog.Error(err, "Failed to create the hub client")
os.Exit(1)
Expand Down
22 changes: 15 additions & 7 deletions operators/endpointmetrics/pkg/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"context"
"os"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

ocpClientSet "github.com/openshift/client-go/config/clientset/versioned"

oav1beta1 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta1"
oav1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
)

const (
Expand All @@ -33,7 +36,7 @@ var (
)

// GetOrCreateOCPClient get an existing hub client or create new one if it doesn't exist.
func GetOrCreateHubClient(renew bool) (client.Client, error) {
func GetOrCreateHubClient(renew bool, clientScheme *runtime.Scheme) (client.Client, error) {
if os.Getenv("UNIT_TEST") == "true" {
return hubClient, nil
}
Expand All @@ -48,13 +51,18 @@ func GetOrCreateHubClient(renew bool) (client.Client, error) {
return nil, err
}

s := scheme.Scheme
if err := oav1beta1.AddToScheme(s); err != nil {
return nil, err
if clientScheme == nil {
clientScheme := scheme.Scheme
if err := oav1beta1.AddToScheme(clientScheme); err != nil {
return nil, err
}
if err := oav1beta2.AddToScheme(clientScheme); err != nil {
return nil, err
}
}

// generate the client based off of the config
hubClient, err := client.New(config, client.Options{Scheme: s})
hubClient, err := client.New(config, client.Options{Scheme: clientScheme})

if err != nil {
log.Error(err, "Failed to create hub client")
Expand Down Expand Up @@ -90,10 +98,10 @@ func SetHubClient(c client.Client) {
hubClient = c
}

func RenewAndRetry(ctx context.Context) (client.Client, *oav1beta1.ObservabilityAddon, error) {
func RenewAndRetry(ctx context.Context, scheme *runtime.Scheme) (client.Client, *oav1beta1.ObservabilityAddon, error) {
// try to renew the hub client
log.Info("renew hub client")
hubClient, err := GetOrCreateHubClient(true)
hubClient, err := GetOrCreateHubClient(true, scheme)
if err != nil {
log.Error(err, "Failed to create the hub client")
return nil, nil, err
Expand Down
4 changes: 2 additions & 2 deletions operators/endpointmetrics/pkg/util/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func init() {
func TestRenewAndRetry(t *testing.T) {
hubClient := fake.NewClientBuilder().Build()
SetHubClient(hubClient)
_, _, err := RenewAndRetry(context.TODO())
_, _, err := RenewAndRetry(context.TODO(), nil)
if err == nil {
t.Fatal("missing error")
}

hubClient1 := fake.NewClientBuilder().WithRuntimeObjects(newObservabilityAddon(name, testNamespace)).Build()
SetHubClient(hubClient1)
_, _, err = RenewAndRetry(context.TODO())
_, _, err = RenewAndRetry(context.TODO(), nil)
if err != nil {
t.Fatalf("Error caught: %v", err)
}
Expand Down

0 comments on commit 1cb5bad

Please sign in to comment.