-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(qrm): support nic allocation reactor
- Loading branch information
1 parent
945a96d
commit 71ecc19
Showing
9 changed files
with
252 additions
and
3 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
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
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
86 changes: 86 additions & 0 deletions
86
pkg/agent/qrm-plugins/network/staticpolicy/reactor/nic_allocation_reactor.go
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,86 @@ | ||
/* | ||
Copyright 2022 The Katalyst 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 reactor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
|
||
apiconsts "github.com/kubewharf/katalyst-api/pkg/consts" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/commonstate" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/network/state" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util/reactor" | ||
) | ||
|
||
type nicPodAllocationWrapper struct { | ||
*state.AllocationInfo | ||
} | ||
|
||
func (p nicPodAllocationWrapper) UpdateAllocation(pod *v1.Pod) error { | ||
if p.AllocationInfo == nil { | ||
return nil | ||
} | ||
|
||
annotations := pod.GetAnnotations() | ||
if annotations == nil { | ||
annotations = make(map[string]string) | ||
} | ||
|
||
annotations[apiconsts.PodAnnotationNICSelectionResultKey] = p.AllocationInfo.IfName | ||
pod.SetAnnotations(annotations) | ||
|
||
return nil | ||
} | ||
|
||
func (p nicPodAllocationWrapper) NeedUpdateAllocation(pod *v1.Pod) bool { | ||
if p.CheckSideCar() { | ||
return false | ||
} | ||
|
||
if _, ok := pod.Annotations[apiconsts.PodAnnotationNICSelectionResultKey]; !ok { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
type nicPodAllocationReactor struct { | ||
reactor.AllocationReactor | ||
} | ||
|
||
func NewNICPodAllocationReactor(r reactor.AllocationReactor) reactor.AllocationReactor { | ||
return &nicPodAllocationReactor{ | ||
AllocationReactor: r, | ||
} | ||
} | ||
|
||
func (r *nicPodAllocationReactor) UpdateAllocation(ctx context.Context, allocation commonstate.Allocation) error { | ||
if allocation == nil { | ||
return fmt.Errorf("allocation info is nil") | ||
} | ||
|
||
allocationInfo, ok := allocation.(*state.AllocationInfo) | ||
if !ok { | ||
return fmt.Errorf("allocation info is not of type network.AllocationInfo") | ||
} | ||
|
||
return r.AllocationReactor.UpdateAllocation(ctx, nicPodAllocationWrapper{ | ||
AllocationInfo: allocationInfo, | ||
}) | ||
} |
131 changes: 131 additions & 0 deletions
131
pkg/agent/qrm-plugins/network/staticpolicy/reactor/nic_allocation_reactor_test.go
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,131 @@ | ||
/* | ||
Copyright 2022 The Katalyst 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 reactor | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/kubernetes/fake" | ||
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1" | ||
|
||
"github.com/kubewharf/katalyst-api/pkg/consts" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/commonstate" | ||
cpuconsts "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/consts" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/network/state" | ||
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util/reactor" | ||
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod" | ||
) | ||
|
||
func Test_podNICAllocationReactor_UpdateAllocation(t *testing.T) { | ||
t.Parallel() | ||
|
||
type fields struct { | ||
podFetcher pod.PodFetcher | ||
client kubernetes.Interface | ||
} | ||
type args struct { | ||
allocation *state.AllocationInfo | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantPod *v1.Pod | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "actual_nic_selection_pod", | ||
fields: fields{ | ||
podFetcher: &pod.PodFetcherStub{ | ||
PodList: []*v1.Pod{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-1", | ||
Namespace: "test", | ||
UID: "test-1-uid", | ||
}, | ||
}, | ||
}, | ||
}, | ||
client: fake.NewSimpleClientset( | ||
&v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-1", | ||
Namespace: "test", | ||
UID: "test-1-uid", | ||
}, | ||
}, | ||
), | ||
}, | ||
args: args{ | ||
allocation: &state.AllocationInfo{ | ||
AllocationMeta: commonstate.AllocationMeta{ | ||
PodUid: "test-1-uid", | ||
PodNamespace: "test", | ||
PodName: "test-1", | ||
ContainerName: "container-1", | ||
ContainerType: pluginapi.ContainerType_MAIN.String(), | ||
ContainerIndex: 0, | ||
QoSLevel: consts.PodAnnotationQoSLevelSharedCores, | ||
Annotations: map[string]string{ | ||
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores, | ||
consts.PodAnnotationMemoryEnhancementNumaBinding: consts.PodAnnotationMemoryEnhancementNumaBindingEnable, | ||
cpuconsts.CPUStateAnnotationKeyNUMAHint: "0", | ||
}, | ||
Labels: map[string]string{ | ||
consts.PodAnnotationQoSLevelKey: consts.PodAnnotationQoSLevelSharedCores, | ||
}, | ||
}, | ||
IfName: "eth0", | ||
}, | ||
}, | ||
wantPod: &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-1", | ||
Namespace: "test", | ||
UID: types.UID("test-1-uid"), | ||
Annotations: map[string]string{ | ||
consts.PodAnnotationNICSelectionResultKey: "eth0", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
r := NewNICPodAllocationReactor(reactor.NewPodAllocationReactor(tt.fields.podFetcher, tt.fields.client)) | ||
if err := r.UpdateAllocation(context.Background(), tt.args.allocation); (err != nil) != tt.wantErr { | ||
t.Errorf("UpdateAllocation() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
getPod, err := tt.fields.client.CoreV1().Pods(tt.args.allocation.PodNamespace).Get(context.Background(), tt.args.allocation.PodName, metav1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("GetPod() error = %v, wantErr %v", err, tt.wantErr) | ||
} else { | ||
assert.Equal(t, tt.wantPod, getPod) | ||
} | ||
}) | ||
} | ||
} |
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