+/*
+Copyright 2019 Intel Corporation.
+Licensed 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 collectdplugin
import (
onapv1alpha1 "collectd-operator/pkg/apis/onap/v1alpha1"
collectdutils "collectd-operator/pkg/controller/utils"
+ dspredicate "collectd-operator/pkg/controller/utils"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
- extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
var log = logf.Log.WithName("controller_collectdplugin")
-
// Add creates a new CollectdPlugin Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
err = c.Watch(
&source.Kind{Type: &appsv1.DaemonSet{}},
&handler.EnqueueRequestsFromMapFunc{
- ToRequests: handler.ToRequestsFunc(func (a handler.MapObject) []reconcile.Request {
+ ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
labelSelector, err := collectdutils.GetWatchLabels()
labels := strings.Split(labelSelector, "=")
if err != nil {
log.Error(err, "Failed to get watch labels, continuing with default label")
}
rcp := r.(*ReconcileCollectdPlugin)
- // Select the Daemonset with labelSelector (Defautl is app=collectd)
- if a.Meta.GetLabels()[labels[0]] == labels[1] {
+ // Select the Daemonset with labelSelector (Default is app=collectd)
+ if a.Meta.GetLabels()[labels[0]] == labels[1] {
var requests []reconcile.Request
cpList, err := collectdutils.GetCollectdPluginList(rcp.client, a.Meta.GetNamespace())
- if err != nil {
+ if err != nil || cpList == nil || cpList.Items == nil || len(cpList.Items) == 0 {
+ log.V(1).Info("No CollectdPlugin CR instance Exist")
return nil
}
for _, cp := range cpList.Items {
}
return nil
}),
- })
- if err != nil {
- return err
- }
+ }, dspredicate.DaemonSetStatusChangedPredicate{})
return nil
}
-
// blank assignment to verify that ReconcileCollectdPlugin implements reconcile.Reconciler
var _ reconcile.Reconciler = &ReconcileCollectdPlugin{}
scheme *runtime.Scheme
}
-
// Reconcile reads that state of the cluster for a CollectdPlugin object and makes changes based on the state read
// and what is in the CollectdPlugin.Spec
// TODO(user): Modify this Reconcile function to implement your Controller logic. This example creates
if err := r.addFinalizer(reqLogger, instance); err != nil {
return reconcile.Result{}, err
}
- return reconcile.Result{}, nil
+ //return reconcile.Result{}, nil
}
// Handle the reconciliation for CollectdPlugin.
// At this stage the Status of the CollectdPlugin should NOT be ""
// handleCollectdPlugin regenerates the collectd conf on CR Create, Update, Delete events
func (r *ReconcileCollectdPlugin) handleCollectdPlugin(reqLogger logr.Logger, cr *onapv1alpha1.CollectdPlugin, isDelete bool) error {
+ collectdutils.ReconcileLock.Lock()
+ defer collectdutils.ReconcileLock.Unlock()
+ retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
+ cm, err := collectdutils.GetConfigMap(r.client, reqLogger, cr.Namespace)
+ if err != nil {
+ reqLogger.Error(err, "Skip reconcile: ConfigMap not found")
+ return err
+ }
+ reqLogger.V(1).Info(":::: ConfigMap Info ::::", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
- rmap, err := collectdutils.FindResourceMapForCR(r.client, reqLogger, cr.Namespace)
- if err != nil {
- reqLogger.Error(err, "Skip reconcile: Resources not found")
- return err
- }
-
- cm := rmap.ConfigMap
- reqLogger.V(1).Info("Found ResourceMap")
- reqLogger.V(1).Info(":::: ConfigMap Info ::::", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
-
- collectdConf, err := collectdutils.RebuildCollectdConf(r.client, cr.Namespace, isDelete, cr.Spec.PluginName)
- if err != nil {
- reqLogger.Error(err, "Skip reconcile: Rebuild conf failed")
- return err
- }
-
- cm.SetAnnotations(map[string]string{
- "daaas-random": collectdutils.ComputeSHA256([]byte(collectdConf)),
- })
-
- cm.Data["collectd.conf"] = collectdConf
+ collectdConf, err := collectdutils.RebuildCollectdConf(r.client, cr.Namespace, isDelete, cr.Spec.PluginName)
+ if err != nil {
+ reqLogger.Error(err, "Skip reconcile: Rebuild conf failed")
+ return err
+ }
- // Update the ConfigMap with new Spec and reload DaemonSets
- reqLogger.Info("Updating the ConfigMap", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
- log.V(1).Info("ConfigMap Data", "Map: ", cm.Data)
- err = r.client.Update(context.TODO(), cm)
- if err != nil {
- reqLogger.Error(err, "Update the ConfigMap failed", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
- return err
- }
+ cm.SetAnnotations(map[string]string{
+ "daaas-random": collectdutils.ComputeSHA256([]byte(collectdConf)),
+ })
+ cm.Data["collectd.conf"] = collectdConf
+
+ // Update the ConfigMap with new Spec and reload DaemonSets
+ reqLogger.Info("Updating the ConfigMap", "ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
+ updateErr := r.client.Update(context.TODO(), cm)
+ if updateErr != nil {
+ reqLogger.Error(updateErr, "Update ConfigMap failed")
+ return updateErr
+ }
- retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Retrieve the latest version of Daemonset before attempting update
// RetryOnConflict uses exponential backoff to avoid exhausting the apiserver
// Select DaemonSets with label
- dsList := &extensionsv1beta1.DaemonSetList{}
+ dsList := &appsv1.DaemonSetList{}
opts := &client.ListOptions{}
labelSelector, err := collectdutils.GetWatchLabels()
if err != nil {
ds.Spec.Template.SetAnnotations(map[string]string{
"daaas-random": collectdutils.ComputeSHA256([]byte(collectdConf)),
})
- updateErr := r.client.Update(context.TODO(), ds)
+ updateErr = r.client.Update(context.TODO(), ds)
return updateErr
})
if retryErr != nil {
panic(fmt.Errorf("Update failed: %v", retryErr))
}
- err = r.updateStatus(cr)
- if err != nil {
- reqLogger.Error(err, "Unable to update status")
+ retryErr = retry.RetryOnConflict(retry.DefaultRetry, func() error {
+ err := r.updateStatus(cr)
return err
+ })
+
+ if retryErr != nil {
+ panic(fmt.Errorf("Update failed: %v", retryErr))
}
// Reconcile success
reqLogger.Info("Reconcile success!!")
}
func (r *ReconcileCollectdPlugin) updateStatus(cr *onapv1alpha1.CollectdPlugin) error {
- switch cr.Status.Status {
+ // Fetch the CollectdPlugin instance
+ instance := &onapv1alpha1.CollectdPlugin{}
+ key := types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name}
+ err := r.client.Get(context.TODO(), key, instance)
+ if err != nil {
+ return err
+ }
+ switch instance.Status.Status {
case onapv1alpha1.Initial:
- cr.Status.Status = onapv1alpha1.Created
+ instance.Status.Status = onapv1alpha1.Created
case onapv1alpha1.Created, onapv1alpha1.Enabled:
- pods, err := collectdutils.GetPodList(r.client, cr.Namespace)
+ pods, err := collectdutils.GetPodList(r.client, instance.Namespace)
if err != nil {
return err
}
- if !reflect.DeepEqual(pods, cr.Status.CollectdAgents) {
- cr.Status.CollectdAgents = pods
- cr.Status.Status = onapv1alpha1.Enabled
+ if !reflect.DeepEqual(pods, instance.Status.CollectdAgents) {
+ instance.Status.CollectdAgents = pods
+ instance.Status.Status = onapv1alpha1.Enabled
}
case onapv1alpha1.Deleting, onapv1alpha1.Deprecated:
return nil
}
- err := r.client.Status().Update(context.TODO(), cr)
+ err = r.client.Status().Update(context.TODO(), instance)
return err
}
// Update status from Initial to Created
// Since addFinalizer will be executed only once,
// the status will be changed from Initial state to Created
- updateErr := r.updateStatus(cr)
- if updateErr != nil {
- reqLogger.Error(updateErr, "Failed to update status from Initial state")
- }
+ // updateErr := r.updateStatus(cr)
+ // if updateErr != nil {
+ // reqLogger.Error(updateErr, "Failed to update status from Initial state")
+ // }
// Update CR
+ cr.Status.Status = onapv1alpha1.Created
err := r.client.Update(context.TODO(), cr)
if err != nil {
reqLogger.Error(err, "Failed to update CollectdPlugin with finalizer")