Skip to content

Commit

Permalink
Merge pull request #893 from jhadvig/CONSOLE-4014_2
Browse files Browse the repository at this point in the history
CONSOLE-4014: Pass OCM organization ID and OCP cluster ID to console-config.yaml
  • Loading branch information
openshift-merge-bot[bot] committed May 2, 2024
2 parents dac8cc8 + b3ba896 commit 0120ab8
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 115 deletions.
14 changes: 11 additions & 3 deletions pkg/console/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/openshift/console-operator/pkg/console/subresource/configmap"
"github.com/openshift/console-operator/pkg/console/subresource/deployment"
"github.com/openshift/console-operator/pkg/console/subresource/secret"
telemetry "github.com/openshift/console-operator/pkg/console/telemetry"
)

type consoleOperator struct {
Expand All @@ -65,10 +66,12 @@ type consoleOperator struct {
proxyConfigLister configlistersv1.ProxyLister
oauthConfigLister configlistersv1.OAuthLister
authnConfigLister configlistersv1.AuthenticationLister
clusterVersionLister configlistersv1.ClusterVersionLister
dynamicClient dynamic.Interface
// core kube
secretsClient coreclientv1.SecretsGetter
secretsLister corev1listers.SecretLister
configNSSecretLister corev1listers.SecretLister
configMapClient coreclientv1.ConfigMapsGetter
targetNSConfigMapLister corev1listers.ConfigMapLister // for openshift-console namespace
managedNSConfigMapLister corev1listers.ConfigMapLister // for openshift-config-managed namespace
Expand Down Expand Up @@ -120,6 +123,7 @@ func NewConsoleOperator(
consolePluginInformer consoleinformersv1.ConsolePluginInformer,
// openshift config
configNSConfigMapInformer corev1.ConfigMapInformer,
configSecretsInformer corev1.SecretInformer,
// openshift config managed
managedCoreV1 corev1.Interface,
// event handling
Expand All @@ -135,6 +139,7 @@ func NewConsoleOperator(
nodeInformer := coreV1.Nodes()
configV1Informers := configInformer.Config().V1()
configNameFilter := util.IncludeNamesFilter(api.ConfigResourceName)

targetNameFilter := util.IncludeNamesFilter(api.OpenShiftConsoleName)

c := &consoleOperator{
Expand All @@ -147,11 +152,14 @@ func NewConsoleOperator(
ingressConfigLister: configInformer.Config().V1().Ingresses().Lister(),
proxyConfigLister: configInformer.Config().V1().Proxies().Lister(),
oauthConfigLister: configInformer.Config().V1().OAuths().Lister(),
clusterVersionLister: configInformer.Config().V1().ClusterVersions().Lister(),
authnConfigLister: configV1Informers.Authentications().Lister(),
// console resources
// core kube
secretsClient: corev1Client,
secretsLister: secretsInformer.Lister(),
secretsClient: corev1Client,
secretsLister: secretsInformer.Lister(),
configNSSecretLister: configSecretsInformer.Lister(),

configMapClient: corev1Client,

targetNSConfigMapLister: targetNSConfigMapInformer.Lister(),
Expand Down Expand Up @@ -224,7 +232,7 @@ func NewConsoleOperator(
util.IncludeNamesFilter(deployment.ConsoleOauthConfigName),
secretsInformer.Informer(),
).WithFilteredEventsInformers(
util.IncludeNamesFilter(deployment.TelemeterClientDeploymentName),
util.IncludeNamesFilter(telemetry.TelemeterClientDeploymentName),
monitoringDeploymentInformer.Informer(),
).ResyncEvery(time.Minute).WithSync(c.Sync).
ToController("ConsoleOperator", recorder.WithComponentSuffix("console-operator"))
Expand Down
59 changes: 54 additions & 5 deletions pkg/console/operator/sync_v400.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

// kube
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -21,14 +22,14 @@ import (
v1 "github.com/openshift/api/console/v1"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/openshift/console-operator/pkg/api"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
"github.com/openshift/library-go/pkg/operator/resourcesynccontroller"

// operator
"github.com/openshift/console-operator/pkg/api"
customerrors "github.com/openshift/console-operator/pkg/console/errors"
"github.com/openshift/console-operator/pkg/console/metrics"
"github.com/openshift/console-operator/pkg/console/status"
Expand All @@ -38,6 +39,7 @@ import (
routesub "github.com/openshift/console-operator/pkg/console/subresource/route"
secretsub "github.com/openshift/console-operator/pkg/console/subresource/secret"
utilsub "github.com/openshift/console-operator/pkg/console/subresource/util"
telemetry "github.com/openshift/console-operator/pkg/console/telemetry"
)

// The sync loop starts from zero and works its way through the requirements for a running console.
Expand Down Expand Up @@ -365,9 +367,9 @@ func (co *consoleOperator) SyncConfigMap(
monitoringSharedConfig = &corev1.ConfigMap{}
}

telemeterClientIsAvailable, err := deploymentsub.IsTelemeterClientAvailable(co.monitoringDeploymentLister)
if err != nil {
return nil, false, "FailedTelemeterClientCheck", err
telemeterConfig, tcErr := co.GetTelemeterConfiguration(ctx, operatorConfig)
if tcErr != nil {
return nil, false, "FailedGetTelemetryConfig", tcErr
}

var (
Expand Down Expand Up @@ -395,7 +397,7 @@ func (co *consoleOperator) SyncConfigMap(
nodeArchitectures,
nodeOperatingSystems,
copiedCSVsDisabled,
telemeterClientIsAvailable,
telemeterConfig,
)
if err != nil {
return nil, false, "FailedConsoleConfigBuilder", err
Expand All @@ -411,6 +413,53 @@ func (co *consoleOperator) SyncConfigMap(
return cm, cmChanged, "ConsoleConfigBuilder", cmErr
}

// Build telemetry configuration in following order:
// 1. check if the telemetry client is available and set the "TELEMETER_CLIENT_DISABLED" annotation accordingly
// 2. get telemetry annotation from console-operator config
// 3. get CLUSTER_ID from the cluster-version config
// 4. get ORGANIZATION_ID from OCM
func (co *consoleOperator) GetTelemeterConfiguration(ctx context.Context, operatorConfig *operatorv1.Console) (map[string]string, error) {
telemetryConfig := make(map[string]string)
telemeterClientIsAvailable, err := telemetry.IsTelemeterClientAvailable(co.monitoringDeploymentLister)
if err != nil {
return telemetryConfig, err
}

if len(operatorConfig.Annotations) > 0 {
for k, v := range operatorConfig.Annotations {
if strings.HasPrefix(k, telemetry.TelemetryAnnotationPrefix) && len(k) > len(telemetry.TelemetryAnnotationPrefix) {
telemetryConfig[k[len(telemetry.TelemetryAnnotationPrefix):]] = v
}
}
}

clusterID, err := telemetry.GetClusterID(co.clusterVersionLister)
if err != nil {
return nil, err
}
telemetryConfig["CLUSTER_ID"] = clusterID

if !telemeterClientIsAvailable {
telemetryConfig["TELEMETER_CLIENT_DISABLED"] = "true"
return telemetryConfig, nil
}

accessToken, err := telemetry.GetAccessToken(co.configNSSecretLister)
if err != nil {
return nil, err
}

// If for any reason the getting the ORGANIZATION_ID fails,
// log the error and set ORGANIZATION_ID to empty string.
organizationID, err := telemetry.GetOrganizationID(clusterID, accessToken)
if err != nil {
klog.Errorf("telemetry config error: %s", err)
}
telemetryConfig["ORGANIZATION_ID"] = organizationID

return telemetryConfig, nil
}

// apply service-ca configmap
func (co *consoleOperator) SyncServiceCAConfigMap(ctx context.Context, operatorConfig *operatorv1.Console) (consoleCM *corev1.ConfigMap, changed bool, reason string, err error) {
required := configmapsub.DefaultServiceCAConfigMap(operatorConfig)
Expand Down
6 changes: 4 additions & 2 deletions pkg/console/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
upgradenotification "github.com/openshift/console-operator/pkg/console/controllers/upgradenotification"
"github.com/openshift/console-operator/pkg/console/controllers/util"
"github.com/openshift/console-operator/pkg/console/operatorclient"
"github.com/openshift/console-operator/pkg/console/subresource/deployment"
"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
"github.com/openshift/library-go/pkg/operator/managementstatecontroller"
Expand All @@ -62,6 +61,8 @@ import (
consolev1client "github.com/openshift/client-go/console/clientset/versioned"
consoleinformers "github.com/openshift/client-go/console/informers/externalversions"

telemetry "github.com/openshift/console-operator/pkg/console/telemetry"

"github.com/openshift/console-operator/pkg/console/operator"
"github.com/openshift/library-go/pkg/operator/loglevel"
)
Expand Down Expand Up @@ -126,7 +127,7 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
kubeInformersMonitoringNamespaced := informers.NewSharedInformerFactoryWithOptions(
kubeClient,
resync,
informers.WithNamespace(deployment.TelemeterClientDeploymentNamespace),
informers.WithNamespace(telemetry.TelemeterClientDeploymentNamespace),
)

//configs are all named "cluster", but our clusteroperator is named "console"
Expand Down Expand Up @@ -235,6 +236,7 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
consoleInformers.Console().V1().ConsolePlugins(),
// openshift
kubeInformersConfigNamespaced.Core().V1().ConfigMaps(), // openshift-config configMaps
kubeInformersConfigNamespaced.Core().V1().Secrets(), // openshift-config secrets
// openshift managed
kubeInformersManagedNamespaced.Core().V1(), // Managed ConfigMaps
// event handling
Expand Down
28 changes: 5 additions & 23 deletions pkg/console/subresource/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package configmap
import (
"fmt"
"net/url"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
Expand All @@ -21,10 +20,9 @@ import (
)

const (
consoleConfigYamlFile = "console-config.yaml"
defaultLogoutURL = ""
pluginProxyEndpoint = "/api/proxy/plugin/"
telemetryAnnotationPrefix = "telemetry.console.openshift.io/"
consoleConfigYamlFile = "console-config.yaml"
defaultLogoutURL = ""
pluginProxyEndpoint = "/api/proxy/plugin/"
)

func statusPageId(operatorConfig *operatorv1.Console) string {
Expand All @@ -48,7 +46,7 @@ func DefaultConfigMap(
nodeArchitectures []string,
nodeOperatingSystems []string,
copiedCSVsDisabled bool,
telemeterClientIsAvailable bool,
telemeterConfig map[string]string,
) (consoleConfigMap *corev1.ConfigMap, unsupportedOverridesHaveMerged bool, err error) {

apiServerURL := infrastructuresub.GetAPIServerURL(infrastructureConfig)
Expand Down Expand Up @@ -93,7 +91,7 @@ func DefaultConfigMap(
Perspectives(operatorConfig.Spec.Customization.Perspectives).
StatusPageID(statusPageId(operatorConfig)).
InactivityTimeout(inactivityTimeoutSeconds).
TelemetryConfiguration(GetTelemetryConfiguration(operatorConfig, telemeterClientIsAvailable)).
TelemetryConfiguration(telemeterConfig).
ReleaseVersion().
NodeArchitectures(nodeArchitectures).
NodeOperatingSystems(nodeOperatingSystems).
Expand Down Expand Up @@ -174,22 +172,6 @@ func getPluginsProxyServices(availablePlugins []*v1.ConsolePlugin) []consoleserv
return proxyServices
}

func GetTelemetryConfiguration(operatorConfig *operatorv1.Console, telemeterClientIsAvailable bool) map[string]string {
telemetry := make(map[string]string)
if len(operatorConfig.Annotations) > 0 {
for k, v := range operatorConfig.Annotations {
if strings.HasPrefix(k, telemetryAnnotationPrefix) && len(k) > len(telemetryAnnotationPrefix) {
telemetry[k[len(telemetryAnnotationPrefix):]] = v
}
}
}

if !telemeterClientIsAvailable {
telemetry["TELEMETER_CLIENT_DISABLED"] = "true"
}
return telemetry
}

func getConsoleAPIPath(pluginName string, service *v1.ConsolePluginProxy) string {
return fmt.Sprintf("%s%s/%s/", pluginProxyEndpoint, pluginName, service.Alias)
}
Expand Down
64 changes: 7 additions & 57 deletions pkg/console/subresource/configmap/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestDefaultConfigMap(t *testing.T) {
consoleConfig *configv1.Console
managedConfig *corev1.ConfigMap
monitoringSharedConfig *corev1.ConfigMap
clusterMonitoringConfig *corev1.ConfigMap
authServerCAConfig *corev1.ConfigMap
infrastructureConfig *configv1.Infrastructure
rt *routev1.Route
Expand All @@ -64,6 +63,7 @@ func TestDefaultConfigMap(t *testing.T) {
nodeArchitectures []string
nodeOperatingSystems []string
copiedCSVsDisabled bool
telemetryConfig map[string]string
}
t.Setenv("OPERATOR_IMAGE_VERSION", testReleaseVersion)
tests := []struct {
Expand Down Expand Up @@ -228,6 +228,9 @@ customization:
},
inactivityTimeoutSeconds: 0,
nodeArchitectures: []string{"amd64", "arm64"},
telemetryConfig: map[string]string{
"telemetry.console.openshift.io/TELEMETER_CLIENT_DISABLED": "true",
},
},
want: &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -268,6 +271,8 @@ servingInfo:
certFile: /var/serving-cert/tls.crt
keyFile: /var/serving-cert/tls.key
providers: {}
telemetry:
telemetry.console.openshift.io/TELEMETER_CLIENT_DISABLED: "true"
`,
},
},
Expand Down Expand Up @@ -1063,7 +1068,7 @@ providers: {}
tt.args.nodeArchitectures,
tt.args.nodeOperatingSystems,
tt.args.copiedCSVsDisabled,
true, // TODO add test cases for telemetry client
tt.args.telemetryConfig,
)

// marshall the exampleYaml to map[string]interface{} so we can use it in diff below
Expand Down Expand Up @@ -1152,61 +1157,6 @@ func testPluginsWithI18nPreloadType(pluginName, serviceName, serviceNamespace st
return plugin
}

func TestTelemetryConfiguration(t *testing.T) {
tests := []struct {
name string
operatorConfig *operatorv1.Console
clusterMonitoringConfig *corev1.ConfigMap
expectedConfiguration map[string]string
}{
{
name: "Should pass without any telemetry annotations",
operatorConfig: &operatorv1.Console{},
expectedConfiguration: map[string]string{},
},
{
name: "Should extract telemetry annotations correctly",
operatorConfig: &operatorv1.Console{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"release.openshift.io/create-only": "true",
"telemetry.console.openshift.io/A_CONFIG_KEY": "API_KEY",
"telemetry.console.openshift.io/disabled": "true",
},
},
},
expectedConfiguration: map[string]string{
"A_CONFIG_KEY": "API_KEY",
"disabled": "true",
},
},
{
name: "Should ignore an empty annotation suffix",
operatorConfig: &operatorv1.Console{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"release.openshift.io/create-only": "true",
"telemetry.console.openshift.io/A_CONFIG_KEY": "API_KEY",
"telemetry.console.openshift.io/disabled": "true",
"telemetry.console.openshift.io/": "invalid annotation",
},
},
},
expectedConfiguration: map[string]string{
"A_CONFIG_KEY": "API_KEY",
"disabled": "true",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if diff := deep.Equal(GetTelemetryConfiguration(tt.operatorConfig, true), tt.expectedConfiguration); diff != nil { // TODO add test cases for telemetry client
t.Error(diff)
}
})
}
}

func TestStub(t *testing.T) {
tests := []struct {
name string
Expand Down
25 changes: 0 additions & 25 deletions pkg/console/subresource/deployment/telemeter-client.go

This file was deleted.

0 comments on commit 0120ab8

Please sign in to comment.