Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions pkg/applier/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/GoogleContainerTools/config-sync/pkg/api/configsync"
kptv1alpha1 "github.com/GoogleContainerTools/config-sync/pkg/api/kpt.dev/v1alpha1"
"github.com/GoogleContainerTools/config-sync/pkg/applier/stats"
"github.com/GoogleContainerTools/config-sync/pkg/core"
"github.com/GoogleContainerTools/config-sync/pkg/declared"
Expand Down Expand Up @@ -186,21 +187,23 @@ func NewSupervisor(cs *ClientSet, scope declared.Scope, syncName string, reconci
// trying to add any new resources from source.
func (s *supervisor) UpdateStatusMode(ctx context.Context) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
u := newInventoryUnstructured(s.syncName, s.syncNamespace)
key := client.ObjectKeyFromObject(u)
err := s.clientSet.Client.Get(ctx, key, u)
var rg kptv1alpha1.ResourceGroup
rg.SetGroupVersionKind(kinds.ResourceGroup())
key := resourcegroup.ObjectKey(s.syncName, s.syncNamespace)
err := s.clientSet.Client.Get(ctx, key, &rg)

if err != nil {
// RG doesn't exist, it will be created by applier with appropriate status mode
if apierrors.IsNotFound(err) {
return nil
}
return status.APIServerErrorf(err, "failed to get %s: %s", u.GetKind(), key)
return status.APIServerErrorf(err, "failed to get %v: %s", rg.GetObjectKind().GroupVersionKind(), key)
}
if core.SetAnnotation(u, metadata.StatusModeAnnotationKey, s.clientSet.StatusMode.String()) {
klog.V(3).Infof("Updating %s annotation: %s: %s", u.GetKind(), metadata.StatusModeAnnotationKey, s.clientSet.StatusMode)
err := s.clientSet.Client.Update(ctx, u, client.FieldOwner(configsync.FieldManager))
if core.SetAnnotation(&rg, metadata.StatusModeAnnotationKey, s.clientSet.StatusMode.String()) {
klog.V(3).Infof("Updating %v annotation: %s: %s", rg.GetObjectKind().GroupVersionKind(), metadata.StatusModeAnnotationKey, s.clientSet.StatusMode)
err := s.clientSet.Client.Update(ctx, &rg, client.FieldOwner(configsync.FieldManager))
if err != nil {
return status.APIServerErrorf(err, "failed to update %s: %s", u.GetKind(), key)
return status.APIServerErrorf(err, "failed to update %v: %s", rg.GetObjectKind().GroupVersionKind(), key)
}
}
return nil
Expand Down Expand Up @@ -511,10 +514,13 @@ func handleMetrics(ctx context.Context, operation string, err error) {
// checkInventoryObjectSize checks the inventory object size limit.
// If it is close to the size limit 1M, log a warning.
func (s *supervisor) checkInventoryObjectSize(ctx context.Context, c client.Client) {
u := newInventoryUnstructured(s.syncName, s.syncNamespace)
err := c.Get(ctx, client.ObjectKey{Namespace: s.syncNamespace, Name: s.syncName}, u)
var rg kptv1alpha1.ResourceGroup
rg.SetGroupVersionKind(kinds.ResourceGroup())
key := resourcegroup.ObjectKey(s.syncName, s.syncNamespace)
err := c.Get(ctx, key, &rg)

if err == nil {
size, err := getObjectSize(u)
size, err := getObjectSize(&rg)
if err != nil {
klog.Warningf("Failed to marshal ResourceGroup %s/%s to get its size: %s", s.syncNamespace, s.syncName, err)
}
Expand Down Expand Up @@ -746,13 +752,6 @@ func (s *supervisor) Destroy(ctx context.Context, eventHandler func(Event)) (Obj
return s.destroyInner(ctx, eventHandler)
}

// newInventoryUnstructured creates an inventory object as an unstructured.
func newInventoryUnstructured(name, namespace string) *unstructured.Unstructured {
id := InventoryID(name, namespace)
u := resourcegroup.Unstructured(name, namespace, id)
return u
}

// InventoryID returns the inventory id of an inventory object.
// The inventory object generated by ConfigSync is in the same namespace as RootSync or RepoSync.
// The inventory ID is assigned as <NAMESPACE>_<NAME>.
Expand Down
4 changes: 2 additions & 2 deletions pkg/applier/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func removeFrom(all []object.ObjMetadata, toRemove []client.Object) []object.Obj
return results
}

func getObjectSize(u *unstructured.Unstructured) (int, error) {
data, err := json.Marshal(u)
func getObjectSize(rg client.Object) (int, error) {
data, err := json.Marshal(rg)
if err != nil {
return 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/applier/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func TestRemoveFrom(t *testing.T) {
}

func TestGetObjectSize(t *testing.T) {
u := newInventoryUnstructured("inv-1", "test")
size, err := getObjectSize(u)
rg := k8sobjects.ResourceGroupObject("inv-1", "test")
size, err := getObjectSize(rg)
if err != nil {
t.Fatal(err)
}
Expand Down
37 changes: 10 additions & 27 deletions pkg/resourcegroup/resourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,11 @@
package resourcegroup

import (
"fmt"

"github.com/GoogleContainerTools/config-sync/pkg/api/kpt.dev/v1alpha1"
"github.com/GoogleContainerTools/config-sync/pkg/metadata"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Unstructured creates a ResourceGroup object
// TODO: Replace Unstructured with kptv1alpha1.ResourceGroup
func Unstructured(name, namespace, id string) *unstructured.Unstructured {
groupVersion := fmt.Sprintf("%s/%s", v1alpha1.SchemeGroupVersionKind().Group, v1alpha1.SchemeGroupVersionKind().Version)
inventoryObj := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": groupVersion,
"kind": v1alpha1.SchemeGroupVersionKind().Kind,
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
"labels": map[string]interface{}{
common.InventoryLabel: id,
},
},
"spec": map[string]interface{}{
"resources": []interface{}{},
},
},
}
return inventoryObj
}

// GetSourceHash returns the source hash that is defined in the
// source hash annotation.
func GetSourceHash(annotations map[string]string) string {
Expand All @@ -72,3 +46,12 @@ func IsStatusDisabled(resgroup *v1alpha1.ResourceGroup) bool {
val, found := annotations[metadata.StatusModeAnnotationKey]
return found && val == metadata.StatusDisabled.String()
}

// ObjectKey returns a key appropriate for fetching a ResourceGroup in the given
// namespace.
func ObjectKey(name, namespace string) client.ObjectKey {
return client.ObjectKey{
Namespace: namespace,
Name: name,
}
}