Skip to content

Commit 242af30

Browse files
authored
check for gvk in the patcher (#3041)
1 parent 1d63275 commit 242af30

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

pkg/controller/state/patcher.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2121
"k8s.io/apimachinery/pkg/runtime"
2222
"sigs.k8s.io/controller-runtime/pkg/client"
23+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2324
)
2425

2526
const FieldOwner = "mongodb-atlas-kubernetes"
@@ -89,8 +90,14 @@ func (p *Patcher) patchObject(ctx context.Context, c client.Client) {
8990
return
9091
}
9192

92-
applyConfig := client.ApplyConfigurationFromUnstructured(p.patchedObj)
93-
err := c.Apply(ctx, applyConfig, client.FieldOwner(FieldOwner), client.ForceOwnership)
93+
patchedCopy, err := p.copyPatchedObject(c)
94+
if err != nil {
95+
p.err = err
96+
return
97+
}
98+
99+
applyConfig := client.ApplyConfigurationFromUnstructured(patchedCopy)
100+
err = c.Apply(ctx, applyConfig, client.FieldOwner(FieldOwner), client.ForceOwnership)
94101
p.err = err
95102
}
96103

@@ -99,13 +106,31 @@ func (p *Patcher) patchStatus(ctx context.Context, c client.Client) {
99106
return
100107
}
101108

109+
patchedCopy, err := p.copyPatchedObject(c)
110+
if err != nil {
111+
p.err = err
112+
return
113+
}
114+
102115
// SSA Apply() method for sub-resources is not yet supported, so we use Patch here.
103116
// See the following issue for more details: https://github.com/kubernetes-sigs/controller-runtime/issues/3183
104-
patchedCopy := p.patchedObj.DeepCopy()
105-
err := c.Status().Patch(ctx, patchedCopy, client.Apply, client.FieldOwner(FieldOwner), client.ForceOwnership)
117+
err = c.Status().Patch(ctx, patchedCopy, client.Apply, client.FieldOwner(FieldOwner), client.ForceOwnership)
106118
p.err = err
107119
}
108120

121+
func (p *Patcher) copyPatchedObject(c client.Client) (*unstructured.Unstructured, error) {
122+
patchedCopy := p.patchedObj.DeepCopy()
123+
if patchedCopy.GetObjectKind().GroupVersionKind().Empty() {
124+
gvk, err := apiutil.GVKForObject(p.obj, c.Scheme())
125+
if err != nil {
126+
return nil, err
127+
}
128+
patchedCopy.SetAPIVersion(gvk.GroupVersion().String())
129+
patchedCopy.SetKind(gvk.Kind)
130+
}
131+
return patchedCopy, nil
132+
}
133+
109134
// Patch applies the patches to the given object and updates both status and the annotations if they were modified.
110135
func (p *Patcher) Patch(ctx context.Context, c client.Client) error {
111136
if p.err != nil {

pkg/controller/state/patcher_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ func TestPatcher(t *testing.T) {
3636
_ = appsv1.AddToScheme(scheme)
3737

3838
baseDeployment := &appsv1.Deployment{
39-
TypeMeta: metav1.TypeMeta{
40-
APIVersion: "v1",
41-
Kind: "Deployment",
42-
},
4339
ObjectMeta: metav1.ObjectMeta{
4440
Name: "test",
4541
Namespace: "default",
@@ -71,10 +67,6 @@ func TestPatcher(t *testing.T) {
7167
},
7268
wantErr: "",
7369
wantObj: &appsv1.Deployment{
74-
TypeMeta: metav1.TypeMeta{
75-
APIVersion: "v1",
76-
Kind: "Deployment",
77-
},
7870
ObjectMeta: metav1.ObjectMeta{
7971
Name: "test",
8072
Namespace: "default",
@@ -91,10 +83,6 @@ func TestPatcher(t *testing.T) {
9183
},
9284
wantErr: "",
9385
wantObj: &appsv1.Deployment{
94-
TypeMeta: metav1.TypeMeta{
95-
APIVersion: "v1",
96-
Kind: "Deployment",
97-
},
9886
ObjectMeta: metav1.ObjectMeta{
9987
Name: "test",
10088
Namespace: "default",
@@ -113,10 +101,6 @@ func TestPatcher(t *testing.T) {
113101
},
114102
wantErr: "",
115103
wantObj: &appsv1.Deployment{
116-
TypeMeta: metav1.TypeMeta{
117-
APIVersion: "v1",
118-
Kind: "Deployment",
119-
},
120104
ObjectMeta: metav1.ObjectMeta{
121105
Name: "test",
122106
Namespace: "default",
@@ -136,10 +120,6 @@ func TestPatcher(t *testing.T) {
136120
},
137121
wantErr: "",
138122
wantObj: &appsv1.Deployment{
139-
TypeMeta: metav1.TypeMeta{
140-
APIVersion: "v1",
141-
Kind: "Deployment",
142-
},
143123
ObjectMeta: metav1.ObjectMeta{
144124
Name: "test",
145125
Namespace: "default",

0 commit comments

Comments
 (0)