Skip to content

Commit c1b7342

Browse files
committed
CORS-4264: Update the GCP provider to allow users to skip firewall actions
cluster: Update the scripts to include the new variables providers/gce: Update the config to include the new `ManageFirewallRules` boolean setting. This variable will allow users to skip the creation, deletion, and updates to firewall rules when set to false. Users may not want or have the ability to add the permissions to perform these actions on their service account. When this is the case the firewall rules should be pre created and managed by someone with permissions to achieve the same goal.
1 parent 2e53900 commit c1b7342

File tree

7 files changed

+115
-39
lines changed

7 files changed

+115
-39
lines changed

cluster/gce/gci/configure-helper.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,14 @@ EOF
958958
use_cloud_config="true"
959959
cat <<EOF >>/etc/gce.conf
960960
regional = ${MULTIMASTER}
961+
EOF
962+
fi
963+
# UnmanageFirewallRules indicates that the firewall
964+
# rules should not be managed by the provider.
965+
if [[ -n "${UNMANAGEFIREWALLRULES:-}" ]]; then
966+
use_cloud_config="true"
967+
cat <<EOF >>/etc/gce.conf
968+
unmanage-firewall-rules = ${UNMANAGEFIREWALLRULES}
961969
EOF
962970
fi
963971
if [[ -n "${GCE_ALPHA_FEATURES:-}" ]]; then

cluster/gce/util.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ KUBE_DOCKER_REGISTRY: $(yaml-quote "${KUBE_DOCKER_REGISTRY:-}")
11901190
KUBE_ADDON_REGISTRY: $(yaml-quote "${KUBE_ADDON_REGISTRY:-}")
11911191
MULTIZONE: $(yaml-quote "${MULTIZONE:-}")
11921192
MULTIMASTER: $(yaml-quote "${MULTIMASTER:-}")
1193+
UNMANAGEFIREWALLRULES: $(yaml-quote "${UNMANAGEFIREWALLRULES:-}")
11931194
NON_MASQUERADE_CIDR: $(yaml-quote "${NON_MASQUERADE_CIDR:-}")
11941195
ENABLE_DEFAULT_STORAGE_CLASS: $(yaml-quote "${ENABLE_DEFAULT_STORAGE_CLASS:-}")
11951196
# (TODO/cloud-provider-gcp): Need to figure out how to inject this

providers/gce/gce.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ type Cloud struct {
209209

210210
// enableRBSDefaultForL4NetLB disable Service controller from picking up services by default
211211
enableRBSDefaultForL4NetLB bool
212+
213+
unmanageFirewallRules bool
212214
}
213215

214216
// ConfigGlobal is the in memory representation of the gce.conf config data
@@ -246,6 +248,11 @@ type ConfigGlobal struct {
246248
// Default to none.
247249
// For example: MyFeatureFlag
248250
AlphaFeatures []string `gcfg:"alpha-features"`
251+
252+
// UnmanageFirewallRules should be set to true when the provider should not
253+
// create, delete, or update firewall rules. For instance, when a user does not have the
254+
// permissions to create firewall rules the rule creation should be skipped to avoid errors.
255+
UnmanageFirewallRules bool `gcfg:"manage-firewall-rules"`
249256
}
250257

251258
// ConfigFile is the struct used to parse the /etc/gce.conf configuration file.
@@ -273,13 +280,14 @@ type CloudConfig struct {
273280
SubnetworkName string
274281
SubnetworkURL string
275282
// DEPRECATED: Do not rely on this value as it may be incorrect.
276-
SecondaryRangeName string
277-
NodeTags []string
278-
NodeInstancePrefix string
279-
TokenSource oauth2.TokenSource
280-
UseMetadataServer bool
281-
AlphaFeatureGate *AlphaFeatureGate
282-
StackType string
283+
SecondaryRangeName string
284+
NodeTags []string
285+
NodeInstancePrefix string
286+
TokenSource oauth2.TokenSource
287+
UseMetadataServer bool
288+
AlphaFeatureGate *AlphaFeatureGate
289+
StackType string
290+
UnmanageFirewallRules bool
283291
}
284292

