diff --git a/krm-functions/nfdeploy-fn/fn/function.go b/krm-functions/nfdeploy-fn/fn/function.go index fa502894..4129b239 100644 --- a/krm-functions/nfdeploy-fn/fn/function.go +++ b/krm-functions/nfdeploy-fn/fn/function.go @@ -259,6 +259,11 @@ func (f *NfDeployFn) UpdateResourceFn(nfDeploymentObj *fn.KubeObject, objs fn.Ku return nil, err } + // add all the existing param ref to the struct + // we need to compare for deduplication logic + // slice append is not ideal, it breaks the function idempotency requirement + f.paramRef = nf.Spec.ParametersRefs + capObjs := objs.Where(fn.IsGroupVersionKind(nephioreqv1alpha1.CapacityGroupVersionKind)) for _, o := range capObjs { if err := f.CapacityUpdate(o); err != nil { @@ -271,6 +276,7 @@ func (f *NfDeployFn) UpdateResourceFn(nfDeploymentObj *fn.KubeObject, objs fn.Ku return nil, err } } + itfceObjs := objs.Where(fn.IsGroupVersionKind(nephioreqv1alpha1.InterfaceGroupVersionKind)) for _, o := range itfceObjs { if err := f.InterfaceUpdate(o); err != nil { @@ -287,11 +293,7 @@ func (f *NfDeployFn) UpdateResourceFn(nfDeploymentObj *fn.KubeObject, objs fn.Ku f.FillCapacityDetails(nf) - if len(nf.Spec.ParametersRefs) == 0 { - nf.Spec.ParametersRefs = f.paramRef - } else { - nf.Spec.ParametersRefs = append(nf.Spec.ParametersRefs, f.paramRef...) - } + nf.Spec.ParametersRefs = f.paramRef //sort the paramRefs sort.Slice(nf.Spec.ParametersRefs, func(i, j int) bool { diff --git a/krm-functions/nfdeploy-fn/fn/helper.go b/krm-functions/nfdeploy-fn/fn/helper.go index 33f80eb6..8bae5ae1 100644 --- a/krm-functions/nfdeploy-fn/fn/helper.go +++ b/krm-functions/nfdeploy-fn/fn/helper.go @@ -121,9 +121,20 @@ func (h *NfDeployFn) FillCapacityDetails(nf *workloadv1alpha1.NFDeployment) { } func (h *NfDeployFn) AddDependencyRef(ref corev1.ObjectReference) { - h.paramRef = append(h.paramRef, workloadv1alpha1.ObjectReference{ - Name: &ref.Name, - Kind: ref.Kind, - APIVersion: ref.APIVersion, - }) + if !h.checkDependencyExist(ref) { + h.paramRef = append(h.paramRef, workloadv1alpha1.ObjectReference{ + Name: &ref.Name, + Kind: ref.Kind, + APIVersion: ref.APIVersion, + }) + } +} + +func (h *NfDeployFn) checkDependencyExist(ref corev1.ObjectReference) bool { + for _, p := range h.paramRef { + if *p.Name == ref.Name && p.Kind == ref.Kind && p.APIVersion == ref.APIVersion { + return true + } + } + return false } diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/Kptfile b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/Kptfile new file mode 100644 index 00000000..99373f61 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/Kptfile @@ -0,0 +1,34 @@ +apiVersion: kpt.dev/v1 +kind: Kptfile +metadata: + name: cluster01-amf + annotations: + config.kubernetes.io/local-config: "true" +info: + description: amf package example +pipeline: {} +status: + conditions: + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Capacity.dataplane + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.DataNetwork.internet + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Interface.n2 + - message: update for condition + status: "False" + type: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Dependency.upf + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Dependency.upf_duplicate \ No newline at end of file diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/_expected.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/_expected.yaml new file mode 100644 index 00000000..928960bd --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/_expected.yaml @@ -0,0 +1,156 @@ +apiVersion: config.kubernetes.io/v1 +kind: ResourceList +items: +- apiVersion: infra.nephio.org/v1alpha1 + kind: WorkloadCluster + metadata: + name: cluster01 + annotations: + config.kubernetes.io/local-config: "true" + spec: + clusterName: cluster01 + cnis: + - macvlan + - ipvlan + - sriov + masterInterface: eth1 +- apiVersion: kpt.dev/v1 + kind: Kptfile + metadata: + name: cluster01-amf + annotations: + config.kubernetes.io/local-config: "true" + info: + description: amf package example + readinessGates: + - conditionType: nephio.org.Specializer.specialize + pipeline: {} + status: + conditions: + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Capacity.dataplane + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.DataNetwork.internet + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Interface.n2 + - message: update done + status: "True" + type: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Dependency.upf + - message: update condition for initial resource + reason: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + status: "True" + type: req.nephio.org/v1alpha1.Dependency.upf_duplicate + - reason: Ready + status: "True" + type: nephio.org.Specializer.specialize +- apiVersion: req.nephio.org/v1alpha1 + kind: Capacity + metadata: + name: dataplane + annotations: + config.kubernetes.io/local-config: "true" + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + spec: + maxUplinkThroughput: 0 + maxSubscribers: 1000 + maxDownlinkThroughput: 0 +- apiVersion: req.nephio.org/v1alpha1 + kind: DataNetwork + metadata: + name: internet + annotations: + config.kubernetes.io/local-config: "true" + prefix: 10.0.0.0/8 + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + spec: + status: {} +- apiVersion: req.nephio.org/v1alpha1 + kind: Dependency + metadata: + name: upf + annotations: + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + specializer.nephio.org/namespace: free5gc-cp + spec: + packageName: free5gc-upf + injectors: + - apiVersion: workload.nephio.org/v1alpha1 + kind: NFDeployment + status: + injected: + - name: amf-regional-upf-edge02 + namespace: free5gc-cp + apiVersion: ref.nephio.org/v1alpha1 + kind: Config +- apiVersion: req.nephio.org/v1alpha1 + kind: Dependency + metadata: + name: upf_duplicate + annotations: + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + specializer.nephio.org/namespace: free5gc-cp + spec: + packageName: free5gc-upf + injectors: + - apiVersion: workload.nephio.org/v1alpha1 + kind: NFDeployment + status: + injected: + - name: amf-regional-upf-edge02 + namespace: free5gc-cp + apiVersion: ref.nephio.org/v1alpha1 + kind: Config +- apiVersion: req.nephio.org/v1alpha1 + kind: Interface + metadata: + name: n2 + annotations: + config.kubernetes.io/local-config: "true" + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + spec: + networkInstance: + name: vpc-ran + cniType: sriov + attachmentType: vlan + status: + ipClaimStatus: + - prefix: 10.0.0.3/24 + gateway: 10.0.0.1 + vlanClaimStatus: + vlanID: 100 +- apiVersion: workload.nephio.org/v1alpha1 + kind: NFDeployment + metadata: + name: amf-cluster01 + annotations: + specializer.nephio.org/debug: "true" + spec: + provider: amf.free5gc.io + interfaces: + - name: n2 + ipv4: + address: 10.0.0.3/24 + gateway: 10.0.0.1 + vlanID: 100 + networkInstances: + - name: vpc-ran + interfaces: + - n2 + parametersRefs: + - name: amf-regional-upf-edge02 + apiVersion: ref.nephio.org/v1alpha1 + kind: Config + capacity: + maxDownlinkThroughput: "0" + maxSubscribers: 1000 + maxUplinkThroughput: "0" diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/capacity.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/capacity.yaml new file mode 100644 index 00000000..97416ba0 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/capacity.yaml @@ -0,0 +1,11 @@ +apiVersion: req.nephio.org/v1alpha1 +kind: Capacity +metadata: + name: dataplane + annotations: + config.kubernetes.io/local-config: "true" + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 +spec: + maxUplinkThroughput: 0 + maxSubscribers: 1000 + maxDownlinkThroughput: 0 diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dependency.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dependency.yaml new file mode 100644 index 00000000..967796b6 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dependency.yaml @@ -0,0 +1,18 @@ +apiVersion: req.nephio.org/v1alpha1 +kind: Dependency +metadata: + name: upf + annotations: + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + specializer.nephio.org/namespace: free5gc-cp +spec: + packageName: free5gc-upf + injectors: + - apiVersion: workload.nephio.org/v1alpha1 + kind: NFDeployment +status: + injected: + - name: amf-regional-upf-edge02 + namespace: free5gc-cp + apiVersion: ref.nephio.org/v1alpha1 + kind: Config \ No newline at end of file diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dependency_1.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dependency_1.yaml new file mode 100644 index 00000000..385ce475 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dependency_1.yaml @@ -0,0 +1,18 @@ +apiVersion: req.nephio.org/v1alpha1 +kind: Dependency +metadata: + name: upf_duplicate + annotations: + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 + specializer.nephio.org/namespace: free5gc-cp +spec: + packageName: free5gc-upf + injectors: + - apiVersion: workload.nephio.org/v1alpha1 + kind: NFDeployment +status: + injected: + - name: amf-regional-upf-edge02 + namespace: free5gc-cp + apiVersion: ref.nephio.org/v1alpha1 + kind: Config \ No newline at end of file diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dnn.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dnn.yaml new file mode 100644 index 00000000..201c621b --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/dnn.yaml @@ -0,0 +1,10 @@ +apiVersion: req.nephio.org/v1alpha1 +kind: DataNetwork +metadata: + name: internet + annotations: + config.kubernetes.io/local-config: "true" + prefix: 10.0.0.0/8 + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 +spec: +status: {} \ No newline at end of file diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/interface-n2.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/interface-n2.yaml new file mode 100644 index 00000000..b4e59f25 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/interface-n2.yaml @@ -0,0 +1,18 @@ +apiVersion: req.nephio.org/v1alpha1 +kind: Interface +metadata: + name: n2 + annotations: + config.kubernetes.io/local-config: "true" + specializer.nephio.org/owner: workload.nephio.org/v1alpha1.NFDeployment.amf-cluster01 +spec: + networkInstance: + name: vpc-ran + cniType: sriov + attachmentType: vlan +status: + ipClaimStatus: + - prefix: 10.0.0.3/24 + gateway: 10.0.0.1 + vlanClaimStatus: + vlanID: 100 \ No newline at end of file diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/nfdeployment.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/nfdeployment.yaml new file mode 100644 index 00000000..d5beb796 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/nfdeployment.yaml @@ -0,0 +1,8 @@ +apiVersion: workload.nephio.org/v1alpha1 +kind: NFDeployment +metadata: + name: amf-cluster01 + annotations: + specializer.nephio.org/debug: "true" +spec: + provider: amf.free5gc.io \ No newline at end of file diff --git a/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/workload_cluster.yaml b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/workload_cluster.yaml new file mode 100644 index 00000000..fdcebf54 --- /dev/null +++ b/krm-functions/nfdeploy-fn/fn/testdata/golden/nfdeployment_witthduplicatedependency/workload_cluster.yaml @@ -0,0 +1,13 @@ +apiVersion: infra.nephio.org/v1alpha1 +kind: WorkloadCluster +metadata: + name: cluster01 + annotations: + config.kubernetes.io/local-config: "true" +spec: + clusterName: cluster01 + cnis: + - macvlan + - ipvlan + - sriov + masterInterface: eth1