-
Notifications
You must be signed in to change notification settings - Fork 0
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
create clusterwidenetworkpolicy from AccessList.SourceRanges #67
Changes from 5 commits
fa0b551
6f3a8cf
7a5041c
07c8bb4
d6a755e
e3a8bab
ddf10f6
328b8fc
744bb71
cc56183
71d6f6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,16 @@ limitations under the License. | |
package v1 | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
firewall "github.com/metal-stack/firewall-controller/api/v1" | ||
"inet.af/netaddr" | ||
corev1 "k8s.io/api/core/v1" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! | ||
|
@@ -69,8 +75,8 @@ type PostgresSpec struct { | |
|
||
// AccessList defines the type of restrictions to access the database | ||
type AccessList struct { | ||
// SourceRanges defines a list of prefixes in CIDR Notation e.g. 1.2.3.0/24 | ||
// FIXME implement validation if source is a parsable CIDR | ||
// +kubebuilder:validation:Required | ||
// SourceRanges defines a list of prefixes in CIDR Notation e.g. 1.2.3.0/24 or fdaa::/104 | ||
SourceRanges []string `json:"sourceRanges,omitempty"` | ||
} | ||
|
||
|
@@ -143,6 +149,35 @@ func (p *Postgres) IsBeingDeleted() bool { | |
return !p.ObjectMeta.DeletionTimestamp.IsZero() | ||
} | ||
|
||
func (p *Postgres) ToCWNP(port int) (*firewall.ClusterwideNetworkPolicy, error) { | ||
portObj := intstr.FromInt(port) | ||
tcp := corev1.ProtocolTCP | ||
ports := []networkingv1.NetworkPolicyPort{ | ||
{Port: &portObj, Protocol: &tcp}, | ||
} | ||
|
||
ipblocks := []networkingv1.IPBlock{} | ||
for _, src := range p.Spec.AccessList.SourceRanges { | ||
parsedSrc, err := netaddr.ParseIPPrefix(src) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse source range %s: %w", src, err) | ||
} | ||
ipblock := networkingv1.IPBlock{ | ||
CIDR: parsedSrc.String(), | ||
} | ||
ipblocks = append(ipblocks, ipblock) | ||
} | ||
|
||
policy := &firewall.ClusterwideNetworkPolicy{} | ||
policy.Namespace = "firewall" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no there is no exported const: https://github.com/metal-stack/firewall-controller/blob/master/controllers/clusterwidenetworkpolicy_controller.go#L40 |
||
policy.Name = p.Spec.ProjectID + "--" + string(p.UID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the same, centralized logic for generating a name as the postgresql resources so that both names match. Currently, this is the case ( |
||
policy.Spec.Ingress = []firewall.IngressRule{ | ||
{Ports: ports, From: ipblocks}, | ||
} | ||
|
||
return policy, nil | ||
} | ||
|
||
func (p *Postgres) ToKey() *types.NamespacedName { | ||
return &types.NamespacedName{ | ||
Namespace: p.Namespace, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: database | ||
--- | ||
apiVersion: database.fits.cloud/v1 | ||
kind: Postgres | ||
metadata: | ||
namespace: database | ||
name: sample-name-a | ||
spec: | ||
accessList: | ||
sourceRanges: | ||
- 1.2.3.4/24 | ||
backup: | ||
s3BucketURL: "" | ||
numberOfInstances: 2 | ||
partitionID: sample-partition | ||
projectID: projectid-a | ||
size: | ||
storageSize: 1Gi | ||
tenant: sample-tenant | ||
version: "12" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SourceRanges might be empty list if customer doesn't care