-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dnn-fn #70
dnn-fn #70
Conversation
krm-functions/dnn-fn/main.go
Outdated
Watch: map[corev1.ObjectReference]kptcondsdk.WatchCallbackFn{ | ||
{ | ||
APIVersion: infrav1alpha1.GroupVersion.Identifier(), | ||
Kind: "ClusterContext", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Instead of embeded "ClusterContext" string you can use..
Kind: reflect.TypeOf(infrav1alpha1.ClusterContext{}).Name(),
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are absolutely right. the string literal was meant to be temporary, I just forgot to put a TODO next to it :)
Since ClusterContext is a proper kubebuilder-style K8s API object I am planning to use the usual "Scheme" method to get the name of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
krm-functions/lib/parser/parser.go
Outdated
@@ -17,11 +17,218 @@ limitations under the License. | |||
package parser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the lib files are already part of seperate PR, these needs to be removed from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's actually a very good topic. What I did is that I merged that PR's branch into mine, and this is how the files from the sdk PR ended up in my branch.
I thought this is the way to base your PR to another one. Do we have a better method on basing PR on other PRs? Was it already discussed, or is it written somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
@@ -21,12 +21,7 @@ import ( | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the lib files are already part of seperate PR, these needs to be removed from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
@@ -0,0 +1,436 @@ | |||
package v1alpha1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the lib files are already part of seperate PR, these needs to be removed from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
@@ -0,0 +1,180 @@ | |||
# Conditional kpt fn sdk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the lib files are already part of seperate PR, these needs to be removed from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
krm-functions/dnn-fn/main.go
Outdated
}, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"nephio.org/site": f.site, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req to use SiteAnnotationKey instead of embeded string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, you are right of course :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
krm-functions/dnn-fn/main.go
Outdated
if err != nil { | ||
return err | ||
} | ||
f.site = *cluster.Spec.SiteCode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here if the siteCode field is not available on clustercontext yaml then following occurs:
panic: runtime error: invalid memory address or nil pointer dereference
it would be better to handle it here..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3395624
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we using the runtime scheme if we can access the schema direct. Avoiding it removes some code
krm-functions/dnn-fn/main.go
Outdated
|
||
// var _ fn.Runner = &DnnFn{} | ||
|
||
var theScheme *runtime.Scheme = runtime.NewScheme() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we add this here? we can access the schema direct w/o going to runtime scheme. avoids some code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, of course. in this case using runtime is way too complicated to obtain the Kind
of a go type.
These helper functions are just experimental code that I hoped can be the basis for future convenience methods (like FilterByType in lib.go). I am happy to remove it, if it is confusing, or we can keep it, so that it inspires others to create other convenient generic helper functions.
krm-functions/dnn-fn/main.go
Outdated
|
||
// GetKindOrPanic returns with the Kind of a Kubernetes API resource type `T`. | ||
// Panics if `T` is not registered in the `theScheme` | ||
func GetKindOrPanic[T any, PT interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be avoided if we don't go to the scheme
krm-functions/dnn-fn/main.go
Outdated
if found { | ||
pools = append(pools, DnnPoolStatus{Name: poolName, Ip: ipalloc.Status}) | ||
} else { | ||
f.rl.Results.Warningf("found an owned IPAllocation with a suspicious name: %v", ipalloc.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it not better to log rather than adding to resourceList? I would like to avoid adding to resourceList.
Need to discuss the test strategy if adding all these files is a good approach or not |
you are right, of course. in this case using runtime is way too complicated to obtain the These helper functions are just experimental code that I hoped can be the basis for future convenience methods (like FilterByType in lib.go). I am happy to remove it, if it is confusing, or we can keep it, so that it inspires others to create other convenient generic helper functions. |
Based on our discussion yesterday I've created a PR for moving my test helper functions into lib/: #134 |
@kispaljr can you look at the build issues ? |
Thanks @henderiw . A rebase needed for this PR. |
Tests will pass only after #170 is merged |
krm-functions/dnn-fn/lib.go
Outdated
|
||
var theScheme *runtime.Scheme = runtime.NewScheme() | ||
|
||
func init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you still need the schema stuff?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't use it to get the Kind of ClusterContext and others, but I use it in functions like FilterByType[IPAllocation]
, where I think it's quite elegant (however I admit that it is subjective)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example how I did it in the interface-fn. You basically know the types based on GVK as it is 1:1 mapping
ipallocs := objs.Where(fn.IsGroupVersionKind(ipamv1alpha1.IPAllocationGroupVersionKind))
for _, ipalloc := range ipallocs {
if ipalloc.GetName() == forObj.GetName() {
alloc, err := kubeobject.NewFromKubeObjectipamv1alpha1.IPAllocation
if err != nil {
return nil, err
}
allocGoStruct, err := alloc.GetGoStruct()
if err != nil {
return nil, err
}
itfce.Status.IPAllocationStatus = &allocGoStruct.Status
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly. FilterByType[IPAllocation]
implements the exact same functionality as the first 12 lines of your code. Since this code is copy-pasted into all of our KRM functions, I thought it is a prime candidate to be factored out into a utility function.
Does this cause a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw with latest lib, the above doesn't work instead it has to be:
objs.Where(fn.IsGroupVersionKind(schema.GroupVersionKind(ipamv1alpha1.IPAllocationGroupVersionKind)))
krm-functions/dnn-fn/.gitignore
Outdated
@@ -0,0 +1 @@ | |||
_actual_output.yaml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this file be ignored? Plus new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline added (will be pushed in a sec)
_actual_output.yaml
files are a byproduct of running the tests, but they are not needed in the repo
krm-functions/dnn-fn/Dockerfile
Outdated
|
||
FROM gcr.io/distroless/static:latest | ||
COPY --from=0 /usr/local/bin/function /usr/local/bin/function | ||
ENTRYPOINT ["function"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New line
matchLabels: | ||
nephio.org/site: edge1 | ||
networkInstance: {} | ||
prefixLength: 8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixing it
we should consider adding a presubmit test that automatically checks newline-at-the-end-of-file :)
krm-functions/dnn-fn/main.go
Outdated
myFn := DnnFn{rl: rl} | ||
|
||
// get ClusterContext | ||
myFn.ClusterContext, err = GetSingleton[infrav1alpha1.ClusterContext](rl.Items) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we want to avoid doing this. We should use the watch to handle this. The watch adds functionality you will not get if we use it this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was meant to be a temporary solution until the singleton functionality we talked about will be part of the SDK. Is it already available? I will update this to the SDK if it is available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked, and didn't find, so I am reverting back to the old Watch callback-based version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can add singleton to the sdk. it would be better to have it there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted back from calling the GetSingleton[ClusterContext]()
utility function to manually checking in ClusterContextCallback and PopulateFnCallback the validity of ClusterContexts
krm-functions/dnn-fn/lib.go
Outdated
|
||
var theScheme *runtime.Scheme = runtime.NewScheme() | ||
|
||
func init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example how I did it in the interface-fn. You basically know the types based on GVK as it is 1:1 mapping
ipallocs := objs.Where(fn.IsGroupVersionKind(ipamv1alpha1.IPAllocationGroupVersionKind))
for _, ipalloc := range ipallocs {
if ipalloc.GetName() == forObj.GetName() {
alloc, err := kubeobject.NewFromKubeObjectipamv1alpha1.IPAllocation
if err != nil {
return nil, err
}
allocGoStruct, err := alloc.GetGoStruct()
if err != nil {
return nil, err
}
itfce.Status.IPAllocationStatus = &allocGoStruct.Status
}
}
GitHub working strange to me, somehow I have trouble replying to this, so I will just copy-paste: Here is an example how I did it in the interface-fn. You basically know the types based on GVK as it is 1:1 mapping
Yes, exactly. |
/retest |
krm-functions/dnn-fn/README.md
Outdated
|
||
<!--mdtogo:Long--> | ||
|
||
## Usage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion, for the sake of alignment what do you think about changing the content of usage
sub-section?
https://github.com/nephio-project/nephio/tree/main/krm-functions/interface-fn#usage
https://github.com/nephio-project/nephio/blob/main/krm-functions/ipam-fn/README.md
https://github.com/nephio-project/nephio/blob/main/krm-functions/nad-fn/README.md#usage
This section can be named as Implementation details
, perhaps?
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: henderiw The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/lgtm |
DNN-fn operates on DNN resource and can request IP Allocations.