Skip to content

Commit

Permalink
fix k8s client init in-cluster and some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nikmel2803 committed Sep 23, 2023
1 parent 20b6844 commit c785154
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
52 changes: 30 additions & 22 deletions cmd/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ import (
"path/filepath"
)

func getConfig() *rest.Config {
func getConfig() (*rest.Config, error) {
// Try in-cluster configuration
config, err := rest.InClusterConfig()
if err == nil {
return config
}

// Fallback to kubeconfig
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
// Fallback to kubeconfig
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
}

schema := scheme.Scheme
Expand All @@ -39,34 +37,44 @@ func getConfig() *rest.Config {
config.NegotiatedSerializer = serializer.NewCodecFactory(schema)
//config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(scheme)}

return config
return config, nil
}

func getClient() *kubernetes.Clientset {
config := getConfig()
func getClient() (*kubernetes.Clientset, error) {
config, err := getConfig()
if err != nil {
return nil, err
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
return nil, err
}

return client
return client, nil
}

func getRestClient() *rest.RESTClient {
config := getConfig()
func getRestClient() (*rest.RESTClient, error) {
config, err := getConfig()
if err != nil {
return nil, err
}
client, err := rest.RESTClientFor(config)
if err != nil {
log.Fatal(err)
return nil, err
}

return client
return client, nil
}

func getDynamicClient() *dynamic.DynamicClient {
config := getConfig()
func getDynamicClient() (*dynamic.DynamicClient, error) {
config, err := getConfig()
if err != nil {
return nil, err
}
client, err := dynamic.NewForConfig(config)
if err != nil {
log.Fatal(err)
return nil, err
}
return client
return client, nil
}
12 changes: 10 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ import (
)

func setupServer(config Config, logger *zap.Logger) {
reconciler := NewReconciler(logger)
k8sClient, err := getRestClient()
if err != nil {
logger.Fatal("Failed to get Kubernetes client", zap.Error(err))
}
reconciler := NewReconciler(k8sClient, logger)
handlers := NewHandlers(config, reconciler, logger)
http.Handle("/webhook", WithLogging(http.HandlerFunc(handlers.Webhook), logger))
http.Handle("/subscribe", WithLogging(http.HandlerFunc(handlers.Subscribe), logger))
}

func setupClient(config Config, shutdownChan chan struct{}, logger *zap.Logger) {
reconciler := NewReconciler(logger)
k8sClient, err := getRestClient()
if err != nil {
logger.Fatal("Failed to get Kubernetes client", zap.Error(err))
}
reconciler := NewReconciler(k8sClient, logger)

u, err := url.Parse(config.ServerEndpoint)
if err != nil {
Expand Down
12 changes: 4 additions & 8 deletions cmd/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ type Reconciler struct {
logger *zap.Logger
}

func NewReconciler(logger *zap.Logger) *Reconciler {
func NewReconciler(client *rest.RESTClient, logger *zap.Logger) *Reconciler {
return &Reconciler{
restClient: getRestClient(),
restClient: client,
logger: logger,
}
}

func (r *Reconciler) ReconcileSources(ociUrl string, tag string) {
restClient := getRestClient()

var res sourceController.OCIRepositoryList
err := restClient.Get().Resource("ocirepositories").Namespace("").Do(context.Background()).Into(&res)
err := r.restClient.Get().Resource("ocirepositories").Namespace("").Do(context.Background()).Into(&res)
if err != nil {
r.logger.Error("Failed to get OCIRepositories", zap.Error(err))
}
Expand All @@ -46,8 +44,6 @@ func (r *Reconciler) ReconcileSources(ociUrl string, tag string) {
}

func (r *Reconciler) annotateRepository(repository sourceController.OCIRepository) error {
restClient := getRestClient()

patch := struct {
Metadata struct {
Annotations map[string]string `json:"annotations"`
Expand All @@ -61,7 +57,7 @@ func (r *Reconciler) annotateRepository(repository sourceController.OCIRepositor
patchJson, _ := json.Marshal(patch)

var res sourceController.OCIRepository
return restClient.
return r.restClient.
Patch(types.MergePatchType).
Resource("ocirepositories").
Namespace(repository.Namespace).
Expand Down

0 comments on commit c785154

Please sign in to comment.