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 2 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 @@ -529,6 +529,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 @@ -642,7 +647,7 @@ spec:
fieldRef:
fieldPath: metadata.labels['name']
- name: DEBUG
value: "false"
value: "true"
ricardozanini marked this conversation as resolved.
Show resolved Hide resolved
image: quay.io/kiegroup/kogito-cloud-operator:1.0.0-snapshot
imagePullPolicy: Always
name: kogito-cloud-operator
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 @@ -529,6 +529,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 Expand Up @@ -642,7 +647,7 @@ spec:
fieldRef:
fieldPath: metadata.labels['name']
- name: DEBUG
value: "false"
value: "true"
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
image: quay.io/kiegroup/kogito-cloud-operator:1.0.0-snapshot
imagePullPolicy: Always
name: kogito-cloud-operator
Expand Down
2 changes: 1 addition & 1 deletion deploy/operator.yaml
Expand Up @@ -35,4 +35,4 @@ spec:
fieldRef:
fieldPath: metadata.labels['name']
- name: DEBUG
value: "false"
value: "true"
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
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.

51 changes: 50 additions & 1 deletion pkg/client/prometheus/servicemonitor.go
Expand Up @@ -19,13 +19,18 @@ import (
monv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/kiegroup/kogito-cloud-operator/pkg/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientv1 "sigs.k8s.io/controller-runtime/pkg/client"
)

// ServiceMonitorInterface has functions that interact with Service Monitor instances in the Kubernetes cluster
type ServiceMonitorInterface interface {
List(namespace string) (*monv1.ServiceMonitorList, error)
Create(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.CreateOption) error
Update(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.UpdateOption) error
Delete(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.DeleteOption) error
DeleteAllOf(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.DeleteAllOfOption) error
Patch(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, patch clientv1.Patch, opts ...clientv1.PatchOption) error
}

type serviceMonitor struct {
client *client.Client
}
Expand All @@ -48,3 +53,47 @@ func (s *serviceMonitor) List(namespace string) (*monv1.ServiceMonitorList, erro

return pList, nil
}

// Create implements client.Client
func (s *serviceMonitor) Create(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.CreateOption) error {
log.Debugf("Create service monitor instance : %s", serviceMonitor)
createOpts := &clientv1.CreateOptions{}
createOpts.ApplyOptions(opts)
vaibhavjainwiz marked this conversation as resolved.
Show resolved Hide resolved
_, err := s.client.PrometheusCli.ServiceMonitors(serviceMonitor.Namespace).Create(ctx, serviceMonitor, *createOpts.AsCreateOptions())
return err
}

func (s *serviceMonitor) Update(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.UpdateOption) error {
log.Debugf("Update service monitor instance : %s", serviceMonitor)
updateOpts := &clientv1.UpdateOptions{}
updateOpts.ApplyOptions(opts)
_, err := s.client.PrometheusCli.ServiceMonitors(serviceMonitor.Namespace).Update(ctx, serviceMonitor, *updateOpts.AsUpdateOptions())
return err
}

func (s *serviceMonitor) Delete(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.DeleteOption) error {
log.Debugf("Delete service monitor instance : %s", serviceMonitor)
DeleteOpts := &clientv1.DeleteOptions{}
DeleteOpts.ApplyOptions(opts)
err := s.client.PrometheusCli.ServiceMonitors(serviceMonitor.Namespace).Delete(ctx, serviceMonitor.Name, *DeleteOpts.AsDeleteOptions())
return err
}

func (s *serviceMonitor) DeleteAllOf(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, opts ...clientv1.DeleteAllOfOption) error {
DeleteAllOpts := &clientv1.DeleteAllOfOptions{}
DeleteAllOpts.ApplyOptions(opts)
err := s.client.PrometheusCli.ServiceMonitors(serviceMonitor.Namespace).DeleteCollection(ctx, *DeleteAllOpts.AsDeleteOptions(), *DeleteAllOpts.AsListOptions())
return err
}

func (s *serviceMonitor) Patch(ctx context.Context, serviceMonitor *monv1.ServiceMonitor, patch clientv1.Patch, opts ...clientv1.PatchOption) error {
log.Debugf("Patch service monitor instance : %s", serviceMonitor)
PatchOpts := &clientv1.PatchOptions{}
PatchOpts.ApplyOptions(opts)
data, err := patch.Data(serviceMonitor)
if err != nil {
return err
}
_, err = s.client.PrometheusCli.ServiceMonitors(serviceMonitor.Namespace).Patch(ctx, serviceMonitor.Name, patch.Type(), data, *PatchOpts.AsPatchOptions(), "")
return err
}