Skip to content

Commit

Permalink
Add kubeconfig flag for cache initialization (#155)
Browse files Browse the repository at this point in the history
* Fix delete model issue

* Add kubeconfig flag

---------

Co-authored-by: varungupta <varungupta@BYTEDANCE.COM>
  • Loading branch information
varungup90 and varungupta authored Sep 10, 2024
1 parent 4ac2b43 commit 578fb83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 21 additions & 3 deletions cmd/controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ package main
import (
"crypto/tls"
"flag"
"log"
"os"
"time"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/leaderelection/resourcelock"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
Expand Down Expand Up @@ -71,6 +73,7 @@ func init() {
}

func main() {
var kubeConfig string
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
Expand All @@ -81,6 +84,7 @@ func main() {
var renewDeadLine time.Duration
var leaderElectionResourceLock string
var leaderElectionId string
flag.StringVar(&kubeConfig, "kubeconfig", "", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand Down Expand Up @@ -165,7 +169,21 @@ func main() {
setupLog.Info("starting cache")
stopCh := make(chan struct{})
defer close(stopCh)
cache.NewCache(stopCh)
var config *rest.Config

if kubeConfig == "" {
log.Printf("using in-cluster configuration")
config, err = rest.InClusterConfig()
} else {
log.Printf("using configuration from '%s'", kubeConfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeConfig)
}

if err != nil {
panic(err)
}

cache.NewCache(config, stopCh)

// Kind controller registration is encapsulated inside the pkg/controller/controller.go
// So here we can use more clean registration flow and there's no need to change logics in future.
Expand Down
18 changes: 1 addition & 17 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cache
import (
"errors"
"fmt"
"log"
"strings"
"sync"

Expand All @@ -30,7 +29,6 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"

modelv1alpha1 "github.com/aibrix/aibrix/api/model/v1alpha1"
Expand Down Expand Up @@ -62,22 +60,8 @@ func GetCache() (*Cache, error) {
return &instance, nil
}

func NewCache(stopCh <-chan struct{}) *Cache {
func NewCache(config *rest.Config, stopCh <-chan struct{}) *Cache {
once.Do(func() {
var config *rest.Config
var err error

if kubeconfig == "" {
log.Printf("using in-cluster configuration")
config, err = rest.InClusterConfig()
} else {
log.Printf("using configuration from '%s'", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
}

if err != nil {
panic(err)
}

if err := v1alpha1scheme.AddToScheme(scheme.Scheme); err != nil {
panic(err)
Expand Down

0 comments on commit 578fb83

Please sign in to comment.