Skip to content

Commit

Permalink
chore: refine oteld controller
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Oct 23, 2023
1 parent 3c4035b commit 07e0a0a
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 297 deletions.
37 changes: 19 additions & 18 deletions controllers/monitor/oteld_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type OTeldReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Config *monitortypes.Config

// sub-controllers
tasks []monitortypes.ReconcileTask
Expand Down Expand Up @@ -99,8 +98,11 @@ func (o *OTeldReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

func (o *OTeldReconciler) runTasks(reqCtx intctrlutil.RequestCtx, oteld *monitorv1alpha1.OTeld) error {
oteldCtx := monitortypes.ReconcileCtx{
RequestCtx: reqCtx,
OteldCfgRef: &monitortypes.OteldCfgRef{},
RequestCtx: reqCtx,
OTeld: oteld,
OteldCfgRef: monitortypes.OteldCfgRef{
OTeldAgentConfig: &monitortypes.OTeldAgentConfig{},
},
}

for _, task := range o.tasks {
Expand All @@ -116,21 +118,20 @@ func New(params monitortypes.OTeldParams) *OTeldReconciler {
Client: params.Client,
Scheme: params.Scheme,
Recorder: params.Recorder,
// Config: config,

// sub-controllers
tasks: []monitortypes.ReconcileTask{
monitortypes.NewReconcileTask(monitorreconsile.OTeldName, monitortypes.WithReconcileOption(monitorreconsile.OTeld, params)),
monitortypes.NewReconcileTask(monitorreconsile.OteldSecretName, monitortypes.WithReconcileOption(monitorreconsile.Secret, params)),
monitortypes.NewReconcileTask(monitorreconsile.OteldConfigMapNamePattern, monitortypes.WithReconcileOption(monitorreconsile.ConfigMap, params)),
monitortypes.NewReconcileTask(monitorreconsile.OteldServiceNamePattern, monitortypes.WithReconcileOption(monitorreconsile.Service, params)),
monitortypes.NewReconcileTask(monitorreconsile.OTeldAPIServerName, monitortypes.WithReconcileOption(monitorreconsile.Deployment, params)),
monitortypes.NewReconcileTask(monitorreconsile.OTeldAgentName, monitortypes.WithReconcileOption(monitorreconsile.OTeldAgent, params)),
monitortypes.NewReconcileTask(monitorreconsile.PrometheusName, monitortypes.WithReconcileOption(monitorreconsile.Prometheus, params)),
monitortypes.NewReconcileTask(monitorreconsile.LokiName, monitortypes.WithReconcileOption(monitorreconsile.Loki, params)),
monitortypes.NewReconcileTask(monitorreconsile.GrafnaName, monitortypes.WithReconcileOption(monitorreconsile.Grafana, params)),
monitortypes.NewReconcileTask(monitorreconsile.VMAgentName, monitortypes.WithReconcileOption(monitorreconsile.VMAgent, params)),
},
}

// sub-controllers
reconcile.tasks = []monitortypes.ReconcileTask{
monitortypes.NewReconcileTask(monitorreconsile.OTeldName, monitortypes.WithReconcileOption(monitorreconsile.OTeld, params)),
monitortypes.NewReconcileTask(monitorreconsile.OteldSecretName, monitortypes.WithReconcileOption(monitorreconsile.Secret, params)),
monitortypes.NewReconcileTask(monitorreconsile.OteldConfigMapNamePattern, monitortypes.WithReconcileOption(monitorreconsile.ConfigMap, params)),
monitortypes.NewReconcileTask(monitorreconsile.OteldServiceNamePattern, monitortypes.WithReconcileOption(monitorreconsile.Service, params)),
monitortypes.NewReconcileTask(monitorreconsile.OTeldAPIServerName, monitortypes.WithReconcileOption(monitorreconsile.Deployment, params)),
monitortypes.NewReconcileTask(monitorreconsile.OTeldAgentName, monitortypes.WithReconcileOption(monitorreconsile.OTeldAgent, params)),
monitortypes.NewReconcileTask(monitorreconsile.PrometheusName, monitortypes.WithReconcileOption(monitorreconsile.Prometheus, params)),
monitortypes.NewReconcileTask(monitorreconsile.LokiName, monitortypes.WithReconcileOption(monitorreconsile.Loki, params)),
monitortypes.NewReconcileTask(monitorreconsile.GrafnaName, monitortypes.WithReconcileOption(monitorreconsile.Grafana, params)),
monitortypes.NewReconcileTask(monitorreconsile.VMAgentName, monitortypes.WithReconcileOption(monitorreconsile.VMAgent, params)),
}

return &reconcile
Expand Down
17 changes: 7 additions & 10 deletions controllers/monitor/reconcile/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ import (
)

func ConfigMap(reqCtx types.ReconcileCtx, params types.OTeldParams) error {
desired := []*corev1.ConfigMap{}
desired := make([]*corev1.ConfigMap, 0)

cg := reqCtx.GetConfigGenerator()
if cg == nil {
return fmt.Errorf("config generator is nil")
}
if reqCtx.GetOteldInstance(monitorv1alpha1.ModeDaemonSet) != nil {
configmap, _ := buildConfigMapForOteld(reqCtx.Config, reqCtx.GetOteldInstance(monitorv1alpha1.ModeDaemonSet), reqCtx.Namespace, reqCtx.GetExporters(), cg)
cg := types.NewConfigGenerator()
if daemont := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDaemonSet); daemont != nil {
configmap, _ := buildConfigMapForOteld(daemont, reqCtx.OTeld.Namespace, reqCtx.OteldCfgRef.Exporters, cg)
if configmap != nil {
desired = append(desired, configmap)
}
}

if reqCtx.GetOteldInstance(monitorv1alpha1.ModeDeployment) != nil {
configmap, _ := buildConfigMapForOteld(reqCtx.Config, reqCtx.GetOteldInstance(monitorv1alpha1.ModeDeployment), reqCtx.Namespace, reqCtx.GetExporters(), cg)
if deployment := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDeployment); deployment != nil {
configmap, _ := buildConfigMapForOteld(deployment, reqCtx.OTeld.Namespace, reqCtx.OteldCfgRef.Exporters, cg)
if configmap != nil {
desired = append(desired, configmap)
}
Expand Down Expand Up @@ -114,7 +111,7 @@ func expectedConfigMap(reqCtx types.ReconcileCtx, params types.OTeldParams, desi

func deleteConfigMap(reqCtx types.ReconcileCtx, params types.OTeldParams, desired []*corev1.ConfigMap) error {
listopts := []client.ListOption{
client.InNamespace(reqCtx.Namespace),
client.InNamespace(reqCtx.OTeld.Namespace),
client.MatchingLabels(map[string]string{
constant.AppManagedByLabelKey: constant.AppName,
constant.AppNameLabelKey: OTeldName,
Expand Down
8 changes: 4 additions & 4 deletions controllers/monitor/reconcile/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func Deployment(reqCtx monitortypes.ReconcileCtx, params monitortypes.OTeldParam
namespace = viper.GetString(constant.MonitorNamespaceEnvName)
)

instance := reqCtx.GetOteldInstance(monitorv1alpha1.ModeDeployment)
instance := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDeployment)

oteldDeployment := buildDeploymentForOteld(reqCtx.Config, instance, namespace, OTeldName)
oteldDeployment := buildDeploymentForOteld(instance, namespace, OTeldName)

existingDeployment := &appsv1.Deployment{}
err := k8sClient.Get(reqCtx.Ctx, client.ObjectKey{Name: OTeldName, Namespace: namespace}, existingDeployment)
Expand Down Expand Up @@ -77,7 +77,7 @@ func Deployment(reqCtx monitortypes.ReconcileCtx, params monitortypes.OTeldParam
return k8sClient.Update(reqCtx.Ctx, oteldDeployment)
}

func buildDeploymentForOteld(config *monitortypes.Config, instance *monitortypes.OteldInstance, namespace, name string) *appsv1.Deployment {
func buildDeploymentForOteld(instance *monitortypes.OteldInstance, namespace, name string) *appsv1.Deployment {
if instance == nil || instance.Oteld == nil {
return nil
}
Expand All @@ -94,7 +94,7 @@ func buildDeploymentForOteld(config *monitortypes.Config, instance *monitortypes
}

template := instance.Oteld
podSpec := buildPodSpecForOteld(config, template)
podSpec := buildPodSpecForOteld(template)

podBuilder := builder.NewPodBuilder("", "").
AddLabelsInMap(commonLabels)
Expand Down
45 changes: 10 additions & 35 deletions controllers/monitor/reconcile/oteld.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package reconcile
import (
"github.com/apecloud/kubeblocks/apis/monitor/v1alpha1"
monitortype "github.com/apecloud/kubeblocks/controllers/monitor/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const OTeldName = "apecloudoteld"
Expand All @@ -33,64 +34,38 @@ func OTeld(reqCtx monitortype.ReconcileCtx, params monitortype.OTeldParams) erro
k8sClient = params.Client
)

reqCtx.SetConfigGenerator(monitortype.NewConfigGenerator())

exporter := monitortype.Exporters{}
metricsExporters := &v1alpha1.MetricsExporterSinkList{}
if err := k8sClient.List(ctx, metricsExporters); err != nil {
return err
}
exporter.Metricsexporter = metricsExporters.Items
exporter.MetricsExporter = metricsExporters.Items

logsExporters := &v1alpha1.LogsExporterSinkList{}
if err := k8sClient.List(ctx, logsExporters); err != nil {
return err
}
exporter.Logsexporter = logsExporters.Items

reqCtx.SetExporters(&exporter)
exporter.LogsExporter = logsExporters.Items

datasources := &v1alpha1.CollectorDataSourceList{}
if err := k8sClient.List(ctx, datasources); err != nil {
if err := k8sClient.List(ctx, datasources, client.InNamespace(reqCtx.OTeld.GetNamespace())); err != nil {
return err
}

oteldTemplates := &v1alpha1.OTeldList{}
if err := k8sClient.List(ctx, oteldTemplates); err != nil {
return err
}
instanceMap, err := buildOteldInstance(datasources, oteldTemplates)

for i := 0; i < len(oteldTemplates.Items); i++ {
template := oteldTemplates.Items[i]
instance := instanceMap[template.Spec.Mode]
if instance == nil {
continue
}
instance.Oteld = &template
}

reqCtx.SetOteldInstanceMap(instanceMap)
instanceMap, err := BuildInstanceMapForPipline(datasources, reqCtx.OTeld)
if err != nil {
return err
}

if err = reqCtx.VerifyOteldInstance(metricsExporters, logsExporters); err != nil {
reqCtx.OteldCfgRef.Exporters = &exporter
reqCtx.OteldCfgRef.OteldInstanceMap = instanceMap
if err = monitortype.VerifyOteldInstance(metricsExporters, logsExporters, instanceMap); err != nil {
return err
}
return err
}

func buildOteldInstance(datasources *v1alpha1.CollectorDataSourceList, templates *v1alpha1.OTeldList) (map[v1alpha1.Mode]*monitortype.OteldInstance, error) {
instanceMap, err := BuildInstanceMapForPipline(datasources)
if err != nil {
return nil, err
}

return instanceMap, nil
}

func BuildInstanceMapForPipline(datasources *v1alpha1.CollectorDataSourceList) (map[v1alpha1.Mode]*monitortype.OteldInstance, error) {
func BuildInstanceMapForPipline(datasources *v1alpha1.CollectorDataSourceList, oteld *v1alpha1.OTeld) (map[v1alpha1.Mode]*monitortype.OteldInstance, error) {
instanceMap := map[v1alpha1.Mode]*monitortype.OteldInstance{}
for _, dataSource := range datasources.Items {
mode := dataSource.Spec.Mode
Expand All @@ -99,7 +74,7 @@ func BuildInstanceMapForPipline(datasources *v1alpha1.CollectorDataSourceList) (
}
oteldInstance, ok := instanceMap[mode]
if !ok {
oteldInstance = monitortype.NewOteldInstance()
oteldInstance = monitortype.NewOteldInstance(oteld)
}
switch dataSource.Spec.Type {
case v1alpha1.MetricsDatasourceType:
Expand Down
8 changes: 4 additions & 4 deletions controllers/monitor/reconcile/oteld_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func OTeldAgent(reqCtx types.ReconcileCtx, params types.OTeldParams) error {
namespace = viper.GetString(constant.MonitorNamespaceEnvName)
)

instance := reqCtx.GetOteldInstance(monitorv1alpha1.ModeDaemonSet)
instance := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDaemonSet)

oteldDaemonset := buildDaemonsetForOteld(reqCtx.Config, instance, namespace, OTeldName)
oteldDaemonset := buildDaemonsetForOteld(instance, namespace, OTeldName)

existingDaemonset := &appsv1.DaemonSet{}
err := k8sClient.Get(reqCtx.Ctx, client.ObjectKey{Name: OTeldName, Namespace: namespace}, existingDaemonset)
Expand All @@ -70,7 +70,7 @@ func OTeldAgent(reqCtx types.ReconcileCtx, params types.OTeldParams) error {
return k8sClient.Update(reqCtx.Ctx, oteldDaemonset)
}

func buildDaemonsetForOteld(config *types.Config, instance *types.OteldInstance, namespace string, name string) *appsv1.DaemonSet {
func buildDaemonsetForOteld(instance *types.OteldInstance, namespace string, name string) *appsv1.DaemonSet {
if instance == nil || instance.Oteld == nil {
return nil
}
Expand All @@ -87,7 +87,7 @@ func buildDaemonsetForOteld(config *types.Config, instance *types.OteldInstance,
}

template := instance.Oteld
podSpec := buildPodSpecForOteld(config, template)
podSpec := buildPodSpecForOteld(template)

podBuilder := builder.NewPodBuilder("", "").
AddLabelsInMap(commonLabels)
Expand Down
4 changes: 1 addition & 3 deletions controllers/monitor/reconcile/oteld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ import (

var _ = Describe("monitor_controller", func() {
var (
config *types.Config
instance *types.OteldInstance
)

BeforeEach(func() {
config = &types.Config{}
instance = &types.OteldInstance{}
})

It("should generate config correctly from config yaml", func() {
Eventually(func(g Gomega) {
otel := buildDaemonsetForOteld(config, instance, "test", "test")
otel := buildDaemonsetForOteld(instance, "test", "test")
g.Expect(otel).ShouldNot(BeNil())
g.Expect(otel.Name).Should(Equal("test"))
}).Should(Succeed())
Expand Down
19 changes: 8 additions & 11 deletions controllers/monitor/reconcile/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,20 @@ import (
const OteldSecretName = "oteld-secret"

func Secret(reqCtx types.ReconcileCtx, params types.OTeldParams) error {
desired := []*corev1.Secret{}
desired := make([]*corev1.Secret, 0)

cg := reqCtx.GetConfigGenerator()
if cg == nil {
return fmt.Errorf("config generator is nil")
}
daemonsetInstance := reqCtx.GetOteldInstance(monitorv1alpha1.ModeDaemonSet)
if daemonsetInstance != nil {
secret, _ := buildSecretForOteld(reqCtx.Config, daemonsetInstance, reqCtx.Namespace, reqCtx.GetExporters(), cg)
cg := types.NewConfigGenerator()
daemonSetInstance := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDaemonSet)
if daemonSetInstance != nil {
secret, _ := buildSecretForOteld(daemonSetInstance, reqCtx.OTeld.Namespace, reqCtx.OteldCfgRef.Exporters, cg)
if secret != nil {
desired = append(desired, secret)
}
}

deploymentInstance := reqCtx.GetOteldInstance(monitorv1alpha1.ModeDeployment)
deploymentInstance := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDeployment)
if deploymentInstance != nil {
secret, _ := buildSecretForOteld(reqCtx.Config, deploymentInstance, reqCtx.Namespace, reqCtx.GetExporters(), cg)
secret, _ := buildSecretForOteld(deploymentInstance, reqCtx.OTeld.Namespace, reqCtx.OteldCfgRef.Exporters, cg)
if secret != nil {
desired = append(desired, secret)
}
Expand Down Expand Up @@ -118,7 +115,7 @@ func expectedSecret(reqCtx types.ReconcileCtx, params types.OTeldParams, desired

func deleteSecret(reqCtx types.ReconcileCtx, params types.OTeldParams, desired []*corev1.Secret) error {
listopts := []client.ListOption{
client.InNamespace(reqCtx.Namespace),
client.InNamespace(reqCtx.OTeld.Namespace),
client.MatchingLabels(map[string]string{
constant.AppManagedByLabelKey: constant.AppName,
constant.AppNameLabelKey: OTeldName,
Expand Down
46 changes: 5 additions & 41 deletions controllers/monitor/reconcile/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,28 @@ import (

monitorv1alpha1 "github.com/apecloud/kubeblocks/apis/monitor/v1alpha1"
"github.com/apecloud/kubeblocks/controllers/monitor/types"
"github.com/apecloud/kubeblocks/pkg/constant"
)

func Service(reqCtx types.ReconcileCtx, params types.OTeldParams) error {
desired := make([]*corev1.Service, 0)

if daemon := reqCtx.GetOteldInstance(monitorv1alpha1.ModeDaemonSet); daemon != nil {
svc, err := buildSvcForOtel(reqCtx.Config, daemon.Oteld, reqCtx.Namespace)
if daemon := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDaemonSet); daemon != nil {
svc, err := buildSvcForOtel(daemon.Oteld, reqCtx.OTeld.Namespace)
if err != nil {
return err
}
if svc != nil {
desired = append(desired, svc)
}
}
if deploy := reqCtx.GetOteldInstance(monitorv1alpha1.ModeDeployment); deploy != nil {
svc, _ := buildSvcForOtel(reqCtx.Config, deploy.Oteld, reqCtx.Namespace)
if deploy := reqCtx.OteldCfgRef.GetOteldInstance(monitorv1alpha1.ModeDeployment); deploy != nil {
svc, _ := buildSvcForOtel(deploy.Oteld, reqCtx.OTeld.Namespace)
if svc != nil {
desired = append(desired, svc)
}
}

if err := expectedService(reqCtx, params, desired); err != nil {
return err
}
return deleteService(reqCtx, params, desired)
return expectedService(reqCtx, params, desired)
}

func expectedService(reqCtx types.ReconcileCtx, params types.OTeldParams, desired []*corev1.Service) error {
Expand Down Expand Up @@ -103,35 +99,3 @@ func expectedService(reqCtx types.ReconcileCtx, params types.OTeldParams, desire
}
return nil
}

func deleteService(reqCtx types.ReconcileCtx, params types.OTeldParams, desired []*corev1.Service) error {
listopts := []client.ListOption{
client.InNamespace(reqCtx.Namespace),
client.MatchingLabels(map[string]string{
constant.AppManagedByLabelKey: constant.AppName,
constant.AppNameLabelKey: OTeldName,
}),
}

serviceList := &corev1.ServiceList{}
if params.Client.List(reqCtx.Ctx, serviceList, listopts...) != nil {
return nil
}

for _, configMap := range serviceList.Items {
isdel := true
for _, keep := range desired {
if keep.Name == configMap.Name && keep.Namespace == configMap.Namespace {
isdel = false
break
}
}

if isdel {
if err := params.Client.Delete(reqCtx.Ctx, &configMap); err != nil {
return fmt.Errorf("failed to delete: %w", err)
}
}
}
return nil
}
Loading

0 comments on commit 07e0a0a

Please sign in to comment.