-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation of the DNN KRM function
- Loading branch information
Showing
17 changed files
with
618 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ | |
# Tox environments | ||
.tox/ | ||
*.dic | ||
/.vscode | ||
_actual_output.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# some-function-name | ||
|
||
Note: Please ensure you follow the [kpt doc style guide]. | ||
|
||
## Overview | ||
|
||
<!--mdtogo:Short--> | ||
|
||
Explain what this function does in one or two sentences. | ||
|
||
<!--mdtogo--> | ||
|
||
Describe why the user should care about this function. | ||
|
||
What problem does it solve? | ||
|
||
Provide some context (e.g. In the `gatekeeper` function, explain what's | ||
is `Gatekeeper` project) | ||
|
||
[//]: <> (Note: The content between `<!--mdtogo:Short-->` and the following | ||
`<!--mdtogo-->` will be used as the short description for the command.) | ||
|
||
<!--mdtogo:Long--> | ||
|
||
## Usage | ||
|
||
How do I use this function? | ||
|
||
Explain what does it do in details. | ||
|
||
Is this function meant to be used declaratively, imperatively or both? | ||
|
||
### FunctionConfig | ||
|
||
Omit this section, if the function doesn't support any `functionConfigs`. | ||
Otherwise, explain the function config and behavior for this function in detail. | ||
For each field in the function config, specify: | ||
|
||
- An example value | ||
- Whether it is optional, and if so, the default value | ||
|
||
If showing the function orchestrator (e.g. kpt) can make it clear about how to | ||
use the function, it's recommended to use it. | ||
|
||
[//]: <> (Note: The content between `<!--mdtogo:Long-->` and the following | ||
`<!--mdtogo-->` will be used as the long description for the command.) | ||
|
||
<!--mdtogo--> | ||
|
||
## Examples | ||
|
||
<!--mdtogo:Examples--> | ||
|
||
Omit this section if you are providing complete example kpt packages which are | ||
linked from the catalog site. | ||
|
||
Otherwise, provide inline examples in this section. | ||
|
||
[//]: <> (Note: The content between `<!--mdtogo:Examples-->` and the following | ||
`<!--mdtogo-->` will be used as the examples for the command.) | ||
|
||
<!--mdtogo--> | ||
|
||
[kpt doc style guide]: https://github.com/GoogleContainerTools/kpt/blob/main/docs/style-guides/docs.md |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2023 The Nephio 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. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/testhelpers" | ||
) | ||
|
||
const TestDataPath = "testdata" | ||
|
||
func TestFunction(t *testing.T) { | ||
//fnRunner := fn.WithContext(context.TODO(), &DnnFn{}) | ||
fnRunner := fn.ResourceListProcessorFunc(Run) | ||
|
||
// This golden test expects each sub-directory of `testdata` can has its input resources (in `resources.yaml`) | ||
// be modified to the output resources (in `_expected.yaml`). | ||
testhelpers.RunGoldenTests(t, TestDataPath, fnRunner) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
Copyright 2023 The Nephio 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. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"fmt" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
nephioreqv1alpha1 "github.com/nephio-project/api/nf_requirements/v1alpha1" | ||
infrav1alpha1 "github.com/nephio-project/nephio-controller-poc/apis/infra/v1alpha1" | ||
kptcondsdk "github.com/nephio-project/nephio/krm-functions/lib/condkptsdk" | ||
ipamv1alpha1 "github.com/nokia/k8s-ipam/apis/alloc/ipam/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// var _ fn.Runner = &DnnFn{} | ||
|
||
const ( | ||
// TODO: this should go to a lib somewhere | ||
SiteAnnotationKey = "nephio.org/site" | ||
) | ||
|
||
type DnnFn struct { | ||
sdk kptcondsdk.KptCondSDK | ||
site string | ||
} | ||
|
||
func Run(rl *fn.ResourceList) (bool, error) { | ||
myFn := DnnFn{} | ||
var err error | ||
myFn.sdk, err = kptcondsdk.New( | ||
rl, | ||
&kptcondsdk.Config{ | ||
For: corev1.ObjectReference{ | ||
APIVersion: nephioreqv1alpha1.GroupVersion.Identifier(), | ||
Kind: nephioreqv1alpha1.DataNetworkKind, | ||
}, | ||
Owns: map[corev1.ObjectReference]kptcondsdk.ResourceKind{ | ||
{ | ||
APIVersion: ipamv1alpha1.GroupVersion.Identifier(), | ||
Kind: ipamv1alpha1.IPAllocationKind, | ||
}: kptcondsdk.ChildRemote, | ||
}, | ||
Watch: map[corev1.ObjectReference]kptcondsdk.WatchCallbackFn{ | ||
{ | ||
APIVersion: infrav1alpha1.GroupVersion.Identifier(), | ||
Kind: "ClusterContext", | ||
}: myFn.ClusterContextCallbackFn, | ||
}, | ||
PopulateOwnResourcesFn: myFn.desiredOwnedResourceList, | ||
GenerateResourceFn: myFn.updateDnnResource, | ||
}, | ||
) | ||
if err != nil { | ||
rl.Results.ErrorE(err) | ||
return false, err | ||
} | ||
myFn.sdk.Run() | ||
return true, nil | ||
} | ||
|
||
func (f *DnnFn) ClusterContextCallbackFn(o *fn.KubeObject) error { | ||
var cluster infrav1alpha1.ClusterContext | ||
err := o.As(&cluster) | ||
if err != nil { | ||
return err | ||
} | ||
f.site = *cluster.Spec.SiteCode | ||
return nil | ||
} | ||
|
||
func (f *DnnFn) desiredOwnedResourceList(o *fn.KubeObject) ([]*fn.KubeObject, error) { | ||
resources := []*fn.KubeObject{} | ||
|
||
// get "parent"| DNN struct | ||
var dnn nephioreqv1alpha1.DataNetwork | ||
err := o.As(&dnn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// add "network" IPAllocation | ||
ipalloc := ipamv1alpha1.BuildIPAllocation( | ||
metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%s-network", dnn.Name), | ||
}, | ||
ipamv1alpha1.IPAllocationSpec{ | ||
PrefixKind: ipamv1alpha1.PrefixKindNetwork, | ||
NetworkInstance: &corev1.ObjectReference{ | ||
Name: dnn.Spec.NetworkInstance.Name, | ||
}, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
SiteAnnotationKey: f.site, | ||
}, | ||
}, | ||
}, | ||
ipamv1alpha1.IPAllocationStatus{}, | ||
) | ||
|
||
ipallocObj, err := fn.NewFromTypedObject(ipalloc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resources = append(resources, ipallocObj) | ||
|
||
// add IPAllocation for each pool | ||
for _, pool := range dnn.Spec.Pools { | ||
ipalloc := ipamv1alpha1.BuildIPAllocation( | ||
metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%s-%s", dnn.Name, pool.Name), | ||
}, | ||
ipamv1alpha1.IPAllocationSpec{ | ||
PrefixKind: ipamv1alpha1.PrefixKindPool, | ||
NetworkInstance: &corev1.ObjectReference{ | ||
Name: dnn.Spec.NetworkInstance.Name, | ||
}, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"nephio.org/site": f.site, | ||
}, | ||
}, | ||
PrefixLength: pool.PrefixLength, | ||
}, | ||
ipamv1alpha1.IPAllocationStatus{}, | ||
) | ||
|
||
ipallocObj, err := fn.NewFromTypedObject(ipalloc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resources = append(resources, ipallocObj) | ||
} | ||
return resources, nil | ||
} | ||
|
||
func (f *DnnFn) updateDnnResource(dnnObj *fn.KubeObject, owned_ []*fn.KubeObject) (*fn.KubeObject, error) { | ||
owned := fn.KubeObjects(owned_) | ||
// we expect a for object here | ||
if dnnObj == nil { | ||
return nil, fmt.Errorf("expected a for object but got nil") | ||
} | ||
for _, o := range owned.Where(fn.IsGroupVersionKind(ipamv1alpha1.IPAllocationGroupVersionKind)) { | ||
var ipalloc ipamv1alpha1.IPAllocation | ||
err := o.As(&ipalloc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
prefix := ipalloc.Status.AllocatedPrefix | ||
|
||
// TODO: create DNN manipulation interface and the manipulation via that | ||
// TODO: add allocatedPrefixes field to DataNetworkStatus | ||
// TODO: parse the name of the pool back from ipalloc.Name | ||
dnnObj.SetNestedField(prefix, "status", "allocatedPrefixes", ipalloc.Name) | ||
} | ||
return dnnObj, nil | ||
} | ||
|
||
func main() { | ||
// runner := fn.WithContext(context.Background(), &DnnFn{}) | ||
runner := fn.ResourceListProcessorFunc(Run) | ||
|
||
if err := fn.AsMain(runner); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: kpt.dev/v1 | ||
kind: Kptfile | ||
# comment A | ||
metadata: | ||
name: pkg-upf | ||
#commentB | ||
annotations: | ||
config.kubernetes.io/local-config: "true" | ||
info: | ||
description: upf package example | ||
pipeline: {} | ||
|
Oops, something went wrong.