From 23b5483f8d648c9759401b18fe9ccc2d67acc754 Mon Sep 17 00:00:00 2001 From: Zirko <64951262+QuantumEnigmaa@users.noreply.github.com> Date: Tue, 13 Feb 2024 16:35:59 +0100 Subject: [PATCH] Add update method to cilium resource (#1525) * add update method to ciliumnetpol resource * changelog * fix method name --- CHANGELOG.md | 4 +++ .../resource/ciliumnetpol/create.go | 28 +++++++++++++++++-- .../resource/ciliumnetpol/resource.go | 6 ++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c4504e9e..92033aab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `update` method to cilium netpol resource. + ## [4.67.2] - 2024-02-13 ### Fixed diff --git a/service/controller/resource/ciliumnetpol/create.go b/service/controller/resource/ciliumnetpol/create.go index 874d5690c..420de109a 100644 --- a/service/controller/resource/ciliumnetpol/create.go +++ b/service/controller/resource/ciliumnetpol/create.go @@ -23,15 +23,39 @@ func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error { return microerror.Mask(err) } - _, err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{}) + current, err := r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Get(ctx, desired.GetName(), metav1.GetOptions{}) if apierrors.IsNotFound(err) { - _, err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{}) + current, err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Create(ctx, desired, metav1.CreateOptions{}) } if err != nil { return microerror.Mask(err) } + + if hasCiliumNetworkPolicyChanged(current, desired) { + updateMeta(current, desired) + _, err = r.dynamicK8sClient.Resource(resource).Namespace(desired.GetNamespace()).Update(ctx, desired, metav1.UpdateOptions{}) + if err != nil { + return microerror.Mask(err) + } + } } r.logger.Debugf(ctx, "created") return nil } + +func updateMeta(c, d metav1.Object) { + d.SetGenerateName(c.GetGenerateName()) + d.SetUID(c.GetUID()) + d.SetResourceVersion(c.GetResourceVersion()) + d.SetGeneration(c.GetGeneration()) + d.SetSelfLink(c.GetSelfLink()) + d.SetCreationTimestamp(c.GetCreationTimestamp()) + d.SetDeletionTimestamp(c.GetDeletionTimestamp()) + d.SetDeletionGracePeriodSeconds(c.GetDeletionGracePeriodSeconds()) + d.SetLabels(c.GetLabels()) + d.SetAnnotations(c.GetAnnotations()) + d.SetFinalizers(c.GetFinalizers()) + d.SetOwnerReferences(c.GetOwnerReferences()) + d.SetManagedFields(c.GetManagedFields()) +} diff --git a/service/controller/resource/ciliumnetpol/resource.go b/service/controller/resource/ciliumnetpol/resource.go index ae932a680..a358c6abc 100644 --- a/service/controller/resource/ciliumnetpol/resource.go +++ b/service/controller/resource/ciliumnetpol/resource.go @@ -1,6 +1,8 @@ package ciliumnetpol import ( + "reflect" + "github.com/giantswarm/microerror" "github.com/giantswarm/micrologger" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -113,3 +115,7 @@ func toCiliumNetworkPolicy(v interface{}) (*unstructured.Unstructured, error) { return ciliumNetworkPolicy, nil } + +func hasCiliumNetworkPolicyChanged(current *unstructured.Unstructured, desired *unstructured.Unstructured) bool { + return !reflect.DeepEqual(current.Object["spec"], desired.Object["spec"]) +}