Skip to content

Commit

Permalink
fix(network policies) reviewer comment
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Guyennet <simon.guyennet@corp.ovh.com>
  • Loading branch information
sguyennet authored and holyhope committed Feb 19, 2021
1 parent 6e9fede commit e73fbd8
Show file tree
Hide file tree
Showing 37 changed files with 296 additions and 218 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ diff:
git status
git diff --stat --diff-filter=d --exit-code HEAD

GO_TEST_OPTS ?= -vet=off
GO_TEST_OPTS ?= -p 1 -vet=off

.PHONY: go-test
go-test: install
Expand Down Expand Up @@ -448,7 +448,7 @@ certmanager: helm jetstack
$(HELM) repo add jetstack https://charts.jetstack.io # https://cert-manager.io/docs/installation/kubernetes/
$(HELM) upgrade --install certmanager jetstack/cert-manager \
--namespace $(CERTMANAGER_NAMESPACE) \
--version v0.15.1 \
--version v1.0.3 \
--set installCRDs=true

.PHONY: jetstack
Expand Down
7 changes: 7 additions & 0 deletions apis/meta/v1alpha1/network_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v1alpha1

const (
NetworkPoliciesAnnotationName = "goharbor.io/network-policies"
NetworkPoliciesAnnotationEnabled = "true"
NetworkPoliciesAnnotationDisabled = "false"
)
12 changes: 12 additions & 0 deletions charts/harbor-operator/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,16 @@ rules:
- patch
- update
- watch
- apiGroups:
- networking.k8s.io
resources:
- networkpolicies
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
{{- end -}}
38 changes: 28 additions & 10 deletions controllers/goharbor/chartmuseum/networkpolicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ import (

type NetworkPolicy graph.Resource

func (r *Reconciler) AddNetworkPolicies(ctx context.Context, chartMuseum *goharborv1alpha2.ChartMuseum) error {
areNetworkPoliciesEnabled, err := r.AreNetworkPoliciesEnabled(ctx, chartMuseum)
if err != nil {
return errors.Wrapf(err, "cannot get status")
}

if !areNetworkPoliciesEnabled {
return nil
}

_, err = r.AddIngressNetworkPolicy(ctx, chartMuseum)
if err != nil {
return errors.Wrapf(err, "ingress")
}

return nil
}

func (r *Reconciler) AddIngressNetworkPolicy(ctx context.Context, chartmuseum *goharborv1alpha2.ChartMuseum) (NetworkPolicy, error) {
networkPolicy, err := r.GetIngressNetworkPolicy(ctx, chartmuseum)
if err != nil {
Expand All @@ -26,8 +44,13 @@ func (r *Reconciler) AddIngressNetworkPolicy(ctx context.Context, chartmuseum *g
}

func (r *Reconciler) GetIngressNetworkPolicy(ctx context.Context, chartmuseum *goharborv1alpha2.ChartMuseum) (*netv1.NetworkPolicy, error) {
httpPort := intstr.FromString(harbormetav1.ChartMuseumHTTPPortName)
httpsPort := intstr.FromString(harbormetav1.ChartMuseumHTTPSPortName)
var port intstr.IntOrString

if chartmuseum.Spec.Server.TLS != nil {
port = intstr.FromString(harbormetav1.ChartMuseumHTTPSPortName)
} else {
port = intstr.FromString(harbormetav1.ChartMuseumHTTPPortName)
}

return &netv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -37,14 +60,9 @@ func (r *Reconciler) GetIngressNetworkPolicy(ctx context.Context, chartmuseum *g
Spec: netv1.NetworkPolicySpec{
Ingress: []netv1.NetworkPolicyIngressRule{
{
Ports: []netv1.NetworkPolicyPort{
{
Port: &httpPort,
},
{
Port: &httpsPort,
},
},
Ports: []netv1.NetworkPolicyPort{{
Port: &port,
}},
},
},
PodSelector: metav1.LabelSelector{
Expand Down
14 changes: 2 additions & 12 deletions controllers/goharbor/chartmuseum/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,7 @@ func (r *Reconciler) AddResources(ctx context.Context, resource resources.Resour
return errors.Wrapf(err, "cannot add deployment %s", deployment.GetName())
}

areNetworkPoliciesEnabled, err := r.AreNetworkPoliciesEnabled(ctx, chartMuseum)
if err != nil {
return errors.Wrapf(err, "cannot get network policies status")
}

if areNetworkPoliciesEnabled {
_, err = r.AddIngressNetworkPolicy(ctx, chartMuseum)
if err != nil {
return errors.Wrapf(err, "ingress network policy")
}
}
err = r.AddNetworkPolicies(ctx, chartMuseum)

return nil
return errors.Wrap(err, "network policies")
}
38 changes: 28 additions & 10 deletions controllers/goharbor/core/networkpolicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ import (

type NetworkPolicy graph.Resource

func (r *Reconciler) AddNetworkPolicies(ctx context.Context, core *goharborv1alpha2.Core) error {
areNetworkPoliciesEnabled, err := r.AreNetworkPoliciesEnabled(ctx, core)
if err != nil {
return errors.Wrapf(err, "cannot get status")
}

if !areNetworkPoliciesEnabled {
return nil
}

_, err = r.AddIngressNetworkPolicy(ctx, core)
if err != nil {
return errors.Wrapf(err, "ingress")
}

return nil
}

func (r *Reconciler) AddIngressNetworkPolicy(ctx context.Context, core *goharborv1alpha2.Core) (NetworkPolicy, error) {
networkPolicy, err := r.GetIngressNetworkPolicy(ctx, core)
if err != nil {
Expand All @@ -26,8 +44,13 @@ func (r *Reconciler) AddIngressNetworkPolicy(ctx context.Context, core *goharbor
}

func (r *Reconciler) GetIngressNetworkPolicy(ctx context.Context, core *goharborv1alpha2.Core) (*netv1.NetworkPolicy, error) {
httpPort := intstr.FromString(harbormetav1.CoreHTTPPortName)
httpsPort := intstr.FromString(harbormetav1.CoreHTTPSPortName)
var port intstr.IntOrString

if core.Spec.Components.TLS != nil {
port = intstr.FromString(harbormetav1.CoreHTTPSPortName)
} else {
port = intstr.FromString(harbormetav1.CoreHTTPPortName)
}

return &netv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -37,14 +60,9 @@ func (r *Reconciler) GetIngressNetworkPolicy(ctx context.Context, core *goharbor
Spec: netv1.NetworkPolicySpec{
Ingress: []netv1.NetworkPolicyIngressRule{
{
Ports: []netv1.NetworkPolicyPort{
{
Port: &httpPort,
},
{
Port: &httpsPort,
},
},
Ports: []netv1.NetworkPolicyPort{{
Port: &port,
}},
},
},
PodSelector: metav1.LabelSelector{
Expand Down
14 changes: 2 additions & 12 deletions controllers/goharbor/core/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,7 @@ func (r *Reconciler) AddResources(ctx context.Context, resource resources.Resour
return errors.Wrapf(err, "cannot add deployment %s", deployment.GetName())
}

areNetworkPoliciesEnabled, err := r.AreNetworkPoliciesEnabled(ctx, core)
if err != nil {
return errors.Wrapf(err, "cannot get network policies status")
}

if areNetworkPoliciesEnabled {
_, err = r.AddIngressNetworkPolicy(ctx, core)
if err != nil {
return errors.Wrapf(err, "ingress network policy")
}
}
err = r.AddNetworkPolicies(ctx, core)

return nil
return errors.Wrap(err, "network policies")
}
10 changes: 6 additions & 4 deletions controllers/goharbor/harbor/chartmuseum.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *Reconciler) AddChartMuseum(ctx context.Context, harbor *goharborv1alpha
return ChartMuseum(chartmuseumRes), errors.Wrap(err, "add")
}

func (r *Reconciler) GetChartMuseum(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*goharborv1alpha2.ChartMuseum, error) {
func (r *Reconciler) GetChartMuseum(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*goharborv1alpha2.ChartMuseum, error) { //nolint:funlen
name := r.NormalizeName(ctx, harbor.GetName())
namespace := harbor.GetNamespace()

Expand All @@ -89,9 +89,11 @@ func (r *Reconciler) GetChartMuseum(ctx context.Context, harbor *goharborv1alpha

return &goharborv1alpha2.ChartMuseum{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.ChartMuseumSpec{
ComponentSpec: harbor.Spec.ChartMuseum.ComponentSpec,
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,11 @@ func (r *Reconciler) GetCore(ctx context.Context, harbor *goharborv1alpha2.Harbo

return &goharborv1alpha2.Core{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.CoreSpec{
ComponentSpec: harbor.Spec.Registry.ComponentSpec,
Expand Down
1 change: 1 addition & 0 deletions controllers/goharbor/harbor/harbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Reconciler struct {
// +kubebuilder:rbac:groups=goharbor.io,resources=harbors/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=goharbor.io,resources=chartmuseums;cores;jobservices;notaryservers;notarysigners;portals;registries;registrycontrollers;trivies,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.k8s.io,resources=networkpolicies,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=cert-manager.io,resources=issuers;certificates,verbs=get;list;watch;create;update;patch;delete

func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/jobservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ func (r *Reconciler) GetJobService(ctx context.Context, harbor *goharborv1alpha2

return &goharborv1alpha2.JobService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.JobServiceSpec{
ComponentSpec: harbor.Spec.Registry.ComponentSpec,
Expand Down
20 changes: 10 additions & 10 deletions controllers/goharbor/harbor/networkpolicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ func (r *Reconciler) AddCoreIngressNetworkPolicy(ctx context.Context, harbor *go
}

func (r *Reconciler) GetCoreIngressNetworkPolicy(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*netv1.NetworkPolicy, error) { //nolint:dupl
httpPort := intstr.FromString(harbormetav1.CoreHTTPPortName)
httpsPort := intstr.FromString(harbormetav1.CoreHTTPSPortName)
var port intstr.IntOrString

if harbor.Spec.Expose.Core.TLS != nil {
port = intstr.FromString(harbormetav1.CoreHTTPSPortName)
} else {
port = intstr.FromString(harbormetav1.CoreHTTPPortName)
}

return &netv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -132,14 +137,9 @@ func (r *Reconciler) GetCoreIngressNetworkPolicy(ctx context.Context, harbor *go
Spec: netv1.NetworkPolicySpec{
Ingress: []netv1.NetworkPolicyIngressRule{
{
Ports: []netv1.NetworkPolicyPort{
{
Port: &httpPort,
},
{
Port: &httpsPort,
},
},
Ports: []netv1.NetworkPolicyPort{{
Port: &port,
}},
},
},
PodSelector: metav1.LabelSelector{
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/notaryserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ func (r *Reconciler) GetNotaryServer(ctx context.Context, harbor *goharborv1alph

return &goharborv1alpha2.NotaryServer{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.NotaryServerSpec{
ComponentSpec: harbor.Spec.Notary.Server,
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/notarysigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,11 @@ func (r *Reconciler) GetNotarySigner(ctx context.Context, harbor *goharborv1alph

return &goharborv1alpha2.NotarySigner{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.NotarySignerSpec{
ComponentSpec: harbor.Spec.Notary.Signer,
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ func (r *Reconciler) GetPortal(ctx context.Context, harbor *goharborv1alpha2.Har

return &goharborv1alpha2.Portal{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.PortalSpec{
ComponentSpec: harbor.Spec.Portal,
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ func (r *Reconciler) GetRegistry(ctx context.Context, harbor *goharborv1alpha2.H

return &goharborv1alpha2.Registry{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.RegistrySpec{
ComponentSpec: harbor.Spec.Registry.ComponentSpec,
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/registryctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func (r *Reconciler) GetRegistryCtl(ctx context.Context, harbor *goharborv1alpha

return &goharborv1alpha2.RegistryController{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.RegistryControllerSpec{
ComponentSpec: harbor.Spec.Registry.ComponentSpec,
Expand Down
8 changes: 5 additions & 3 deletions controllers/goharbor/harbor/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ func (r *Reconciler) GetTrivy(ctx context.Context, harbor *goharborv1alpha2.Harb

return &goharborv1alpha2.Trivy{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: r.NetworkPolicyAnnotationDisabled(),
Name: name,
Namespace: namespace,
Annotations: map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
},
},
Spec: goharborv1alpha2.TrivySpec{
ComponentSpec: harbor.Spec.Trivy.ComponentSpec,
Expand Down

0 comments on commit e73fbd8

Please sign in to comment.