Skip to content

Commit

Permalink
Merge pull request #58 from edenlabllc/bugfix/v0.45.0
Browse files Browse the repository at this point in the history
back merge
  • Loading branch information
anovikov-el authored Jan 13, 2025
2 parents e62ed9e + 0a38f73 commit 11c237c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
9 changes: 8 additions & 1 deletion cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"

"rmk/config"
"rmk/git_handler"
"rmk/providers/aws_provider"
"rmk/providers/azure_provider"
"rmk/providers/google_provider"
Expand Down Expand Up @@ -448,12 +449,18 @@ func clusterSwitchAction(conf *config.Config) cli.ActionFunc {
}
}

func CAPIInitAction(conf *config.Config) cli.AfterFunc {
func CAPIInitAction(conf *config.Config, gitSpec *git_handler.GitSpec) cli.AfterFunc {
return func(c *cli.Context) error {
if err := util.ValidateNArg(c, 0); err != nil {
return err
}

// Additional checking is needed because After() is run even if Action() panics
configPath := util.GetHomePath(util.RMKDir, util.RMKConfig, gitSpec.ID+".yaml")
if !util.IsExists(configPath, true) {
return nil
}

cc := newClusterCommands(conf, c, util.GetPwdPath())
if err := cc.switchKubeContext(); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Commands() []*cli.Command {
Category: "capi",
BashComplete: util.ShellCompleteCustomOutput,
Action: K3DCreateAction(conf),
After: CAPIInitAction(conf),
After: CAPIInitAction(conf, gitSpec),
},
{
Name: "delete",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.8

require (
cloud.google.com/go/auth v0.10.2
cloud.google.com/go/compute v1.24.0
cloud.google.com/go/secretmanager v1.11.5
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ cloud.google.com/go/compute v1.6.0/go.mod h1:T29tfhtVbq1wvAPo0E3+7vhgmkOYeXjhFvz
cloud.google.com/go/compute v1.6.1/go.mod h1:g85FgpzFvNULZ+S8AYq87axRKuf2Kh7deLqV/jJ3thU=
cloud.google.com/go/compute v1.7.0/go.mod h1:435lt8av5oL9P3fv1OEzSbSUe+ybHXGMPQHHZWZxy9U=
cloud.google.com/go/compute v1.10.0/go.mod h1:ER5CLbMxl90o2jtNbGSbtfOpQKR0t15FOtRsugnLrlU=
cloud.google.com/go/compute v1.24.0 h1:phWcR2eWzRJaL/kOiJwfFsPs4BaKq1j6vnpZrc1YlVg=
cloud.google.com/go/compute v1.24.0/go.mod h1:kw1/T+h/+tK2LJK0wiPPx1intgdAM3j/g3hFDlscY40=
cloud.google.com/go/compute/metadata v0.5.1 h1:NM6oZeZNlYjiwYje+sYFjEpP0Q0zCan1bmQW/KmIrGs=
cloud.google.com/go/compute/metadata v0.5.1/go.mod h1:C66sj2AluDcIqakBq/M8lw8/ybHgOZqin2obFxa/E5k=
cloud.google.com/go/containeranalysis v0.5.1/go.mod h1:1D92jd8gRR/c0fGMlymRgxWD3Qw9C1ff6/T7mLgVL8I=
Expand Down
32 changes: 25 additions & 7 deletions providers/google_provider/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"cloud.google.com/go/auth"
"cloud.google.com/go/auth/credentials"
"cloud.google.com/go/compute/apiv1/computepb"
secretmanager "cloud.google.com/go/secretmanager/apiv1"
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
"github.com/googleapis/gax-go/v2/apierror"
Expand Down Expand Up @@ -212,12 +213,12 @@ func (gcp *GCPConfigure) CreateGCPCloudNATGateway(region string) error {
}

routerNat := &compute.RouterNat{
AutoNetworkTier: "STANDARD",
EndpointTypes: []string{"ENDPOINT_TYPE_VM"},
AutoNetworkTier: computepb.RouterNat_STANDARD.String(),
EndpointTypes: []string{computepb.RouterNat_ENDPOINT_TYPE_VM.String()},
Name: "default-nat-" + region,
NatIpAllocateOption: "AUTO_ONLY",
SourceSubnetworkIpRangesToNat: "ALL_SUBNETWORKS_ALL_IP_RANGES",
Type: "PUBLIC",
NatIpAllocateOption: computepb.RouterNat_AUTO_ONLY.String(),
SourceSubnetworkIpRangesToNat: computepb.RouterNat_ALL_SUBNETWORKS_ALL_IP_RANGES.String(),
Type: computepb.RouterNat_PUBLIC.String(),
}

router := &compute.Router{
Expand Down Expand Up @@ -248,12 +249,29 @@ func (gcp *GCPConfigure) DeleteGCPCloudNATGateway(region string) error {
return err
}

client, err := compute.NewService(gcp.Ctx, option.WithCredentialsJSON(gcp.AppCredentials.JSON()))
containerClient, err := container.NewService(gcp.Ctx, option.WithCredentialsJSON(gcp.AppCredentials.JSON()))
if err != nil {
return err
}

parent := fmt.Sprintf("projects/%s/locations/%s", gcp.ProjectID, region)
resp, err := containerClient.Projects.Locations.Clusters.List(parent).Context(gcp.Ctx).Do()
if err != nil {
return err
}

if len(resp.Clusters) > 0 {
zap.S().Infof("skipped deleting GCP router %s because there are %d clusters in %s region",
"default-router-"+region, len(resp.Clusters), region)
return nil
}

computeClient, err := compute.NewService(gcp.Ctx, option.WithCredentialsJSON(gcp.AppCredentials.JSON()))
if err != nil {
return err
}

_, err = client.Routers.Delete(gcp.ProjectID, region, "default-router-"+region).Context(gcp.Ctx).Do()
_, err = computeClient.Routers.Delete(gcp.ProjectID, region, "default-router-"+region).Context(gcp.Ctx).Do()
if err != nil {
var respError *googleapi.Error
if errors.As(err, &respError) && respError.Code == 404 && respError.Errors[0].Reason == apiErrorNotFound {
Expand Down

0 comments on commit 11c237c

Please sign in to comment.