Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,28 @@ func TestProcessEmptyPhaseRss(t *testing.T) {
expectedNeedRetry: false,
},
} {
needRetry, err := rc.processNormal(rss)
if err != nil {
t.Errorf("process rss object failed: %v", err)
return
}
if needRetry != tt.expectedNeedRetry {
t.Errorf("unexpected result indicates whether to retrys: %v, expected: %v",
needRetry, tt.expectedNeedRetry)
return
}
updatedRss, getErr := rssClient.UniffleV1alpha1().RemoteShuffleServices(rss.Namespace).
Get(context.TODO(), rss.Name, metav1.GetOptions{})
if getErr != nil {
t.Errorf("get updated rss object failed: %v", err)
return
}
if !reflect.DeepEqual(updatedRss.Status, tt.expectedRssStatus) {
t.Errorf("unexpected status of updated rss object: %+v, expected: %+v",
updatedRss.Status, tt.expectedRssStatus)
return
}
t.Run(tt.name, func(tc *testing.T) {
needRetry, err := rc.processNormal(rss)
if err != nil {
tc.Errorf("process rss object failed: %v", err)
return
}
if needRetry != tt.expectedNeedRetry {
tc.Errorf("unexpected result indicates whether to retrys: %v, expected: %v",
needRetry, tt.expectedNeedRetry)
return
}
updatedRss, getErr := rssClient.UniffleV1alpha1().RemoteShuffleServices(rss.Namespace).
Get(context.TODO(), rss.Name, metav1.GetOptions{})
if getErr != nil {
tc.Errorf("get updated rss object failed: %v", err)
return
}
if !reflect.DeepEqual(updatedRss.Status, tt.expectedRssStatus) {
tc.Errorf("unexpected status of updated rss object: %+v, expected: %+v",
updatedRss.Status, tt.expectedRssStatus)
return
}
})
}
}
14 changes: 12 additions & 2 deletions deploy/kubernetes/operator/pkg/controller/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,19 @@ func generateLogVolumeMount(rssPodSpec *v1alpha1.RSSPodSpec) *corev1.VolumeMount

func generateMakeDataDirCommand(rssPodSpec *v1alpha1.RSSPodSpec) []string {
var commands []string
fsGroup := *rssPodSpec.SecurityContext.FSGroup

var runAsUser int64
if rssPodSpec.SecurityContext != nil && rssPodSpec.SecurityContext.RunAsUser != nil {
runAsUser = *rssPodSpec.SecurityContext.RunAsUser
}

var fsGroup int64
if rssPodSpec.SecurityContext != nil && rssPodSpec.SecurityContext.FSGroup != nil {
fsGroup = *rssPodSpec.SecurityContext.FSGroup
}

for _, mountPath := range rssPodSpec.HostPathMounts {
commands = append(commands, fmt.Sprintf("chown -R %v:%v %v", fsGroup, fsGroup, mountPath))
commands = append(commands, fmt.Sprintf("chown -R %v:%v %v", runAsUser, fsGroup, mountPath))
}
return commands
}
Expand Down
99 changes: 99 additions & 0 deletions deploy/kubernetes/operator/pkg/controller/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 util

import (
"sort"
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"

"github.com/apache/incubator-uniffle/deploy/kubernetes/operator/api/uniffle/v1alpha1"
)

func TestGenerateMakeDataDirCommand(t *testing.T) {
for _, tt := range []struct {
name string
rssPodSpec *v1alpha1.RSSPodSpec
expectedCommands []string
}{
{
name: "empty security context",
rssPodSpec: &v1alpha1.RSSPodSpec{
HostPathMounts: map[string]string{
"/data1": "/mnt/data1",
},
},
expectedCommands: []string{
"chown -R 0:0 /mnt/data1",
},
},
{
name: "empty runAsUser field in security context",
rssPodSpec: &v1alpha1.RSSPodSpec{
HostPathMounts: map[string]string{
"/data2": "/mnt/data2",
},
SecurityContext: &corev1.PodSecurityContext{
FSGroup: pointer.Int64(1000),
},
},
expectedCommands: []string{
"chown -R 0:1000 /mnt/data2",
},
},
{
name: "non empty field of runAsUser and fsGroup in security context",
rssPodSpec: &v1alpha1.RSSPodSpec{
HostPathMounts: map[string]string{
"/data3": "/mnt/data3",
},
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(2000),
FSGroup: pointer.Int64(1000),
},
},
expectedCommands: []string{
"chown -R 2000:1000 /mnt/data3",
},
},
} {
t.Run(tt.name, func(tc *testing.T) {
commands := generateMakeDataDirCommand(tt.rssPodSpec)
if !isEqualStringSlice(commands, tt.expectedCommands) {
tc.Errorf("unexpected commands: %+v, expected: %+v", commands, tt.expectedCommands)
return
}
})
}
}

func isEqualStringSlice(a, b []string) bool {
if len(a) != len(b) {
return false
}
sort.Strings(a)
sort.Strings(b)
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
15 changes: 0 additions & 15 deletions deploy/kubernetes/operator/pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
Expand All @@ -35,20 +34,6 @@ import (
"github.com/apache/incubator-uniffle/deploy/kubernetes/operator/pkg/constants"
)

// CreatePodInformerFactory creates pod informer factory by label selector.
func CreatePodInformerFactory(kubeClient kubernetes.Interface,
key, value string) informers.SharedInformerFactory {
option := func(options *metav1.ListOptions) {
if len(value) > 0 {
options.LabelSelector = key + "=" + value
} else {
options.LabelSelector = key
}
}
return informers.NewSharedInformerFactoryWithOptions(kubeClient, 0,
informers.WithTweakListOptions(option))
}

// GetCurrentNamespace returns current namespace.
func GetCurrentNamespace() string {
namespace := os.Getenv(constants.PodNamespaceEnv)
Expand Down