Skip to content

Commit b3e51d7

Browse files
committed
CORS-4230: Add a firewall spec and the ability to manage or unmanaged firewall rule creation
api: Add API changes to Skip firewall rule creation. When unmanaged, the firewall rules will not be created. When this is the case, the firewall rules should exist prior to creating the network. This will allow ServiceAccounts to skip the rules: compute.firewalls.create cloud: Update the services and interfaces. The firewall service will no longer create firewall rules when the firewall policy is set to unmanaged OR when a shared vpc is used during installation and resource creation. Note: This commit has been cherry-picked
1 parent 7689efd commit b3e51d7

File tree

10 files changed

+179
-2
lines changed

10 files changed

+179
-2
lines changed

api/v1beta1/types.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,36 @@ type Network struct {
107107
APIInternalForwardingRule *string `json:"apiInternalForwardingRule,omitempty"`
108108
}
109109

110+
// FirewallSpec contains configuration for the firewall.
111+
type FirewallSpec struct {
112+
// DefaultRulesManagement determines the management policy for the default firewall rules
113+
// created by the controller. DefaultRulesManagement has no effect on user specified firewall
114+
// rules. DefaultRulesManagement has no effect when a HostProject is specified.
115+
// "Managed": The controller will create and manage firewall rules.
116+
// "Unmanaged": The controller will not create or modify any firewall rules. If
117+
// the RulesManagement is changed from Managed to Unmanaged after the firewall rules
118+
// have been created, then the firewall rules will not be deleted.
119+
//
120+
// Defaults to "Managed".
121+
// +optional
122+
// +kubebuilder:default:="Managed"
123+
DefaultRulesManagement RulesManagementPolicy `json:"defaultRulesManagement,omitempty"`
124+
}
125+
126+
// RulesManagementPolicy is a string enum type for managing firewall rules.
127+
// +kubebuilder:validation:Enum=Managed;Unmanaged
128+
type RulesManagementPolicy string
129+
130+
const (
131+
// RulesManagementManaged indicates that the controller should create and manage
132+
// firewall rules. This is the default behavior.
133+
RulesManagementManaged RulesManagementPolicy = "Managed"
134+
135+
// RulesManagementUnmanaged indicates that the controller should not create or manage
136+
// any firewall rules. If rules already exist, they will be left as-is.
137+
RulesManagementUnmanaged RulesManagementPolicy = "Unmanaged"
138+
)
139+
110140
// NetworkSpec encapsulates all things related to a GCP network.
111141
type NetworkSpec struct {
112142
// Name is the name of the network to be used.
@@ -137,6 +167,10 @@ type NetworkSpec struct {
137167
// +optional
138168
HostProject *string `json:"hostProject,omitempty"`
139169

170+
// Firewall configuration.
171+
// +optional
172+
Firewall FirewallSpec `json:"firewall,omitempty,omitzero"`
173+
140174
// Mtu: Maximum Transmission Unit in bytes. The minimum value for this field is
141175
// 1300 and the maximum value is 8896. The suggested value is 1500, which is
142176
// the default MTU used on the Internet, or 8896 if you want to use Jumbo

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type ClusterGetter interface {
5858
NetworkName() string
5959
NetworkProject() string
6060
IsSharedVpc() bool
61+
SkipFirewallRuleCreation() bool
6162
Network() *infrav1.Network
6263
AdditionalLabels() infrav1.Labels
6364
FailureDomains() clusterv1.FailureDomains

cloud/scope/cluster.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ func (s *ClusterScope) NetworkProject() string {
106106
return ptr.Deref(s.GCPCluster.Spec.Network.HostProject, s.Project())
107107
}
108108

109+
// SkipFirewallRuleCreation returns whether the spec indicates that firewall rules
110+
// should be created or not. If the RulesManagement for the default firewall rules is
111+
// set to unmanaged or when the cluster will include a shared VPC, the default firewall
112+
// rule creation will be skipped.
113+
func (s *ClusterScope) SkipFirewallRuleCreation() bool {
114+
return (s.GCPCluster.Spec.Network.Firewall.DefaultRulesManagement == infrav1.RulesManagementUnmanaged) || s.IsSharedVpc()
115+
}
116+
109117
// IsSharedVpc returns true If sharedVPC used else , returns false.
110118
func (s *ClusterScope) IsSharedVpc() bool {
111119
return s.NetworkProject() != s.Project()

cloud/scope/managedcluster.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ func (s *ManagedClusterScope) NetworkProject() string {
129129
return ptr.Deref(s.GCPManagedCluster.Spec.Network.HostProject, s.Project())
130130
}
131131

132+
// SkipFirewallRuleCreation returns whether the spec indicates that firewall rules
133+
// should be created or not. If the RulesManagement for the default firewall rules is
134+
// set to unmanaged or when the cluster will include a shared VPC, the default firewall
135+
// rule creation will be skipped.
136+
func (s *ManagedClusterScope) SkipFirewallRuleCreation() bool {
137+
return (s.GCPManagedCluster.Spec.Network.Firewall.DefaultRulesManagement == infrav1.RulesManagementUnmanaged) || s.IsSharedVpc()
138+
}
139+
132140
// IsSharedVpc returns true If sharedVPC used else , returns false.
133141
func (s *ManagedClusterScope) IsSharedVpc() bool {
134142
return s.NetworkProject() != s.Project()

cloud/services/compute/firewalls/reconcile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
// Reconcile reconcile cluster firewall compoenents.
2929
func (s *Service) Reconcile(ctx context.Context) error {
3030
log := log.FromContext(ctx)
31-
if s.scope.IsSharedVpc() {
32-
log.V(2).Info("Shared VPC enabled. Ignore Reconciling firewall resources")
31+
if s.scope.SkipFirewallRuleCreation() {
32+
log.V(2).Info("Ignore Reconciling firewall resources")
3333
return nil
3434
}
3535
log.Info("Reconciling firewall resources")

cloud/services/compute/firewalls/reconcile_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ var fakeGCPClusterSharedVPC = &infrav1.GCPCluster{
109109
},
110110
}
111111

112+
var fakeGCPClusterUnmanagedFirewalls = &infrav1.GCPCluster{
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "my-cluster",
115+
Namespace: "default",
116+
},
117+
Spec: infrav1.GCPClusterSpec{
118+
Project: "my-proj",
119+
Region: "us-central1",
120+
Network: infrav1.NetworkSpec{
121+
Name: ptr.To("my-network"),
122+
Subnets: infrav1.Subnets{
123+
infrav1.SubnetSpec{
124+
Name: "workers",
125+
CidrBlock: "10.0.0.1/28",
126+
Region: "us-central1",
127+
Purpose: ptr.To[string]("INTERNAL_HTTPS_LOAD_BALANCER"),
128+
},
129+
},
130+
Firewall: infrav1.FirewallSpec{
131+
DefaultRulesManagement: infrav1.RulesManagementUnmanaged,
132+
},
133+
},
134+
},
135+
Status: infrav1.GCPClusterStatus{
136+
Network: infrav1.Network{},
137+
},
138+
}
139+
112140
type testCase struct {
113141
name string
114142
scope func() Scope
@@ -146,6 +174,18 @@ func TestService_Reconcile(t *testing.T) {
146174
t.Fatal(err)
147175
}
148176

177+
clusterScopeUnmanagedFirewalls, err := scope.NewClusterScope(context.TODO(), scope.ClusterScopeParams{
178+
Client: fakec,
179+
Cluster: fakeCluster,
180+
GCPCluster: fakeGCPClusterUnmanagedFirewalls,
181+
GCPServices: scope.GCPServices{
182+
Compute: &compute.Service{},
183+
},
184+
})
185+
if err != nil {
186+
t.Fatal(err)
187+
}
188+
149189
tests := []testCase{
150190
{
151191
name: "firewall rule does not exist successful create",
@@ -211,6 +251,16 @@ func TestService_Reconcile(t *testing.T) {
211251
},
212252
},
213253
},
254+
{
255+
name: "firewall return no error using unmanaged firewall settings",
256+
scope: func() Scope { return clusterScopeUnmanagedFirewalls },
257+
mockFirewalls: &cloud.MockFirewalls{
258+
ProjectRouter: &cloud.SingleProjectRouter{ID: "my-proj"},
259+
Objects: map[meta.Key]*cloud.MockFirewallsObj{
260+
*meta.GlobalKey(fmt.Sprintf("allow-%s-healthchecks", fakeGCPCluster.Name)): {},
261+
},
262+
},
263+
},
214264
}
215265
for _, tt := range tests {
216266
t.Run(tt.name, func(t *testing.T) {

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclusters.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ spec:
173173
174174
Defaults to true.
175175
type: boolean
176+
firewall:
177+
description: Firewall configuration.
178+
properties:
179+
defaultRulesManagement:
180+
default: Managed
181+
description: |-
182+
DefaultRulesManagement determines the management policy for the default firewall rules
183+
created by the controller. DefaultRulesManagement has no effect on user specified firewall
184+
rules. DefaultRulesManagement has no effect when a HostProject is specified.
185+
"Managed": The controller will create and manage firewall rules.
186+
"Unmanaged": The controller will not create or modify any firewall rules. If
187+
the RulesManagement is changed from Managed to Unmanaged after the firewall rules
188+
have been created, then the firewall rules will not be deleted.
189+
190+
Defaults to "Managed".
191+
enum:
192+
- Managed
193+
- Unmanaged
194+
type: string
195+
type: object
176196
hostProject:
177197
description: HostProject is the name of the project hosting the
178198
shared VPC network resources.

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,26 @@ spec:
192192
193193
Defaults to true.
194194
type: boolean
195+
firewall:
196+
description: Firewall configuration.
197+
properties:
198+
defaultRulesManagement:
199+
default: Managed
200+
description: |-
201+
DefaultRulesManagement determines the management policy for the default firewall rules
202+
created by the controller. DefaultRulesManagement has no effect on user specified firewall
203+
rules. DefaultRulesManagement has no effect when a HostProject is specified.
204+
"Managed": The controller will create and manage firewall rules.
205+
"Unmanaged": The controller will not create or modify any firewall rules. If
206+
the RulesManagement is changed from Managed to Unmanaged after the firewall rules
207+
have been created, then the firewall rules will not be deleted.
208+
209+
Defaults to "Managed".
210+
enum:
211+
- Managed
212+
- Unmanaged
213+
type: string
214+
type: object
195215
hostProject:
196216
description: HostProject is the name of the project hosting
197217
the shared VPC network resources.

config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmanagedclusters.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ spec:
169169
170170
Defaults to true.
171171
type: boolean
172+
firewall:
173+
description: Firewall configuration.
174+
properties:
175+
defaultRulesManagement:
176+
default: Managed
177+
description: |-
178+
DefaultRulesManagement determines the management policy for the default firewall rules
179+
created by the controller. DefaultRulesManagement has no effect on user specified firewall
180+
rules. DefaultRulesManagement has no effect when a HostProject is specified.
181+
"Managed": The controller will create and manage firewall rules.
182+
"Unmanaged": The controller will not create or modify any firewall rules. If
183+
the RulesManagement is changed from Managed to Unmanaged after the firewall rules
184+
have been created, then the firewall rules will not be deleted.
185+
186+
Defaults to "Managed".
187+
enum:
188+
- Managed
189+
- Unmanaged
190+
type: string
191+
type: object
172192
hostProject:
173193
description: HostProject is the name of the project hosting the
174194
shared VPC network resources.

0 commit comments

Comments
 (0)