-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: loadbalancer source range #611
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f6f111
feat(crd): add LoadBalancerSourceRanges field and integrate with serv…
jds9090 c4cf905
test(crd): add tests for CEL validation logic
jds9090 b7a65c5
feat(webhook): implement LoadBalancerSourceRanges validation logic fo…
jds9090 44c64de
test(webhook): add tests for webhook validation logic
jds9090 9aba832
test: modify Makefile for envtest setup
jds9090 723827f
docs: add LoadBalancerSourceRanges field to API reference
jds9090 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package v1alpha1 | ||
|
||
import ( | ||
"path/filepath" | ||
"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" | ||
) | ||
|
||
var ( | ||
cfg *rest.Config | ||
k8sClient client.Client | ||
testEnv *envtest.Environment | ||
) | ||
|
||
func TestAPIs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "TenantControlPlane Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
By("bootstrapping test environment") | ||
testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{ | ||
filepath.Join("..", "..", "charts", "kamaji", "crds"), | ||
// filepath.Join("../..", "chart", "kamaji", "crds"), | ||
}, | ||
} | ||
|
||
var err error | ||
cfg, err = testEnv.Start() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(cfg).ToNot(BeNil()) | ||
|
||
err = AddToScheme(scheme.Scheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(k8sClient).ToNot(BeNil()) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
By("tearing down the test environment") | ||
err := testEnv.Stop() | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2022 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("Cluster controller", func() { | ||
var ( | ||
ctx context.Context | ||
tcp *TenantControlPlane | ||
) | ||
|
||
BeforeEach(func() { | ||
ctx = context.Background() | ||
tcp = &TenantControlPlane{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "tcp", | ||
Namespace: "default", | ||
}, | ||
Spec: TenantControlPlaneSpec{}, | ||
} | ||
}) | ||
|
||
AfterEach(func() { | ||
if err := k8sClient.Delete(ctx, tcp); err != nil && !apierrors.IsNotFound(err) { | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
|
||
Context("LoadBalancer Source Ranges", func() { | ||
It("allows creation when no CIDR ranges are provided", func() { | ||
tcp.Spec.ControlPlane.Service.ServiceType = ServiceTypeLoadBalancer | ||
|
||
err := k8sClient.Create(ctx, tcp) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("allows creation with an explicitly empty CIDR list", func() { | ||
tcp.Spec.ControlPlane.Service.ServiceType = ServiceTypeLoadBalancer | ||
tcp.Spec.NetworkProfile.LoadBalancerSourceRanges = []string{} | ||
|
||
err := k8sClient.Create(ctx, tcp) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("allows creation when service type is not LoadBalancer and it has an empty CIDR list", func() { | ||
tcp.Spec.ControlPlane.Service.ServiceType = ServiceTypeNodePort | ||
|
||
err := k8sClient.Create(ctx, tcp) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("allows CIDR ranges when service type is LoadBalancer", func() { | ||
tcp.Spec.ControlPlane.Service.ServiceType = ServiceTypeLoadBalancer | ||
tcp.Spec.NetworkProfile.LoadBalancerSourceRanges = []string{"192.168.0.0/24"} | ||
|
||
err := k8sClient.Create(ctx, tcp) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("denies CIDR ranges when service type is not LoadBalancer", func() { | ||
tcp.Spec.ControlPlane.Service.ServiceType = ServiceTypeNodePort | ||
tcp.Spec.NetworkProfile.LoadBalancerSourceRanges = []string{"192.168.0.0/24"} | ||
|
||
err := k8sClient.Create(ctx, tcp) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("LoadBalancer source ranges are supported only with LoadBalancer service type")) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"gomodules.xyz/jsonpatch/v2" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1" | ||
"github.com/clastix/kamaji/internal/webhook/utils" | ||
) | ||
|
||
type TenantControlPlaneLoadBalancerSourceRanges struct{} | ||
|
||
func (t TenantControlPlaneLoadBalancerSourceRanges) handle(tcp *kamajiv1alpha1.TenantControlPlane) error { | ||
for _, sourceCIDR := range tcp.Spec.NetworkProfile.LoadBalancerSourceRanges { | ||
_, _, err := net.ParseCIDR(sourceCIDR) | ||
if err != nil { | ||
return fmt.Errorf("invalid LoadBalancer source CIDR %s, %s", sourceCIDR, err.Error()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t TenantControlPlaneLoadBalancerSourceRanges) OnCreate(object runtime.Object) AdmissionResponse { | ||
return func(context.Context, admission.Request) ([]jsonpatch.JsonPatchOperation, error) { | ||
tcp := object.(*kamajiv1alpha1.TenantControlPlane) //nolint:forcetypeassert | ||
|
||
if err := t.handle(tcp); err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
} | ||
} | ||
|
||
func (t TenantControlPlaneLoadBalancerSourceRanges) OnDelete(runtime.Object) AdmissionResponse { | ||
return utils.NilOp() | ||
} | ||
|
||
func (t TenantControlPlaneLoadBalancerSourceRanges) OnUpdate(object runtime.Object, _ runtime.Object) AdmissionResponse { | ||
return func(ctx context.Context, req admission.Request) ([]jsonpatch.JsonPatchOperation, error) { | ||
tcp := object.(*kamajiv1alpha1.TenantControlPlane) //nolint:forcetypeassert | ||
|
||
if err := t.handle(tcp); err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're able to achieve the same result with CEL we can decrease the validation at the webhook server, and Kamaji will benefit a lot!