Skip to content

Commit 82df2e0

Browse files
committed
init commit for xset util package
1 parent 36b676a commit 82df2e0

33 files changed

+5960
-3
lines changed

controller/expectations/cache_expectation.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func NewxCacheExpectations(reader client.Reader, scheme *runtime.Scheme, clock c
6161
}
6262
}
6363

64+
func (r *CacheExpectations) CreateExpectations(controllerKey string) (*CacheExpectation, error) {
65+
return r.initExpectations(controllerKey)
66+
}
67+
6468
// GetExpectations returns the ControlleeExpectations of the given controller.
6569
func (r *CacheExpectations) GetExpectations(controllerKey string) (*CacheExpectation, bool, error) {
6670
exp, exists, err := r.GetByKey(controllerKey)
@@ -182,6 +186,16 @@ func (e *CacheExpectation) Fulfilled() bool {
182186
return satisfied
183187
}
184188

189+
func (e *CacheExpectation) FulFilledFor(gvk schema.GroupVersionKind, namespace, name string) bool {
190+
key := e.getKey(gvk, namespace, name)
191+
item, ok, err := e.items.GetByKey(key)
192+
if err != nil || !ok {
193+
return true
194+
}
195+
eitem := item.(*CacheExpectationItem)
196+
return eitem.Fulfilled()
197+
}
198+
185199
func (e *CacheExpectation) ExpectCreation(gvk schema.GroupVersionKind, namespace, name string) error {
186200
return e.expect(e.getKey(gvk, namespace, name), e.creationObserved(gvk, namespace, name))
187201
}

controller/merge/utils.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2024-2025 KusionStack Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package merge
18+
19+
import (
20+
"encoding/json"
21+
22+
"k8s.io/apimachinery/pkg/util/strategicpatch"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
)
25+
26+
// ThreeWayMergeToTarget Use three-way merge to get a updated instance.
27+
func ThreeWayMergeToTarget(currentRevisionTarget, updateRevisionTarget, currentTarget, emptyObj client.Object) error {
28+
currentRevisionTargetBytes, err := json.Marshal(currentRevisionTarget)
29+
if err != nil {
30+
return err
31+
}
32+
updateRevisionTargetBytes, err := json.Marshal(updateRevisionTarget)
33+
if err != nil {
34+
return err
35+
}
36+
37+
// 1. find the extra changes based on current revision
38+
patch, err := strategicpatch.CreateTwoWayMergePatch(currentRevisionTargetBytes, updateRevisionTargetBytes, emptyObj)
39+
if err != nil {
40+
return err
41+
}
42+
43+
// 2. apply above changes to current target object
44+
// We don't apply the diff between currentTarget and currentRevisionTarget to updateRevisionTarget,
45+
// because the TargetTemplate changes should have the highest priority.
46+
currentTargetBytes, err := json.Marshal(currentTarget)
47+
if err != nil {
48+
return err
49+
}
50+
if updateRevisionTargetBytes, err = strategicpatch.StrategicMergePatch(currentTargetBytes, patch, emptyObj); err != nil {
51+
return err
52+
}
53+
54+
err = json.Unmarshal(updateRevisionTargetBytes, currentTarget)
55+
return err
56+
}

0 commit comments

Comments
 (0)