diff --git a/pkg/ensurance/analyzer/analyzer.go b/pkg/ensurance/analyzer/analyzer.go index 561e18a46..55aad6ffe 100644 --- a/pkg/ensurance/analyzer/analyzer.go +++ b/pkg/ensurance/analyzer/analyzer.go @@ -6,8 +6,6 @@ import ( "strings" "time" - "github.com/gocrane/crane/pkg/ensurance/util" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -29,6 +27,7 @@ import ( ecache "github.com/gocrane/crane/pkg/ensurance/cache" "github.com/gocrane/crane/pkg/ensurance/executor" "github.com/gocrane/crane/pkg/ensurance/executor/podinfo" + "github.com/gocrane/crane/pkg/ensurance/util" "github.com/gocrane/crane/pkg/known" "github.com/gocrane/crane/pkg/metrics" "github.com/gocrane/crane/pkg/utils" diff --git a/pkg/webhooks/pod/mutating.go b/pkg/webhooks/pod/mutating.go index 6abd05374..89bd80222 100644 --- a/pkg/webhooks/pod/mutating.go +++ b/pkg/webhooks/pod/mutating.go @@ -4,17 +4,17 @@ import ( "context" "fmt" - "github.com/gocrane/crane/pkg/ensurance/util" "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - - corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" "github.com/gocrane/api/ensurance/v1alpha1" + "github.com/gocrane/crane/pkg/ensurance/config" + "github.com/gocrane/crane/pkg/ensurance/util" ) type MutatingAdmission struct { @@ -38,11 +38,7 @@ func (m *MutatingAdmission) Default(ctx context.Context, obj runtime.Object) err klog.V(2).Infof("Mutating started for pod %s/%s", pod.Namespace, pod.Name) - if m.Config == nil || !m.Config.QOSInitializer.Enable { - return nil - } - - if pod.Labels == nil { + if !m.available() { return nil } @@ -76,6 +72,7 @@ func (m *MutatingAdmission) Default(ctx context.Context, obj runtime.Object) err klog.V(2).Infof("Injection skipped: not a low CPUPriority pod, qos %s", qos.Name) return nil } + for _, container := range pod.Spec.InitContainers { if container.Name == m.Config.QOSInitializer.InitContainerTemplate.Name { klog.V(2).Infof("Injection skipped: pod has initializerContainer already") @@ -102,3 +99,10 @@ func (m *MutatingAdmission) Default(ctx context.Context, obj runtime.Object) err return nil } + +func (m *MutatingAdmission) available() bool { + return m.Config != nil && + m.Config.QOSInitializer.Enable && + m.Config.QOSInitializer.InitContainerTemplate != nil && + m.Config.QOSInitializer.VolumeTemplate != nil +} diff --git a/pkg/webhooks/pod/mutating_test.go b/pkg/webhooks/pod/mutating_test.go index 8d2d2c4cd..e060f1ce5 100644 --- a/pkg/webhooks/pod/mutating_test.go +++ b/pkg/webhooks/pod/mutating_test.go @@ -4,14 +4,14 @@ import ( "context" "testing" - "github.com/gocrane/api/ensurance/v1alpha1" "github.com/stretchr/testify/assert" - "k8s.io/utils/pointer" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/pointer" "sigs.k8s.io/yaml" + "github.com/gocrane/api/ensurance/v1alpha1" + "github.com/gocrane/crane/pkg/ensurance/config" ) @@ -47,6 +47,21 @@ func TestDefaultingPodQOSInitializer(t *testing.T) { } } +func TestPrecheck(t *testing.T) { + configYaml := "apiVersion: ensurance.crane.io/v1alpha1\nkind: QOSConfig\nqosInitializer:\n enable: true\n selector: \n matchLabels:\n app: nginx\n" + + config := &config.QOSConfig{} + err := yaml.Unmarshal([]byte(configYaml), config) + if err != nil { + t.Errorf("unmarshal config failed:%v", err) + } + m := MutatingAdmission{ + Config: config, + listPodQOS: MockListPodQOSFunc, + } + assert.False(t, m.available()) +} + func MockListPodQOSFunc() ([]*v1alpha1.PodQOS, error) { return []*v1alpha1.PodQOS{ { @@ -90,14 +105,21 @@ func MockListPodQOSFunc() ([]*v1alpha1.PodQOS, error) { } func MockPod(name string, labels ...string) *v1.Pod { - labelmap := map[string]string{} - for i := 0; i < len(labels)-1; i += 2 { - labelmap[labels[i]] = labels[i+1] - } - return &v1.Pod{ + pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Labels: labelmap, + Labels: nil, }, } + + if len(labels) < 2 { + return pod + } + + labelmap := map[string]string{} + for i := 0; i < len(labels)-1; i += 2 { + labelmap[labels[i]] = labels[i+1] + } + pod.Labels = labelmap + return pod }