Skip to content

Commit

Permalink
Add resources for etcd
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Mange <[email protected]>
  • Loading branch information
thibaultmg committed Jun 28, 2024
1 parent 7330b67 commit b76ed53
Show file tree
Hide file tree
Showing 7 changed files with 618 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/collector"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/hypershift"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/microshift"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/openshift"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/rendering"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/status"
Expand Down Expand Up @@ -68,6 +69,7 @@ type ObservabilityAddonReconciler struct {
HubNamespace string
ServiceAccountName string
InstallPrometheus bool
HostIP string
}

// +kubebuilder:rbac:groups=observability.open-cluster-management.io.open-cluster-management.io,resources=observabilityaddons,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -236,6 +238,21 @@ func (r *ObservabilityAddonReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, fmt.Errorf("failed to render prometheus templates: %w", err)
}

if !r.IsHubMetricsCollector {
microshiftVersion, err := microshift.IsMicroshiftCluster(ctx, r.Client)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to check if the cluster is microshift: %w", err)
}

if len(microshiftVersion) > 0 {
mcs := microshift.NewMicroshift(r.Client, r.Namespace, r.HostIP)
toDeploy, err = mcs.Render(ctx, toDeploy)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to render microshift templates: %w", err)
}
}
}

deployer := deploying.NewDeployer(r.Client)

// Ordering resources to ensure they are applied in the correct order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
observabilityshared "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/shared"
oav1beta1 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta1"
mcov1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
"github.com/stolostron/multicluster-observability-operator/operators/pkg/rendering/templates"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -123,6 +124,100 @@ func TestIntegrationReconcileHypershift(t *testing.T) {
assert.NoError(t, err)
}

// TestIntegrationReconcileHypershift tests the reconcile function for hypershift CRDs.
func TestIntegrationReconcileMicroshift(t *testing.T) {
testNamespace := "open-cluster-management-addon-observability"
hubNamespace := "microshift-cluster-a"

wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working dir %v", err)
}
os.Setenv(templates.TemplatesPathEnvVar, filepath.Join(filepath.Dir(filepath.Dir(wd)), "manifests"))

scheme := createBaseScheme()

k8sClientSpoke, err := client.New(restCfgSpoke, client.Options{Scheme: scheme})
if err != nil {
t.Fatal(err)
}

setupCommonSpokeResources(t, k8sClientSpoke)
defer tearDownCommonSpokeResources(t, k8sClientSpoke)

resourcesDeps := []client.Object{
newObservabilityAddonBis("observability-addon", testNamespace),
newMicroshiftVersionCM("kube-public"),
newMetricsAllowlistCM(testNamespace),
}
if err := createResources(k8sClientSpoke, resourcesDeps...); err != nil {
t.Fatalf("Failed to create resources: %v", err)
}

k8sHubClient, err := client.New(restCfgHub, client.Options{Scheme: scheme})
if err != nil {
t.Fatal(err)
}

setupCommonHubResources(t, k8sHubClient, testNamespace)
defer tearDownCommonHubResources(t, k8sHubClient, testNamespace)

// Create resources required for the microshift case on the hub
resourcesDeps = []client.Object{
makeNamespace(hubNamespace),
newObservabilityAddonBis("observability-addon", hubNamespace),
}
if err := createResources(k8sHubClient, resourcesDeps...); err != nil {
t.Fatalf("Failed to create resources on hub: %v", err)
}

mgr, err := ctrl.NewManager(testEnvSpoke.Config, ctrl.Options{
Scheme: k8sClientSpoke.Scheme(),
Metrics: metricsserver.Options{BindAddress: "0"}, // Avoids port conflict with the default port 8080
})
assert.NoError(t, err)

hubClientWithReload, err := util.NewReloadableHubClientWithReloadFunc(func() (client.Client, error) {
return k8sHubClient, nil
})
assert.NoError(t, err)
reconciler := ObservabilityAddonReconciler{
Client: k8sClientSpoke,
HubClient: hubClientWithReload,
HostIP: "192.168.10.10",
Scheme: scheme,
IsHubMetricsCollector: false,
Namespace: testNamespace,
HubNamespace: hubNamespace,
ServiceAccountName: "endpoint-monitoring-operator",
InstallPrometheus: true,
}

err = reconciler.SetupWithManager(mgr)
assert.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
err = mgr.Start(ctx)
assert.NoError(t, err)
}()

// Microshift resources must be created
// Checking the etcd service monitor that is specific to microshift
err = wait.Poll(1*time.Second, 5*time.Second, func() (bool, error) {
etcdSm := &promv1.ServiceMonitor{}
err := k8sClientSpoke.Get(context.Background(), types.NamespacedName{Namespace: testNamespace, Name: "etcd"}, etcdSm)
if err != nil && errors.IsNotFound(err) {
return false, nil
}

return true, err
})
assert.NoError(t, err)
}

func TestMain(m *testing.M) {
opts := zap.Options{
Development: true,
Expand Down
1 change: 1 addition & 0 deletions operators/endpointmetrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func main() {
ServiceAccountName: os.Getenv("SERVICE_ACCOUNT"),
IsHubMetricsCollector: os.Getenv("HUB_ENDPOINT_OPERATOR") == "true",
InstallPrometheus: installPrometheus,
HostIP: os.Getenv("HOST_IP"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ObservabilityAddon")
os.Exit(1)
Expand Down
Loading

0 comments on commit b76ed53

Please sign in to comment.