Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
@@ -8,6 +8,8 @@ package telemetry
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
)
|
||||
@@ -21,8 +23,8 @@ const (
|
||||
type TelemetryOptions struct {
|
||||
// should enable the telemetry.
|
||||
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty" mapstructure:"enabled"`
|
||||
// KSCloudURL for kubesphere cloud
|
||||
KSCloudURL string `json:"ksCloudURL,omitempty" yaml:"ksCloudURL,omitempty" mapstructure:"ksCloudURL"`
|
||||
// Endpoint for kubesphere cloud
|
||||
Endpoint string `json:"endpoint,omitempty" yaml:"endpoint,omitempty" mapstructure:"endpoint"`
|
||||
// collect period
|
||||
// The schedule in telemetry clusterInfo format, see https://en.wikipedia.org/wiki/Cron.
|
||||
Schedule string `json:"schedule,omitempty" yaml:"schedule,omitempty" mapstructure:"schedule"`
|
||||
@@ -36,12 +38,12 @@ func NewTelemetryOptions() *TelemetryOptions {
|
||||
|
||||
// LoadPlatformConfig from given ConfigMap.
|
||||
func LoadTelemetryConfig(secret *corev1.Secret) (*TelemetryOptions, error) {
|
||||
value, ok := secret.Data[ConfigDataKey]
|
||||
value, ok := secret.Data[constants.GenericPlatformConfigFileName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to get config %s from secret %s value", ConfigDataKey, ConfigName)
|
||||
}
|
||||
o := &TelemetryOptions{}
|
||||
if err := yaml.Unmarshal([]byte(value), o); err != nil {
|
||||
if err := yaml.Unmarshal(value, o); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal value from configmap. err: %s", err)
|
||||
}
|
||||
return o, nil
|
||||
|
||||
@@ -54,7 +54,7 @@ func (r *runnable) startTask() error {
|
||||
// Add the task to the cron scheduler
|
||||
id, err := r.cron.AddFunc(r.TelemetryOptions.Schedule, func() {
|
||||
var args = []string{
|
||||
"--url", r.TelemetryOptions.KSCloudURL,
|
||||
"--url", r.TelemetryOptions.Endpoint,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
|
||||
@@ -60,12 +60,12 @@ func (r *Reconciler) SetupWithManager(mgr *kscontroller.Manager) error {
|
||||
}
|
||||
return secret.Namespace == constants.KubeSphereNamespace &&
|
||||
secret.Name == ConfigName &&
|
||||
secret.Type == constants.SecretTypePlatformConfig
|
||||
secret.Type == constants.SecretTypeGenericPlatformConfig
|
||||
}))).
|
||||
Complete(r)
|
||||
}
|
||||
|
||||
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
|
||||
func (r *Reconciler) Reconcile(ctx context.Context, _ reconcile.Request) (reconcile.Result, error) {
|
||||
secret := &corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: ConfigName,
|
||||
@@ -73,7 +73,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
|
||||
},
|
||||
}
|
||||
if err := r.Client.Get(ctx, runtimeclient.ObjectKeyFromObject(secret), secret); err != nil {
|
||||
if errors.IsNotFound(err) { // not found. telemetry is disabled.
|
||||
if errors.IsNotFound(err) {
|
||||
// not found. telemetry is disabled.
|
||||
if r.telemetryRunnable != nil {
|
||||
r.telemetryRunnable.Close()
|
||||
}
|
||||
@@ -95,9 +96,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
|
||||
}
|
||||
|
||||
// check value when telemetry is enabled.
|
||||
if conf.Enabled &&
|
||||
(conf.KSCloudURL == "" || conf.Schedule == "") {
|
||||
klog.V(9).ErrorS(nil, "ksCloudURL and schedule should not be empty when telemetry enabled is true.")
|
||||
if conf.Enabled && (conf.Endpoint == "" || conf.Schedule == "") {
|
||||
klog.V(9).ErrorS(nil, "endpoint and schedule should not be empty when telemetry enabled is true.")
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user