-
Notifications
You must be signed in to change notification settings - Fork 119
Add --create-nat gke-deployer flag
#307
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
base: master
Are you sure you want to change the base?
Changes from all commits
cf9f1e6
e903782
cbc3658
e4f9e89
4e79900
2ee9260
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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| Copyright 2025 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package deployer | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| "sigs.k8s.io/kubetest2/pkg/exec" | ||
| ) | ||
|
|
||
| func (d *Deployer) EnsureNat() error { | ||
| if !d.CreateNat { | ||
| return nil | ||
| } | ||
| if d.Network == "default" { | ||
| return fmt.Errorf("NAT router should be set manually for the default network") | ||
| } | ||
| region := regionFromLocation(d.Regions, d.Zones, d.retryCount) | ||
| nat := d.getNatName() | ||
| hostProject := d.Projects[0] | ||
|
Contributor
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 it always in the first project?
Contributor
Author
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 is a common assumption throughout the codebase, for example in func (d *Deployer) ensureFirewallRulesForMultiProjects() error {
hostProject := d.Projects[0]
...But also in multiple places in |
||
|
|
||
| if runWithNoOutput(exec.Command("gcloud", "compute", "routers", "describe", nat, | ||
| "--project="+hostProject, | ||
| "--region="+region, | ||
| "--format=value(name)")) != nil { | ||
| klog.V(1).Infof("Couldn't describe router '%s', assuming it doesn't exist and creating it", nat) | ||
| if err := runWithOutput(exec.Command("gcloud", "compute", "routers", "create", nat, | ||
| "--project="+hostProject, | ||
| "--network="+d.Network, | ||
| "--region="+region)); err != nil { | ||
| return fmt.Errorf("error creating NAT router: %w", err) | ||
| } | ||
| } | ||
| // Create this unique NAT configuration only if it does not exist yet. | ||
| if runWithNoOutput(exec.Command("gcloud", "compute", "routers", "nats", "describe", nat, | ||
| "--project="+hostProject, | ||
| "--router="+nat, | ||
| "--region="+region, | ||
| "--format=value(name)")) != nil { | ||
| klog.V(1).Infof("Couldn't describe NAT '%s', assuming it doesn't exist and creating it", nat) | ||
| if err := runWithOutput(exec.Command("gcloud", "compute", "routers", "nats", "create", nat, | ||
| "--project="+hostProject, | ||
| "--router="+nat, | ||
| "--region="+region, | ||
| "--auto-allocate-nat-external-ips", | ||
| "--nat-primary-subnet-ip-ranges")); err != nil { | ||
| return fmt.Errorf("error adding NAT to a router: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (d *Deployer) getNatName() string { | ||
| return "nat-router-" + d.Clusters[0] | ||
| } | ||
|
|
||
| func (d *Deployer) CleanupNat() error { | ||
| if !d.CreateNat { | ||
| return nil | ||
| } | ||
| region := regionFromLocation(d.Regions, d.Zones, d.retryCount) | ||
| nat := d.getNatName() | ||
| hostProject := d.Projects[0] | ||
|
|
||
| // Delete NAT router. That will remove NAT configuration as well. | ||
| if runWithNoOutput(exec.Command("gcloud", "compute", "routers", "describe", nat, | ||
| "--project="+hostProject, | ||
| "--region="+region, | ||
| "--format=value(name)")) == nil { | ||
| klog.V(1).Infof("Found NAT router '%s', deleting", nat) | ||
| err := runWithOutput(exec.Command("gcloud", "compute", "routers", "delete", "-q", nat, | ||
| "--project="+hostProject, | ||
| "--region="+region)) | ||
| if err != nil { | ||
| return fmt.Errorf("error deleting NAT router: %w", err) | ||
| } | ||
| } else { | ||
| klog.V(1).Infof("Found no NAT router '%s', assuming resources are clean", nat) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.
why is this?
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.
So my understanding is that using custom networks for test environments is best practice as it provides isolation and control over resources, like nat and firewalls (reason why in
EnsureFirewallRuleswe've got the same check). Cleaning up test-specific resources on the default network could become difficult/messyThere 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.
the
EnsureFirewallRuleshasso, it is better to fail on validation of the flags that at runtime, specially for these options.
Do we have more clear the use case for enabling the nat creation or are we just trying to map 1 to 1 options?