285293
func init() {
@@ -370,6 +378,7 @@ func generateCloudConfig(configFile *ConfigFile) (cloudConfig *CloudConfig, err
370378
cloudConfig.NodeTags = configFile.Global.NodeTags
371379
cloudConfig.NodeInstancePrefix = configFile.Global.NodeInstancePrefix
372380
cloudConfig.AlphaFeatureGate = NewAlphaFeatureGate(configFile.Global.AlphaFeatures)
381+
cloudConfig.UnmanageFirewallRules = configFile.Global.UnmanageFirewallRules
373382
}
374383

375384
// retrieve projectID and zone
@@ -573,6 +582,7 @@ func CreateGCECloud(config *CloudConfig) (*Cloud, error) {
573582
metricsCollector: newLoadBalancerMetrics(),
574583
projectsBasePath: getProjectsBasePath(service.BasePath),
575584
stackType: StackType(config.StackType),
585+
unmanageFirewallRules: config.UnmanageFirewallRules,
576586
}
577587

578588
gce.manager = &gceServiceManager{gce}
@@ -784,6 +794,11 @@ func (g *Cloud) IsLegacyNetwork() bool {
784794
return g.unsafeIsLegacyNetwork
785795
}
786796

797+
// UnmanageFirewallRules returns true if the provider is not managing firewall rule creation, deletion, and updates.
798+
func (g *Cloud) UnmanageFirewallRules() bool {
799+
return g.unmanageFirewallRules
800+
}
801+
787802
// SetInformers sets up the zone handlers we need watching for node changes.
788803
func (g *Cloud) SetInformers(informerFactory informers.SharedInformerFactory) {
789804
klog.Infof("Setting up informers for Cloud")

providers/gce/gce_fake.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,32 @@ import (
3030

3131
// TestClusterValues holds the config values for the fake/test gce cloud object.
3232
type TestClusterValues struct {
33-
ProjectID string
34-
Region string
35-
ZoneName string
36-
SecondaryZoneName string
37-
ClusterID string
38-
ClusterName string
39-
OnXPN bool
40-
Regional bool
41-
NetworkURL string
42-
SubnetworkURL string
43-
StackType StackType
33+
ProjectID string
34+
Region string
35+
ZoneName string
36+
SecondaryZoneName string
37+
ClusterID string
38+
ClusterName string
39+
OnXPN bool
40+
Regional bool
41+
NetworkURL string
42+
SubnetworkURL string
43+
StackType StackType
44+
UnmanageFirewallRules bool
4445
}
4546

4647
// DefaultTestClusterValues Creates a reasonable set of default cluster values
4748
// for generating a new test fake GCE cloud instance.
4849
func DefaultTestClusterValues() TestClusterValues {
4950
return TestClusterValues{
50-
ProjectID: "test-project",
51-
Region: "us-central1",
52-
ZoneName: "us-central1-b",
53-
SecondaryZoneName: "us-central1-c",
54-
ClusterID: "test-cluster-id",
55-
ClusterName: "Test-Cluster-Name",
56-
StackType: clusterStackIPV4,
51+
ProjectID: "test-project",
52+
Region: "us-central1",
53+
ZoneName: "us-central1-b",
54+
SecondaryZoneName: "us-central1-c",
55+
ClusterID: "test-cluster-id",
56+
ClusterName: "Test-Cluster-Name",
57+
StackType: clusterStackIPV4,
58+
UnmanageFirewallRules: false,
5759
}
5860
}
5961

@@ -75,20 +77,21 @@ func NewFakeGCECloud(vals TestClusterValues) *Cloud {
7577
panic(err)
7678
}
7779
gce := &Cloud{
78-
region: vals.Region,
79-
service: service,
80-
managedZones: []string{vals.ZoneName},
81-
localZone: vals.ZoneName,
82-
projectID: vals.ProjectID,
83-
networkProjectID: vals.ProjectID,
84-
ClusterID: fakeClusterID(vals.ClusterID),
85-
onXPN: vals.OnXPN,
86-
metricsCollector: newLoadBalancerMetrics(),
87-
projectsBasePath: getProjectsBasePath(service.BasePath),
88-
regional: vals.Regional,
89-
networkURL: vals.NetworkURL,
90-
unsafeSubnetworkURL: vals.SubnetworkURL,
91-
stackType: vals.StackType,
80+
region: vals.Region,
81+
service: service,
82+
managedZones: []string{vals.ZoneName},
83+
localZone: vals.ZoneName,
84+
projectID: vals.ProjectID,
85+
networkProjectID: vals.ProjectID,
86+
ClusterID: fakeClusterID(vals.ClusterID),
87+
onXPN: vals.OnXPN,
88+
metricsCollector: newLoadBalancerMetrics(),
89+
projectsBasePath: getProjectsBasePath(service.BasePath),
90+
regional: vals.Regional,
91+
networkURL: vals.NetworkURL,
92+
unsafeSubnetworkURL: vals.SubnetworkURL,
93+
stackType: vals.StackType,
94+
unmanageFirewallRules: vals.UnmanageFirewallRules,
9295
}
9396
c := cloud.NewMockGCE(&gceProjectRouter{gce})
9497
gce.c = c

providers/gce/gce_firewall.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func newFirewallMetricContext(request string) *metricContext {
3232

3333
// GetFirewall returns the Firewall by name.
3434
func (g *Cloud) GetFirewall(name string) (*compute.Firewall, error) {
35+
if g.unmanageFirewallRules {
36+
return nil, nil
37+
}
38+
3539
ctx, cancel := cloud.ContextWithCallTimeout()
3640
defer cancel()
3741

@@ -42,6 +46,10 @@ func (g *Cloud) GetFirewall(name string) (*compute.Firewall, error) {
4246

4347
// CreateFirewall creates the passed firewall
4448
func (g *Cloud) CreateFirewall(f *compute.Firewall) error {
49+
if g.unmanageFirewallRules {
50+
return nil
51+
}
52+
4553
ctx, cancel := cloud.ContextWithCallTimeout()
4654
defer cancel()
4755

@@ -51,6 +59,10 @@ func (g *Cloud) CreateFirewall(f *compute.Firewall) error {
5159

5260
// DeleteFirewall deletes the given firewall rule.
5361
func (g *Cloud) DeleteFirewall(name string) error {
62+
if g.unmanageFirewallRules {
63+
return nil
64+
}
65+
5466
ctx, cancel := cloud.ContextWithCallTimeout()
5567
defer cancel()
5668

@@ -60,6 +72,10 @@ func (g *Cloud) DeleteFirewall(name string) error {
6072

6173
// UpdateFirewall applies the given firewall as an update to an existing service.
6274
func (g *Cloud) UpdateFirewall(f *compute.Firewall) error {
75+
if g.unmanageFirewallRules {
76+
return nil
77+
}
78+
6379
ctx, cancel := cloud.ContextWithCallTimeout()
6480
defer cancel()
6581

@@ -69,6 +85,10 @@ func (g *Cloud) UpdateFirewall(f *compute.Firewall) error {
6985

7086
// PatchFirewall applies the given firewall as an update to an existing service.
7187
func (g *Cloud) PatchFirewall(f *compute.Firewall) error {
88+
if g.unmanageFirewallRules {
89+
return nil
90+
}
91+
7292
ctx, cancel := cloud.ContextWithCallTimeout()
7393
defer cancel()
7494

providers/gce/gce_loadbalancer_external.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,11 @@ func translateAffinityType(affinityType v1.ServiceAffinity) string {
964964
}
965965

966966
func (g *Cloud) firewallNeedsUpdate(name, serviceName, ipAddress string, ports []v1.ServicePort, sourceRanges utilnet.IPNetSet) (exists bool, needsUpdate bool, err error) {
967+
if g.unmanageFirewallRules {
968+
klog.V(2).Infof("firewallNeedsUpdate(%v): firewall rules are unmanaged", name)
969+
return false, false, nil
970+
}
971+
967972
fw, err := g.GetFirewall(MakeFirewallName(name))
968973
if err != nil {
969974
if isHTTPErrorCode(err, http.StatusNotFound) {
@@ -1009,6 +1014,11 @@ func (g *Cloud) firewallNeedsUpdate(name, serviceName, ipAddress string, ports [
10091014
}
10101015

10111016
func (g *Cloud) ensureHTTPHealthCheckFirewall(svc *v1.Service, serviceName, ipAddress, region, clusterID string, hosts []*gceInstance, hcName string, hcPort int32, isNodesHealthCheck bool) error {
1017+
if g.unmanageFirewallRules {
1018+
klog.V(2).Infof("ensureHTTPHealthCheckFirewall(%v): firewall rules are unmanaged", hcName)
1019+
return nil
1020+
}
1021+
10121022
// Prepare the firewall params for creating / checking.
10131023
desc := fmt.Sprintf(`{"kubernetes.io/cluster-id":"%s"}`, clusterID)
10141024
if !isNodesHealthCheck {
@@ -1083,6 +1093,11 @@ func createForwardingRule(s CloudForwardingRuleService, name, serviceName, regio
10831093
}
10841094

10851095
func (g *Cloud) createFirewall(svc *v1.Service, name, desc, destinationIP string, sourceRanges utilnet.IPNetSet, ports []v1.ServicePort, hosts []*gceInstance) error {
1096+
if g.unmanageFirewallRules {
1097+
klog.V(2).Infof("createFirewall(%v): firewall rules are unmanaged", name)
1098+
return nil
1099+
}
1100+
10861101
firewall, err := g.firewallObject(name, desc, destinationIP, sourceRanges, ports, hosts)
10871102
if err != nil {
10881103
return err

providers/gce/gce_loadbalancer_internal.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ func (g *Cloud) ensureInternalLoadBalancerDeleted(clusterName, clusterID string,
406406
}
407407

408408
deleteFunc := func(fwName string) error {
409+
if g.unmanageFirewallRules {
410+
klog.V(2).Infof("ensureInternalLoadBalancerDeleted(%v): firewall rules are unmanaged", fwName)
411+
return nil
412+
}
409413
if err := ignoreNotFound(g.DeleteFirewall(fwName)); err != nil {
410414
if isForbidden(err) && g.OnXPN() {
411415
klog.V(2).Infof("ensureInternalLoadBalancerDeleted(%v): could not delete traffic firewall on XPN cluster. Raising event.", loadBalancerName)
@@ -481,6 +485,11 @@ func (g *Cloud) teardownInternalHealthCheckAndFirewall(svc *v1.Service, hcName s
481485
}
482486
klog.V(2).Infof("teardownInternalHealthCheckAndFirewall(%v): health check deleted", hcName)
483487

488+
if g.unmanageFirewallRules {
489+
klog.V(2).Infof("teardownInternalHealthCheckAndFirewall(%v): unmanaged firewall rules", hcName)
490+
return nil
491+
}
492+
484493
hcFirewallName := makeHealthCheckFirewallNameFromHC(hcName)
485494
if err := ignoreNotFound(g.DeleteFirewall(hcFirewallName)); err != nil {
486495
if isForbidden(err) && g.OnXPN() {
@@ -497,6 +506,11 @@ func (g *Cloud) teardownInternalHealthCheckAndFirewall(svc *v1.Service, hcName s
497506

498507
func (g *Cloud) ensureInternalFirewall(svc *v1.Service, fwName, fwDesc, destinationIP string, sourceRanges []string, portRanges []string, protocol v1.Protocol, nodes []*v1.Node, legacyFwName string) error {
499508
klog.V(2).Infof("ensureInternalFirewall(%v): checking existing firewall", fwName)
509+
if g.unmanageFirewallRules {
510+
klog.V(2).Infof("ensureInternalFirewall(%v): firewall rules are unmanaged", fwName)
511+
return nil
512+
}
513+
500514
targetTags, err := g.GetNodeTags(nodeNames(nodes))
501515
if err != nil {
502516
return err

0 commit comments

Comments
 (0)