Skip to content

Commit 7dac2a8

Browse files
Merge pull request #225 from vmware-tanzu/kp-image-trigger-patch
use patch operation for kp image trigger
2 parents 6410a6b + 783f1d9 commit 7dac2a8

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

pkg/commands/image/trigger.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/pkg/errors"
1313
"github.com/spf13/cobra"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/apimachinery/pkg/types"
1516

1617
"github.com/vmware-tanzu/kpack-cli/pkg/build"
1718
"github.com/vmware-tanzu/kpack-cli/pkg/commands"
@@ -53,9 +54,16 @@ The namespace defaults to the kubernetes current-context namespace.`,
5354
} else {
5455
sort.Slice(buildList.Items, build.Sort(buildList.Items))
5556

56-
build := buildList.Items[len(buildList.Items)-1].DeepCopy()
57-
build.Annotations[BuildNeededAnnotation] = time.Now().String()
58-
_, err := cs.KpackClient.KpackV1alpha1().Builds(cs.Namespace).Update(ctx, build, metav1.UpdateOptions{})
57+
original := buildList.Items[len(buildList.Items)-1].DeepCopy()
58+
patched := original.DeepCopy()
59+
patched.Annotations[BuildNeededAnnotation] = time.Now().String()
60+
61+
patch, err := k8s.CreatePatch(original, patched)
62+
if err != nil {
63+
return err
64+
}
65+
66+
_, err = cs.KpackClient.KpackV1alpha1().Builds(cs.Namespace).Patch(ctx, original.Name, types.MergePatchType, patch, metav1.PatchOptions{})
5967
if err != nil {
6068
return err
6169
}

pkg/commands/image/trigger_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package image_test
55

66
import (
77
"bytes"
8+
"encoding/json"
89
"testing"
910

10-
"github.com/pivotal/kpack/pkg/apis/build/v1alpha1"
1111
"github.com/pivotal/kpack/pkg/client/clientset/versioned/fake"
1212
"github.com/sclevine/spec"
1313
"github.com/stretchr/testify/require"
@@ -47,10 +47,14 @@ func testImageTrigger(t *testing.T, when spec.G, it spec.S) {
4747
actions, err := testhelpers.ActionRecorderList{clientSet}.ActionsByVerb()
4848
require.NoError(t, err)
4949

50-
require.Len(t, actions.Updates, 1)
51-
build := actions.Updates[0].GetObject().(*v1alpha1.Build)
52-
require.Equal(t, build.Name, "build-three")
53-
require.NotEmpty(t, build.Annotations[image.BuildNeededAnnotation])
50+
require.Len(t, actions.Patches, 1)
51+
52+
var patch buildNeededPatch
53+
err = json.Unmarshal(actions.Patches[0].GetPatch(), &patch)
54+
require.NoError(t, err)
55+
56+
require.Equal(t, actions.Patches[0].GetName(), "build-three")
57+
require.NotEmpty(t, patch.Metadata.Annotations.BuildNeededAnnotation)
5458
})
5559
})
5660

@@ -88,10 +92,14 @@ func testImageTrigger(t *testing.T, when spec.G, it spec.S) {
8892
actions, err := testhelpers.ActionRecorderList{clientSet}.ActionsByVerb()
8993
require.NoError(t, err)
9094

91-
require.Len(t, actions.Updates, 1)
92-
build := actions.Updates[0].GetObject().(*v1alpha1.Build)
93-
require.Equal(t, build.Name, "build-three")
94-
require.NotEmpty(t, build.Annotations[image.BuildNeededAnnotation])
95+
require.Len(t, actions.Patches, 1)
96+
97+
var patch buildNeededPatch
98+
err = json.Unmarshal(actions.Patches[0].GetPatch(), &patch)
99+
require.NoError(t, err)
100+
101+
require.Equal(t, actions.Patches[0].GetName(), "build-three")
102+
require.NotEmpty(t, patch.Metadata.Annotations.BuildNeededAnnotation)
95103
})
96104
})
97105

@@ -111,3 +119,11 @@ func testImageTrigger(t *testing.T, when spec.G, it spec.S) {
111119
})
112120
})
113121
}
122+
123+
type buildNeededPatch struct {
124+
Metadata struct {
125+
Annotations struct {
126+
BuildNeededAnnotation string `json:"image.kpack.io/additionalBuildNeeded"`
127+
} `json:"annotations"`
128+
} `json:"metadata"`
129+
}

0 commit comments

Comments
 (0)