fgksgf commented on code in PR #113: URL: https://github.com/apache/skywalking-swck/pull/113#discussion_r1564601301
########## operator/controllers/operator/eventexporter_controller.go: ########## @@ -0,0 +1,210 @@ +// 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" + + "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" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "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" +) + +// 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 Review Comment: I think we also need to create configmaps ########## operator/apis/operator/v1alpha1/eventexporter_types.go: ########## @@ -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. + +package v1alpha1 + +import ( + appsv1 "k8s.io/api/apps/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EventExporterSpec defines the desired state of EventExporter +type EventExporterSpec struct { + // Version of EventExporter. + // +kubebuilder:validation:Required + Version string `json:"version,omitempty"` + // Image is the event exporter Docker image to deploy. + Image string `json:"image,omitempty"` + // Replicas is the number of event exporter pods + // +kubebuilder:validation:Required + Replicas int32 `json:"replicas,omitempty"` + // Config of filters and exporters + // +kubebuilder:validation:Optional + Config string `json:"config,omitempty"` +} + +// Important: Run "make" to regenerate code after modifying this file + +// EventExporterStatus defines the observed state of EventExporter +type EventExporterStatus struct { Review Comment: Maybe we need a field to indicate the name of the configmap that the event exporter is currently using. For different config content, the configmap will have different suffix like `event-exporter-cm-<hash>`. So every time the reconciler just need to hash the config in the spec, and compare it with the configmap name. This has another advantage, when config is changed, we just need to calculate the new configmap name and update it in the deployment's spec, pods will be rolling updated automatically to use the new config. ########## operator/controllers/operator/eventexporter_controller.go: ########## @@ -0,0 +1,210 @@ +// 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" + + "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" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "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" +) + +// 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 +// +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) + } + + if _, err := r.overlayData(ctx, log, &eventExporter); err != nil { + l.Error(err, "failed to overlay 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{ + "md5Data": func() string { return MD5Hash(eventExporter.Spec.Config) }, + }, + } + + 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) (changed bool, err error) { + + configmap := core.ConfigMap{} + err = r.Client.Get(ctx, client.ObjectKey{Namespace: eventExporter.Namespace, Name: eventExporter.Name}, &configmap) + if err != nil && !apierrors.IsNotFound(err) { + log.Error(err, "failed to get the eventexporter's configmap") + return false, err + } + + newMd5 := MD5Hash(eventExporter.Spec.Config) + oldMd5 := MD5Hash("") + if !apierrors.IsNotFound(err) { + oldMd5 = configmap.Labels["md5-data"] + } + + if newMd5 == oldMd5 { + log.Info("eventexporter configuration keeps the same as before") + return false, nil + } + + 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 + } + } + + configmap = core.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventExporter.Name, + Namespace: eventExporter.Namespace, + OwnerReferences: []metav1.OwnerReference{ + { + APIVersion: eventExporter.APIVersion, + Kind: eventExporter.Kind, + Name: eventExporter.Name, + UID: eventExporter.UID, + }, + }, + Labels: map[string]string{ + "version": eventExporter.Spec.Version, + "md5-data": newMd5, + }, + }, + Data: map[string]string{"config.yaml": eventExporter.Spec.Config}, Review Comment: If we create a new configmap for every new config in the spec, we can make the configmap immutable: https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-immutable -- 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]
