Skip to content

Commit

Permalink
[CHORE] starting operator deployment
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 May 27, 2024
1 parent a45a5b9 commit 537dbea
Show file tree
Hide file tree
Showing 6 changed files with 616 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func Execute() {
}
}

var kubeconfig string

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
Expand All @@ -39,5 +41,6 @@ func init() {

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "path to the kubeconfig file, defaults to $KUBECONFIG")
log.RegisterFlags(rootCmd.PersistentFlags())
}
97 changes: 95 additions & 2 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,64 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"context"
"fmt"
"log/slog"
"os"

"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/client-go/kubernetes"
)

// stackCmd represents the stack command.
var stackCmd = &cobra.Command{
Use: "stack",
Short: "create a stack of Prometheus Operator resources.",
Long: `create a stack of Prometheus Operator resources.`,
Run: func(_ *cobra.Command, _ []string) {
Run: func(cmd *cobra.Command, _ []string) {
logger, err := log.NewLogger()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

logger.Info("stack command called")
//TODO(nicolastakashi): Replace it when the PR #6623 is merged
restConfig, err := k8sutil.GetRestConfig(logger, kubeconfig)
if err != nil {
logger.With("error", err.Error()).Error("error while getting kubeconfig")
os.Exit(1)
}

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

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

if err != nil {
logger.With("error", err.Error()).Error("error while getting kubeconfig")
os.Exit(1)
}

cmd.Context()

if err := createPrometheusOperator(cmd.Context(), logger, kclient, mclient, metav1.NamespaceDefault, "0.73.2"); err != nil {
logger.With("error", err.Error()).Error("error while creating Prometheus Operator")
os.Exit(1)
}

logger.Info("Prometheus Operator stack created successfully.")
},
}

Expand All @@ -40,3 +78,58 @@ func init() {
// is called directly, e.g.:
// stackCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func createPrometheusOperator(
ctx context.Context,
logger *slog.Logger,
k8sClient *kubernetes.Clientset,
poClient *monitoringclient.Clientset,
namespace, version string) error {
manifests := builder.NewOperator(namespace, version).
WithServiceAccount().
WithClusterRole().
WithClusterRoleBinding().
WithService().
WithServiceMonitor().
WithDeployment().
Build()

_, err := k8sClient.CoreV1().ServiceAccounts(namespace).Create(ctx, manifests.ServiceAccount, metav1.CreateOptions{})
if err != nil {
logger.With("error", err.Error()).Error("error while creating ServiceAccount")
return err
}

_, err = k8sClient.RbacV1().ClusterRoles().Create(ctx, manifests.ClusterRole, metav1.CreateOptions{})
if err != nil {
logger.With("error", err.Error()).Error("error while creating ClusterRole")
return err
}

_, err = k8sClient.RbacV1().ClusterRoleBindings().Create(ctx, manifests.ClusterRoleBinding, metav1.CreateOptions{})
if err != nil {
logger.With("error", err.Error()).Error("error while creating ClusterRoleBinding")
return err
}

_, err = k8sClient.CoreV1().Services(namespace).Create(ctx, manifests.Service, metav1.CreateOptions{})
if err != nil {
logger.With("error", err.Error()).Error("error while creating Service")
return err
}

_, err = poClient.MonitoringV1().ServiceMonitors(namespace).Create(ctx, manifests.ServiceMonitor, metav1.CreateOptions{})
if err != nil {
logger.With("error", err.Error()).Error("error while creating ServiceMonitor")
return err
}

_, err = k8sClient.AppsV1().Deployments(namespace).Create(ctx, manifests.Deployment, metav1.CreateOptions{})
if err != nil {
logger.With("error", err.Error()).Error("error while creating Deployment")
return err
}

logger.Debug("Prometheus Operator manifest created successfully.")
return nil
}
49 changes: 48 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,56 @@ module github.com/prometheus-operator/poctl

go 1.22.0

require k8s.io/client-go v0.30.1

require (
k8s.io/apiextensions-apiserver v0.30.1 // indirect
sigs.k8s.io/controller-runtime v0.18.2 // indirect
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.74.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240322212309-b815d8309940 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

require github.com/inconshreveable/mousetrap v1.1.0 // indirect
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/client v0.74.0
)
Loading

0 comments on commit 537dbea

Please sign in to comment.