Skip to content

Commit

Permalink
Support running ip reconciler with KUBECONFIG
Browse files Browse the repository at this point in the history
Sometimes, we want to run the reconciler outside of the cluster
for development convenience. This patch allows the reconciler
process to use the KUBECONFIG environment variable.

Signed-off-by: Peng Liu <[email protected]>
  • Loading branch information
pliurh committed Mar 4, 2024
1 parent 271aa86 commit c9e55dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
15 changes: 13 additions & 2 deletions cmd/controlloop/controlloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/tools/clientcmd"

nadclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
nadinformers "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions"
Expand Down Expand Up @@ -125,9 +126,19 @@ func handleSignals(stopChannel chan struct{}, signals ...os.Signal) {
}

func newPodController(stopChannel chan struct{}) (*controlloop.PodController, error) {
cfg, err := rest.InClusterConfig()
var cfg *rest.Config
var err error
cfg, err = rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to implicitly generate the kubeconfig: %w", err)
logging.Debugf("failed to generate the kubeconfig from service account: %v", err)
kubeConfigFile := os.Getenv("KUBECONFIG")
if kubeConfigFile == "" {
return nil, fmt.Errorf("KUBECONFIG environment variable not set")
}
cfg, err = clientcmd.BuildConfigFromFlags("", kubeConfigFile)
if err != nil {
return nil, fmt.Errorf("failed to generate kubeconfig from file %s: %v", kubeConfigFile, err)
}
}

k8sClientSet, err := kubernetes.NewForConfig(cfg)
Expand Down
12 changes: 10 additions & 2 deletions pkg/reconciler/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reconciler

import (
"context"
"os"
"time"

"github.com/k8snetworkplumbingwg/whereabouts/pkg/logging"
Expand All @@ -16,13 +17,20 @@ func ReconcileIPs(errorChan chan error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(defaultReconcilerTimeout*time.Second))
defer cancel()

ipReconcileLoop, err := NewReconcileLooper(ctx, defaultReconcilerTimeout)
var err error
var ipReconcileLoop *ReconcileLooper
kubeConfigFile := os.Getenv("KUBECONFIG")

if kubeConfigFile == "" {
ipReconcileLoop, err = NewReconcileLooper(ctx, defaultReconcilerTimeout)
} else {
ipReconcileLoop, err = NewReconcileLooperWithKubeconfig(ctx, kubeConfigFile, defaultReconcilerTimeout)
}
if err != nil {
_ = logging.Errorf("failed to create the reconcile looper: %v", err)
errorChan <- err
return
}

cleanedUpIps, err := ipReconcileLoop.ReconcileIPPools(ctx)
if err != nil {
_ = logging.Errorf("failed to clean up IP for allocations: %v", err)
Expand Down

0 comments on commit c9e55dd

Please sign in to comment.