|
| 1 | +// Copyright (c) 2025 Tigera, Inc. All rights reserved. |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package convert |
| 15 | + |
| 16 | +import ( |
| 17 | + . "github.com/onsi/ginkgo" |
| 18 | + . "github.com/onsi/gomega" |
| 19 | + |
| 20 | + operatorv1 "github.com/tigera/operator/api/v1" |
| 21 | + "github.com/tigera/operator/pkg/apis" |
| 22 | + crdv1 "github.com/tigera/operator/pkg/apis/crd.projectcalico.org/v1" |
| 23 | + pcv1 "github.com/tigera/operator/pkg/apis/crd.projectcalico.org/v1" |
| 24 | + ctrlrfake "github.com/tigera/operator/pkg/ctrlruntime/client/fake" |
| 25 | + |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + kscheme "k8s.io/client-go/kubernetes/scheme" |
| 28 | +) |
| 29 | + |
| 30 | +var _ = Describe("convert nftables mode", func() { |
| 31 | + var ( |
| 32 | + comps = emptyComponents() |
| 33 | + i = &operatorv1.Installation{} |
| 34 | + f = &crdv1.FelixConfiguration{} |
| 35 | + scheme = kscheme.Scheme |
| 36 | + ) |
| 37 | + |
| 38 | + BeforeEach(func() { |
| 39 | + comps = emptyComponents() |
| 40 | + i = &operatorv1.Installation{} |
| 41 | + f = emptyFelixConfig() |
| 42 | + Expect(apis.AddToScheme(scheme)).ToNot(HaveOccurred()) |
| 43 | + }) |
| 44 | + |
| 45 | + It("converts nftables mode from FelixConfiguration Enabled", func() { |
| 46 | + nftMode := pcv1.NFTablesModeEnabled |
| 47 | + f.Spec.NFTablesMode = &nftMode |
| 48 | + comps.client = ctrlrfake.DefaultFakeClientBuilder(scheme).WithObjects(endPointCM, f).Build() |
| 49 | + |
| 50 | + err := handleNftables(&comps, i) |
| 51 | + Expect(err).ToNot(HaveOccurred()) |
| 52 | + Expect(*i.Spec.CalicoNetwork.LinuxDataplane).To(BeEquivalentTo(operatorv1.LinuxDataplaneNftables)) |
| 53 | + Expect(i.Spec.CalicoNetwork.HostPorts).To(BeNil()) |
| 54 | + }) |
| 55 | + |
| 56 | + It("converts nftables mode from FelixConfiguration Disabled", func() { |
| 57 | + nftMode := pcv1.NFTablesModeDisabled |
| 58 | + f.Spec.NFTablesMode = &nftMode |
| 59 | + comps.client = ctrlrfake.DefaultFakeClientBuilder(scheme).WithObjects(endPointCM, f).Build() |
| 60 | + err := handleNftables(&comps, i) |
| 61 | + Expect(err).ToNot(HaveOccurred()) |
| 62 | + Expect(i.Spec.CalicoNetwork).To(BeNil()) |
| 63 | + }) |
| 64 | + |
| 65 | + It("rejects migration if another dataplane is already set", func() { |
| 66 | + nftMode := pcv1.NFTablesModeEnabled |
| 67 | + f.Spec.NFTablesMode = &nftMode |
| 68 | + comps.client = ctrlrfake.DefaultFakeClientBuilder(scheme).WithObjects(endPointCM, f).Build() |
| 69 | + |
| 70 | + // Set the Installation to already have a dataplane mode set. |
| 71 | + bpf := operatorv1.LinuxDataplaneBPF |
| 72 | + i.Spec.CalicoNetwork = &operatorv1.CalicoNetworkSpec{LinuxDataplane: &bpf} |
| 73 | + |
| 74 | + err := handleNftables(&comps, i) |
| 75 | + Expect(err).To(HaveOccurred()) |
| 76 | + }) |
| 77 | + |
| 78 | + It("check with no felixconfig", func() { |
| 79 | + comps.client = ctrlrfake.DefaultFakeClientBuilder(scheme).WithObjects(endPointCM).Build() |
| 80 | + err := handleNftables(&comps, i) |
| 81 | + Expect(err).To(HaveOccurred()) |
| 82 | + }) |
| 83 | + |
| 84 | + It("converts nftables mode from environment variable (enabled)", func() { |
| 85 | + comps.client = ctrlrfake.DefaultFakeClientBuilder(scheme).WithObjects(endPointCM, f).Build() |
| 86 | + comps.node.Spec.Template.Spec.Containers[0].Env = []v1.EnvVar{{ |
| 87 | + Name: "FELIX_NFTABLESMODE", |
| 88 | + Value: "Enabled", |
| 89 | + }} |
| 90 | + err := handleNftables(&comps, i) |
| 91 | + Expect(err).ToNot(HaveOccurred()) |
| 92 | + Expect(*i.Spec.CalicoNetwork.LinuxDataplane).To(BeEquivalentTo(operatorv1.LinuxDataplaneNftables)) |
| 93 | + }) |
| 94 | + |
| 95 | + It("converts nftables mode from environment variable (disabled)", func() { |
| 96 | + comps.client = ctrlrfake.DefaultFakeClientBuilder(scheme).WithObjects(endPointCM, f).Build() |
| 97 | + comps.node.Spec.Template.Spec.Containers[0].Env = []v1.EnvVar{{ |
| 98 | + Name: "FELIX_NFTABLESMODE", |
| 99 | + Value: "Disabled", |
| 100 | + }} |
| 101 | + err := handleNftables(&comps, i) |
| 102 | + Expect(err).ToNot(HaveOccurred()) |
| 103 | + Expect(i.Spec.CalicoNetwork).To(BeNil()) |
| 104 | + }) |
| 105 | +}) |
0 commit comments