Skip to content
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

Add network policies for admission controller feature #1515

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ package admissioncontroller

import (
"encoding/json"
"strconv"

apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
apiutils "github.com/DataDog/datadog-operator/api/utils"
componentdca "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/clusteragent"
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/objects"
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature"
cilium "github.com/DataDog/datadog-operator/pkg/cilium/v1"
"github.com/DataDog/datadog-operator/pkg/defaulting"

corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand All @@ -38,6 +42,7 @@ type admissionControllerFeature struct {
registry string
serviceAccountName string
owner metav1.Object
networkPolicy v2alpha1.NetworkPolicyFlavor

cwsInstrumentationEnabled bool
cwsInstrumentationMode string
Expand Down Expand Up @@ -118,6 +123,8 @@ func (f *admissionControllerFeature) Configure(dda *v2alpha1.DatadogAgent) (reqC
f.cwsInstrumentationMode = apiutils.StringValue(ac.CWSInstrumentation.Mode)
}

_, f.networkPolicy = v2alpha1.IsNetworkPolicyEnabled(dda)

sidecarConfig := dda.Spec.Features.AdmissionController.AgentSidecarInjection
if shouldEnablesidecarInjection(sidecarConfig) {
f.agentSidecarConfig = &AgentSidecarInjectionConfig{}
Expand Down Expand Up @@ -224,7 +231,62 @@ func (f *admissionControllerFeature) ManageDependencies(managers feature.Resourc
if err := managers.RBACManager().AddClusterPolicyRules(ns, rbacName, f.serviceAccountName, getRBACClusterPolicyRules(f.webhookName, f.cwsInstrumentationEnabled, f.cwsInstrumentationMode)); err != nil {
return err
}
return managers.RBACManager().AddPolicyRules(ns, rbacName, f.serviceAccountName, getRBACPolicyRules())
if err := managers.RBACManager().AddPolicyRules(ns, rbacName, f.serviceAccountName, getRBACPolicyRules()); err != nil {
return err
}

if f.networkPolicy != "" {
policyName, podSelector := objects.GetNetworkPolicyMetadata(f.owner, v2alpha1.ClusterAgentComponentName)
switch f.networkPolicy {
case v2alpha1.NetworkPolicyFlavorKubernetes:
ingressRules := []netv1.NetworkPolicyIngressRule{
{
Ports: []netv1.NetworkPolicyPort{
{
Port: &intstr.IntOrString{
Type: intstr.Int,
IntVal: v2alpha1.DefaultAdmissionControllerTargetPort,
},
},
},
},
}
return managers.NetworkPolicyManager().AddKubernetesNetworkPolicy(
policyName,
f.owner.GetNamespace(),
podSelector,
nil,
ingressRules,
nil,
)
case v2alpha1.NetworkPolicyFlavorCilium:
policySpecs := []cilium.NetworkPolicySpec{
{
Description: "Ingress from API server for admission controller",
EndpointSelector: podSelector,
Ingress: []cilium.IngressRule{
{
FromEntities: []cilium.Entity{
"kube-apiserver",
},
ToPorts: []cilium.PortRule{
{
Ports: []cilium.PortProtocol{
{
Port: strconv.Itoa(v2alpha1.DefaultAdmissionControllerTargetPort),
Protocol: cilium.ProtocolTCP,
},
},
},
},
},
},
},
}
return managers.CiliumPolicyManager().AddCiliumPolicy(policyName, f.owner.GetNamespace(), policySpecs)
}
}
return nil
}

func (f *admissionControllerFeature) ManageClusterAgent(managers feature.PodTemplateManagers) error {
Expand Down
Loading