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

NETOBSERV-1377: Add controller to deploy netobserv network policy #690

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions apis/flowcollector/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions apis/flowcollector/v1beta2/flowcollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ type FlowCollectorSpec struct {
// +optional
// +k8s:conversion-gen=false
Exporters []*FlowCollectorExporter `json:"exporters"`

// `networkPolicy` defines ingress network policy settings for NetObserv components isolation.
// +k8s:conversion-gen=false
NetworkPolicy NetworkPolicy `json:"networkPolicy,omitempty"`
}

type NetworkPolicy struct {
// Set `enable` to `true` to deploy network policies on the namespaces used by NetObserv (main and privileged). It is disabled by default.
// These network policies better isolate the NetObserv components to prevent undesired connections to them.
// We recommend you either enable it, or create your own network policy for NetObserv.
// +optional
Enable *bool `json:"enable,omitempty"`

// `additionalNamespaces` contains additional namespaces allowed to connect to the NetObserv namespace.
// It gives some flexibility in the network policy configuration, however should you need a more specific
// configuration, you can disable it and install your own instead.
//+kubebuilder:default:={"openshift-console", "openshift-monitoring"}
//+optional
AdditionalNamespaces []string `json:"additionalNamespaces"`
}

type FlowCollectorAgentType string
Expand Down
26 changes: 26 additions & 0 deletions apis/flowcollector/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions bundle/manifests/flows.netobserv.io_flowcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6382,6 +6382,28 @@ spec:
default: netobserv
description: Namespace where NetObserv pods are deployed.
type: string
networkPolicy:
description: '`networkPolicy` defines ingress network policy settings
for NetObserv components isolation.'
properties:
additionalNamespaces:
default:
- openshift-console
- openshift-monitoring
description: |-
`additionalNamespaces` contains additional namespaces allowed to connect to the NetObserv namespace.
It gives some flexibility in the network policy configuration, however should you need a more specific
configuration, you can disable it and install your own instead.
items:
type: string
type: array
enable:
description: |-
Set `enable` to `true` to deploy network policies on the namespaces used by NetObserv (main and privileged). It is disabled by default.
These network policies better isolate the NetObserv components to prevent undesired connections to them.
We recommend you either enable it, or create your own network policy for NetObserv.
type: boolean
type: object
processor:
description: |-
`processor` defines the settings of the component that receives the flows from the agent,
Expand Down
22 changes: 22 additions & 0 deletions bundle/manifests/netobserv-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ metadata:
"writeTimeout": "10s"
},
"namespace": "netobserv",
"networkPolicy": {
"additionalNamespaces": [],
"enable": false
},
"processor": {
"imagePullPolicy": "IfNotPresent",
"kafkaConsumerAutoscaler": null,
Expand Down Expand Up @@ -776,6 +780,12 @@ spec:
path: loki.readTimeout
- displayName: Namespace
path: namespace
- displayName: Network policy
path: networkPolicy
- displayName: Additional namespaces
path: networkPolicy.additionalNamespaces
- displayName: Enable
path: networkPolicy.enable
- displayName: Log types
path: processor.logTypes
- displayName: Disable alerts
Expand Down Expand Up @@ -1070,6 +1080,18 @@ spec:
- patch
- update
- watch
- apiGroups:
- networking.k8s.io
resources:
- networkpolicies
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- operator.openshift.io
resources:
Expand Down
21 changes: 21 additions & 0 deletions config/crd/bases/flows.netobserv.io_flowcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5857,6 +5857,27 @@ spec:
default: netobserv
description: Namespace where NetObserv pods are deployed.
type: string
networkPolicy:
description: '`networkPolicy` defines ingress network policy settings for NetObserv components isolation.'
properties:
additionalNamespaces:
default:
- openshift-console
- openshift-monitoring
description: |-
`additionalNamespaces` contains additional namespaces allowed to connect to the NetObserv namespace.
It gives some flexibility in the network policy configuration, however should you need a more specific
configuration, you can disable it and install your own instead.
items:
type: string
type: array
enable:
description: |-
Set `enable` to `true` to deploy network policies on the namespaces used by NetObserv (main and privileged). It is disabled by default.
These network policies better isolate the NetObserv components to prevent undesired connections to them.
We recommend you either enable it, or create your own network policy for NetObserv.
type: boolean
type: object
processor:
description: |-
`processor` defines the settings of the component that receives the flows from the agent,
Expand Down
12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- networking.k8s.io
resources:
- networkpolicies
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- operator.openshift.io
resources:
Expand Down
3 changes: 3 additions & 0 deletions config/samples/flows_v1beta2_flowcollector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ metadata:
spec:
namespace: netobserv
deploymentModel: Direct
networkPolicy:
enable: false
additionalNamespaces: []
agent:
type: eBPF
ebpf:
Expand Down
2 changes: 2 additions & 0 deletions controllers/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (

MonitoringNamespace = "openshift-monitoring"
MonitoringServiceAccount = "prometheus-k8s"
UWMonitoringNamespace = "openshift-user-workload-monitoring"
ConsoleNamespace = "openshift-console"

// Loki roles
LokiCRWriter = "netobserv-writer"
Expand Down
3 changes: 2 additions & 1 deletion controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package controllers
import (
"github.com/netobserv/network-observability-operator/controllers/flp"
"github.com/netobserv/network-observability-operator/controllers/monitoring"
"github.com/netobserv/network-observability-operator/controllers/networkpolicy"
"github.com/netobserv/network-observability-operator/pkg/manager"
)

var Registerers = []manager.Registerer{Start, flp.Start, monitoring.Start}
var Registerers = []manager.Registerer{Start, flp.Start, monitoring.Start, networkpolicy.Start}
4 changes: 4 additions & 0 deletions controllers/flowcollector_controller_iso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func flowCollectorIsoSpecs() {
},
},
Exporters: []*flowslatest.FlowCollectorExporter{},
NetworkPolicy: flowslatest.NetworkPolicy{
Enable: nil,
AdditionalNamespaces: []string{},
},
}

