Skip to content

Commit

Permalink
Merge pull request #259 from nokia/rework-krmfn-with-workloadcluster
Browse files Browse the repository at this point in the history
Rework krmfn with workloadcluster
  • Loading branch information
johnbelamaric authored Jun 5, 2023
2 parents b88eb5b + 1dbedcc commit a1e470f
Show file tree
Hide file tree
Showing 439 changed files with 2,468 additions and 1,712 deletions.
2 changes: 1 addition & 1 deletion krm-functions/dnn-fn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

`dnn-fn` is primarily meant to be used declaratively, as part of the pipeline of a kpt package. It reads all of its inputs from the resources present in the package, and writes all of its outputs back into the package by creating/updating resources. It doesn't have any configuration parameters.

`dnn-fn` iterates through all resources of type `DataNetwork.req.nephio.org/v1alpha1`, and creates an IPAllocation resource for each `pool` listed in the `spec` of the `DataNetwork`. It also uses information from the singleton `ClusterContext` type resource that the kpt package is expected to contain.
`dnn-fn` iterates through all resources of type `DataNetwork.req.nephio.org/v1alpha1`, and creates an IPAllocation resource for each `pool` listed in the `spec` of the `DataNetwork`. It also uses information from the singleton `WorkloadCluster` type resource that the kpt package is expected to contain.

`dnn-fn` keeps track of the resources it created by setting their `specializer.nephio.org/owner` annotation to point to the `DataNetwork` resource that it was created for.

Expand Down
46 changes: 25 additions & 21 deletions krm-functions/dnn-fn/fn/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package fn

