Skip to content

Commit

Permalink
Expose pendingChange
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangpengcheng committed Sep 13, 2024
1 parent ad35675 commit 06ed371
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/compute/v1alpha1/function_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type FunctionStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
GlobalBackendConfigRevision string `json:"globalBackendConfigRevision,omitempty"`
NamespacedBackendConfigRevision string `json:"namespacedBackendConfigRevision,omitempty"`
PendingChange string `json:"pendingChange,omitempty"`
}

// +genclient
Expand Down
1 change: 1 addition & 0 deletions api/compute/v1alpha1/sink_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type SinkStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
GlobalBackendConfigRevision string `json:"globalBackendConfigRevision,omitempty"`
NamespacedBackendConfigRevision string `json:"namespacedBackendConfigRevision,omitempty"`
PendingChange string `json:"pendingChange,omitempty"`
}

// +genclient
Expand Down
1 change: 1 addition & 0 deletions api/compute/v1alpha1/source_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type SourceStatus struct {
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
GlobalBackendConfigRevision string `json:"globalBackendConfigRevision,omitempty"`
NamespacedBackendConfigRevision string `json:"namespacedBackendConfigRevision,omitempty"`
PendingChange string `json:"pendingChange,omitempty"`
}

// +genclient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3846,6 +3846,8 @@ spec:
observedGeneration:
format: int64
type: integer
pendingChange:
type: string
replicas:
format: int32
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,8 @@ spec:
observedGeneration:
format: int64
type: integer
pendingChange:
type: string
replicas:
format: int32
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3546,6 +3546,8 @@ spec:
observedGeneration:
format: int64
type: integer
pendingChange:
type: string
replicas:
format: int32
type: integer
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/compute.functionmesh.io_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3824,6 +3824,8 @@ spec:
observedGeneration:
format: int64
type: integer
pendingChange:
type: string
replicas:
format: int32
type: integer
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/compute.functionmesh.io_sinks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3543,6 +3543,8 @@ spec:
observedGeneration:
format: int64
type: integer
pendingChange:
type: string
replicas:
format: int32
type: integer
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/compute.functionmesh.io_sources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3524,6 +3524,8 @@ spec:
observedGeneration:
format: int64
type: integer
pendingChange:
type: string
replicas:
format: int32
type: integer
Expand Down
5 changes: 5 additions & 0 deletions controllers/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ func (r *FunctionReconciler) checkIfStatefulSetNeedUpdate(ctx context.Context, s
if err != nil {
return false, err
}
diff, err := spec.CreateDiff(statefulSet, desiredStatefulSet)
if err != nil {
return false, err
}
function.Status.PendingChange = diff
return !spec.CheckIfStatefulSetSpecIsEqual(&statefulSet.Spec, &desiredStatefulSet.Spec), nil
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/function_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func (r *FunctionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
} else {
function.Status.PendingChange = ""
}

err = r.ObserveFunctionService(ctx, function)
Expand Down
5 changes: 5 additions & 0 deletions controllers/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ func (r *SinkReconciler) checkIfStatefulSetNeedUpdate(ctx context.Context, state
if err != nil {
return false, err
}
diff, err := spec.CreateDiff(statefulSet, desiredStatefulSet)
if err != nil {
return false, err
}
sink.Status.PendingChange = diff
return !spec.CheckIfStatefulSetSpecIsEqual(&statefulSet.Spec, &desiredStatefulSet.Spec), nil
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/sink_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (r *SinkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
} else {
sink.Status.PendingChange = ""
}

err = r.ObserveSinkService(ctx, sink)
Expand Down
5 changes: 5 additions & 0 deletions controllers/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ func (r *SourceReconciler) checkIfStatefulSetNeedUpdate(ctx context.Context, sta
if err != nil {
return false, err
}
diff, err := spec.CreateDiff(statefulSet, desiredStatefulSet)
if err != nil {
return false, err
}
source.Status.PendingChange = diff
return !spec.CheckIfStatefulSetSpecIsEqual(&statefulSet.Spec, &desiredStatefulSet.Spec), nil
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/source_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (r *SourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
} else {
source.Status.PendingChange = ""
}

err = r.ObserveSourceService(ctx, source)
Expand Down
21 changes: 21 additions & 0 deletions controllers/spec/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
Expand Down Expand Up @@ -2374,3 +2376,22 @@ func makeFilebeatContainer(volumeMounts []corev1.VolumeMount, envVar []corev1.En
},
}
}

func CreateDiff(orj, modified runtime.Object) (string, error) {
orjCopy := orj.DeepCopyObject()
modifiedCopy := modified.DeepCopyObject()

orjData, err := json.Marshal(orjCopy)
if err != nil {
return "", fmt.Errorf("marshal origin %w", err)
}
modifiedData, err := json.Marshal(modifiedCopy)
if err != nil {
return "", fmt.Errorf("marshal modified %w", err)
}
patch, err := strategicpatch.CreateTwoWayMergePatch(orjData, modifiedData, orj)
if err != nil {
return "", fmt.Errorf("create diff %w", err)
}
return string(patch), nil
}

0 comments on commit 06ed371

Please sign in to comment.