Skip to content
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

[YUNIKORN-1875] Support for Compressed Encoded configMap BinaryData, gzip compression support #675

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions pkg/common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ const AnnotationEnableYuniKorn = "yunikorn.apache.org/namespace.enableYuniKorn"
// Admission Controller pod label update constants
const AutoGenAppPrefix = "yunikorn"
const AutoGenAppSuffix = "autogen"

// Compression Algorithms for schedulerConfig
const GzipSuffix = "gzip"
23 changes: 23 additions & 0 deletions pkg/common/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
package utils

import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"testing"
"time"
Expand All @@ -29,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apache/yunikorn-core/pkg/common/configs"
"github.com/apache/yunikorn-k8shim/pkg/common"
"github.com/apache/yunikorn-k8shim/pkg/common/constants"
"github.com/apache/yunikorn-k8shim/pkg/conf"
Expand Down Expand Up @@ -1137,6 +1141,25 @@ func TestGetCoreSchedulerConfigFromConfigMap(t *testing.T) {
assert.Equal(t, "test", GetCoreSchedulerConfigFromConfigMap(cm))
}

func TestGzipCompressedConfigMap(t *testing.T) {
var b bytes.Buffer
gzWriter := gzip.NewWriter(&b)
if _, err := gzWriter.Write([]byte(configs.DefaultSchedulerConfig)); err != nil {
t.Fatal("expected nil, got error while compressing test schedulerConfig")
}
if err := gzWriter.Close(); err != nil {
t.Fatal("expected nil, got error")
}
encodedConfigString := make([]byte, base64.StdEncoding.EncodedLen(len(b.Bytes())))
base64.StdEncoding.Encode(encodedConfigString, b.Bytes())
confMap := conf.FlattenConfigMaps([]*v1.ConfigMap{
{Data: map[string]string{}},
{Data: map[string]string{conf.CMSvcClusterID: "new"}, BinaryData: map[string][]byte{"queues.yaml.gzip": encodedConfigString}},
})
config := GetCoreSchedulerConfigFromConfigMap(confMap)
assert.DeepEqual(t, configs.DefaultSchedulerConfig, config)
}

func TestGetExtraConfigFromConfigMapNil(t *testing.T) {
res := GetExtraConfigFromConfigMap(nil)
assert.Equal(t, 0, len(res))
Expand Down
42 changes: 42 additions & 0 deletions pkg/conf/schedulerconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
package conf

import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -456,13 +460,51 @@
}
}

func Decompress(key string, value []byte) (string, string) {
var uncompressedData string
decodedValue := make([]byte, base64.StdEncoding.DecodedLen(len(value)))
n, err := base64.StdEncoding.Decode(decodedValue, value)
if err != nil {
log.Log(log.ShimConfig).Error("failed to decode schedulerConfig entry", zap.Error(err))
return "", ""
}

Check warning on line 470 in pkg/conf/schedulerconf.go

View check run for this annotation

Codecov / codecov/patch

pkg/conf/schedulerconf.go#L468-L470

Added lines #L468 - L470 were not covered by tests
decodedValue = decodedValue[:n]
splitKey := strings.Split(key, ".")
compressionAlgo := splitKey[len(splitKey)-1]
if strings.EqualFold(compressionAlgo, constants.GzipSuffix) {
reader := bytes.NewReader(decodedValue)
gzReader, err := gzip.NewReader(reader)
sidbroski marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Log(log.ShimConfig).Error("failed to decompress decoded schedulerConfig entry", zap.Error(err))
return "", ""
}
defer func() {
if err = gzReader.Close(); err != nil {
log.Log(log.ShimConfig).Debug("gzip Reader could not be closed ", zap.Error(err))
}

Check warning on line 484 in pkg/conf/schedulerconf.go

View check run for this annotation

Codecov / codecov/patch

pkg/conf/schedulerconf.go#L483-L484

Added lines #L483 - L484 were not covered by tests
}()
decompressedBytes, err := io.ReadAll(gzReader)
if err != nil {
log.Log(log.ShimConfig).Error("failed to decompress decoded schedulerConfig entry", zap.Error(err))
return "", ""
}

Check warning on line 490 in pkg/conf/schedulerconf.go

View check run for this annotation

Codecov / codecov/patch

pkg/conf/schedulerconf.go#L488-L490

Added lines #L488 - L490 were not covered by tests
uncompressedData = string(decompressedBytes)
}
strippedKey, _ := strings.CutSuffix(key, "."+compressionAlgo)
return strippedKey, uncompressedData
}

