From 118e936ed36b1bf04216630024fa297ebdebe081 Mon Sep 17 00:00:00 2001
From: Yi Tao <tao.yi@konghq.com>
Date: Thu, 19 Sep 2024 11:39:57 +0800
Subject: [PATCH 1/3] remove controlPlaneRef in KongTarget because it always
 refers to KongUpstream

---
 .../v1alpha1/kong_target_types.go             |  5 -
 .../v1alpha1/zz_generated.deepcopy.go         |  5 -
 .../configuration.konghq.com_kongtargets.yaml | 43 --------
 docs/api-reference.md                         |  2 -
 .../kongtarget/testcases/common.go            |  2 +-
 .../kongtarget/testcases/controlplaneref.go   | 99 -------------------
 .../kongtarget/testcases/kongtargetapispec.go |  4 -
 .../kongtarget/testcases/upstreamref.go       |  4 -
 8 files changed, 1 insertion(+), 163 deletions(-)
 delete mode 100644 test/crdsvalidation/kongtarget/testcases/controlplaneref.go

diff --git a/api/configuration/v1alpha1/kong_target_types.go b/api/configuration/v1alpha1/kong_target_types.go
index 2c28281e..d6d78fd0 100644
--- a/api/configuration/v1alpha1/kong_target_types.go
+++ b/api/configuration/v1alpha1/kong_target_types.go
@@ -30,8 +30,6 @@ import (
 // +kubebuilder:storageversion
 // +kubebuilder:subresource:status
 // +kubebuilder:printcolumn:name="Programmed",description="The Resource is Programmed on Konnect",type=string,JSONPath=`.status.conditions[?(@.type=='Programmed')].status`
-// +kubebuilder:validation:XValidation:rule="!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)", message="controlPlaneRef is required once set"
-// +kubebuilder:validation:XValidation:rule="(!self.status.conditions.exists(c, c.type == 'Programmed' && c.status == 'True')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef", message="spec.controlPlaneRef is immutable when an entity is already Programmed"
 // +kubebuilder:validation:XValidation:rule="oldSelf.spec.upstreamRef == self.spec.upstreamRef", message="spec.upstreamRef is immutable"
 type KongTarget struct {
 	metav1.TypeMeta   `json:",inline"`
@@ -102,9 +100,6 @@ func (t *KongTarget) SetConditions(conditions []metav1.Condition) {
 }
 
 type KongTargetSpec struct {
-	// ControlPlaneRef is a reference to a ControlPlane this KongTarget is associated with.
-	// +optional
-	ControlPlaneRef *ControlPlaneRef `json:"controlPlaneRef,omitempty"`
 	// UpstreamRef is a reference to a KongUpstream this KongTarget is attached to.
 	UpstreamRef TargetRef `json:"upstreamRef"`
 	// KongTargetAPISpec are the attributes of the Kong Target itself.
diff --git a/api/configuration/v1alpha1/zz_generated.deepcopy.go b/api/configuration/v1alpha1/zz_generated.deepcopy.go
index 59d57ff8..36fe3a76 100644
--- a/api/configuration/v1alpha1/zz_generated.deepcopy.go
+++ b/api/configuration/v1alpha1/zz_generated.deepcopy.go
@@ -1116,11 +1116,6 @@ func (in *KongTargetList) DeepCopyObject() runtime.Object {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *KongTargetSpec) DeepCopyInto(out *KongTargetSpec) {
 	*out = *in
-	if in.ControlPlaneRef != nil {
-		in, out := &in.ControlPlaneRef, &out.ControlPlaneRef
-		*out = new(ControlPlaneRef)
-		(*in).DeepCopyInto(*out)
-	}
 	out.UpstreamRef = in.UpstreamRef
 	in.KongTargetAPISpec.DeepCopyInto(&out.KongTargetAPISpec)
 }
diff --git a/config/crd/bases/configuration.konghq.com_kongtargets.yaml b/config/crd/bases/configuration.konghq.com_kongtargets.yaml
index 2ad63e79..8f2ae70b 100644
--- a/config/crd/bases/configuration.konghq.com_kongtargets.yaml
+++ b/config/crd/bases/configuration.konghq.com_kongtargets.yaml
@@ -44,44 +44,6 @@ spec:
             type: object
           spec:
             properties:
-              controlPlaneRef:
-                description: ControlPlaneRef is a reference to a ControlPlane this
-                  KongTarget is associated with.
-                properties:
-                  konnectID:
-                    description: |-
-                      KonnectID is the schema for the KonnectID type.
-                      This field is required when the Type is konnectID.
-                    type: string
-                  konnectNamespacedRef:
-                    description: |-
-                      KonnectNamespacedRef is a reference to a Konnect Control Plane entity inside the cluster.
-                      It contains the name of the Konnect Control Plane.
-                      This field is required when the Type is konnectNamespacedRef.
-                    properties:
-                      name:
-                        description: Name is the name of the Konnect Control Plane.
-                        type: string
-                    required:
-                    - name
-                    type: object
-                  type:
-                    description: |-
-                      Type can be one of:
-                      - konnectID
-                      - konnectNamespacedRef
-                    enum:
-                    - konnectID
-                    - konnectNamespacedRef
-                    type: string
-                type: object
-                x-kubernetes-validations:
-                - message: when type is konnectNamespacedRef, konnectNamespacedRef
-                    must be set
-                  rule: 'self.type == ''konnectNamespacedRef'' ? has(self.konnectNamespacedRef)
-                    : true'
-                - message: when type is konnectID, konnectID must be set
-                  rule: 'self.type == ''konnectID'' ? has(self.konnectID) : true'
               tags:
                 description: Tags is an optional set of strings associated with the
                   Target for grouping and filtering.
@@ -210,11 +172,6 @@ spec:
         - spec
         type: object
         x-kubernetes-validations:
-        - message: controlPlaneRef is required once set
-          rule: '!has(oldSelf.spec.controlPlaneRef) || has(self.spec.controlPlaneRef)'
-        - message: spec.controlPlaneRef is immutable when an entity is already Programmed
-          rule: '(!self.status.conditions.exists(c, c.type == ''Programmed'' && c.status
-            == ''True'')) ? true : oldSelf.spec.controlPlaneRef == self.spec.controlPlaneRef'
         - message: spec.upstreamRef is immutable
           rule: oldSelf.spec.upstreamRef == self.spec.upstreamRef
     served: true
diff --git a/docs/api-reference.md b/docs/api-reference.md
index 5c214397..ac5c8164 100644
--- a/docs/api-reference.md
+++ b/docs/api-reference.md
@@ -525,7 +525,6 @@ _Appears in:_
 - [KongConsumerSpec](#kongconsumerspec)
 - [KongPluginBindingSpec](#kongpluginbindingspec)
 - [KongServiceSpec](#kongservicespec)
-- [KongTargetSpec](#kongtargetspec)
 - [KongUpstreamSpec](#kongupstreamspec)
 - [KongVaultSpec](#kongvaultspec)
 
@@ -856,7 +855,6 @@ _Appears in:_
 
 | Field | Description |
 | --- | --- |
-| `controlPlaneRef` _[ControlPlaneRef](#controlplaneref)_ | ControlPlaneRef is a reference to a ControlPlane this KongTarget is associated with. |
 | `upstreamRef` _[TargetRef](#targetref)_ | UpstreamRef is a reference to a KongUpstream this KongTarget is attached to. |
 | `target` _string_ | Target is the target address of the upstream. |
 | `weight` _integer_ | Weight is the weight this target gets within the upstream loadbalancer. |
diff --git a/test/crdsvalidation/kongtarget/testcases/common.go b/test/crdsvalidation/kongtarget/testcases/common.go
index 00b61e31..8fa2c2f0 100644
--- a/test/crdsvalidation/kongtarget/testcases/common.go
+++ b/test/crdsvalidation/kongtarget/testcases/common.go
@@ -25,7 +25,7 @@ type testCasesGroup struct {
 var TestCases = []testCasesGroup{}
 
 func init() {
-	TestCases = append(TestCases, controlPlaneRef, upstreamRef, kongTargetAPISpec)
+	TestCases = append(TestCases, upstreamRef, kongTargetAPISpec)
 }
 
 var commonObjectMeta = metav1.ObjectMeta{
diff --git a/test/crdsvalidation/kongtarget/testcases/controlplaneref.go b/test/crdsvalidation/kongtarget/testcases/controlplaneref.go
deleted file mode 100644
index 7b03f3c3..00000000
--- a/test/crdsvalidation/kongtarget/testcases/controlplaneref.go
+++ /dev/null
@@ -1,99 +0,0 @@
-package testcases
-
-import (
-	"github.com/samber/lo"
-	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-
-	configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
-	konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1"
-)
-
-var controlPlaneRef = testCasesGroup{
-	Name: "controlPlaneRef",
-	TestCases: []testCase{
-		{
-			Name: "no control plane ref to have control plane ref in valid",
-			KongTarget: configurationv1alpha1.KongTarget{
-				ObjectMeta: commonObjectMeta,
-				Spec: configurationv1alpha1.KongTargetSpec{
-					UpstreamRef: configurationv1alpha1.TargetRef{
-						Name: "upstream",
-					},
-					KongTargetAPISpec: configurationv1alpha1.KongTargetAPISpec{
-						Target: "example.com",
-						Weight: 100,
-					},
-				},
-			},
-			Update: func(kt *configurationv1alpha1.KongTarget) {
-				kt.Spec.ControlPlaneRef = &configurationv1alpha1.ControlPlaneRef{
-					Type:      configurationv1alpha1.ControlPlaneRefKonnectID,
-					KonnectID: lo.ToPtr("konnect-1"),
-				}
-			},
-		},
-		{
-			Name: "have control plane to no control plane is invalid",
-			KongTarget: configurationv1alpha1.KongTarget{
-				ObjectMeta: commonObjectMeta,
-				Spec: configurationv1alpha1.KongTargetSpec{
-					ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
-						Type:      configurationv1alpha1.ControlPlaneRefKonnectID,
-						KonnectID: lo.ToPtr("konnect-1"),
-					},
-					UpstreamRef: configurationv1alpha1.TargetRef{
-						Name: "upstream",
-					},
-					KongTargetAPISpec: configurationv1alpha1.KongTargetAPISpec{
-						Target: "example.com",
-						Weight: 100,
-					},
-				},
-			},
-			Update: func(kt *configurationv1alpha1.KongTarget) {
-				kt.Spec.ControlPlaneRef = nil
-			},
-			ExpectedUpdateErrorMessage: lo.ToPtr("controlPlaneRef is required once set"),
-		},
-		{
-			Name: "control plane is immutable once programmed",
-			KongTarget: configurationv1alpha1.KongTarget{
-				ObjectMeta: commonObjectMeta,
-				Spec: configurationv1alpha1.KongTargetSpec{
-					ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
-						Type:      configurationv1alpha1.ControlPlaneRefKonnectID,
-						KonnectID: lo.ToPtr("konnect-1"),
-					},
-					UpstreamRef: configurationv1alpha1.TargetRef{
-						Name: "upstream",
-					},
-					KongTargetAPISpec: configurationv1alpha1.KongTargetAPISpec{
-						Target: "example.com",
-						Weight: 100,
-					},
-				},
-			},
-			KongTargetStatus: &configurationv1alpha1.KongTargetStatus{
-				Konnect: &konnectv1alpha1.KonnectEntityStatusWithControlPlaneRef{
-					ControlPlaneID: "konnect-1",
-				},
-				Conditions: []metav1.Condition{
-					{
-						Type:               "Programmed",
-						Status:             metav1.ConditionTrue,
-						ObservedGeneration: 1,
-						Reason:             "Programmed",
-						LastTransitionTime: metav1.Now(),
-					},
-				},
-			},
-			Update: func(kt *configurationv1alpha1.KongTarget) {
-				kt.Spec.ControlPlaneRef = &configurationv1alpha1.ControlPlaneRef{
-					Type:      configurationv1alpha1.ControlPlaneRefKonnectID,
-					KonnectID: lo.ToPtr("konnect-2"),
-				}
-			},
-			ExpectedUpdateErrorMessage: lo.ToPtr("spec.controlPlaneRef is immutable when an entity is already Programmed"),
-		},
-	},
-}
diff --git a/test/crdsvalidation/kongtarget/testcases/kongtargetapispec.go b/test/crdsvalidation/kongtarget/testcases/kongtargetapispec.go
index 2f121d32..52eea0d8 100644
--- a/test/crdsvalidation/kongtarget/testcases/kongtargetapispec.go
+++ b/test/crdsvalidation/kongtarget/testcases/kongtargetapispec.go
@@ -14,10 +14,6 @@ var kongTargetAPISpec = testCasesGroup{
 			KongTarget: configurationv1alpha1.KongTarget{
 				ObjectMeta: commonObjectMeta,
 				Spec: configurationv1alpha1.KongTargetSpec{
-					ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
-						Type:      configurationv1alpha1.ControlPlaneRefKonnectID,
-						KonnectID: lo.ToPtr("konnect-1"),
-					},
 					UpstreamRef: configurationv1alpha1.TargetRef{
 						Name: "upstream",
 					},
diff --git a/test/crdsvalidation/kongtarget/testcases/upstreamref.go b/test/crdsvalidation/kongtarget/testcases/upstreamref.go
index 3c04d1f0..9dfaf439 100644
--- a/test/crdsvalidation/kongtarget/testcases/upstreamref.go
+++ b/test/crdsvalidation/kongtarget/testcases/upstreamref.go
@@ -14,10 +14,6 @@ var upstreamRef = testCasesGroup{
 			KongTarget: configurationv1alpha1.KongTarget{
 				ObjectMeta: commonObjectMeta,
 				Spec: configurationv1alpha1.KongTargetSpec{
-					ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
-						Type:      configurationv1alpha1.ControlPlaneRefKonnectID,
-						KonnectID: lo.ToPtr("konnect-1"),
-					},
 					UpstreamRef: configurationv1alpha1.TargetRef{
 						Name: "upstream",
 					},

From 0a10e1e87f4fedf6e05b3bb6839959a8c7d4126a Mon Sep 17 00:00:00 2001
From: Yi Tao <tao.yi@konghq.com>
Date: Thu, 19 Sep 2024 11:49:25 +0800
Subject: [PATCH 2/3] update konnect status of KongTarget to include upstreamID

---
 api/configuration/v1alpha1/kong_target_types.go  |  4 ++--
 .../v1alpha1/zz_generated.deepcopy.go            |  2 +-
 api/konnect/v1alpha1/konnect_entity_status.go    | 10 ++++++++++
 api/konnect/v1alpha1/zz_generated.deepcopy.go    | 16 ++++++++++++++++
 .../configuration.konghq.com_kongtargets.yaml    |  4 ++++
 docs/konnect-api-reference.md                    |  3 +++
 6 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/api/configuration/v1alpha1/kong_target_types.go b/api/configuration/v1alpha1/kong_target_types.go
index d6d78fd0..f9a98ea0 100644
--- a/api/configuration/v1alpha1/kong_target_types.go
+++ b/api/configuration/v1alpha1/kong_target_types.go
@@ -41,7 +41,7 @@ type KongTarget struct {
 }
 
 func (t *KongTarget) initKonnectStatus() {
-	t.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneRef{}
+	t.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndUpstreamRefs{}
 }
 
 // GetKonnectStatus returns the Konnect status contained in the KongTarget status.
@@ -121,7 +121,7 @@ type KongTargetAPISpec struct {
 type KongTargetStatus struct {
 	// Konnect contains the Konnect entity status.
 	// +optional
-	Konnect *konnectv1alpha1.KonnectEntityStatusWithControlPlaneRef `json:"konnect,omitempty"`
+	Konnect *konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndUpstreamRefs `json:"konnect,omitempty"`
 
 	// Conditions describe the status of the Konnect entity.
 	// +listType=map
diff --git a/api/configuration/v1alpha1/zz_generated.deepcopy.go b/api/configuration/v1alpha1/zz_generated.deepcopy.go
index 36fe3a76..293dd754 100644
--- a/api/configuration/v1alpha1/zz_generated.deepcopy.go
+++ b/api/configuration/v1alpha1/zz_generated.deepcopy.go
@@ -1135,7 +1135,7 @@ func (in *KongTargetStatus) DeepCopyInto(out *KongTargetStatus) {
 	*out = *in
 	if in.Konnect != nil {
 		in, out := &in.Konnect, &out.Konnect
-		*out = new(konnectv1alpha1.KonnectEntityStatusWithControlPlaneRef)
+		*out = new(konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndUpstreamRefs)
 		**out = **in
 	}
 	if in.Conditions != nil {
diff --git a/api/konnect/v1alpha1/konnect_entity_status.go b/api/konnect/v1alpha1/konnect_entity_status.go
index 72a8b2c3..6f306f87 100644
--- a/api/konnect/v1alpha1/konnect_entity_status.go
+++ b/api/konnect/v1alpha1/konnect_entity_status.go
@@ -87,3 +87,13 @@ func (in *KonnectEntityStatusWithControlPlaneAndServiceRefs) SetControlPlaneID(i
 func (in *KonnectEntityStatusWithControlPlaneAndServiceRefs) GetControlPlaneID() string {
 	return in.ControlPlaneID
 }
+
+type KonnectEntityStatusWithControlPlaneAndUpstreamRefs struct {
+	KonnectEntityStatus `json:",inline"`
+
+	// ControlPlaneID is the Konnect ID of the ControlPlane this Route is associated with.
+	ControlPlaneID string `json:"controlPlaneID,omitempty"`
+
+	// UpstreamID is the Konnect ID of the Upstream this entity is associated with.
+	UpstreamID string `json:"upstreamID,omitempty"`
+}
diff --git a/api/konnect/v1alpha1/zz_generated.deepcopy.go b/api/konnect/v1alpha1/zz_generated.deepcopy.go
index 7e5a5407..4da9aeae 100644
--- a/api/konnect/v1alpha1/zz_generated.deepcopy.go
+++ b/api/konnect/v1alpha1/zz_generated.deepcopy.go
@@ -173,6 +173,22 @@ func (in *KonnectEntityStatusWithControlPlaneAndServiceRefs) DeepCopy() *Konnect
 	return out
 }
 
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
+func (in *KonnectEntityStatusWithControlPlaneAndUpstreamRefs) DeepCopyInto(out *KonnectEntityStatusWithControlPlaneAndUpstreamRefs) {
+	*out = *in
+	out.KonnectEntityStatus = in.KonnectEntityStatus
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KonnectEntityStatusWithControlPlaneAndUpstreamRefs.
+func (in *KonnectEntityStatusWithControlPlaneAndUpstreamRefs) DeepCopy() *KonnectEntityStatusWithControlPlaneAndUpstreamRefs {
+	if in == nil {
+		return nil
+	}
+	out := new(KonnectEntityStatusWithControlPlaneAndUpstreamRefs)
+	in.DeepCopyInto(out)
+	return out
+}
+
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *KonnectEntityStatusWithControlPlaneRef) DeepCopyInto(out *KonnectEntityStatusWithControlPlaneRef) {
 	*out = *in
diff --git a/config/crd/bases/configuration.konghq.com_kongtargets.yaml b/config/crd/bases/configuration.konghq.com_kongtargets.yaml
index 8f2ae70b..198afe87 100644
--- a/config/crd/bases/configuration.konghq.com_kongtargets.yaml
+++ b/config/crd/bases/configuration.konghq.com_kongtargets.yaml
@@ -166,6 +166,10 @@ spec:
                     description: ServerURL is the URL of the Konnect server in which
                       the entity exists.
                     type: string
+                  upstreamID:
+                    description: UpstreamID is the Konnect ID of the Upstream this
+                      entity is associated with.
+                    type: string
                 type: object
             type: object
         required:
diff --git a/docs/konnect-api-reference.md b/docs/konnect-api-reference.md
index 6eaa5788..8dacc759 100644
--- a/docs/konnect-api-reference.md
+++ b/docs/konnect-api-reference.md
@@ -123,6 +123,7 @@ _Appears in:_
 
 _Appears in:_
 - [KonnectEntityStatusWithControlPlaneAndServiceRefs](#konnectentitystatuswithcontrolplaneandservicerefs)
+- [KonnectEntityStatusWithControlPlaneAndUpstreamRefs](#konnectentitystatuswithcontrolplaneandupstreamrefs)
 - [KonnectEntityStatusWithControlPlaneRef](#konnectentitystatuswithcontrolplaneref)
 - [KonnectGatewayControlPlaneStatus](#konnectgatewaycontrolplanestatus)
 
@@ -130,6 +131,8 @@ _Appears in:_
 
 
 
+
+
 #### KonnectGatewayControlPlaneSpec
 
 

From de1840ce893aac149e9193cb306bfb851ebe0f1d Mon Sep 17 00:00:00 2001
From: Yi Tao <tao.yi@konghq.com>
Date: Thu, 19 Sep 2024 15:38:20 +0800
Subject: [PATCH 3/3] fix description of ControlPlaneID

---
 api/konnect/v1alpha1/konnect_entity_status.go              | 2 +-
 config/crd/bases/configuration.konghq.com_kongtargets.yaml | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/api/konnect/v1alpha1/konnect_entity_status.go b/api/konnect/v1alpha1/konnect_entity_status.go
index 6f306f87..4e098af0 100644
--- a/api/konnect/v1alpha1/konnect_entity_status.go
+++ b/api/konnect/v1alpha1/konnect_entity_status.go
@@ -91,7 +91,7 @@ func (in *KonnectEntityStatusWithControlPlaneAndServiceRefs) GetControlPlaneID()
 type KonnectEntityStatusWithControlPlaneAndUpstreamRefs struct {
 	KonnectEntityStatus `json:",inline"`
 
-	// ControlPlaneID is the Konnect ID of the ControlPlane this Route is associated with.
+	// ControlPlaneID is the Konnect ID of the ControlPlane this entity is associated with.
 	ControlPlaneID string `json:"controlPlaneID,omitempty"`
 
 	// UpstreamID is the Konnect ID of the Upstream this entity is associated with.
diff --git a/config/crd/bases/configuration.konghq.com_kongtargets.yaml b/config/crd/bases/configuration.konghq.com_kongtargets.yaml
index 198afe87..ad258ac4 100644
--- a/config/crd/bases/configuration.konghq.com_kongtargets.yaml
+++ b/config/crd/bases/configuration.konghq.com_kongtargets.yaml
@@ -151,7 +151,7 @@ spec:
                 properties:
                   controlPlaneID:
                     description: ControlPlaneID is the Konnect ID of the ControlPlane
-                      this Route is associated with.
+                      this entity is associated with.
                     type: string
                   id:
                     description: |-