import (
"reflect"
"strings"

"fmt"
Expand All @@ -38,16 +39,16 @@ func init() {
_ = ipamv1alpha1.AddToScheme(ko.TheScheme)
}

type DnnFn struct {
sdk condkptsdk.KptCondSDK
ClusterContext *infrav1alpha1.ClusterContext
rl *fn.ResourceList
type dnnFn struct {
sdk condkptsdk.KptCondSDK
workloadCluster *infrav1alpha1.WorkloadCluster
rl *fn.ResourceList
}

// Run is the entry point of the KRM function (called by the upstream fn SDK)
func Run(rl *fn.ResourceList) (bool, error) {
var err error
myFn := DnnFn{rl: rl}
myFn := dnnFn{rl: rl}

myFn.sdk, err = condkptsdk.New(
rl,
Expand All @@ -65,11 +66,11 @@ func Run(rl *fn.ResourceList) (bool, error) {
Watch: map[corev1.ObjectReference]condkptsdk.WatchCallbackFn{
{
APIVersion: infrav1alpha1.GroupVersion.Identifier(),
Kind: infrav1alpha1.ClusterContextKind,
}: myFn.ClusterContextCallbackFn,
Kind: reflect.TypeOf(infrav1alpha1.WorkloadCluster{}).Name(),
}: myFn.WorkloadClusterCallbackFn,
},
PopulateOwnResourcesFn: myFn.desiredOwnedResourceList,
GenerateResourceFn: myFn.updateDnnResource,
UpdateResourceFn: myFn.updateDnnResource,
},
)
if err != nil {
Expand All @@ -79,29 +80,32 @@ func Run(rl *fn.ResourceList) (bool, error) {
return myFn.sdk.Run()
}

// called for all CLusterContext resources in the package
func (f *DnnFn) ClusterContextCallbackFn(o *fn.KubeObject) error {
// WorkloadClusterCallbackFn provides a callback for the workload cluster
// resources in the resourceList
func (f *dnnFn) WorkloadClusterCallbackFn(o *fn.KubeObject) error {
var err error

if f.ClusterContext != nil {
return fmt.Errorf("multiple ClusterContext objects found in the kpt package")
if f.workloadCluster != nil {
return fmt.Errorf("multiple WorkloadCluster objects found in the kpt package")
}
f.ClusterContext, err = KubeObjectToStruct[infrav1alpha1.ClusterContext](o)
f.workloadCluster, err = ko.KubeObjectToStruct[infrav1alpha1.WorkloadCluster](o)
if err != nil {
return err
}
return f.ClusterContext.Spec.Validate()

// validate check the specifics of the spec, like mandatory fields
return f.workloadCluster.Spec.Validate()
}

// desiredOwnedResourceList returns with the list of all KubeObjects that the DNN "for object" should own in the package
func (f *DnnFn) desiredOwnedResourceList(o *fn.KubeObject) (fn.KubeObjects, error) {
if f.ClusterContext == nil {
// no ClusterContext in the package
return nil, fmt.Errorf("ClusterContext is missing from the kpt package")
func (f *dnnFn) desiredOwnedResourceList(o *fn.KubeObject) (fn.KubeObjects, error) {
if f.workloadCluster == nil {
// no WorkloadCluster resource in the package
return nil, fmt.Errorf("workload cluster is missing from the kpt package")
}

// get "parent"| DNN struct
dnn, err := KubeObjectToStruct[nephioreqv1alpha1.DataNetwork](o)
dnn, err := ko.KubeObjectToStruct[nephioreqv1alpha1.DataNetwork](o)
if err != nil {
return nil, err
}
Expand All @@ -120,7 +124,7 @@ func (f *DnnFn) desiredOwnedResourceList(o *fn.KubeObject) (fn.KubeObjects, erro
AllocationLabels: ipam_common.AllocationLabels{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
ipam_common.NephioSiteKey: *f.ClusterContext.Spec.SiteCode, // NOTE: at this point ClusterContext is validated, so this is safe
ipam_common.NephioClusterNameKey: f.workloadCluster.Spec.ClusterName, // NOTE: at this point WorkloadCluster is validated, so this is safe
},
},
},
Expand All @@ -138,7 +142,7 @@ func (f *DnnFn) desiredOwnedResourceList(o *fn.KubeObject) (fn.KubeObjects, erro
}

// updateDnnResource assembles the Status of the DNN "for object" from the status of the owned IPAllocations
func (f *DnnFn) updateDnnResource(dnnObj_ *fn.KubeObject, owned fn.KubeObjects) (*fn.KubeObject, error) {
func (f *dnnFn) updateDnnResource(dnnObj_ *fn.KubeObject, owned fn.KubeObjects) (*fn.KubeObject, error) {
dnnObj, err := ko.NewFromKubeObject[nephioreqv1alpha1.DataNetwork](dnnObj_)
if err != nil {
return nil, err
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- apiVersion: infra.nephio.org/v1alpha1
kind: ClusterContext
kind: WorkloadCluster
metadata:
name: cluster-context
name: cluster01
annotations:
config.kubernetes.io/local-config: "true"
spec:
cniConfig:
cniType: macvlan
masterInterface: eth1
siteCode: edge1
clusterName: cluster01
cnis:
- macvlan
- ipvlan
- sriov
masterInterface: eth1
- apiVersion: ipam.alloc.nephio.org/v1alpha1
kind: IPAllocation
metadata:
Expand All @@ -22,7 +24,7 @@ items:
kind: pool
selector:
matchLabels:
nephio.org/site: edge1
nephio.org/cluster-name: cluster01
networkInstance:
name: vpc-internet
prefixLength: 8
Expand Down
Original file line number Diff line number Diff line change
@@ -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
16 changes: 9 additions & 7 deletions krm-functions/dnn-fn/fn/testdata/deleted_pool/_expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- apiVersion: infra.nephio.org/v1alpha1
kind: ClusterContext
kind: WorkloadCluster
metadata:
name: cluster-context
name: cluster01
annotations:
config.kubernetes.io/local-config: "true"
spec:
cniConfig:
cniType: macvlan
masterInterface: eth1
siteCode: edge1
clusterName: cluster01
cnis:
- macvlan
- ipvlan
- sriov
masterInterface: eth1
- apiVersion: ipam.alloc.nephio.org/v1alpha1
kind: IPAllocation
metadata:
Expand All @@ -23,7 +25,7 @@ items:
kind: pool
selector:
matchLabels:
nephio.org/site: edge1
nephio.org/cluster-name: cluster01
networkInstance: {}
prefixLength: 8
- apiVersion: kpt.dev/v1
Expand Down
11 changes: 0 additions & 11 deletions krm-functions/dnn-fn/fn/testdata/deleted_pool/cluster_context.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ spec:
kind: pool
selector:
matchLabels:
nephio.org/site: edge1
nephio.org/cluster-name: cluster01
networkInstance: {}
prefixLength: 8
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- apiVersion: infra.nephio.org/v1alpha1
kind: ClusterContext
kind: WorkloadCluster
metadata:
name: cluster-context
name: cluster01
annotations:
config.kubernetes.io/local-config: "true"
spec:
cniConfig:
cniType: macvlan
masterInterface: eth1
siteCode: edge1
clusterName: cluster01
cnis:
- macvlan
- ipvlan
- sriov
masterInterface: eth1
- apiVersion: ipam.alloc.nephio.org/v1alpha1
kind: IPAllocation
metadata:
Expand All @@ -23,7 +25,7 @@ items:
kind: pool
selector:
matchLabels:
nephio.org/site: edge1
nephio.org/cluster-name: cluster01
networkInstance: {}
prefixLength: 8
- apiVersion: kpt.dev/v1
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ spec:
kind: pool
selector:
matchLabels:
nephio.org/site: edge1
nephio.org/cluster-name: cluster01
networkInstance: {}
prefixLength: 8
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workload cluster is missing from the kpt package
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- message: workload cluster is missing from the kpt package
severity: error

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- message: multiple WorkloadCluster objects found in the kpt package
severity: error
Loading

0 comments on commit a1e470f

Please sign in to comment.