Skip to content

Commit

Permalink
Add egress rule to kubernetes service when configured from configmap (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-dekker authored Dec 19, 2024
1 parent 02b0263 commit aa18265
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/render/kubecontrollers/kube-controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,14 @@ func kubeControllersAllowTigeraPolicy(cfg *KubeControllersConfiguration) *v3.Net
})
}

if r, err := cfg.K8sServiceEp.DestinationEntityRule(); r != nil && err == nil {
egressRules = append(egressRules, v3.Rule{
Action: v3.Allow,
Protocol: &networkpolicy.TCPProtocol,
Destination: *r,
})
}

return &v3.NetworkPolicy{
TypeMeta: metav1.TypeMeta{Kind: "NetworkPolicy", APIVersion: "projectcalico.org/v3"},
ObjectMeta: metav1.ObjectMeta{
Expand Down
22 changes: 21 additions & 1 deletion pkg/render/kubecontrollers/kube-controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/tigera/operator/pkg/dns"
"github.com/tigera/operator/pkg/render"
rmeta "github.com/tigera/operator/pkg/render/common/meta"
"github.com/tigera/operator/pkg/render/common/networkpolicy"
rtest "github.com/tigera/operator/pkg/render/common/test"
"github.com/tigera/operator/pkg/render/kubecontrollers"
"github.com/tigera/operator/pkg/render/testutils"
Expand Down Expand Up @@ -1127,6 +1128,26 @@ var _ = Describe("kube-controllers rendering tests", func() {
Expect(csrInitContainer.Name).To(Equal(fmt.Sprintf("%v-key-cert-provisioner", kubecontrollers.KubeControllerPrometheusTLSSecret)))
})

It("should add egress policy with Enterprise variant and K8SServiceEndpoint defined", func() {
cfg.K8sServiceEp.Host = "k8shost"
cfg.K8sServiceEp.Port = "1234"
objects, _ := kubecontrollers.NewCalicoKubeControllersPolicy(&cfg).Objects()
Expect(objects).To(HaveLen(1))
policy, ok := objects[0].(*v3.NetworkPolicy)
Expect(ok).To(BeTrue())
Expect(policy).ToNot(BeNil())
Expect(policy.Spec).ToNot(BeNil())
Expect(policy.Spec.Egress).ToNot(BeNil())
Expect(policy.Spec.Egress).To(ContainElement(v3.Rule{
Action: v3.Allow,
Protocol: &networkpolicy.TCPProtocol,
Destination: v3.EntityRule{
Ports: networkpolicy.Ports(1234),
Domains: []string{"k8shost"},
},
}))
})

Context("multi-tenant rendering", func() {
//var installation *operatorv1.InstallationSpec
var tenant *operatorv1.Tenant
Expand Down Expand Up @@ -1328,6 +1349,5 @@ var _ = Describe("kube-controllers rendering tests", func() {
Expect(d.Spec.Template.Spec.Containers[0].Name).To(Equal("es-calico-kube-controllers"))
Expect(d.Spec.Template.Spec.Containers[0].Resources).To(Equal(overwrites))
})

})
})

0 comments on commit aa18265

Please sign in to comment.