Skip to content

Commit ea63b3b

Browse files
authored
Merge pull request #336 from ChristopherFry/cfry/applyset-tool
fix: use apply set tooling in apply set applier
2 parents 78572f1 + 3e82b3e commit ea63b3b

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

applylib/applyset/applyset.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"reflect"
24+
"strings"
2425
"sync"
2526

2627
"k8s.io/apimachinery/pkg/api/meta"
@@ -70,7 +71,7 @@ type ApplySet struct {
7071
// deployment manifests.
7172
parent Parent
7273
// If not given, the tooling value will be the `Parent` Kind.
73-
tooling string
74+
tooling kubectlapply.ApplySetTooling
7475
}
7576

7677
// Options holds the parameters for building an ApplySet.
@@ -93,7 +94,16 @@ type Options struct {
9394
func New(options Options) (*ApplySet, error) {
9495
parent := options.Parent
9596
parentRef := &kubectlapply.ApplySetParentRef{Name: parent.Name(), Namespace: parent.Namespace(), RESTMapping: parent.RESTMapping()}
96-
kapplyset := kubectlapply.NewApplySet(parentRef, kubectlapply.ApplySetTooling{Name: options.Tooling}, options.RESTMapper)
97+
98+
// The tooling string slash cutting is to support the ApplySetTooling struct that kubectlapply.NewApplySet() expects.
99+
// For instance, 'kpt/v1.0.0' will map to ApplySetTooling{Name: "kpt", Version: "v1.0.0"}.
100+
toolName, toolVersion, _ := strings.Cut(options.Tooling, "/")
101+
if toolName == "" {
102+
toolName = parent.GetSubject().GetObjectKind().GroupVersionKind().Kind
103+
}
104+
tooling := kubectlapply.ApplySetTooling{Name: toolName, Version: toolVersion}
105+
106+
kapplyset := kubectlapply.NewApplySet(parentRef, tooling, options.RESTMapper)
97107
if options.PatchOptions.FieldManager == "" {
98108
options.PatchOptions.FieldManager = kapplyset.FieldManager()
99109
}
@@ -105,7 +115,7 @@ func New(options Options) (*ApplySet, error) {
105115
deleteOptions: options.DeleteOptions,
106116
prune: options.Prune,
107117
parent: parent,
108-
tooling: options.Tooling,
118+
tooling: tooling,
109119
}
110120
a.trackers = &objectTrackerList{}
111121
return a, nil
@@ -149,7 +159,7 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
149159
// single actuation and not for reconciliation.
150160
// Note: The Kubectl ApplySet will share the RESTMapper with k-d-p/ApplySet, which caches all the manifests in the past.
151161
parentRef := &kubectlapply.ApplySetParentRef{Name: a.parent.Name(), Namespace: a.parent.Namespace(), RESTMapping: a.parent.RESTMapping()}
152-
kapplyset := kubectlapply.NewApplySet(parentRef, kubectlapply.ApplySetTooling{Name: a.tooling}, a.restMapper)
162+
kapplyset := kubectlapply.NewApplySet(parentRef, a.tooling, a.restMapper)
153163

154164
// Cache the current RESTMappings to avoid re-fetching the bad ones.
155165
restMappings := make(map[schema.GroupVersionKind]restMappingResult)
@@ -365,7 +375,7 @@ func (a *ApplySet) WithParent(ctx context.Context, kapplyset *kubectlapply.Apply
365375
if annotations == nil {
366376
annotations = make(map[string]string)
367377
}
368-
annotations[kubectlapply.ApplySetToolingAnnotation] = a.tooling
378+
annotations[kubectlapply.ApplySetToolingAnnotation] = a.tooling.String()
369379
if _, ok := annotations[kubectlapply.ApplySetGRsAnnotation]; !ok {
370380
annotations[kubectlapply.ApplySetGRsAnnotation] = ""
371381
}

pkg/test/testreconciler/simpletest/testdata/reconcile/ssa/create/expected-http.yaml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,6 @@ Date: (removed)
257257

258258
---
259259

260-
PUT http://kube-apiserver/apis/addons.example.org/v1alpha1/namespaces/ns1/simpletests/simple1
261-
Accept: application/json, */*
262-
Content-Type: application/json
263-
264-
{"apiVersion":"addons.example.org/v1alpha1","kind":"SimpleTest","metadata":{"annotations":{"applyset.kubernetes.io/additional-namespaces":"","applyset.kubernetes.io/contains-group-resources":"configmaps,deployments.apps","applyset.kubernetes.io/tooling":"SimpleTest/"},"creationTimestamp":"2022-01-01T00:00:01Z","labels":{"applyset.kubernetes.io/id":"applyset-xbxAWnAItX3p1Gxrs86F-ZQAGwGoys9xxQGK3IED7bY-v1"},"name":"simple1","namespace":"ns1","resourceVersion":"6","uid":"00000000-0000-0000-0000-000000000002"},"spec":{"channel":"stable"},"status":{"conditions":[{"lastTransitionTime":"2022-01-01T00:00:00Z","message":"all manifests are reconciled.","reason":"Normal","status":"True","type":"Ready"}],"healthy":true,"phase":"Current"}}
265-
266-
200 OK
267-
Cache-Control: no-cache, private
268-
Content-Length: 736
269-
Content-Type: application/json
270-
Date: (removed)
271-
272-
{"apiVersion":"addons.example.org/v1alpha1","kind":"SimpleTest","metadata":{"annotations":{"applyset.kubernetes.io/additional-namespaces":"","applyset.kubernetes.io/contains-group-resources":"configmaps,deployments.apps","applyset.kubernetes.io/tooling":"SimpleTest/"},"creationTimestamp":"2022-01-01T00:00:01Z","labels":{"applyset.kubernetes.io/id":"applyset-xbxAWnAItX3p1Gxrs86F-ZQAGwGoys9xxQGK3IED7bY-v1"},"name":"simple1","namespace":"ns1","resourceVersion":"6","uid":"00000000-0000-0000-0000-000000000002"},"spec":{"channel":"stable"},"status":{"conditions":[{"lastTransitionTime":"2022-01-01T00:00:00Z","message":"all manifests are reconciled.","reason":"Normal","status":"True","type":"Ready"}],"healthy":true,"phase":"Current"}}
273-
274-
---
275-
276260
PATCH http://kube-apiserver/api/v1/namespaces/ns1/configmaps/foo?fieldManager=kdp-test&force=true
277261
Accept: application/json
278262
Content-Type: application/apply-patch+yaml

0 commit comments

Comments
 (0)