Skip to content

Commit a9a0d76

Browse files
committed
refactor: replace unstructured with ResourceGroup type
1 parent 48833b9 commit a9a0d76

File tree

4 files changed

+31
-49
lines changed

4 files changed

+31
-49
lines changed

pkg/applier/applier.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/GoogleContainerTools/config-sync/pkg/api/configsync"
25+
kptv1alpha1 "github.com/GoogleContainerTools/config-sync/pkg/api/kpt.dev/v1alpha1"
2526
"github.com/GoogleContainerTools/config-sync/pkg/applier/stats"
2627
"github.com/GoogleContainerTools/config-sync/pkg/core"
2728
"github.com/GoogleContainerTools/config-sync/pkg/declared"
@@ -186,21 +187,23 @@ func NewSupervisor(cs *ClientSet, scope declared.Scope, syncName string, reconci
186187
// trying to add any new resources from source.
187188
func (s *supervisor) UpdateStatusMode(ctx context.Context) error {
188189
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
189-
u := newInventoryUnstructured(s.syncName, s.syncNamespace)
190-
key := client.ObjectKeyFromObject(u)
191-
err := s.clientSet.Client.Get(ctx, key, u)
190+
var rg kptv1alpha1.ResourceGroup
191+
rg.SetGroupVersionKind(kinds.ResourceGroup())
192+
key := resourcegroup.ObjectKey(s.syncName, s.syncNamespace)
193+
err := s.clientSet.Client.Get(ctx, key, &rg)
194+
192195
if err != nil {
193196
// RG doesn't exist, it will be created by applier with appropriate status mode
194197
if apierrors.IsNotFound(err) {
195198
return nil
196199
}
197-
return status.APIServerErrorf(err, "failed to get %s: %s", u.GetKind(), key)
200+
return status.APIServerErrorf(err, "failed to get %v: %s", rg.GetObjectKind().GroupVersionKind(), key)
198201
}
199-
if core.SetAnnotation(u, metadata.StatusModeAnnotationKey, s.clientSet.StatusMode.String()) {
200-
klog.V(3).Infof("Updating %s annotation: %s: %s", u.GetKind(), metadata.StatusModeAnnotationKey, s.clientSet.StatusMode)
201-
err := s.clientSet.Client.Update(ctx, u, client.FieldOwner(configsync.FieldManager))
202+
if core.SetAnnotation(&rg, metadata.StatusModeAnnotationKey, s.clientSet.StatusMode.String()) {
203+
klog.V(3).Infof("Updating %v annotation: %s: %s", rg.GetObjectKind().GroupVersionKind(), metadata.StatusModeAnnotationKey, s.clientSet.StatusMode)
204+
err := s.clientSet.Client.Update(ctx, &rg, client.FieldOwner(configsync.FieldManager))
202205
if err != nil {
203-
return status.APIServerErrorf(err, "failed to update %s: %s", u.GetKind(), key)
206+
return status.APIServerErrorf(err, "failed to update %v: %s", rg.GetObjectKind().GroupVersionKind(), key)
204207
}
205208
}
206209
return nil
@@ -511,10 +514,13 @@ func handleMetrics(ctx context.Context, operation string, err error) {
511514
// checkInventoryObjectSize checks the inventory object size limit.
512515
// If it is close to the size limit 1M, log a warning.
513516
func (s *supervisor) checkInventoryObjectSize(ctx context.Context, c client.Client) {
514-
u := newInventoryUnstructured(s.syncName, s.syncNamespace)
515-
err := c.Get(ctx, client.ObjectKey{Namespace: s.syncNamespace, Name: s.syncName}, u)
517+
var rg kptv1alpha1.ResourceGroup
518+
rg.SetGroupVersionKind(kinds.ResourceGroup())
519+
key := resourcegroup.ObjectKey(s.syncName, s.syncNamespace)
520+
err := c.Get(ctx, key, &rg)
521+
516522
if err == nil {
517-
size, err := getObjectSize(u)
523+
size, err := getObjectSize(&rg)
518524
if err != nil {
519525
klog.Warningf("Failed to marshal ResourceGroup %s/%s to get its size: %s", s.syncNamespace, s.syncName, err)
520526
}
@@ -746,13 +752,6 @@ func (s *supervisor) Destroy(ctx context.Context, eventHandler func(Event)) (Obj
746752
return s.destroyInner(ctx, eventHandler)
747753
}
748754

749-
// newInventoryUnstructured creates an inventory object as an unstructured.
750-
func newInventoryUnstructured(name, namespace string) *unstructured.Unstructured {
751-
id := InventoryID(name, namespace)
752-
u := resourcegroup.Unstructured(name, namespace, id)
753-
return u
754-
}
755-
756755
// InventoryID returns the inventory id of an inventory object.
757756
// The inventory object generated by ConfigSync is in the same namespace as RootSync or RepoSync.
758757
// The inventory ID is assigned as <NAMESPACE>_<NAME>.

pkg/applier/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func removeFrom(all []object.ObjMetadata, toRemove []client.Object) []object.Obj
136136
return results
137137
}
138138

139-
func getObjectSize(u *unstructured.Unstructured) (int, error) {
140-
data, err := json.Marshal(u)
139+
func getObjectSize(rg client.Object) (int, error) {
140+
data, err := json.Marshal(rg)
141141
if err != nil {
142142
return 0, err
143143
}

pkg/applier/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ func TestRemoveFrom(t *testing.T) {
171171
}
172172

173173
func TestGetObjectSize(t *testing.T) {
174-
u := newInventoryUnstructured("inv-1", "test")
175-
size, err := getObjectSize(u)
174+
rg := k8sobjects.ResourceGroupObject("inv-1", "test")
175+
size, err := getObjectSize(rg)
176176
if err != nil {
177177
t.Fatal(err)
178178
}

pkg/resourcegroup/resourcegroup.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,11 @@
1515
package resourcegroup
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/GoogleContainerTools/config-sync/pkg/api/kpt.dev/v1alpha1"
2119
"github.com/GoogleContainerTools/config-sync/pkg/metadata"
22-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23-
"sigs.k8s.io/cli-utils/pkg/common"
20+
"sigs.k8s.io/controller-runtime/pkg/client"
2421
)
2522

26-
// Unstructured creates a ResourceGroup object
27-
// TODO: Replace Unstructured with kptv1alpha1.ResourceGroup
28-
func Unstructured(name, namespace, id string) *unstructured.Unstructured {
29-
groupVersion := fmt.Sprintf("%s/%s", v1alpha1.SchemeGroupVersionKind().Group, v1alpha1.SchemeGroupVersionKind().Version)
30-
inventoryObj := &unstructured.Unstructured{
31-
Object: map[string]interface{}{
32-
"apiVersion": groupVersion,
33-
"kind": v1alpha1.SchemeGroupVersionKind().Kind,
34-
"metadata": map[string]interface{}{
35-
"name": name,
36-
"namespace": namespace,
37-
"labels": map[string]interface{}{
38-
common.InventoryLabel: id,
39-
},
40-
},
41-
"spec": map[string]interface{}{
42-
"resources": []interface{}{},
43-
},
44-
},
45-
}
46-
return inventoryObj
47-
}
48-
4923
// GetSourceHash returns the source hash that is defined in the
5024
// source hash annotation.
5125
func GetSourceHash(annotations map[string]string) string {
@@ -72,3 +46,12 @@ func IsStatusDisabled(resgroup *v1alpha1.ResourceGroup) bool {
7246
val, found := annotations[metadata.StatusModeAnnotationKey]
7347
return found && val == metadata.StatusDisabled.String()
7448
}
49+
50+
// ObjectKey returns a key appropriate for fetching a ResourceGroup in the given
51+
// namespace.
52+
func ObjectKey(name, namespace string) client.ObjectKey {
53+
return client.ObjectKey{
54+
Namespace: namespace,
55+
Name: name,
56+
}
57+
}

0 commit comments

Comments
 (0)