Files
Shingo OmuraandGitHub 566d62aa81 Bump k8s to v1.35.4 and controller-runtime to v0.23.3 (#2946)
* Bump k8s api to 0.35.4, and controller runtime to v0.23.3

Signed-off-by: Shingo Omura <[email protected]>

* Migrated defaulter/validator to be Generic ones

Signed-off-by: Shingo Omura <[email protected]>

* Fixed Endpoints deprecation lint errors (replaced with EndpointSlice)

Signed-off-by: Shingo Omura <[email protected]>

* Deleted unused internal/controller/sparkapplication/validator.go

Signed-off-by: Shingo Omura <[email protected]>

* Add SA1019 exclusion to golangci-lint config so to preserve Requeue behavior of controllers

Signed-off-by: Shingo Omura <[email protected]>

* Fixed util.WriteObjectToFile test. Null creationTimestamp is not marshalled anymore.

Signed-off-by: Shingo Omura <[email protected]>

* Updated codegen and API definitions

Signed-off-by: Shingo Omura <[email protected]>

* Fixed mgr.GetEventRecorderFor() deprecation (replace with mgr.GetEventRecorder())

Signed-off-by: Shingo Omura <[email protected]>

---------

Signed-off-by: Shingo Omura <[email protected]>
2026-06-18 15:36:34 +00:00

225 lines
5.9 KiB
Go

/*
Copyright 2024 The Kubeflow 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
https://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 (
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"github.com/kubeflow/spark-operator/v2/pkg/common"
"github.com/kubeflow/spark-operator/v2/pkg/util"
)
var _ = Describe("GetMasterURL", func() {
BeforeEach(func() {
Expect(os.Setenv(common.EnvKubernetesServiceHost, "127.0.0.1")).NotTo(HaveOccurred())
Expect(os.Setenv(common.EnvKubernetesServicePort, "443")).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(os.Unsetenv(common.EnvKubernetesServiceHost)).NotTo(HaveOccurred())
Expect(os.Unsetenv(common.EnvKubernetesServicePort)).NotTo(HaveOccurred())
})
Context("IPv4 address", func() {
It("Should return correct master URL without error", func() {
masterURL, err := util.GetMasterURL()
Expect(masterURL).To(Equal("k8s://https://127.0.0.1:443"))
Expect(err).NotTo(HaveOccurred())
})
})
})
var _ = Describe("GetMasterURL", func() {
BeforeEach(func() {
Expect(os.Setenv(common.EnvKubernetesServiceHost, "::1")).NotTo(HaveOccurred())
Expect(os.Setenv(common.EnvKubernetesServicePort, "443")).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(os.Unsetenv(common.EnvKubernetesServiceHost)).NotTo(HaveOccurred())
Expect(os.Unsetenv(common.EnvKubernetesServicePort)).NotTo(HaveOccurred())
})
Context("IPv6 address", func() {
It("Should return correct master URL without error", func() {
masterURL, err := util.GetMasterURL()
Expect(masterURL).To(Equal("k8s://https://[::1]:443"))
Expect(err).NotTo(HaveOccurred())
})
})
})
var _ = Describe("RemoveString", func() {
Context("When the string is in the slice", func() {
slice := []string{"a", "b", "c"}
expected := []string{"a", "c"}
It("Should remove the string", func() {
Expect(util.RemoveString(slice, "b")).To(Equal(expected))
})
})
Context("When the string is not in the slice", func() {
slice := []string{"a", "b", "c"}
expected := []string{"a", "b", "c"}
It("Should do nothing", func() {
Expect(util.RemoveString(slice, "d")).To(Equal(expected))
})
})
})
var _ = Describe("BoolPtr", func() {
It("Should return a pointer to the given bool value", func() {
b := true
Expect(ptr.To(b)).To(Equal(&b))
})
})
var _ = Describe("Int32Ptr", func() {
It("Should return a pointer to the given int32 value", func() {
i := int32(42)
Expect(ptr.To[int32](i)).To(Equal(&i))
})
})
var _ = Describe("Int64Ptr", func() {
It("Should return a pointer to the given int64 value", func() {
i := int64(42)
Expect(ptr.To[int64](i)).To(Equal(&i))
})
})
var _ = Describe("StringPtr", func() {
It("Should return a pointer to the given string value", func() {
s := "hello"
Expect(ptr.To(s)).To(Equal(&s))
})
})
var _ = Describe("CompareSemanticVersions", func() {
It("Should return 0 if the two versions are equal", func() {
Expect(util.CompareSemanticVersion("1.2.3", "1.2.3"))
Expect(util.CompareSemanticVersion("1.2.3", "v1.2.3")).To(Equal(0))
})
It("Should return -1 if the first version is less than the second version", func() {
Expect(util.CompareSemanticVersion("2.3.4", "2.4.5")).To(Equal(-1))
Expect(util.CompareSemanticVersion("2.4.5", "2.4.8")).To(Equal(-1))
Expect(util.CompareSemanticVersion("2.4.8", "3.5.2")).To(Equal(-1))
})
It("Should return +1 if the first version is greater than the second version", func() {
Expect(util.CompareSemanticVersion("2.4.5", "2.3.4")).To(Equal(1))
Expect(util.CompareSemanticVersion("2.4.8", "2.4.5")).To(Equal(1))
Expect(util.CompareSemanticVersion("3.5.2", "2.4.8")).To(Equal(1))
})
})
var _ = Describe("WriteObjectToFile", func() {
It("Should write the object to the file", func() {
podTemplate := &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Labels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Annotations: map[string]string{
"key3": "value3",
"key4": "value4",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test-container",
Image: "test-image",
},
},
},
}
expected := `metadata:
annotations:
key3: value3
key4: value4
labels:
key1: value1
key2: value2
name: test-pod
spec:
containers:
- image: test-image
name: test-container
resources: {}
`
file := "pod-template.yaml"
Expect(util.WriteObjectToFile(podTemplate, file)).To(Succeed())
data, err := os.ReadFile(file)
Expect(err).NotTo(HaveOccurred())
actual := string(data)
Expect(actual).To(Equal(expected))
Expect(os.Remove(file)).NotTo(HaveOccurred())
})
})
var _ = Describe("ConvertJavaMemoryStringToK8sMemoryString", func() {
It("Should return a memory converted in expected K8s unit", func() {
testCases := map[string]string{
"100k": "100Ki",
"512m": "512Mi",
"2g": "2Gi",
"1t": "1Ti",
"1p": "1Pi",
"100K": "100Ki",
"512M": "512Mi",
"2G": "2Gi",
"1T": "1Ti",
"1P": "1Pi",
"2gb": "2Gi",
"2GB": "2Gi",
"2Gb": "2Gi",
"2gB": "2Gi",
"512mb": "512Mi",
"512MB": "512Mi",
"100Ki": "100Ki",
"512Mi": "512Mi",
"2Gi": "2Gi",
" 2G": "2Gi",
"2G ": "2Gi",
" 2G ": "2Gi",
"1024": "1024",
"": "",
}
for input, expected := range testCases {
Expect(util.ConvertJavaMemoryStringToK8sMemoryString(input)).
To(Equal(expected), "input: %q", input)
}
})
})