From 0c6b2eac16601ce0e936977200d5a6a6981a4394 Mon Sep 17 00:00:00 2001 From: AdheipSingh Date: Tue, 6 Jun 2023 13:20:08 +0530 Subject: [PATCH] add network policy support --- builder/builder.go | 1 + builder/builder_network_policy.go | 55 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 builder/builder_network_policy.go diff --git a/builder/builder.go b/builder/builder.go index f810861..c42b1bb 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -10,6 +10,7 @@ type Builder struct { DeploymentOrStatefulset []BuilderDeploymentStatefulSet StorageConfig []BuilderStorageConfig Service []BuilderService + NetworkPolicy []BuilderNetworkPolicy Recorder BuilderRecorder Context BuilderContext Store InternalStore diff --git a/builder/builder_network_policy.go b/builder/builder_network_policy.go new file mode 100644 index 0000000..59f07a7 --- /dev/null +++ b/builder/builder_network_policy.go @@ -0,0 +1,55 @@ +package builder + +import ( + networkingv1 "k8s.io/api/networking/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" +) + +type BuilderNetworkPolicy struct { + NetworkPolicy *networkingv1.NetworkPolicy + CommonBuilder +} + +func ToNewBuilderNetworkPolicy(builder []BuilderService) func(*Builder) { + return func(s *Builder) { + s.Service = builder + } +} + +func (s *Builder) ReconcileNetworkPolicy() (controllerutil.OperationResult, error) { + + var err error + var result controllerutil.OperationResult + + for _, np := range s.NetworkPolicy { + + if np.NetworkPolicy != nil { + + makeNp := np.makeNetworkPolicy() + + np.DesiredState = makeNp + np.CurrentState = &networkingv1.NetworkPolicy{} + + result, err = np.CreateOrUpdate(s.Context.Context, s.Recorder) + if err != nil { + return controllerutil.OperationResultNone, nil + } + } + } + return result, nil +} + +func (b *BuilderNetworkPolicy) makeNetworkPolicy() *networkingv1.NetworkPolicy { + networkPolicy := &networkingv1.NetworkPolicy{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "networking.k8s.io/v1", + Kind: "NetworkPolicy", + }, + ObjectMeta: b.ObjectMeta, + Spec: *&b.NetworkPolicy.Spec, + } + + return networkPolicy +}