Skip to content

Commit acf99c6

Browse files
authored
applyset: introduce abstraction over objects (#3077)
This lets us use typed and untyped objects.
1 parent 37a6c40 commit acf99c6

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

porch/controllers/remoterootsync/pkg/applyset/applyset.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ package applyset
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"fmt"
2021
"sync"
2122

2223
"k8s.io/apimachinery/pkg/api/meta"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2525
"k8s.io/apimachinery/pkg/runtime/schema"
2626
"k8s.io/apimachinery/pkg/types"
2727
"k8s.io/client-go/dynamic"
@@ -75,7 +75,7 @@ func New(options Options) (*ApplySet, error) {
7575

7676
// ReplaceAllObjects is used to replace the desired state of all the objects.
7777
// Any objects not specified are removed from the "desired" set.
78-
func (a *ApplySet) ReplaceAllObjects(objects []*unstructured.Unstructured) error {
78+
func (a *ApplySet) ReplaceAllObjects(objects []ApplyableObject) error {
7979
a.mutex.Lock()
8080
defer a.mutex.Unlock()
8181

@@ -161,7 +161,7 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
161161

162162
name := obj.GetName()
163163
ns := obj.GetNamespace()
164-
gvk := obj.GetObjectKind().GroupVersionKind()
164+
gvk := obj.GroupVersionKind()
165165
nn := types.NamespacedName{Namespace: ns, Name: name}
166166

167167
restMapping, err := a.restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
@@ -190,7 +190,7 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
190190
return nil, fmt.Errorf("unknown scope for gvk %s: %q", gvk, restMapping.Scope.Name())
191191
}
192192

193-
j, err := obj.MarshalJSON()
193+
j, err := json.Marshal(obj)
194194
if err != nil {
195195
// TODO: Differentiate between server-fixable vs client-fixable errors?
196196
results.applyError(gvk, nn, fmt.Errorf("failed to marshal object to JSON: %w", err))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package applyset
16+
17+
import (
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
"k8s.io/apimachinery/pkg/runtime/schema"
20+
)
21+
22+
type ApplyableObject interface {
23+
GroupVersionKind() schema.GroupVersionKind
24+
GetNamespace() string
25+
GetName() string
26+
27+
GetOwnerReferences() []metav1.OwnerReference
28+
SetOwnerReferences([]metav1.OwnerReference)
29+
}

porch/controllers/remoterootsync/pkg/applyset/tracker.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package applyset
1717
import (
1818
"reflect"
1919

20-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2120
"k8s.io/apimachinery/pkg/runtime"
2221
)
2322

@@ -30,7 +29,7 @@ type objectTrackerList struct {
3029

3130
// objectTracker tracks the state for a single object
3231
type objectTracker struct {
33-
desired *unstructured.Unstructured
32+
desired ApplyableObject
3433
lastApplied runtime.Object
3534

3635
desiredIsApplied bool
@@ -47,7 +46,7 @@ type objectKey struct {
4746
}
4847

4948
// computeKey returns the unique key for the object.
50-
func computeKey(u *unstructured.Unstructured) objectKey {
49+
func computeKey(u ApplyableObject) objectKey {
5150
gvk := u.GroupVersionKind()
5251
return objectKey{
5352
Group: gvk.Group,
@@ -61,7 +60,7 @@ func computeKey(u *unstructured.Unstructured) objectKey {
6160
// replaceAllObjects completely replaces the set of objects we are interested in.
6261
// We aim to reuse the current state where it carries over.
6362
// Because objectTrackerList is immutable, we copy-on-write to a new objectTrackerList and return it.
64-
func (l *objectTrackerList) replaceAllObjects(objects []*unstructured.Unstructured) *objectTrackerList {
63+
func (l *objectTrackerList) replaceAllObjects(objects []ApplyableObject) *objectTrackerList {
6564
existingTrackers := make(map[objectKey]*objectTracker)
6665
for i := range l.items {
6766
tracker := &l.items[i]

porch/controllers/remoterootsync/pkg/controllers/remoterootsyncset/remoterootsync_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (r *RemoteRootSyncSetReconciler) applyToClusterRef(ctx context.Context, sub
291291
}
292292

293293
// BuildObjectsToApply config root sync
294-
func (r *RemoteRootSyncSetReconciler) BuildObjectsToApply(ctx context.Context, subject *api.RemoteRootSyncSet) ([]*unstructured.Unstructured, error) {
294+
func (r *RemoteRootSyncSetReconciler) BuildObjectsToApply(ctx context.Context, subject *api.RemoteRootSyncSet) ([]applyset.ApplyableObject, error) {
295295
repository := subject.GetSpec().GetTemplate().GetOCI().GetRepository()
296296
if repository == "" {
297297
return nil, fmt.Errorf("spec.template.oci.repository is not set")
@@ -312,7 +312,7 @@ func (r *RemoteRootSyncSetReconciler) BuildObjectsToApply(ctx context.Context, s
312312
return nil, err
313313
}
314314

315-
var objects []*unstructured.Unstructured
315+
var objects []applyset.ApplyableObject
316316

317317
for filePath, fileContents := range resources.Contents {
318318
ext := path.Ext(filePath)

0 commit comments

Comments
 (0)