Skip to content

Commit

Permalink
[REFACTOR] clientset init
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Takashi <nicolas.tcs@hotmail.com>
  • Loading branch information
nicolastakashi committed Jul 9, 2024
1 parent 2231184 commit 3e7ade4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 64 deletions.
7 changes: 1 addition & 6 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,11 @@ func run(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("namespace is required")
}

kClient, mClient, err := k8sutil.GetClientSets(kubeconfig)
clientSets, err := k8sutil.GetClientSets(kubeconfig)
if err != nil {
return fmt.Errorf("error while getting clientsets: %v", err)
}

clientSets := k8sutil.ClientSets{
KClient: kClient,
MClient: mClient,
}

switch AnalyzeKind(strings.ToLower(analyzerFlags.Kind)) {
case ServiceMonitor:
return analyzers.RunServiceMonitorAnalyzer(cmd.Context(), clientSets, analyzerFlags.Name, analyzerFlags.Namespace)
Expand Down
13 changes: 5 additions & 8 deletions cmd/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import (
"github.com/prometheus-operator/poctl/internal/k8sutil"
"github.com/prometheus-operator/poctl/internal/log"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
applyConfigMetav1 "k8s.io/client-go/applyconfigurations/meta/v1"

"k8s.io/client-go/kubernetes"
"k8s.io/utils/ptr"
)

Expand All @@ -50,7 +48,7 @@ func runServiceMonitor(_ *cobra.Command, _ []string) error {
return err
}

kClient, mClient, err := k8sutil.GetClientSets(kubeconfig)
clientSets, err := k8sutil.GetClientSets(kubeconfig)
if err != nil {
logger.Error("error while getting client sets", "err", err)
return err
Expand All @@ -61,7 +59,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(), clientSets, namespace, serviceName, port)
if err != nil {
logger.Error("error while creating service monitor", "err", err)
return err
Expand All @@ -72,13 +70,12 @@ func runServiceMonitor(_ *cobra.Command, _ []string) error {

func createFromService(
ctx context.Context,
k8sClient *kubernetes.Clientset,
mClient *monitoringclient.Clientset,
clientSets *k8sutil.ClientSets,
namespace string,
serviceName string,
port string) error {

service, err := k8sClient.CoreV1().Services(namespace).Get(ctx, serviceName, metav1.GetOptions{})
service, err := clientSets.KClient.CoreV1().Services(namespace).Get(ctx, serviceName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("error while getting service %s: %v", serviceName, err)
}
Expand Down Expand Up @@ -111,7 +108,7 @@ func createFromService(
})
}

_, err = mClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, svcMonitor, k8sutil.ApplyOption)
_, err = clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, svcMonitor, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating service monitor %s: %v", serviceName, err)
}
Expand Down
69 changes: 26 additions & 43 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ import (
"github.com/prometheus-operator/poctl/internal/builder"
"github.com/prometheus-operator/poctl/internal/k8sutil"
"github.com/prometheus-operator/poctl/internal/log"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)

