jichengzhi commented on code in PR #113:
URL: https://github.com/apache/skywalking-swck/pull/113#discussion_r1565203673


##########
test/e2e/skywalking-components-with-eventexporter.yaml:
##########
@@ -0,0 +1,78 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you under
+# the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+apiVersion: operator.skywalking.apache.org/v1alpha1
+kind: OAPServer
+metadata:
+  name: skywalking-system
+  namespace: skywalking-system
+spec:
+  version: 9.5.0
+  instances: 1
+  image: apache/skywalking-oap-server:9.5.0
+  service:
+    template:
+      type: ClusterIP
+
+---
+apiVersion: operator.skywalking.apache.org/v1alpha1
+kind: EventExporter
+metadata:
+  name: skywalking-system
+  namespace: skywalking-system
+spec:
+  replicas: 1
+  config: |
+    filters:
+      - reason: ""     
+        message: ""    
+        minCount: 1    
+        type: ""       
+        action: ""     
+        kind: "Pod|Service"
+        namespace: "^skywalking-system$"  
+        name: ""       
+        service: "[^\\s]{1,}"  
+        exporters:     
+          - skywalking 
+    exporters:         
+      skywalking:      
+        template:      
+          source:
+            service: "{{ .Service.Name }}"
+            serviceInstance: "{{ .Pod.Name }}"
+            endpoint: ""
+          message: "{{ .Event.Message }}" 
+        address: "skywalking-system-oap.skywalking-system:11800"
+
+---
+apiVersion: operator.skywalking.apache.org/v1alpha1
+kind: UI
+metadata:
+  name: skywalking-system
+  namespace: skywalking-system
+spec:
+  version: 9.5.0
+  instances: 1
+  image: apache/skywalking-ui:9.5.0
+  OAPServerAddress: http://skywalking-system-oap.skywalking-system:12800
+  service:
+    template:
+      type: ClusterIP
+    ingress:
+      host: demo.ui.skywalking

Review Comment:
   Thx, updated!



##########
operator/controllers/operator/eventexporter_controller.go:
##########
@@ -0,0 +1,218 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package operator
+
+import (
+       "context"
+       "fmt"
+       "text/template"
+
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+       "github.com/go-logr/logr"
+       l "github.com/sirupsen/logrus"
+       apps "k8s.io/api/apps/v1"
+       core "k8s.io/api/core/v1"
+       apiequal "k8s.io/apimachinery/pkg/api/equality"
+       apierrors "k8s.io/apimachinery/pkg/api/errors"
+       "k8s.io/apimachinery/pkg/runtime"
+       "k8s.io/client-go/tools/record"
+       "k8s.io/client-go/util/retry"
+       ctrl "sigs.k8s.io/controller-runtime"
+       "sigs.k8s.io/controller-runtime/pkg/client"
+       runtimelog "sigs.k8s.io/controller-runtime/pkg/log"
+
+       operatorv1alpha1 
"github.com/apache/skywalking-swck/operator/apis/operator/v1alpha1"
+       "github.com/apache/skywalking-swck/operator/pkg/kubernetes"
+)
+
+var useImmutableConfigMap = true
+
+// EventExporterReconciler reconciles a EventExporter object
+type EventExporterReconciler struct {
+       client.Client
+       Scheme   *runtime.Scheme
+       FileRepo kubernetes.Repo
+       Recorder record.EventRecorder
+}
+
+// 
+kubebuilder:rbac:groups=operator.skywalking.apache.org,resources=eventexporters,verbs=get;list;watch;create;update;patch;delete
+// 
+kubebuilder:rbac:groups=operator.skywalking.apache.org,resources=eventexporters/status,verbs=get;update;patch
+// 
+kubebuilder:rbac:groups=operator.skywalking.apache.org,resources=eventexporters/finalizers,verbs=update
+// 
+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
+// 
+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
+// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
+
+func (r *EventExporterReconciler) Reconcile(ctx context.Context, req 
ctrl.Request) (ctrl.Result, error) {
+       log := runtimelog.FromContext(ctx)
+       log.Info(fmt.Sprintf("===============eventexporter reconcile started 
(ns: %s, name: %s)===============", req.Namespace, req.Name))
+
+       eventExporter := operatorv1alpha1.EventExporter{}
+       if err := r.Client.Get(ctx, req.NamespacedName, &eventExporter); err != 
nil {
+               return ctrl.Result{}, client.IgnoreNotFound(err)
+       }
+
+       newConfigMapName := configMapName(&eventExporter)
+       if _, err := r.overlayData(ctx, log, &eventExporter, newConfigMapName); 
err != nil {
+               log.Error(err, "failed to delete eventexporter's configMap")
+               return ctrl.Result{}, err
+       }
+
+       ff, err := r.FileRepo.GetFilesRecursive("templates")
+       if err != nil {
+               log.Error(err, "failed to load resource templates")
+               return ctrl.Result{}, err
+       }
+
+       app := kubernetes.Application{
+               Client:   r.Client,
+               FileRepo: r.FileRepo,
+               CR:       &eventExporter,
+               GVK:      
operatorv1alpha1.GroupVersion.WithKind("EventExporter"),
+               Recorder: r.Recorder,
+               TmplFunc: template.FuncMap{
+                       "configMapName": func() string { return 
newConfigMapName },
+               },
+       }
+
+       if err := app.ApplyAll(ctx, ff, log); err != nil {
+               return ctrl.Result{}, err
+       }
+
+       if err := r.checkState(ctx, log, &eventExporter); err != nil {
+               l.Error(err, "failed to check sub resources state")
+               return ctrl.Result{}, err
+       }
+
+       return ctrl.Result{RequeueAfter: schedDuration}, nil
+
+}
+
+func (r *EventExporterReconciler) overlayData(ctx context.Context, log 
logr.Logger,
+       eventExporter *operatorv1alpha1.EventExporter, newConfigMapName string) 
(changed bool, err error) {
+
+       oldConfigMapName := eventExporter.Status.ConfigMapName
+       if oldConfigMapName == newConfigMapName {
+               log.Info("eventexporter configuration keeps the same as before")
+               return false, nil
+       }
+
+       configmap := core.ConfigMap{}
+       err = r.Client.Get(ctx, client.ObjectKey{Namespace: 
eventExporter.Namespace, Name: oldConfigMapName}, &configmap)
+       if err != nil && !apierrors.IsNotFound(err) {
+               log.Error(err, "failed to get the eventexporter's configmap")
+               return true, err
+       }
+
+       if !apierrors.IsNotFound(err) {
+               if err = r.Client.Delete(ctx, &configmap); err != nil {
+                       log.Error(err, "failed to delete eventexporter's 
configmap")
+                       return true, err

Review Comment:
   Thx, updated!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to