github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/application/applications/logspersisting/logs/logs.go (about)

     1  package logs
     2  
     3  import (
     4  	"github.com/caos/orbos/internal/operator/boom/application/applications/boom"
     5  	"github.com/caos/orbos/internal/operator/boom/application/applications/database"
     6  	"github.com/caos/orbos/internal/operator/boom/application/applications/networking"
     7  	"github.com/caos/orbos/internal/operator/boom/application/applications/orbiter"
     8  	"github.com/caos/orbos/internal/operator/boom/application/applications/zitadel"
     9  	"strings"
    10  
    11  	toolsetslatest "github.com/caos/orbos/internal/operator/boom/api/latest"
    12  	amlogs "github.com/caos/orbos/internal/operator/boom/application/applications/apigateway/logs"
    13  
    14  	ksmlogs "github.com/caos/orbos/internal/operator/boom/application/applications/kubemetricsexporter/logs"
    15  	"github.com/caos/orbos/internal/operator/boom/application/applications/logcollection/logging"
    16  	lologs "github.com/caos/orbos/internal/operator/boom/application/applications/logcollection/logs"
    17  	"github.com/caos/orbos/internal/operator/boom/application/applications/logspersisting/info"
    18  	pologs "github.com/caos/orbos/internal/operator/boom/application/applications/metriccollection/logs"
    19  	plogs "github.com/caos/orbos/internal/operator/boom/application/applications/metricspersisting/logs"
    20  	mslogs "github.com/caos/orbos/internal/operator/boom/application/applications/metricsserver/logs"
    21  	glogs "github.com/caos/orbos/internal/operator/boom/application/applications/monitoring/logs"
    22  	pnelogs "github.com/caos/orbos/internal/operator/boom/application/applications/nodemetricsexporter/logs"
    23  	aglogs "github.com/caos/orbos/internal/operator/boom/application/applications/reconciling/logs"
    24  	pselogs "github.com/caos/orbos/internal/operator/boom/application/applications/systemdmetricsexporter/logs"
    25  	"github.com/caos/orbos/internal/operator/boom/labels"
    26  )
    27  
    28  func GetAllResources(
    29  	toolsetCRDSpec *toolsetslatest.ToolsetSpec,
    30  	withGrafanaCloud bool,
    31  	secretName string,
    32  	orb string,
    33  ) []interface{} {
    34  
    35  	if toolsetCRDSpec.LogCollection == nil || !toolsetCRDSpec.LogCollection.Deploy {
    36  		return nil
    37  	}
    38  
    39  	ret := []interface{}{logging.New(toolsetCRDSpec.LogCollection)}
    40  
    41  	outputNames := toolsetCRDSpec.LogCollection.Outputs
    42  	clusterOutputNames := toolsetCRDSpec.LogCollection.ClusterOutputs
    43  	var outputs []*logging.Output
    44  	// output to loki
    45  	if toolsetCRDSpec.LogsPersisting != nil && toolsetCRDSpec.LogsPersisting.Deploy {
    46  		lokiOutputNames, lokiClusterOutputNames, lokiOutputs := getLokiOutput(toolsetCRDSpec.LogsPersisting.ClusterOutput)
    47  		outputNames = append(outputNames, lokiOutputNames...)
    48  		clusterOutputNames = append(clusterOutputNames, lokiClusterOutputNames...)
    49  		outputs = append(outputs, lokiOutputs...)
    50  	}
    51  
    52  	if withGrafanaCloud {
    53  		lokiOutputNames, lokiClusterOutputNames, lokiOutputs := getCloudLokiOutput(toolsetCRDSpec.LogsPersisting.ClusterOutput, secretName, orb)
    54  		outputs = append(outputs, lokiOutputs...)
    55  
    56  		if len(lokiOutputNames) > 0 || len(lokiClusterOutputNames) > 0 {
    57  			flows := getAllCloudFlows(toolsetCRDSpec, lokiOutputNames, lokiClusterOutputNames)
    58  			for _, flow := range flows {
    59  				ret = append(ret, flow)
    60  			}
    61  		}
    62  	}
    63  
    64  	for _, output := range outputs {
    65  		ret = append(ret, output)
    66  	}
    67  
    68  	// add flows for each application
    69  	if len(outputNames) > 0 || len(clusterOutputNames) > 0 {
    70  		flows := getAllFlows(toolsetCRDSpec, outputNames, clusterOutputNames)
    71  		for _, flow := range flows {
    72  			ret = append(ret, flow)
    73  		}
    74  	}
    75  
    76  	return ret
    77  }
    78  
    79  func getAllCloudFlows(
    80  	toolsetCRDSpec *toolsetslatest.ToolsetSpec,
    81  	cloudOutputNames []string,
    82  	cloudClusterOutputs []string,
    83  ) []*logging.Flow {
    84  
    85  	if toolsetCRDSpec.LogsPersisting == nil || !toolsetCRDSpec.LogsPersisting.Deploy {
    86  		return []*logging.Flow{}
    87  	}
    88  
    89  	flows := make([]*logging.Flow, 0)
    90  	if toolsetCRDSpec.APIGateway != nil && toolsetCRDSpec.APIGateway.Deploy {
    91  		flows = append(flows, logging.NewFlow(amlogs.GetCloudFlow(cloudOutputNames, cloudClusterOutputs)))
    92  	}
    93  
    94  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Zitadel {
    95  		flows = append(flows, logging.NewFlow(zitadel.GetCloudZitadelFlow(cloudOutputNames, cloudClusterOutputs)))
    96  	}
    97  
    98  	return flows
    99  }
   100  
   101  func getAllFlows(
   102  	toolsetCRDSpec *toolsetslatest.ToolsetSpec,
   103  	outputNames []string,
   104  	clusterOutputs []string,
   105  ) []*logging.Flow {
   106  
   107  	if toolsetCRDSpec.LogsPersisting == nil || !toolsetCRDSpec.LogsPersisting.Deploy {
   108  		return []*logging.Flow{}
   109  	}
   110  
   111  	flows := make([]*logging.Flow, 0)
   112  	if toolsetCRDSpec.APIGateway != nil && toolsetCRDSpec.APIGateway.Deploy &&
   113  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Ambassador) {
   114  		flows = append(flows, logging.NewFlow(amlogs.GetFlow(outputNames, clusterOutputs)))
   115  	}
   116  
   117  	if toolsetCRDSpec.Monitoring != nil && toolsetCRDSpec.Monitoring.Deploy &&
   118  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Grafana) {
   119  		flows = append(flows, logging.NewFlow(glogs.GetFlow(outputNames, clusterOutputs)))
   120  	}
   121  
   122  	if toolsetCRDSpec.MetricCollection != nil && toolsetCRDSpec.MetricCollection.Deploy &&
   123  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.PrometheusOperator) {
   124  		flows = append(flows, logging.NewFlow(pologs.GetFlow(outputNames, clusterOutputs)))
   125  	}
   126  
   127  	if toolsetCRDSpec.NodeMetricsExporter != nil && toolsetCRDSpec.NodeMetricsExporter.Deploy &&
   128  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.PrometheusNodeExporter) {
   129  		flows = append(flows, logging.NewFlow(pnelogs.GetFlow(outputNames, clusterOutputs)))
   130  	}
   131  
   132  	if toolsetCRDSpec.KubeMetricsExporter != nil && toolsetCRDSpec.KubeMetricsExporter.Deploy &&
   133  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.KubeStateMetrics) {
   134  		flows = append(flows, logging.NewFlow(ksmlogs.GetFlow(outputNames, clusterOutputs)))
   135  	}
   136  
   137  	if toolsetCRDSpec.Reconciling != nil && toolsetCRDSpec.Reconciling.Deploy &&
   138  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Argocd) {
   139  		flows = append(flows, logging.NewFlow(aglogs.GetFlow(outputNames, clusterOutputs)))
   140  	}
   141  	if toolsetCRDSpec.LogCollection != nil && toolsetCRDSpec.LogCollection.Deploy &&
   142  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.LoggingOperator) {
   143  		flows = append(flows, logging.NewFlow(lologs.GetFlow(outputNames, clusterOutputs)))
   144  	}
   145  
   146  	if toolsetCRDSpec.MetricsPersisting != nil && toolsetCRDSpec.MetricsPersisting.Deploy &&
   147  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Prometheus) {
   148  		flows = append(flows, logging.NewFlow(plogs.GetFlow(outputNames, clusterOutputs)))
   149  	}
   150  
   151  	if toolsetCRDSpec.MetricsServer != nil && toolsetCRDSpec.MetricsServer.Deploy &&
   152  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.MetricsServer) {
   153  		flows = append(flows, logging.NewFlow(mslogs.GetFlow(outputNames, clusterOutputs)))
   154  	}
   155  
   156  	if toolsetCRDSpec.SystemdMetricsExporter != nil && toolsetCRDSpec.SystemdMetricsExporter.Deploy &&
   157  		(toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.PrometheusSystemdExporter) {
   158  		flows = append(flows, logging.NewFlow(pselogs.GetFlow(outputNames, clusterOutputs)))
   159  	}
   160  
   161  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Loki {
   162  		flows = append(flows, logging.NewFlow(getLokiFlow(outputNames, clusterOutputs)))
   163  	}
   164  
   165  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Boom {
   166  		flows = append(flows, logging.NewFlow(boom.GetFlow(outputNames, clusterOutputs)))
   167  	}
   168  
   169  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Orbiter {
   170  		flows = append(flows, logging.NewFlow(orbiter.GetFlow(outputNames, clusterOutputs)))
   171  	}
   172  
   173  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Zitadel {
   174  		for _, flow := range zitadel.GetFlows(outputNames, clusterOutputs) {
   175  			flows = append(flows, logging.NewFlow(flow))
   176  		}
   177  	}
   178  
   179  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Database {
   180  		for _, flow := range database.GetFlows(outputNames, clusterOutputs) {
   181  			flows = append(flows, logging.NewFlow(flow))
   182  		}
   183  	}
   184  
   185  	if toolsetCRDSpec.LogsPersisting.Logs == nil || toolsetCRDSpec.LogsPersisting.Logs.Networking {
   186  		flows = append(flows, logging.NewFlow(networking.GetFlow(outputNames, clusterOutputs)))
   187  	}
   188  
   189  	return flows
   190  }
   191  
   192  func getLokiFlow(outputs []string, clusterOutputs []string) *logging.FlowConfig {
   193  	ls := labels.GetApplicationLabels(info.GetName())
   194  
   195  	return &logging.FlowConfig{
   196  		Name:           "flow-loki",
   197  		Namespace:      "caos-system",
   198  		SelectLabels:   ls,
   199  		Outputs:        outputs,
   200  		ClusterOutputs: clusterOutputs,
   201  		ParserType:     "logfmt",
   202  	}
   203  }
   204  
   205  func getLokiOutput(clusterOutput bool) ([]string, []string, []*logging.Output) {
   206  	outputURL := strings.Join([]string{"http://", info.GetName().String(), ".", info.GetNamespace(), ":3100"}, "")
   207  
   208  	conf := &logging.ConfigOutput{
   209  		Name:      "output-loki",
   210  		Namespace: "caos-system",
   211  		URL:       outputURL,
   212  	}
   213  
   214  	outputs := make([]*logging.Output, 0)
   215  	outputs = append(outputs, logging.NewOutput(clusterOutput, conf))
   216  
   217  	outputNames := make([]string, 0)
   218  	clusterOutputs := make([]string, 0)
   219  	if clusterOutput {
   220  		clusterOutputs = append(clusterOutputs, conf.Name)
   221  	} else {
   222  		outputNames = append(outputNames, conf.Name)
   223  	}
   224  
   225  	return outputNames, clusterOutputs, outputs
   226  }
   227  
   228  func getCloudLokiOutput(
   229  	clusterOutput bool,
   230  	secretName string,
   231  	orb string,
   232  ) ([]string, []string, []*logging.Output) {
   233  	conf := &logging.ConfigOutput{
   234  		EnabledNamespaces: []string{
   235  			"caos-system",
   236  			"caos-zitadel",
   237  		},
   238  		ConfigureKubernetesLabels: true,
   239  		ExtractKubernetesLabels:   true,
   240  		ExtraLabels: map[string]string{
   241  			"orb": orb,
   242  		},
   243  		Labels: map[string]string{
   244  			"id":    "$.id",
   245  			"level": "$.level",
   246  		},
   247  		RemoveKeys: []string{
   248  			"kubernetes",
   249  		},
   250  		Name:      "output-grafana-cloud-loki",
   251  		Namespace: "caos-system",
   252  		URL:       "https://logs-prod-us-central1.grafana.net",
   253  		Username: &logging.SecretKeyRef{
   254  			Key:  "username",
   255  			Name: secretName,
   256  		},
   257  		Password: &logging.SecretKeyRef{
   258  			Key:  "password",
   259  			Name: secretName,
   260  		},
   261  	}
   262  
   263  	outputs := make([]*logging.Output, 0)
   264  	outputs = append(outputs, logging.NewOutput(clusterOutput, conf))
   265  
   266  	outputNames := make([]string, 0)
   267  	clusterOutputs := make([]string, 0)
   268  	if clusterOutput {
   269  		clusterOutputs = append(clusterOutputs, conf.Name)
   270  	} else {
   271  		outputNames = append(outputNames, conf.Name)
   272  	}
   273  
   274  	return outputNames, clusterOutputs, outputs
   275  }