var (
Expand Down Expand Up @@ -84,41 +81,30 @@ func runStack(cmd *cobra.Command, _ []string) error {

logger.Info(version)

kClient, mClient, err := k8sutil.GetClientSets(kubeconfig)
clientSets, err := k8sutil.GetClientSets(kubeconfig)
if err != nil {
logger.Error("error while getting client sets", "err", err)
return err
}

restConfig, err := k8sutil.GetRestConfig(kubeconfig)
if err != nil {
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
}

gitHubClient := github.NewClient(nil)

if err := installCRDs(cmd.Context(), logger, version, kdynamicClient, gitHubClient); err != nil {
if err := installCRDs(cmd.Context(), logger, version, clientSets, 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(), clientSets, 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(), clientSets, 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(), clientSets, metav1.NamespaceDefault); err != nil {
logger.Error("error while creating AlertManager", "error", err)
return err
}
Expand All @@ -131,7 +117,7 @@ func installCRDs(
ctx context.Context,
logger *slog.Logger,
version string,
k8sClient *dynamic.DynamicClient,
clientSets *k8sutil.ClientSets,
gitHubClient *github.Client) error {

nodeResource := schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}
Expand Down Expand Up @@ -160,7 +146,7 @@ func installCRDs(
return fmt.Errorf("error while converting CRDs to Unstructured: %v", err)
}

_, err = k8sClient.Resource(nodeResource).Apply(ctx, fmt.Sprintf("%s.monitoring.coreos.com", crd), &unstructured.Unstructured{Object: unstructuredObj}, k8sutil.ApplyOption)
_, err = clientSets.DClient.Resource(nodeResource).Apply(ctx, fmt.Sprintf("%s.monitoring.coreos.com", crd), &unstructured.Unstructured{Object: unstructuredObj}, k8sutil.ApplyOption)

if err != nil {
return fmt.Errorf("error while applying CRD: %v", err)
Expand All @@ -174,8 +160,7 @@ func installCRDs(

func createPrometheusOperator(
ctx context.Context,
k8sClient *kubernetes.Clientset,
poClient *monitoringclient.Clientset,
clientSets *k8sutil.ClientSets,
namespace, version string) error {
manifests := builder.NewOperator(namespace, version).
WithServiceAccount().
Expand All @@ -186,32 +171,32 @@ func createPrometheusOperator(
WithDeployment().
Build()

_, err := k8sClient.CoreV1().ServiceAccounts(namespace).Apply(ctx, manifests.ServiceAccount, k8sutil.ApplyOption)
_, err := clientSets.KClient.CoreV1().ServiceAccounts(namespace).Apply(ctx, manifests.ServiceAccount, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ServiceAccount: %v", err)
}

_, err = k8sClient.RbacV1().ClusterRoles().Apply(ctx, manifests.ClusterRole, k8sutil.ApplyOption)
_, err = clientSets.KClient.RbacV1().ClusterRoles().Apply(ctx, manifests.ClusterRole, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ClusterRole: %v", err)
}

_, err = k8sClient.RbacV1().ClusterRoleBindings().Apply(ctx, manifests.ClusterRoleBinding, k8sutil.ApplyOption)
_, err = clientSets.KClient.RbacV1().ClusterRoleBindings().Apply(ctx, manifests.ClusterRoleBinding, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ClusterRoleBinding: %v", err)
}

_, err = k8sClient.CoreV1().Services(namespace).Apply(ctx, manifests.Service, k8sutil.ApplyOption)
_, err = clientSets.KClient.CoreV1().Services(namespace).Apply(ctx, manifests.Service, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating Service: %v", err)
}

_, err = poClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, manifests.ServiceMonitor, k8sutil.ApplyOption)
_, err = clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, manifests.ServiceMonitor, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ServiceMonitor: %v", err)
}

_, err = k8sClient.AppsV1().Deployments(namespace).Apply(ctx, manifests.Deployment, k8sutil.ApplyOption)
_, err = clientSets.KClient.AppsV1().Deployments(namespace).Apply(ctx, manifests.Deployment, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating Deployment: %v", err)
}
Expand All @@ -221,8 +206,7 @@ func createPrometheusOperator(

func createPrometheus(
ctx context.Context,
k8sClient *kubernetes.Clientset,
poClient *monitoringclient.Clientset,
clientSets *k8sutil.ClientSets,
namespace string) error {
manifests := builder.NewPrometheus(namespace).
WithServiceAccount().
Expand All @@ -233,32 +217,32 @@ func createPrometheus(
WithPrometheus().
Build()

_, err := k8sClient.CoreV1().ServiceAccounts(namespace).Apply(ctx, manifests.ServiceAccount, k8sutil.ApplyOption)
_, err := clientSets.KClient.CoreV1().ServiceAccounts(namespace).Apply(ctx, manifests.ServiceAccount, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ServiceAccount: %v", err)
}

_, err = k8sClient.RbacV1().ClusterRoles().Apply(ctx, manifests.ClusterRole, k8sutil.ApplyOption)
_, err = clientSets.KClient.RbacV1().ClusterRoles().Apply(ctx, manifests.ClusterRole, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ClusterRole: %v", err)
}

_, err = k8sClient.RbacV1().ClusterRoleBindings().Apply(ctx, manifests.ClusterRoleBinding, k8sutil.ApplyOption)
_, err = clientSets.KClient.RbacV1().ClusterRoleBindings().Apply(ctx, manifests.ClusterRoleBinding, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ClusterRoleBinding: %v", err)
}

_, err = poClient.MonitoringV1().Prometheuses(namespace).Apply(ctx, manifests.Prometheus, k8sutil.ApplyOption)
_, err = clientSets.MClient.MonitoringV1().Prometheuses(namespace).Apply(ctx, manifests.Prometheus, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating Prometheus: %v", err)
}

_, err = k8sClient.CoreV1().Services(namespace).Apply(ctx, manifests.Service, k8sutil.ApplyOption)
_, err = clientSets.KClient.CoreV1().Services(namespace).Apply(ctx, manifests.Service, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating Service: %v", err)
}

_, err = poClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, manifests.ServiceMonitor, k8sutil.ApplyOption)
_, err = clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, manifests.ServiceMonitor, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ServiceMonitor: %v", err)
}
Expand All @@ -268,8 +252,7 @@ func createPrometheus(

func createAlertManager(
ctx context.Context,
k8sClient *kubernetes.Clientset,
poClient *monitoringclient.Clientset,
clientSets *k8sutil.ClientSets,
namespace string) error {
manifests := builder.NewAlertManager(namespace).
WithServiceAccount().
Expand All @@ -278,22 +261,22 @@ func createAlertManager(
WithServiceMonitor().
Build()

_, err := k8sClient.CoreV1().ServiceAccounts(namespace).Apply(ctx, manifests.ServiceAccount, k8sutil.ApplyOption)
_, err := clientSets.KClient.CoreV1().ServiceAccounts(namespace).Apply(ctx, manifests.ServiceAccount, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ServiceAccount: %v", err)
}

_, err = poClient.MonitoringV1().Alertmanagers(namespace).Apply(ctx, manifests.AlertManager, k8sutil.ApplyOption)
_, err = clientSets.MClient.MonitoringV1().Alertmanagers(namespace).Apply(ctx, manifests.AlertManager, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating AlertManager: %v", err)
}

_, err = k8sClient.CoreV1().Services(namespace).Apply(ctx, manifests.Service, k8sutil.ApplyOption)
_, err = clientSets.KClient.CoreV1().Services(namespace).Apply(ctx, manifests.Service, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating Service: %v", err)
}

_, err = poClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, manifests.ServiceMonitor, k8sutil.ApplyOption)
_, err = clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).Apply(ctx, manifests.ServiceMonitor, k8sutil.ApplyOption)
if err != nil {
return fmt.Errorf("error while creating ServiceMonitor: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/analyzers/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func RunServiceMonitorAnalyzer(ctx context.Context, clientSets k8sutil.ClientSets, name, namespace string) error {
func RunServiceMonitorAnalyzer(ctx context.Context, clientSets *k8sutil.ClientSets, name, namespace string) error {
serviceMonitor, err := clientSets.MClient.MonitoringV1().ServiceMonitors(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion internal/analyzers/servicemonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestServiceMonitorAnalyzer(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
clientSets := tc.getMockedClientSets(tc)
err := RunServiceMonitorAnalyzer(context.Background(), clientSets, tc.name, tc.namespace)
err := RunServiceMonitorAnalyzer(context.Background(), &clientSets, tc.name, tc.namespace)
if tc.shouldFail {
assert.Error(t, err)
} else {
Expand Down
22 changes: 17 additions & 5 deletions internal/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -104,23 +105,34 @@ func CrdDeserilezer(logger *slog.Logger, reader io.ReadCloser) (runtime.Object,
type ClientSets struct {
KClient kubernetes.Interface
MClient monitoringclient.Interface
DClient dynamic.Interface
}

func GetClientSets(kubeconfig string) (*kubernetes.Clientset, *monitoringclient.Clientset, error) {
func GetClientSets(kubeconfig string) (*ClientSets, error) {
restConfig, err := GetRestConfig(kubeconfig)
if err != nil {
return nil, nil, fmt.Errorf("error while getting k8s client config: %v", err)
return 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)
return 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 nil, fmt.Errorf("error while creating Prometheus Operator client: %v", err)
}
return kclient, mclient, nil

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

return &ClientSets{
KClient: kclient,
MClient: mclient,
DClient: kdynamicClient,
}, nil
}

0 comments on commit 3e7ade4

Please sign in to comment.