Skip to content

Commit 9a4f2ed

Browse files
authored
fix: refactor ObjectReference common struct (#84)
1 parent 1b2ba2b commit 9a4f2ed

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

api/common/reference_types.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ import (
55
apimachinery "k8s.io/apimachinery/pkg/types"
66
)
77

8+
// ClusterScoped can be passed into a LocalObjectReference's NamespacedName method to indicate that the object is cluster-scoped.
9+
const ClusterScoped = ""
10+
811
// ObjectReference is a reference to an object in any namespace.
9-
type ObjectReference apimachinery.NamespacedName
12+
type ObjectReference struct {
13+
// Name is the name of the object.
14+
Name string `json:"name"`
15+
// Namespace is the namespace of the object.
16+
Namespace string `json:"namespace"`
17+
}
1018

1119
// LocalObjectReference is a reference to an object in the same namespace as the resource referencing it.
1220
type LocalObjectReference corev1.LocalObjectReference
@@ -24,3 +32,24 @@ type LocalSecretReference struct {
2432
// Key is the key in the secret to use.
2533
Key string `json:"key"`
2634
}
35+
36+
// NamespacedName returns the NamespacedName of the ObjectReference.
37+
// This is a convenience method to convert the ObjectReference to a NamespacedName, which can be passed into k8s client methods.
38+
func (r *ObjectReference) NamespacedName() apimachinery.NamespacedName {
39+
return apimachinery.NamespacedName{
40+
Name: r.Name,
41+
Namespace: r.Namespace,
42+
}
43+
}
44+
45+
// NamespacedName returns the NamespacedName of the LocalObjectReference.
46+
// This is a convenience method to convert the LocalObjectReference to a NamespacedName, which can be passed into k8s client methods.
47+
// Since LocalObjectReference refers to an object in the same namespace as the resource referencing it,
48+
// which is only known from context, this method requires a namespace parameter.
49+
// An empty string (or the ClusterScoped constant) can be used to indicate that the object is cluster-scoped.
50+
func (r *LocalObjectReference) NamespacedName(namespace string) apimachinery.NamespacedName {
51+
return apimachinery.NamespacedName{
52+
Name: r.Name,
53+
Namespace: namespace,
54+
}
55+
}

0 commit comments

Comments
 (0)