It("Should create CR successfully", func() {
Expand Down
80 changes: 80 additions & 0 deletions controllers/networkpolicy/np_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package networkpolicy

import (
"context"
"fmt"

networkingv1 "k8s.io/api/networking/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2"
"github.com/netobserv/network-observability-operator/controllers/reconcilers"
"github.com/netobserv/network-observability-operator/pkg/helper"
"github.com/netobserv/network-observability-operator/pkg/manager"
"github.com/netobserv/network-observability-operator/pkg/manager/status"
)

type Reconciler struct {
client.Client
mgr *manager.Manager
status status.Instance
}

func Start(ctx context.Context, mgr *manager.Manager) error {
log := log.FromContext(ctx)
log.Info("Starting Network Policy controller")
r := Reconciler{
Client: mgr.Client,
mgr: mgr,
status: mgr.Status.ForComponent(status.NetworkPolicy),
}
return ctrl.NewControllerManagedBy(mgr).
For(&flowslatest.FlowCollector{}, reconcilers.IgnoreStatusChange).
Named("networkPolicy").
Owns(&networkingv1.NetworkPolicy{}).
Complete(&r)
}

// Reconcile is the controller entry point for reconciling current state with desired state.
// It manages the controller status at a high level. Business logic is delegated into `reconcile`.
func (r *Reconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result, error) {
l := log.Log.WithName("networkpolicy") // clear context (too noisy)
ctx = log.IntoContext(ctx, l)

// Get flowcollector & create dedicated client
clh, desired, err := helper.NewFlowCollectorClientHelper(ctx, r.Client)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get FlowCollector: %w", err)
} else if desired == nil {
// Delete case
return ctrl.Result{}, nil
}

r.status.SetUnknown()
defer r.status.Commit(ctx, r.Client)

err = r.reconcile(ctx, clh, desired)
if err != nil {
l.Error(err, "Network policy reconcile failure")
// Set status failure unless it was already set
if !r.status.HasFailure() {
r.status.SetFailure("NetworkPolicyError", err.Error())
}
return ctrl.Result{}, err
}

r.status.SetReady()
return ctrl.Result{}, nil
}

func (r *Reconciler) reconcile(ctx context.Context, clh *helper.Client, desired *flowslatest.FlowCollector) error {
npName, desiredNp := buildMainNetworkPolicy(desired, r.mgr)
if err := reconcilers.ReconcileNetworkPolicy(ctx, clh, npName, desiredNp); err != nil {
return err
}

privilegedNpName, desiredPrivilegedNp := buildPrivilegedNetworkPolicy(desired, r.mgr)
return reconcilers.ReconcileNetworkPolicy(ctx, clh, privilegedNpName, desiredPrivilegedNp)
}
Loading
Loading