Skip to content

Commit 74132af

Browse files
committed
Add nil check and add unit test
1 parent 3e8bbbe commit 74132af

4 files changed

Lines changed: 45 additions & 20 deletions

File tree

pkg/ensurance/analyzer/analyzer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/gocrane/crane/pkg/ensurance/util"
10-
119
v1 "k8s.io/api/core/v1"
1210
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1311
"k8s.io/apimachinery/pkg/labels"
@@ -29,6 +27,7 @@ import (
2927
ecache "github.com/gocrane/crane/pkg/ensurance/cache"
3028
"github.com/gocrane/crane/pkg/ensurance/executor"
3129
"github.com/gocrane/crane/pkg/ensurance/executor/podinfo"
30+
"github.com/gocrane/crane/pkg/ensurance/util"
3231
"github.com/gocrane/crane/pkg/known"
3332
"github.com/gocrane/crane/pkg/metrics"
3433
"github.com/gocrane/crane/pkg/utils"

pkg/ensurance/collector/cadvisor/cadvisor_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func NewCadvisorManager(cgroupDriver string) Manager {
7878
sysfs := csysfs.NewRealSysFs()
7979
maxHousekeepingConfig := cmanager.HouskeepingConfig{Interval: &maxHousekeepingInterval, AllowDynamic: &allowDynamic}
8080

81-
m, err := cmanager.New(memCache, sysfs, maxHousekeepingConfig, includedMetrics, http.DefaultClient, []string{"/" + utils.CgroupKubePods}, nil /* containerEnvMetadataWhiteList */, "" /* perfEventsFile */, time.Duration(0) /*resctrlInterval*/)
81+
m, err := cmanager.New(memCache, sysfs, maxHousekeepingConfig, includedMetrics, http.DefaultClient, []string{"/" + utils.CgroupKubePods}, nil /* containerEnvMetadataWhiteList */, "" /* perfEventsFile */, time.Duration(0) /*resctrlInterval*/)
8282
if err != nil {
8383
klog.Errorf("Failed to create cadvisor manager start: %v", err)
8484
return nil

pkg/webhooks/pod/mutating.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/gocrane/crane/pkg/ensurance/util"
87
"github.com/pkg/errors"
8+
corev1 "k8s.io/api/core/v1"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/labels"
11-
12-
corev1 "k8s.io/api/core/v1"
1311
"k8s.io/apimachinery/pkg/runtime"
1412
"k8s.io/klog/v2"
1513

1614
"github.com/gocrane/api/ensurance/v1alpha1"
15+
1716
"github.com/gocrane/crane/pkg/ensurance/config"
17+
"github.com/gocrane/crane/pkg/ensurance/util"
1818
)
1919

2020
var (
@@ -46,11 +46,7 @@ func (m *MutatingAdmission) Default(ctx context.Context, obj runtime.Object) err
4646
return nil
4747
}
4848

49-
if m.Config == nil || !m.Config.QOSInitializer.Enable {
50-
return nil
51-
}
52-
53-
if pod.Labels == nil {
49+
if !m.available() {
5450
return nil
5551
}
5652

@@ -84,6 +80,7 @@ func (m *MutatingAdmission) Default(ctx context.Context, obj runtime.Object) err
8480
klog.V(2).Infof("Injection skipped: not a low CPUPriority pod, qos %s", qos.Name)
8581
return nil
8682
}
83+
8784
for _, container := range pod.Spec.InitContainers {
8885
if container.Name == m.Config.QOSInitializer.InitContainerTemplate.Name {
8986
klog.V(2).Infof("Injection skipped: pod has initializerContainer already")
@@ -110,3 +107,10 @@ func (m *MutatingAdmission) Default(ctx context.Context, obj runtime.Object) err
110107

111108
return nil
112109
}
110+
111+
func (m *MutatingAdmission) available() bool {
112+
return m.Config != nil &&
113+
m.Config.QOSInitializer.Enable &&
114+
m.Config.QOSInitializer.InitContainerTemplate != nil &&
115+
m.Config.QOSInitializer.VolumeTemplate != nil
116+
}

pkg/webhooks/pod/mutating_test.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/gocrane/api/ensurance/v1alpha1"
87
"github.com/stretchr/testify/assert"
9-
"k8s.io/utils/pointer"
10-
118
v1 "k8s.io/api/core/v1"
129
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/utils/pointer"
1311
"sigs.k8s.io/yaml"
1412

13+
"github.com/gocrane/api/ensurance/v1alpha1"
14+
1515
"github.com/gocrane/crane/pkg/ensurance/config"
1616
)
1717

@@ -47,6 +47,21 @@ func TestDefaultingPodQOSInitializer(t *testing.T) {
4747
}
4848
}
4949

50+
func TestPrecheck(t *testing.T) {
51+
configYaml := "apiVersion: ensurance.crane.io/v1alpha1\nkind: QOSConfig\nqosInitializer:\n enable: true\n selector: \n matchLabels:\n app: nginx\n"
52+
53+
config := &config.QOSConfig{}
54+
err := yaml.Unmarshal([]byte(configYaml), config)
55+
if err != nil {
56+
t.Errorf("unmarshal config failed:%v", err)
57+
}
58+
m := MutatingAdmission{
59+
Config: config,
60+
listPodQOS: MockListPodQOSFunc,
61+
}
62+
assert.False(t, m.available())
63+
}
64+
5065
func MockListPodQOSFunc() ([]*v1alpha1.PodQOS, error) {
5166
return []*v1alpha1.PodQOS{
5267
{
@@ -90,14 +105,21 @@ func MockListPodQOSFunc() ([]*v1alpha1.PodQOS, error) {
90105
}
91106

92107
func MockPod(name string, labels ...string) *v1.Pod {
93-
labelmap := map[string]string{}
94-
for i := 0; i < len(labels)-1; i += 2 {
95-
labelmap[labels[i]] = labels[i+1]
96-
}
97-
return &v1.Pod{
108+
pod := &v1.Pod{
98109
ObjectMeta: metav1.ObjectMeta{
99110
Name: name,
100-
Labels: labelmap,
111+
Labels: nil,
101112
},
102113
}
114+
115+
if len(labels) < 2 {
116+
return pod
117+
}
118+
119+
labelmap := map[string]string{}
120+
for i := 0; i < len(labels)-1; i += 2 {
121+
labelmap[labels[i]] = labels[i+1]
122+
}
123+
pod.Labels = labelmap
124+
return pod
103125
}

0 commit comments

Comments
 (0)