Skip to content

Commit

Permalink
Add update method to cilium resource (#1525)
Browse files Browse the repository at this point in the history
* add update method to ciliumnetpol resource

* changelog

* fix method name
  • Loading branch information
QuantumEnigmaa authored Feb 13, 2024
1 parent e9799cb commit 23b5483
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions service/controller/resource/ciliumnetpol/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
6 changes: 6 additions & 0 deletions service/controller/resource/ciliumnetpol/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ciliumnetpol

import (
"reflect"

"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -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"])
}

0 comments on commit 23b5483

Please sign in to comment.