Skip to content

Commit

Permalink
fix: ignore non-spec fields update
Browse files Browse the repository at this point in the history
Signed-off-by: minhthong582000 <[email protected]>
  • Loading branch information
minhthong582000 committed Nov 4, 2024
1 parent 73d1bdd commit e3696fe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
11 changes: 10 additions & 1 deletion gitops/example/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: example-application
name: nginx-application
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
path: gitops/example/nginx
---
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: ubuntu-application
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
path: gitops/example/ubuntu
25 changes: 25 additions & 0 deletions gitops/example/ubuntu/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: ubuntu-deployment
labels:
app: ubuntu
spec:
selector:
matchLabels:
app: ubuntu
replicas: 1
template:
metadata:
labels:
app: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu:22.04
resources:
requests:
cpu: 100m
memory: 512Mi
limits:
memory: 512Mi
5 changes: 5 additions & 0 deletions gitops/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ func (c *Controller) handleUdate(old, new interface{}) {
return
}

// If there are changes in fields other than spec, we don't need to reconcile
if !equality.Semantic.DeepEqual(oldApp.ObjectMeta, newApp.ObjectMeta) || !equality.Semantic.DeepEqual(oldApp.Status, newApp.Status) {
return
}

// Compare old and new spec
if equality.Semantic.DeepEqual(oldApp.Spec, newApp.Spec) {
log.Debugf("No changes in application spec: %s", newApp.Name)
Expand Down
7 changes: 4 additions & 3 deletions gitops/utils/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync"

log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/equality"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -232,7 +232,8 @@ func (k *k8s) DiffResources(current []*unstructured.Unstructured, new []*unstruc
continue
}

if !reflect.DeepEqual(c.Object["spec"], n.Object["spec"]) {
// TODO: handle kubernetes default values or else the comparison is useless
if !equality.Semantic.DeepEqual(c.Object["spec"], n.Object["spec"]) {
log.Debugf("Resource %s with name %s has changed", n.GetKind(), n.GetName())
isChanged = true
}
Expand All @@ -242,7 +243,7 @@ func (k *k8s) DiffResources(current []*unstructured.Unstructured, new []*unstruc
// Check if there are resources that need to be deleted
if len(hashTable) > 0 {
for _, c := range hashTable {
log.Debugf("Resource %s with name %s should be deleted", c.GetKind(), c.GetName())
log.Debugf("Resource %s with name %s not found, could be deleted or a child resource", c.GetKind(), c.GetName())
}
isChanged = true
}
Expand Down

0 comments on commit e3696fe

Please sign in to comment.