Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] client initilization #17

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions cmd/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,9 @@ func runServiceMonitor(_ *cobra.Command, _ []string) error {
return err
}

//TODO(nicolastakashi): Replace it when the PR #6623 is merged.
restConfig, err := k8sutil.GetRestConfig(logger, kubeconfig)
kClient, mClient, err := k8sutil.GetClientSets(kubeconfig)
if err != nil {
logger.Error("error while getting kubeconfig", "err", err)
return err
}

kclient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
logger.Error("error while creating k8s client", "err", err)
return err
}

mclient, err := monitoringclient.NewForConfig(restConfig)
if err != nil {
logger.Error("error while creating Prometheus Operator client", "err", err)
logger.Error("error while getting client sets", "err", err)
return err
}

Expand All @@ -73,7 +60,7 @@ func runServiceMonitor(_ *cobra.Command, _ []string) error {
return errors.New("service name is required")
}

err = createFromService(context.Background(), kclient, mclient, namespace, serviceName, port)
err = createFromService(context.Background(), kClient, mClient, namespace, serviceName, port)
if err != nil {
logger.Error("error while creating service monitor", "err", err)
return err
Expand Down
22 changes: 7 additions & 15 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,41 @@ func runStack(cmd *cobra.Command, _ []string) error {

logger.Info(version)

//TODO(nicolastakashi): Replace it when the PR #6623 is merged
restConfig, err := k8sutil.GetRestConfig(logger, kubeconfig)
kClient, mClient, err := k8sutil.GetClientSets(kubeconfig)
if err != nil {
logger.Error("error while getting kubeconfig", "error", err)
logger.Error("error while getting client sets", "err", err)
return err
}

kclient, err := kubernetes.NewForConfig(restConfig)
restConfig, err := k8sutil.GetRestConfig(kubeconfig)
if err != nil {
logger.Error("error while creating k8s client", "error", err)
logger.Error("error while getting kubeconfig", "error", err)
return err
}

kdynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
logger.Error("error while creating dynamic client", "error", err)
return err
}

mclient, err := monitoringclient.NewForConfig(restConfig)
if err != nil {
logger.Error("error while creating Prometheus Operator client", "error", err)
return err
}

gitHubClient := github.NewClient(nil)

if err := installCRDs(cmd.Context(), logger, version, kdynamicClient, gitHubClient); err != nil {
logger.Error("error while installing CRDs", "error", err)
return err
}

if err := createPrometheusOperator(cmd.Context(), kclient, mclient, metav1.NamespaceDefault, version); err != nil {
if err := createPrometheusOperator(cmd.Context(), kClient, mClient, metav1.NamespaceDefault, version); err != nil {
logger.Error("error while creating Prometheus Operator", "error", err)
return err
}

if err := createPrometheus(cmd.Context(), kclient, mclient, metav1.NamespaceDefault); err != nil {
if err := createPrometheus(cmd.Context(), kClient, mClient, metav1.NamespaceDefault); err != nil {
logger.Error("error while creating Prometheus", "error", err)
return err
}

if err := createAlertManager(cmd.Context(), kclient, mclient, metav1.NamespaceDefault); err != nil {
if err := createAlertManager(cmd.Context(), kClient, mClient, metav1.NamespaceDefault); err != nil {
logger.Error("error while creating AlertManager", "error", err)
return err
}
Expand Down
29 changes: 24 additions & 5 deletions internal/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import (

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
apiv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -53,22 +55,20 @@ func getKubeConfig() (string, error) {
return kubeConfig, nil
}

func GetRestConfig(logger *slog.Logger, kubeConfig string) (*rest.Config, error) {
func GetRestConfig(kubeConfig string) (*rest.Config, error) {
var config *rest.Config
var err error

if kubeConfig == "" {
kubeConfig, err = getKubeConfig()
if err != nil {
logger.With("error", err.Error()).Error("error while getting kubeconfig")
return nil, err
return nil, fmt.Errorf("error while getting kubeconfig: %v", err)
}
}

config, err = clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
logger.With("error", err.Error()).Error("error while creating k8s client config")
return nil, err
return nil, fmt.Errorf("error while creating k8s client config: %v", err)
}

return config, nil
Expand Down Expand Up @@ -100,3 +100,22 @@ func CrdDeserilezer(logger *slog.Logger, reader io.ReadCloser) (runtime.Object,

return obj, nil
}

func GetClientSets(kubeconfig string) (*kubernetes.Clientset, *monitoringclient.Clientset, error) {
restConfig, err := GetRestConfig(kubeconfig)
if err != nil {
return nil, nil, fmt.Errorf("error while getting k8s client config: %v", err)

}

kclient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, nil, fmt.Errorf("error while creating k8s client: %v", err)
}

mclient, err := monitoringclient.NewForConfig(restConfig)
if err != nil {
return nil, nil, fmt.Errorf("error while creating Prometheus Operator client: %v", err)
}
return kclient, mclient, nil
}