From 79d5619e6ef9a41f2c414f2269edfee7652385bd Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Tue, 11 Jun 2024 11:04:24 -0600 Subject: [PATCH] Handle non-numeric suffix when detecting OpenShift minor version Signed-off-by: Joel Smith --- .../keda/transform/transform_suite_test.go | 18 ++++- controllers/keda/util/util.go | 11 ++- controllers/keda/util/util_suite_test.go | 38 ++++++++++ controllers/keda/util/util_test.go | 71 +++++++++++++++++++ 4 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 controllers/keda/util/util_suite_test.go create mode 100644 controllers/keda/util/util_test.go diff --git a/controllers/keda/transform/transform_suite_test.go b/controllers/keda/transform/transform_suite_test.go index 90dc795aa..9b147f227 100644 --- a/controllers/keda/transform/transform_suite_test.go +++ b/controllers/keda/transform/transform_suite_test.go @@ -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 ( @@ -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") } diff --git a/controllers/keda/util/util.go b/controllers/keda/util/util.go index 005c4fa0c..96f9c2ad3 100644 --- a/controllers/keda/util/util.go +++ b/controllers/keda/util/util.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "fmt" "strconv" + "unicode" "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" @@ -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 } diff --git a/controllers/keda/util/util_suite_test.go b/controllers/keda/util/util_suite_test.go new file mode 100644 index 000000000..16fe5785e --- /dev/null +++ b/controllers/keda/util/util_suite_test.go @@ -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") +} diff --git a/controllers/keda/util/util_test.go b/controllers/keda/util/util_test.go new file mode 100644 index 000000000..403489019 --- /dev/null +++ b/controllers/keda/util/util_test.go @@ -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)) + }) + }) + + } +})