Skip to content

Commit

Permalink
Merge pull request #230 from joelsmith/kedamain
Browse files Browse the repository at this point in the history
Handle non-numeric suffix when detecting OpenShift minor version
  • Loading branch information
joelsmith authored Jun 11, 2024
2 parents 809c8a3 + 79d5619 commit e79e7db
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
18 changes: 17 additions & 1 deletion controllers/keda/transform/transform_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024 The KEDA 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 transform_test

import (
Expand All @@ -18,5 +34,5 @@ func TestTransform(t *testing.T) {
}

func init() {
flag.StringVar(&testType, "test.type", "", "type of test: functionality / deployment")
flag.StringVar(&testType, "test.type", "", "type of test: unit / functionality / deployment")
}
11 changes: 10 additions & 1 deletion controllers/keda/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"fmt"
"strconv"
"unicode"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -91,7 +92,15 @@ func RunningOnClusterWithoutSeccompProfileDefault(logger logr.Logger, discoveryC
logger.Error(err, "Unable to get numeric major cluster version", "major", versionInfo.Major)
return false
}
if minor, err = strconv.Atoi(versionInfo.Minor); err != nil {
// assume that any runes that follow digits can be ignored. So, "28" -> 28, and also "28+" -> 28
digitsLen := 0
for _, r := range versionInfo.Minor {
if !unicode.IsDigit(r) {
break
}
digitsLen++
}
if minor, err = strconv.Atoi(string([]rune(versionInfo.Minor)[0:digitsLen])); err != nil {
logger.Error(err, "Unable to get numeric minor cluster version", "minor", versionInfo.Minor)
return false
}
Expand Down
38 changes: 38 additions & 0 deletions controllers/keda/util/util_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 The KEDA 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 util_test

import (
"flag"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var (
testType string
)

func TestUtil(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Util Suite")
}

func init() {
flag.StringVar(&testType, "test.type", "", "type of test: unit / functionality / deployment")
}
71 changes: 71 additions & 0 deletions controllers/keda/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2024 The KEDA 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 util_test

import (
"encoding/json"
"net/http"
"net/http/httptest"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
restclient "k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/kedacore/keda-olm-operator/controllers/keda/util"
)

var _ = Describe("Checking for the OpenShift RuntimeDefault seccomp profile", func() {
logger := ctrl.Log.WithName("test")
testData := []struct {
version version.Info
hasRuntimeDefault bool
context string
}{
{version.Info{Major: "1", Minor: "28", GitCommit: "baz"}, false, "When running on a recent cluster"},
{version.Info{Major: "1", Minor: "28+", GitCommit: "baz"}, false, "When running on a recent cluster with extra chars in the minor version"},
{version.Info{Major: "1", Minor: "21", GitCommit: "baz"}, true, "When running on an old cluster"},
{version.Info{Major: "1", Minor: "", GitCommit: "baz"}, false, "When running on a cluster with no minor version"},
{version.Info{Major: "1", Minor: "+", GitCommit: "baz"}, false, "When running on a cluster with a garbage minor version"},
}
for _, tt := range testData {
Context(tt.context, func() {
It("Should be able to determine whether RuntimeDefault exists by getting the cluster version", func() {
if testType != "unit" {
Skip("test.type isn't 'unit'")
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
output, err := json.Marshal(tt.version)
Expect(err).To(BeNil())

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(output)
}))
defer server.Close()

client := discovery.NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
got := util.RunningOnClusterWithoutSeccompProfileDefault(logger, client)

Expect(got).To(Equal(tt.hasRuntimeDefault))
})
})

}
})

0 comments on commit e79e7db

Please sign in to comment.