Skip to content

Commit

Permalink
Merge pull request #681 from l1b0k/main
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
BSWANG authored Sep 4, 2024
2 parents 6233b28 + 10c1604 commit ed121a8
Show file tree
Hide file tree
Showing 12 changed files with 650 additions and 146 deletions.
124 changes: 124 additions & 0 deletions pkg/controller/multi-ip/pod/pod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package pod

import (
"context"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

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

var _ = Describe("Pod Controller", func() {
ctx := context.Background()

BeforeEach(func() {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod", Namespace: "default"},
Spec: corev1.PodSpec{
NodeName: "node-az-1",
Containers: []corev1.Container{
{
Name: "foo",
Image: "busybox",
},
},
},
}
err := k8sClient.Create(ctx, pod)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod", Namespace: "default"},
}
err := k8sClient.Delete(ctx, pod)
Expect(err).NotTo(HaveOccurred())
})

Context("Reconcile", func() {
It("Reconcile should succeed", func() {
n := &ReconcilePod{
client: k8sClient,
}
_, err := n.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: "default",
Name: "pod",
},
})

Expect(err).To(BeNil())
})
})
})

var _ = Describe("NeedProcess Function", func() {
var pod *corev1.Pod

BeforeEach(func() {
pod = &corev1.Pod{
Spec: corev1.PodSpec{
NodeName: "node1",
},
}
})

Context("When object is not a Pod", func() {
It("returns false", func() {
obj := &corev1.Service{}
result := needProcess(obj)
Expect(result).To(BeFalse())
})
})

Context("When Pod has no NodeName", func() {
It("returns false", func() {
pod.Spec.NodeName = ""
result := needProcess(pod)
Expect(result).To(BeFalse())
})
})

Context("When Pod uses HostNetwork", func() {
It("returns false", func() {
pod.Spec.HostNetwork = true
result := needProcess(pod)
Expect(result).To(BeFalse())
})
})

Context("When Pod is ignored by Terway", func() {
It("returns false", func() {
pod.ObjectMeta.Labels = map[string]string{"k8s.aliyun.com/ignore-by-terway": "true"}
result := needProcess(pod)
Expect(result).To(BeFalse())
})
})

Context("When Pod uses ENI", func() {
It("returns false", func() {
pod.ObjectMeta.Annotations = map[string]string{"k8s.aliyun.com/pod-eni": "true"}
result := needProcess(pod)
Expect(result).To(BeFalse())
})
})

Context("When Pod sandbox not exited and has PodIP", func() {
It("returns false", func() {
pod.Status.PodIP = "192.168.1.1"
result := needProcess(pod)
Expect(result).To(BeFalse())
})
})

Context("When Pod is ready to process", func() {
It("returns true", func() {
result := needProcess(pod)
Expect(result).To(BeTrue())
})
})
})
88 changes: 88 additions & 0 deletions pkg/controller/multi-ip/pod/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2024.
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 pod

import (
"fmt"
"path/filepath"
"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

networkv1beta1 "github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "Controller Suite")
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "apis", "crds")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = networkv1beta1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
10 changes: 4 additions & 6 deletions pkg/controller/webhook/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ func ValidateHook() *webhook.Admission {
return admission.Denied("security group can not more than 5")
}

if podNetworking.Spec.AllocationType.ReleaseStrategy == v1beta1.IPAllocTypeFixed {
if podNetworking.Spec.AllocationType.ReleaseStrategy == v1beta1.ReleaseStrategyTTL {
_, err = time.ParseDuration(podNetworking.Spec.AllocationType.ReleaseAfter)
if err != nil {
return webhook.Denied(fmt.Sprintf("invalid releaseAfter %s", podNetworking.Spec.AllocationType.ReleaseAfter))
}
if podNetworking.Spec.AllocationType.ReleaseStrategy == v1beta1.ReleaseStrategyTTL {
_, err = time.ParseDuration(podNetworking.Spec.AllocationType.ReleaseAfter)
if err != nil {
return webhook.Denied(fmt.Sprintf("invalid releaseAfter %s", podNetworking.Spec.AllocationType.ReleaseAfter))
}
}
return webhook.Allowed("checked")
Expand Down
Loading

0 comments on commit ed121a8

Please sign in to comment.