Skip to content

Commit

Permalink
Generate deepcopy methodsets for Unstructured types
Browse files Browse the repository at this point in the history
Removes the tests - we don't need to test generated code.

Signed-off-by: Nic Cope <nicc@rk0n.org>
  • Loading branch information
negz committed Nov 7, 2023
1 parent d54c604 commit fab7762
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 110 deletions.
26 changes: 3 additions & 23 deletions pkg/resource/unstructured/claim/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -56,6 +55,9 @@ func New(opts ...Option) *Unstructured {
return c
}

// +k8s:deepcopy-gen=true
// +kubebuilder:object:root=true

// An Unstructured composite resource claim.
type Unstructured struct {
unstructured.Unstructured
Expand All @@ -76,28 +78,6 @@ type Reference struct {
Namespace string `json:"namespace"`
}

// DeepCopy returns a deep copy of the receiver.
func (c *Unstructured) DeepCopy() *Unstructured {
if c == nil {
return nil
}
out := new(Unstructured)
*out = *c
out.Object = runtime.DeepCopyJSON(c.Object)
return out
}

// DeepCopyInto copies the receiver, writing into out. in must be non-nil.
func (c *Unstructured) DeepCopyInto(out *Unstructured) {
clone := c.DeepCopy()
*out = *clone
}

// DeepCopyObject copies the receiver, creating a new runtime.Object.
func (c *Unstructured) DeepCopyObject() runtime.Object {
return c.DeepCopy()
}

// GetUnstructured returns the underlying *unstructured.Unstructured.
func (c *Unstructured) GetUnstructured() *unstructured.Unstructured {
return &c.Unstructured
Expand Down
14 changes: 0 additions & 14 deletions pkg/resource/unstructured/claim/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ var (
_ client.Object = &Unstructured{}
)

func TestDeepCopyObject(t *testing.T) {
in := New(WithGroupVersionKind(schema.GroupVersionKind{
Group: "test.crossplane.io",
Version: "v1",
Kind: "Example",
}))

out := in.DeepCopyObject()

if diff := cmp.Diff(in, out); diff != "" {
t.Errorf("in.DeepCopyObject(): -want, +got:\n%s", diff)
}
}

func TestWithGroupVersionKind(t *testing.T) {
gvk := schema.GroupVersionKind{
Group: "g",
Expand Down
49 changes: 49 additions & 0 deletions pkg/resource/unstructured/claim/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 3 additions & 23 deletions pkg/resource/unstructured/composed/composed.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package composed
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -58,33 +57,14 @@ func New(opts ...Option) *Unstructured {
return cr
}

// +k8s:deepcopy-gen=true
// +kubebuilder:object:root=true

// An Unstructured composed resource.
type Unstructured struct {
unstructured.Unstructured
}

// DeepCopy returns a deep copy of the receiver.
func (cr *Unstructured) DeepCopy() *Unstructured {
if cr == nil {
return nil
}
out := new(Unstructured)
*out = *cr
out.Object = runtime.DeepCopyJSON(cr.Object)
return out
}

// DeepCopyInto copies the receiver, writing into out. in must be non-nil.
func (cr *Unstructured) DeepCopyInto(out *Unstructured) {
clone := cr.DeepCopy()
*out = *clone
}

// DeepCopyObject copies the receiver, creating a new runtime.Object.
func (cr *Unstructured) DeepCopyObject() runtime.Object {
return cr.DeepCopy()
}

// GetUnstructured returns the underlying *unstructured.Unstructured.
func (cr *Unstructured) GetUnstructured() *unstructured.Unstructured {
return &cr.Unstructured
Expand Down
13 changes: 0 additions & 13 deletions pkg/resource/unstructured/composed/composed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ var (
_ client.Object = &Unstructured{}
)

func TestDeepCopyObject(t *testing.T) {
in := New(FromReference(corev1.ObjectReference{
APIVersion: "testing.crossplane.io/v1",
Kind: "Example",
}))

out := in.DeepCopyObject()

if diff := cmp.Diff(in, out); diff != "" {
t.Errorf("in.DeepCopyObject(): -want, +got:\n%s", diff)
}
}

func TestFromReference(t *testing.T) {
ref := corev1.ObjectReference{
APIVersion: "a/v1",
Expand Down
49 changes: 49 additions & 0 deletions pkg/resource/unstructured/composed/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 3 additions & 23 deletions pkg/resource/unstructured/composite/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -57,33 +56,14 @@ func New(opts ...Option) *Unstructured {
return c
}

// +k8s:deepcopy-gen=true
// +kubebuilder:object:root=true

// An Unstructured composed resource.
type Unstructured struct {
unstructured.Unstructured
}

// DeepCopy returns a deep copy of the receiver.
func (c *Unstructured) DeepCopy() *Unstructured {
if c == nil {
return nil
}
out := new(Unstructured)
*out = *c
out.Object = runtime.DeepCopyJSON(c.Object)
return out
}

// DeepCopyInto copies the receiver, writing into out. in must be non-nil.
func (c *Unstructured) DeepCopyInto(out *Unstructured) {
clone := c.DeepCopy()
*out = *clone
}

// DeepCopyObject copies the receiver, creating a new runtime.Object.
func (c *Unstructured) DeepCopyObject() runtime.Object {
return c.DeepCopy()
}

// GetUnstructured returns the underlying *unstructured.Unstructured.
func (c *Unstructured) GetUnstructured() *unstructured.Unstructured {
return &c.Unstructured
Expand Down
14 changes: 0 additions & 14 deletions pkg/resource/unstructured/composite/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ var (
_ client.Object = &Unstructured{}
)

func TestDeepCopyObject(t *testing.T) {
in := New(WithGroupVersionKind(schema.GroupVersionKind{
Group: "test.crossplane.io",
Version: "v1",
Kind: "Example",
}))

out := in.DeepCopyObject()

if diff := cmp.Diff(in, out); diff != "" {
t.Errorf("in.DeepCopyObject(): -want, +got:\n%s", diff)
}
}

func TestWithGroupVersionKind(t *testing.T) {
gvk := schema.GroupVersionKind{
Group: "g",
Expand Down
49 changes: 49 additions & 0 deletions pkg/resource/unstructured/composite/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions pkg/resource/unstructured/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build generate
// +build generate

/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// NOTE(negz): See the below link for details on what is happening here.
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module

// Generate deepcopy methodsets
//go:generate go run -tags generate sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile=../../../hack/boilerplate.go.txt paths=./...

package unstructured

import (
_ "sigs.k8s.io/controller-tools/cmd/controller-gen" //nolint:typecheck
)

0 comments on commit fab7762

Please sign in to comment.