From f489f25a6f89b9e0dcf1e6c483dad1710926d62d Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Wed, 19 Oct 2022 09:13:22 +0200 Subject: [PATCH] Add DaemonSet module --- modules/common/daemonset/daemonset.go | 130 ++++++++++++++++++++++++++ modules/common/daemonset/types.go | 29 ++++++ 2 files changed, 159 insertions(+) create mode 100644 modules/common/daemonset/daemonset.go create mode 100644 modules/common/daemonset/types.go diff --git a/modules/common/daemonset/daemonset.go b/modules/common/daemonset/daemonset.go new file mode 100644 index 00000000..727057c6 --- /dev/null +++ b/modules/common/daemonset/daemonset.go @@ -0,0 +1,130 @@ +/* +Copyright 2020 Red Hat + +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 daemonset + +import ( + "context" + "fmt" + "time" + + "github.com/openstack-k8s-operators/lib-common/modules/common/helper" + "github.com/openstack-k8s-operators/lib-common/modules/common/util" + appsv1 "k8s.io/api/apps/v1" + k8s_errors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" +) + +// NewDaemonSet returns an initialized DaemonSet +func NewDaemonSet( + daemonset *appsv1.DaemonSet, + timeout time.Duration, +) *DaemonSet { + return &DaemonSet{ + daemonset: daemonset, + timeout: timeout, + } +} + +// CreateOrPatch - creates or patches a DaemonSet, reconciles after Xs if object won't exist. +func (d *DaemonSet) CreateOrPatch( + ctx context.Context, + h *helper.Helper, +) (ctrl.Result, error) { + daemonset := &appsv1.DaemonSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: d.daemonset.Name, + Namespace: d.daemonset.Namespace, + }, + } + + op, err := controllerutil.CreateOrPatch(ctx, h.GetClient(), daemonset, func() error { + // DaemonSet selector is immutable so we set this value only if + // a new object is going to be created + if daemonset.ObjectMeta.CreationTimestamp.IsZero() { + daemonset.Spec.Selector = d.daemonset.Spec.Selector + } + daemonset.Annotations = util.MergeStringMaps(daemonset.Annotations, d.daemonset.Annotations) + daemonset.Labels = util.MergeStringMaps(daemonset.Labels, d.daemonset.Labels) + daemonset.Spec.Template = d.daemonset.Spec.Template + + err := controllerutil.SetControllerReference(h.GetBeforeObject(), daemonset, h.GetScheme()) + if err != nil { + return err + } + + return nil + }) + if err != nil { + if k8s_errors.IsNotFound(err) { + util.LogForObject(h, fmt.Sprintf("DaemonSet not found, reconcile in %s", d.timeout), daemonset) + return ctrl.Result{RequeueAfter: d.timeout}, nil + } + return ctrl.Result{}, err + } + if op != controllerutil.OperationResultNone { + util.LogForObject(h, fmt.Sprintf("DaemonSet: %s", op), daemonset) + } + + // update the daemonset object of the daemonset type + d.daemonset, err = getDaemonSetWithName(ctx, h, daemonset.GetName(), daemonset.GetNamespace()) + if err != nil { + if k8s_errors.IsNotFound(err) { + return ctrl.Result{RequeueAfter: d.timeout}, nil + } + return ctrl.Result{}, err + } + + return ctrl.Result{}, nil +} + +// Delete - delete a daemonset. +func (d *DaemonSet) Delete( + ctx context.Context, + h *helper.Helper, +) error { + err := h.GetClient().Delete(ctx, d.daemonset) + if err != nil && !k8s_errors.IsNotFound(err) { + err = fmt.Errorf("error deleting daemonset %s: %v", d.daemonset.Name, err) + return err + } + + return nil +} + +// GetDaemonSet - get the daemonset object. +func (d *DaemonSet) GetDaemonSet() appsv1.DaemonSet { + return *d.daemonset +} + +func getDaemonSetWithName( + ctx context.Context, + h *helper.Helper, + name string, + namespace string, +) (*appsv1.DaemonSet, error) { + + dset := &appsv1.DaemonSet{} + err := h.GetClient().Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, dset) + if err != nil { + return dset, err + } + + return dset, nil +} diff --git a/modules/common/daemonset/types.go b/modules/common/daemonset/types.go new file mode 100644 index 00000000..9ef305ac --- /dev/null +++ b/modules/common/daemonset/types.go @@ -0,0 +1,29 @@ +/* +Copyright 2021 Red Hat + +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 daemonset + +import ( + "time" + + appsv1 "k8s.io/api/apps/v1" +) + +// DaemonSet - +type DaemonSet struct { + daemonset *appsv1.DaemonSet + timeout time.Duration +}