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

Commit

Permalink
Addon refactoring (#66)
Browse files Browse the repository at this point in the history
* observabilityAddon refacotoring

* obervabilityaddon refactoring

* remove debug code

* update license header

* refacotoring for report status to addon

* code refacotring for sonar error

* license header fix

* add observabilityaddon into client scheme
  • Loading branch information
marcolan018 authored Feb 22, 2021
1 parent d24ebbd commit e361b91
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 296 deletions.
12 changes: 12 additions & 0 deletions pkg/controller/add_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2021 Red Hat, Inc.

package controller

import (
"github.com/open-cluster-management/endpoint-metrics-operator/pkg/controller/status"
)

func init() {
// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
AddToManagerFuncs = append(AddToManagerFuncs, status.Add)
}
29 changes: 15 additions & 14 deletions pkg/controller/observabilityendpoint/metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

oav1beta1 "github.com/open-cluster-management/multicluster-monitoring-operator/pkg/apis/observability/v1beta1"
)

const (
Expand Down Expand Up @@ -50,17 +52,14 @@ type MetricsWhitelist struct {

// HubInfo is the struct for hub info
type HubInfo struct {
ClusterName string `yaml:"cluster-name"`
Endpoint string `yaml:"endpoint"`
EnableMetrics bool `yaml:"enable-metrics"`
Interval int32 `yaml:"internal"`
DeleteFlag bool `yaml:"delete-flag"`
ClusterName string `yaml:"cluster-name"`
Endpoint string `yaml:"endpoint"`
}

func createDeployment(clusterID string, hubInfo HubInfo,
whitelist MetricsWhitelist, replicaCount int32) *v1.Deployment {
interval := fmt.Sprint(hubInfo.Interval) + "s"
if fmt.Sprint(hubInfo.Interval) == "" {
func createDeployment(clusterID string, obsAddonSpec oav1beta1.ObservabilityAddonSpec,
hubInfo HubInfo, whitelist MetricsWhitelist, replicaCount int32) *v1.Deployment {
interval := fmt.Sprint(obsAddonSpec.Interval) + "s"
if fmt.Sprint(obsAddonSpec.Interval) == "" {
interval = defaultInterval
}

Expand All @@ -84,7 +83,7 @@ func createDeployment(clusterID string, hubInfo HubInfo,
if clusterID == "" {
clusterID = hubInfo.ClusterName
// deprecated ca bundle, only used for ocp 3.11 env
caFile = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
caFile = "//run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
} else {
volumes = append(volumes, corev1.Volume{
Name: caVolName,
Expand Down Expand Up @@ -173,7 +172,8 @@ func createDeployment(clusterID string, hubInfo HubInfo,
Value: clusterID,
},
},
VolumeMounts: mounts,
VolumeMounts: mounts,
ImagePullPolicy: corev1.PullAlways,
},
},
Volumes: volumes,
Expand All @@ -183,11 +183,12 @@ func createDeployment(clusterID string, hubInfo HubInfo,
}
}

func updateMetricsCollector(c client.Client, hubInfo HubInfo,
clusterID string, replicaCount int32, forceRestart bool) (bool, error) {
func updateMetricsCollector(c client.Client, obsAddonSpec oav1beta1.ObservabilityAddonSpec,
hubInfo HubInfo, clusterID string,
replicaCount int32, forceRestart bool) (bool, error) {

list := getMetricsWhitelist(c)
deployment := createDeployment(clusterID, hubInfo, list, replicaCount)
deployment := createDeployment(clusterID, obsAddonSpec, hubInfo, list, replicaCount)
found := &v1.Deployment{}
err := c.Get(context.TODO(), types.NamespacedName{Name: metricsCollectorName,
Namespace: namespace}, found)
Expand Down
25 changes: 14 additions & 11 deletions pkg/controller/observabilityendpoint/metrics_collector_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Copyright (c) 2020 Red Hat, Inc.
// Copyright (c) 2021 Red Hat, Inc.
package observabilityendpoint

import (
"testing"

addonv1alpha1 "github.com/open-cluster-management/api/addon/v1alpha1"
"github.com/open-cluster-management/multicluster-monitoring-operator/pkg/apis"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

addonv1alpha1 "github.com/open-cluster-management/api/addon/v1alpha1"
"github.com/open-cluster-management/multicluster-monitoring-operator/pkg/apis"
oav1beta1 "github.com/open-cluster-management/multicluster-monitoring-operator/pkg/apis/observability/v1beta1"
)

func getWhitelistCM() *corev1.ConfigMap {
Expand Down Expand Up @@ -43,32 +45,33 @@ func init() {

func TestMetricsCollector(t *testing.T) {
hubInfo := &HubInfo{
ClusterName: "test-cluster",
Endpoint: "http://test-endpoint",
ClusterName: "test-cluster",
Endpoint: "http://test-endpoint",
}
whitelistCM := getWhitelistCM()
obsAddon := oav1beta1.ObservabilityAddonSpec{
EnableMetrics: true,
Interval: 60,
DeleteFlag: false,
}
whitelistCM := getWhitelistCM()

c := fake.NewFakeClient(whitelistCM)
// Default deployment with instance count 1
_, err := updateMetricsCollector(c, *hubInfo, testClusterID, 1, false)
_, err := updateMetricsCollector(c, obsAddon, *hubInfo, testClusterID, 1, false)
if err != nil {
t.Fatalf("Failed to create metrics collector deployment: (%v)", err)
}
// Update deployment to reduce instance count to zero
_, err = updateMetricsCollector(c, *hubInfo, testClusterID, 0, false)
_, err = updateMetricsCollector(c, obsAddon, *hubInfo, testClusterID, 0, false)
if err != nil {
t.Fatalf("Failed to create metrics collector deployment: (%v)", err)
}

_, err = updateMetricsCollector(c, *hubInfo, testClusterID+"-update", 1, false)
_, err = updateMetricsCollector(c, obsAddon, *hubInfo, testClusterID+"-update", 1, false)
if err != nil {
t.Fatalf("Failed to create metrics collector deployment: (%v)", err)
}

_, err = updateMetricsCollector(c, *hubInfo, testClusterID+"-update", 1, true)
_, err = updateMetricsCollector(c, obsAddon, *hubInfo, testClusterID+"-update", 1, true)
if err != nil {
t.Fatalf("Failed to update metrics collector deployment: (%v)", err)
}
Expand Down
Loading

0 comments on commit e361b91

Please sign in to comment.