Skip to content

Commit

Permalink
Deploy ManifestWorks based on MCO-managed AddonDeploymentConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
periklis committed Jun 6, 2024
1 parent 5f4b5e2 commit f8cdca1
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 125 deletions.
15 changes: 13 additions & 2 deletions deploy/resources/addondeploymentconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@ metadata:
namespace: open-cluster-management
spec:
customizedVariables:
- name: loggingSubscriptionChannel
value: stable-5.9
# Operator Subscription Channels
- name: openshiftLoggingChannel
value: stable-5.9
# Platform Observability
- name: platformLogsCollection
value: clusterlogforwarders.v1.logging.openshift.io
# User Workloads Observability
- name: userWorkloadLogsCollection
value: clusterlogforwarders.v1.logging.openshift.io
- name: userWorkloadTracesCollection
value: opentelemetrycollectors.v1beta1.opentelemetry.io
- name: userWorkloadInstrumentation
value: instrumentations.v1alpha1.opentelemetry.io
55 changes: 11 additions & 44 deletions internal/addon/helm/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package helm

import (
"context"
"strconv"

"github.com/rhobs/multicluster-observability-addon/internal/addon"
lhandlers "github.com/rhobs/multicluster-observability-addon/internal/logging/handlers"
lmanifests "github.com/rhobs/multicluster-observability-addon/internal/logging/manifests"
thandlers "github.com/rhobs/multicluster-observability-addon/internal/tracing/handlers"
tmanifests "github.com/rhobs/multicluster-observability-addon/internal/tracing/manifests"
"k8s.io/klog/v2"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
addonutils "open-cluster-management.io/addon-framework/pkg/utils"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
Expand All @@ -25,36 +23,35 @@ type HelmChartValues struct {
Tracing tmanifests.TracingValues `json:"tracing"`
}

type Options struct {
LoggingDisabled bool
TracingDisabled bool
}

func GetValuesFunc(ctx context.Context, k8s client.Client) addonfactory.GetValuesFunc {
return func(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn,
mcAddon *addonapiv1alpha1.ManagedClusterAddOn,
) (addonfactory.Values, error) {
// if hub cluster, then don't install anything
if isHubCluster(cluster) {
return addonfactory.JsonStructToValues(HelmChartValues{})
}

aodc, err := getAddOnDeploymentConfig(ctx, k8s, addon)
aodc, err := getAddOnDeploymentConfig(ctx, k8s, mcAddon)
if err != nil {
return nil, err
}
opts, err := buildOptions(aodc)
opts, err := addon.BuildOptions(aodc)
if err != nil {
return nil, err
}

if !opts.Platform.Enabled && !opts.UserWorkloads.Enabled {
return addonfactory.JsonStructToValues(HelmChartValues{})
}

userValues := HelmChartValues{
Enabled: true,
}

if !opts.LoggingDisabled {
loggingOpts, err := lhandlers.BuildOptions(ctx, k8s, addon, aodc)
if opts.Platform.Logs.CollectionEnabled || opts.UserWorkloads.Logs.CollectionEnabled {
loggingOpts, err := lhandlers.BuildOptions(ctx, k8s, mcAddon, opts.Platform.Logs, opts.Platform.Logs)
if err != nil {
return nil, err
}
Expand All @@ -66,9 +63,8 @@ func GetValuesFunc(ctx context.Context, k8s client.Client) addonfactory.GetValue
userValues.Logging = *logging
}

if !opts.TracingDisabled {
klog.Info("Tracing enabled")
tracingOpts, err := thandlers.BuildOptions(ctx, k8s, addon, aodc)
if opts.UserWorkloads.Traces.CollectionEnabled {
tracingOpts, err := thandlers.BuildOptions(ctx, k8s, mcAddon, opts.UserWorkloads.Traces)
if err != nil {
return nil, err
}
Expand All @@ -94,35 +90,6 @@ func getAddOnDeploymentConfig(ctx context.Context, k8s client.Client, mcAddon *a
return addOnDeployment, nil
}

func buildOptions(addOnDeployment *addonapiv1alpha1.AddOnDeploymentConfig) (Options, error) {
var opts Options
if addOnDeployment == nil {
return opts, nil
}

if addOnDeployment.Spec.CustomizedVariables == nil {
return opts, nil
}

for _, keyvalue := range addOnDeployment.Spec.CustomizedVariables {
switch keyvalue.Name {
case addon.AdcLoggingDisabledKey:
value, err := strconv.ParseBool(keyvalue.Value)
if err != nil {
return opts, err
}
opts.LoggingDisabled = value
case addon.AdcTracingisabledKey:
value, err := strconv.ParseBool(keyvalue.Value)
if err != nil {
return opts, err
}
opts.TracingDisabled = value
}
}
return opts, nil
}

func isHubCluster(cluster *clusterv1.ManagedCluster) bool {
val, ok := cluster.Labels[annotationLocalCluster]
if !ok {
Expand Down
17 changes: 4 additions & 13 deletions internal/addon/helm/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,7 @@ func Test_Mcoa_Disable_Charts(t *testing.T) {
Namespace: "open-cluster-management",
},
Spec: addonapiv1alpha1.AddOnDeploymentConfigSpec{
CustomizedVariables: []addonapiv1alpha1.CustomizedVariable{
{
Name: "loggingDisabled",
Value: "true",
},
{
Name: "tracingDisabled",
Value: "true",
},
},
CustomizedVariables: []addonapiv1alpha1.CustomizedVariable{},
},
}

Expand All @@ -77,7 +68,7 @@ func Test_Mcoa_Disable_Charts(t *testing.T) {
WithObjects(addOnDeploymentConfig).
Build()

loggingAgentAddon, err := addonfactory.NewAgentAddonFactory(addon.Name, addon.FS, addon.McoaChartDir).
agentAddon, err := addonfactory.NewAgentAddonFactory(addon.Name, addon.FS, addon.McoaChartDir).
WithGetValuesFuncs(GetValuesFunc(context.TODO(), fakeKubeClient)).
WithAgentRegistrationOption(&agent.RegistrationOption{}).
WithScheme(scheme.Scheme).
Expand All @@ -86,9 +77,9 @@ func Test_Mcoa_Disable_Charts(t *testing.T) {
klog.Fatalf("failed to build agent %v", err)
}

objects, err := loggingAgentAddon.Manifests(managedCluster, managedClusterAddOn)
objects, err := agentAddon.Manifests(managedCluster, managedClusterAddOn)
require.NoError(t, err)
require.Equal(t, 2, len(objects))
require.Empty(t, objects)
}

func Test_Mcoa_Disable_Chart_Hub(t *testing.T) {
Expand Down
101 changes: 101 additions & 0 deletions internal/addon/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package addon

import (
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
)

const (
// Operator Subscription Channels
KeyOpenShiftLoggingChannel = "openshiftLoggingChannel"

// Platform Observability Keys
KeyPlatformLogsCollection = "platformLogsCollection"

// User Workloads Observability Keys
KeyUserWorkloadLogsCollection = "userWorkloadLogsCollection"
KeyUserWorkloadTracesCollection = "userWorkloadTracesCollection"
KeyUserWorkloadTracesInstrumentation = "userWorkloadTracesInstrumentation"
)

type CollectionKind string

const (
ClusterLogForwarderV1 CollectionKind = "clusterlogforwarders.v1.logging.openshift.io"
OpenTelemetryCollectorV1beta1 CollectionKind = "opentelemetrycollectors.v1beta1.opentelemetry.io"
)

type InstrumentationKind string

const (
InstrumentationV1alpha1 InstrumentationKind = "instrumentations.v1alpha1.opentelemetry.io"
)

type LogsOptions struct {
CollectionEnabled bool
SubscriptionChannel string
}

type TracesOptions struct {
CollectionEnabled bool
InstrumentationEnabled bool
SubscriptionChannel string
}

type PlatformOptions struct {
Enabled bool
Logs LogsOptions
}

type UserWorkloadOptions struct {
Enabled bool
Logs LogsOptions
Traces TracesOptions
}

type Options struct {
Platform PlatformOptions
UserWorkloads UserWorkloadOptions
}

func BuildOptions(addOnDeployment *addonapiv1alpha1.AddOnDeploymentConfig) (Options, error) {
var opts Options
if addOnDeployment == nil {
return opts, nil
}

if addOnDeployment.Spec.CustomizedVariables == nil {
return opts, nil
}

for _, keyvalue := range addOnDeployment.Spec.CustomizedVariables {
switch keyvalue.Name {
// Operator Subscriptions
case KeyOpenShiftLoggingChannel:
opts.Platform.Logs.SubscriptionChannel = keyvalue.Value
opts.UserWorkloads.Logs.SubscriptionChannel = keyvalue.Value
// Platform Observability Options
case KeyPlatformLogsCollection:
if keyvalue.Value == string(ClusterLogForwarderV1) {
opts.Platform.Enabled = true
opts.Platform.Logs.CollectionEnabled = true
}
// User Workload Observability Options
case KeyUserWorkloadLogsCollection:
if keyvalue.Value == string(ClusterLogForwarderV1) {
opts.UserWorkloads.Enabled = true
opts.UserWorkloads.Logs.CollectionEnabled = true
}
case KeyUserWorkloadTracesCollection:
if keyvalue.Value == string(OpenTelemetryCollectorV1beta1) {
opts.UserWorkloads.Enabled = true
opts.UserWorkloads.Traces.CollectionEnabled = true
}
case KeyUserWorkloadTracesInstrumentation:
if keyvalue.Value == string(InstrumentationV1alpha1) {
opts.UserWorkloads.Enabled = true
opts.UserWorkloads.Traces.InstrumentationEnabled = true
}
}
}
return opts, nil
}
14 changes: 5 additions & 9 deletions internal/addon/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ const (
TracingChartDir = "manifests/charts/mcoa/charts/tracing"

AddonDeploymentConfigResource = "addondeploymentconfigs"

AdcLoggingDisabledKey = "loggingDisabled"
AdcTracingisabledKey = "tracingDisabled"

ClusterLogForwardersResource = "clusterlogforwarders"
SpokeCLFName = "mcoa-instance"
SpokeCLFNamespace = "openshift-logging"
clfProbeKey = "isReady"
clfProbePath = ".status.conditions[?(@.type==\"Ready\")].status"
ClusterLogForwardersResource = "clusterlogforwarders"
SpokeCLFName = "mcoa-instance"
SpokeCLFNamespace = "openshift-logging"
clfProbeKey = "isReady"
clfProbePath = ".status.conditions[?(@.type==\"Ready\")].status"

OpenTelemetryCollectorsResource = "opentelemetrycollectors"
SpokeOTELColName = "mcoa-instance"
Expand Down
25 changes: 16 additions & 9 deletions internal/logging/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func BuildOptions(ctx context.Context, k8s client.Client, mcAddon *addonapiv1alpha1.ManagedClusterAddOn, adoc *addonapiv1alpha1.AddOnDeploymentConfig) (manifests.Options, error) {
resources := manifests.Options{
AddOnDeploymentConfig: adoc,
func BuildOptions(ctx context.Context, k8s client.Client, mcAddon *addonapiv1alpha1.ManagedClusterAddOn, platform, userWorkloads addon.LogsOptions) (manifests.Options, error) {
opts := manifests.Options{
Platform: platform,
UserWorkloads: userWorkloads,
}

if platform.SubscriptionChannel != "" {
opts.SubscriptionChannel = platform.SubscriptionChannel
} else {
opts.SubscriptionChannel = userWorkloads.SubscriptionChannel
}

key := addon.GetObjectKey(mcAddon.Status.ConfigReferences, loggingv1.GroupVersion.Group, addon.ClusterLogForwardersResource)
clf := &loggingv1.ClusterLogForwarder{}
if err := k8s.Get(ctx, key, clf, &client.GetOptions{}); err != nil {
return resources, err
return opts, err
}
resources.ClusterLogForwarder = clf
opts.ClusterLogForwarder = clf

targetSecretName := make(map[authentication.Target]string)
for _, output := range clf.Spec.Outputs {
Expand All @@ -31,13 +38,13 @@ func BuildOptions(ctx context.Context, k8s client.Client, mcAddon *addonapiv1alp
secretsProvider := authentication.NewSecretsProvider(k8s, clf.Namespace, mcAddon.Namespace)
targetSecrets, err := secretsProvider.GenerateSecrets(ctx, clf.Annotations, targetSecretName)
if err != nil {
return resources, err
return opts, err
}

resources.Secrets, err = secretsProvider.FetchSecrets(ctx, targetSecrets)
opts.Secrets, err = secretsProvider.FetchSecrets(ctx, targetSecrets)
if err != nil {
return resources, err
return opts, err
}

return resources, nil
return opts, nil
}
6 changes: 3 additions & 3 deletions internal/logging/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ var (

func fakeGetValues(k8s client.Client) addonfactory.GetValuesFunc {
return func(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn,
_ *clusterv1.ManagedCluster,
mcAddon *addonapiv1alpha1.ManagedClusterAddOn,
) (addonfactory.Values, error) {
opts, err := handlers.BuildOptions(context.TODO(), k8s, addon, nil)
opts, err := handlers.BuildOptions(context.TODO(), k8s, mcAddon, addon.LogsOptions{}, addon.LogsOptions{})
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit f8cdca1

Please sign in to comment.