Skip to content

Commit e79e7db

Browse files
authored
Merge pull request #230 from joelsmith/kedamain
Handle non-numeric suffix when detecting OpenShift minor version
2 parents 809c8a3 + 79d5619 commit e79e7db

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

controllers/keda/transform/transform_suite_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 The KEDA Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package transform_test
218

319
import (
@@ -18,5 +34,5 @@ func TestTransform(t *testing.T) {
1834
}
1935

2036
func init() {
21-
flag.StringVar(&testType, "test.type", "", "type of test: functionality / deployment")
37+
flag.StringVar(&testType, "test.type", "", "type of test: unit / functionality / deployment")
2238
}

controllers/keda/util/util.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/md5"
66
"fmt"
77
"strconv"
8+
"unicode"
89

910
"github.com/go-logr/logr"
1011
corev1 "k8s.io/api/core/v1"
@@ -91,7 +92,15 @@ func RunningOnClusterWithoutSeccompProfileDefault(logger logr.Logger, discoveryC
9192
logger.Error(err, "Unable to get numeric major cluster version", "major", versionInfo.Major)
9293
return false
9394
}
94-
if minor, err = strconv.Atoi(versionInfo.Minor); err != nil {
95+
// assume that any runes that follow digits can be ignored. So, "28" -> 28, and also "28+" -> 28
96+
digitsLen := 0
97+
for _, r := range versionInfo.Minor {
98+
if !unicode.IsDigit(r) {
99+
break
100+
}
101+
digitsLen++
102+
}
103+
if minor, err = strconv.Atoi(string([]rune(versionInfo.Minor)[0:digitsLen])); err != nil {
95104
logger.Error(err, "Unable to get numeric minor cluster version", "minor", versionInfo.Minor)
96105
return false
97106
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2024 The KEDA Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util_test
18+
19+
import (
20+
"flag"
21+
"testing"
22+
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
var (
28+
testType string
29+
)
30+
31+
func TestUtil(t *testing.T) {
32+
RegisterFailHandler(Fail)
33+
RunSpecs(t, "Util Suite")
34+
}
35+
36+
func init() {
37+
flag.StringVar(&testType, "test.type", "", "type of test: unit / functionality / deployment")
38+
}

controllers/keda/util/util_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2024 The KEDA Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util_test
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"net/http/httptest"
23+
24+
. "github.com/onsi/ginkgo"
25+
. "github.com/onsi/gomega"
26+
"k8s.io/apimachinery/pkg/version"
27+
"k8s.io/client-go/discovery"
28+
restclient "k8s.io/client-go/rest"
29+
ctrl "sigs.k8s.io/controller-runtime"
30+
31+
"github.com/kedacore/keda-olm-operator/controllers/keda/util"
32+
)
33+
34+
var _ = Describe("Checking for the OpenShift RuntimeDefault seccomp profile", func() {
35+
logger := ctrl.Log.WithName("test")
36+
testData := []struct {
37+
version version.Info
38+
hasRuntimeDefault bool
39+
context string
40+
}{
41+
{version.Info{Major: "1", Minor: "28", GitCommit: "baz"}, false, "When running on a recent cluster"},
42+
{version.Info{Major: "1", Minor: "28+", GitCommit: "baz"}, false, "When running on a recent cluster with extra chars in the minor version"},
43+
{version.Info{Major: "1", Minor: "21", GitCommit: "baz"}, true, "When running on an old cluster"},
44+
{version.Info{Major: "1", Minor: "", GitCommit: "baz"}, false, "When running on a cluster with no minor version"},
45+
{version.Info{Major: "1", Minor: "+", GitCommit: "baz"}, false, "When running on a cluster with a garbage minor version"},
46+
}
47+
for _, tt := range testData {
48+
Context(tt.context, func() {
49+
It("Should be able to determine whether RuntimeDefault exists by getting the cluster version", func() {
50+
if testType != "unit" {
51+
Skip("test.type isn't 'unit'")
52+
}
53+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
54+
output, err := json.Marshal(tt.version)
55+
Expect(err).To(BeNil())
56+
57+
w.Header().Set("Content-Type", "application/json")
58+
w.WriteHeader(http.StatusOK)
59+
_, _ = w.Write(output)
60+
}))
61+
defer server.Close()
62+
63+
client := discovery.NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
64+
got := util.RunningOnClusterWithoutSeccompProfileDefault(logger, client)
65+
66+
Expect(got).To(Equal(tt.hasRuntimeDefault))
67+
})
68+
})
69+
70+
}
71+
})

0 commit comments

Comments
 (0)