Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

[KOGITO-2960] - Prometheus integration with kogito-cloud-operator #521

Merged
merged 13 commits into from Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions cmd/kogito/command/converter/prometheus_converter.go
@@ -0,0 +1,29 @@
// Copyright 2020 Red Hat, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package converter

import (
"github.com/kiegroup/kogito-cloud-operator/cmd/kogito/command/flag"
"github.com/kiegroup/kogito-cloud-operator/pkg/apis/app/v1alpha1"
)

// FromPrometheusFlagToPrometheus converts given PrometheusFlags into Prometheus
func FromPrometheusFlagToPrometheus(prometheusFlags *flag.PrometheusFlags) v1alpha1.Prometheus {
return v1alpha1.Prometheus{
Scrape: prometheusFlags.Scrape,
Scheme: prometheusFlags.Scheme,
Path: prometheusFlags.Path,
}
}
34 changes: 34 additions & 0 deletions cmd/kogito/command/converter/prometheus_converter_test.go
@@ -0,0 +1,34 @@
// Copyright 2020 Red Hat, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package converter

import (
"github.com/kiegroup/kogito-cloud-operator/cmd/kogito/command/flag"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_FromPrometheusFlagToPrometheus(t *testing.T) {
prometheusFlags := &flag.PrometheusFlags{
Scrape: true,
Scheme: "http",
Path: "/metrix",
}

prometheus := FromPrometheusFlagToPrometheus(prometheusFlags)
assert.True(t, prometheus.Scrape)
assert.Equal(t, "http", prometheus.Scheme)
assert.Equal(t, "/metrix", prometheus.Path)
}
38 changes: 38 additions & 0 deletions cmd/kogito/command/flag/prometheus_flag.go
@@ -0,0 +1,38 @@
// Copyright 2020 Red Hat, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flag

import (
"github.com/spf13/cobra"
)

// PrometheusFlags is common properties used to configure Prometheus
type PrometheusFlags struct {
Scrape bool
Scheme string
Path string
}

// AddPrometheusFlags adds the prometheus flags to the given command
func AddPrometheusFlags(command *cobra.Command, flags *PrometheusFlags) {
command.Flags().BoolVar(&flags.Scrape, "prome-scrape", false, "Flag to allow Prometheus to scraping Kogito service")
command.Flags().StringVar(&flags.Scheme, "prome-scheme", "http", "HTTP scheme to use for scraping.")
command.Flags().StringVar(&flags.Path, "prome-path", "/metrics", "HTTP path to scrape for metrics")
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
}

// CheckPrometheusArgs validates the PrometheusFlags flags
func CheckPrometheusArgs(flags *PrometheusFlags) error {
return nil
}
5 changes: 5 additions & 0 deletions cmd/kogito/command/flag/runtime_flag.go
Expand Up @@ -26,6 +26,7 @@ type RuntimeFlags struct {
InfinispanFlags
KafkaFlags
RuntimeTypeFlags
PrometheusFlags
Name string
EnableIstio bool
EnablePersistence bool
Expand All @@ -38,6 +39,7 @@ func AddRuntimeFlags(command *cobra.Command, flags *RuntimeFlags) {
AddInstallFlags(command, &flags.InstallFlags)
AddInfinispanFlags(command, &flags.InfinispanFlags)
AddKafkaFlags(command, &flags.KafkaFlags)
AddPrometheusFlags(command, &flags.PrometheusFlags)
command.Flags().BoolVar(&flags.EnableIstio, "enable-istio", false, "Enable Istio integration by annotating the Kogito service pods with the right value for Istio controller to inject sidecars on it. Defaults to false")
command.Flags().BoolVar(&flags.EnablePersistence, "enable-persistence", false, "If set to true, deployed Kogito service will support integration with Infinispan server for persistence. Default to false")
command.Flags().BoolVar(&flags.EnableEvents, "enable-events", false, "If set to true, deployed Kogito service will support integration with Kafka cluster for events. Default to false")
Expand All @@ -55,6 +57,9 @@ func CheckRuntimeArgs(flags *RuntimeFlags) error {
if err := CheckKafkaArgs(&flags.KafkaFlags); err != nil {
return err
}
if err := CheckPrometheusArgs(&flags.PrometheusFlags); err != nil {
return err
}
if err := util.CheckKeyPair(flags.ServiceLabels); err != nil {
return fmt.Errorf("service labels are in the wrong format. Valid are key pairs like 'service=myservice', received %s", flags.ServiceLabels)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kogito/command/service/runtime_service.go
Expand Up @@ -65,6 +65,7 @@ func (i runtimeService) InstallRuntimeService(cli *client.Client, flags *flag.Ru
Spec: v1alpha1.KogitoRuntimeSpec{
EnableIstio: flags.EnableIstio,
Runtime: converter.FromRuntimeFlagsToRuntimeType(&flags.RuntimeTypeFlags),
Prometheus: converter.FromPrometheusFlagToPrometheus(&flags.PrometheusFlags),
KogitoServiceSpec: v1alpha1.KogitoServiceSpec{
Replicas: &flags.Replicas,
Envs: converter.FromStringArrayToEnvs(flags.Env, flags.SecretEnv),
Expand Down
13 changes: 13 additions & 0 deletions deploy/crds/app.kiegroup.org_kogitoruntimes_crd.yaml
Expand Up @@ -258,6 +258,19 @@ spec:
infrastructure.
type: boolean
type: object
prometheus:
description: Create Service monitor instance use to connect with Prometheus
properties:
path:
description: HTTP path to scrape for metrics.
type: string
scheme:
description: HTTP scheme to use for scraping.
type: string
scrape:
description: Flag to allow Prometheus to scraping Kogito service.
type: boolean
type: object
replicas:
description: 'Number of replicas that the service will have deployed
in the cluster. Default value: 1.'
Expand Down
Expand Up @@ -258,6 +258,19 @@ spec:
infrastructure.
type: boolean
type: object
prometheus:
description: Create Service monitor instance use to connect with Prometheus
properties:
path:
description: HTTP path to scrape for metrics.
type: string
scheme:
description: HTTP scheme to use for scraping.
type: string
scrape:
description: Flag to allow Prometheus to scraping Kogito service.
type: boolean
type: object
replicas:
description: 'Number of replicas that the service will have deployed
in the cluster. Default value: 1.'
Expand Down
Expand Up @@ -545,6 +545,11 @@ spec:
image.'
displayName: Image
path: image
- description: Create Service monitor instance use to connect with Prometheus
displayName: Prometheus
path: prometheus
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:label
- description: 'Number of replicas that the service will have deployed in the
cluster. Default value: 1.'
displayName: Replicas
Expand Down
Expand Up @@ -258,6 +258,19 @@ spec:
infrastructure.
type: boolean
type: object
prometheus:
description: Create Service monitor instance use to connect with Prometheus
properties:
path:
description: HTTP path to scrape for metrics.
type: string
scheme:
description: HTTP scheme to use for scraping.
type: string
scrape:
description: Flag to allow Prometheus to scraping Kogito service.
type: boolean
type: object
replicas:
description: 'Number of replicas that the service will have deployed
in the cluster. Default value: 1.'
Expand Down
Expand Up @@ -545,6 +545,11 @@ spec:
image.'
displayName: Image
path: image
- description: Create Service monitor instance use to connect with Prometheus
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
displayName: Prometheus
path: prometheus
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:label
- description: 'Number of replicas that the service will have deployed in the
cluster. Default value: 1.'
displayName: Replicas
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/app/v1alpha1/kogitoruntime_types.go
Expand Up @@ -37,6 +37,12 @@ type KogitoRuntimeSpec struct {
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.x-descriptors="urn:alm:descriptor:com.tectonic.ui:label"
// +kubebuilder:validation:Enum=quarkus;springboot
Runtime RuntimeType `json:"runtime,omitempty"`

// Create Service monitor instance use to connect with Prometheus
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.displayName="Prometheus"
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.x-descriptors="urn:alm:descriptor:com.tectonic.ui:label"
Prometheus Prometheus `json:"prometheus,omitempty"`
}

// KogitoRuntimeStatus defines the observed state of KogitoRuntime.
Expand Down
38 changes: 38 additions & 0 deletions pkg/apis/app/v1alpha1/prometheus.go
@@ -0,0 +1,38 @@
// Copyright 2020 Red Hat, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

const (

// PrometheusDefaultPath default path
PrometheusDefaultPath = "/metrics"

// PrometheusDefaultScheme default scheme
PrometheusDefaultScheme = "http"
)

// Prometheus properties to connect with prometheus
type Prometheus struct {
// Flag to allow Prometheus to scraping Kogito service.
Scrape bool `json:"scrape,omitempty"`
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved

// HTTP scheme to use for scraping.
// +optional
Scheme string `json:"scheme,omitempty"`

// HTTP path to scrape for metrics.
// +optional
Path string `json:"path,omitempty"`
}
17 changes: 17 additions & 0 deletions pkg/apis/app/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions pkg/client/custom_reader.go
@@ -0,0 +1,55 @@
// Copyright 2020 Red Hat, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"context"
"fmt"
monv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/kiegroup/kogito-cloud-operator/pkg/client/prometheus"
"k8s.io/apimachinery/pkg/runtime"
clientv1 "sigs.k8s.io/controller-runtime/pkg/client"
)

// CustomReader provides capability to identify client at runtime
type CustomReader struct {
Client *Client
}

// List retrieves list of objects for a given namespace and list options.
func (r *CustomReader) List(ctx context.Context, list runtime.Object, opts ...clientv1.ListOption) error {
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
switch l := list.(type) {
case *monv1.ServiceMonitorList:
for _, opt := range opts {
if namespace, ok := opt.(clientv1.InNamespace); ok {
sList, err := prometheus.ServiceMonitorC(r.Client.PrometheusCli).List(string(namespace))
if err != nil {
return err
}
serviceMonitorList := list.(*monv1.ServiceMonitorList)
*serviceMonitorList = *sList
return nil
}
}
return fmt.Errorf("namespace is not specified, cannot list prometheuses")
default:
return r.Client.ControlCli.List(ctx, l, opts...)
}
}

// Get retrieves an obj for the given object key from the Kubernetes Cluster.
func (r *CustomReader) Get(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error {
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
return r.Client.ControlCli.Get(ctx, key, obj)
}