func FlattenConfigMaps(configMaps []*v1.ConfigMap) map[string]string {
result := make(map[string]string)
for _, configMap := range configMaps {
if configMap != nil {
for k, v := range configMap.Data {
result[k] = v
}
for k, v := range configMap.BinaryData {
strippedKey, uncompressedData := Decompress(k, v)
result[strippedKey] = uncompressedData
}

Check warning on line 507 in pkg/conf/schedulerconf.go

View check run for this annotation

Codecov / codecov/patch

pkg/conf/schedulerconf.go#L505-L507

Added lines #L505 - L507 were not covered by tests
}
}
return result
Expand Down
38 changes: 38 additions & 0 deletions pkg/conf/schedulerconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ limitations under the License.
package conf

import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"reflect"
"strings"
"testing"
"time"

"gotest.tools/v3/assert"
v1 "k8s.io/api/core/v1"

"github.com/apache/yunikorn-core/pkg/common/configs"
"github.com/apache/yunikorn-k8shim/pkg/common/constants"
)

Expand Down Expand Up @@ -72,6 +77,39 @@ func assertDefaults(t *testing.T, conf *SchedulerConf) {
assert.Equal(t, conf.UserLabelKey, constants.DefaultUserLabel)
}

func TestDecompress(t *testing.T) {
var b bytes.Buffer
gzWriter := gzip.NewWriter(&b)
if _, err := gzWriter.Write([]byte(configs.DefaultSchedulerConfig)); err != nil {
assert.NilError(t, err, "expected nil, got error while compressing test schedulerConfig")
}
if err := gzWriter.Close(); err != nil {
assert.NilError(t, err, "expected nil, got error")
t.Fatal("expected nil, got error")
}
encodedConfigString := make([]byte, base64.StdEncoding.EncodedLen(len(b.Bytes())))
base64.StdEncoding.Encode(encodedConfigString, b.Bytes())
key, decodedConfigString := Decompress("queues.yaml."+constants.GzipSuffix, encodedConfigString)
assert.Equal(t, "queues.yaml", key)
assert.Equal(t, configs.DefaultSchedulerConfig, decodedConfigString)
}

func TestDecompressUnkownKey(t *testing.T) {
encodedConfigString := make([]byte, base64.StdEncoding.EncodedLen(len([]byte(configs.DefaultSchedulerConfig))))
base64.StdEncoding.Encode(encodedConfigString, []byte(configs.DefaultSchedulerConfig))
key, decodedConfigString := Decompress("queues.yaml.bin", encodedConfigString)
assert.Equal(t, "queues.yaml", key)
assert.Assert(t, len(decodedConfigString) == 0, "expected decodedConfigString to be nil")
}

func TestDecompressBadCompression(t *testing.T) {
encodedConfigString := make([]byte, base64.StdEncoding.EncodedLen(len([]byte(configs.DefaultSchedulerConfig))))
base64.StdEncoding.Encode(encodedConfigString, []byte(configs.DefaultSchedulerConfig))
key, decodedConfigString := Decompress("queues.yaml."+constants.GzipSuffix, encodedConfigString)
assert.Equal(t, "", key)
assert.Assert(t, !strings.EqualFold(configs.DefaultSchedulerConfig, decodedConfigString), "expected decodedConfigString to be nil")
}

func TestParseConfigMap(t *testing.T) {
testCases := []struct {
name string
Expand Down