Skip to content

Commit

Permalink
code has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhadapaithankar committed Jun 17, 2024
1 parent d859378 commit 771a895
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 105 deletions.
2 changes: 1 addition & 1 deletion docs/deploy-full-rp-service-in-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
export RESOURCEGROUP=myResourceGroup
```

1. Create the if it doesn't exist
1. Create the resource group if it doesn't exist
```bash
az group create --resource-group $RESOURCEGROUP --location $LOCATION
```
Expand Down
116 changes: 13 additions & 103 deletions hack/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@ import (
msgraph_errors "github.com/Azure/ARO-RP/pkg/util/graph/graphsdk/models/odataerrors"
utillog "github.com/Azure/ARO-RP/pkg/util/log"
"github.com/Azure/ARO-RP/pkg/util/version"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)

const (
Cluster = "CLUSTER"
)

func main() {
log := utillog.GetLogger()

if err := run(context.Background(), log); err != nil {
if oDataError, ok := err.(msgraph_errors.ODataErrorable); ok {
spew.Dump(oDataError.GetErrorEscaped())
}
log.Fatal(err)
}
}

func run(ctx context.Context, log *logrus.Entry) error {
if len(os.Args) != 2 {
return fmt.Errorf("usage: CLUSTER=x %s {create,createApp,deleteApp,delete}", os.Args[0])
Expand All @@ -53,43 +37,11 @@ func run(ctx context.Context, log *logrus.Entry) error {
return err
}

clusterName := os.Getenv(Cluster)
vnetName := fmt.Sprintf("%s-vnet", clusterName)
resourceGroup := os.Getenv("RESOURCEGROUP")
location := os.Getenv("LOCATION")

cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}

resourceClient, err := armresources.NewResourceGroupsClient(os.Getenv("AZURE_SUBSCRIPTION_ID"), cred, nil)
if err != nil {
log.Fatalf("failed to create resource group client: %v", err)
}

vnetClient, err := armnetwork.NewVirtualNetworksClient(os.Getenv("AZURE_SUBSCRIPTION_ID"), cred, nil)
if err != nil {
log.Fatalf("failed to create virtual network client: %v", err)
}

// Create the resource group
log.Infof("Creating resource group %s in location %s...\n", resourceGroup, location)
_, err = resourceClient.CreateOrUpdate(ctx, resourceGroup, armresources.ResourceGroup{
Location: to.Ptr(location),
}, nil)
if err != nil {
log.Fatalf("failed to create resource group: %v", err)
vnetResourceGroup := os.Getenv("RESOURCEGROUP") // TODO: remove this when we deploy and peer a vnet per cluster create
if os.Getenv("CI") != "" {
vnetResourceGroup = os.Getenv(Cluster)
}

// Create the virtual network
log.Infof("Creating virtual network %s in resource group %s...\n", vnetName, resourceGroup)
err = createVNet(ctx, log, vnetClient, resourceGroup, vnetName, location)
if err != nil {
return err
}

log.Infof("Created virtual network %s in resource group %s\n", vnetName, resourceGroup)
clusterName := os.Getenv(Cluster)

osClusterVersion := os.Getenv("OS_CLUSTER_VERSION")
if osClusterVersion == "" {
Expand All @@ -106,67 +58,25 @@ func run(ctx context.Context, log *logrus.Entry) error {

switch strings.ToLower(os.Args[1]) {
case "create":
err = c.Create(ctx, resourceGroup, clusterName, osClusterVersion)
if err != nil {
// If cluster creation fails, delete the created VNet
deleteVNet(ctx, log, vnetClient, resourceGroup, vnetName)
}
return err
return c.Create(ctx, vnetResourceGroup, clusterName, osClusterVersion)
case "createapp":
return c.CreateApp(ctx, clusterName)
case "deleteapp":
return c.DeleteApp(ctx)
case "delete":
err = c.Delete(ctx, resourceGroup, clusterName)
if err == nil {
// If cluster deletion succeeds, delete the created VNet
deleteVNet(ctx, log, vnetClient, resourceGroup, vnetName)
}
return err
return c.Delete(ctx, vnetResourceGroup, clusterName)
default:
return fmt.Errorf("invalid command %s", os.Args[1])
}
}

func createVNet(ctx context.Context, log *logrus.Entry, vnetClient *armnetwork.VirtualNetworksClient, resourceGroup string, vnetName string, location string) error {
_, err := vnetClient.BeginCreateOrUpdate(ctx, resourceGroup, vnetName, armnetwork.VirtualNetwork{
Location: to.Ptr(location),
Properties: &armnetwork.VirtualNetworkPropertiesFormat{
AddressSpace: &armnetwork.AddressSpace{
AddressPrefixes: []*string{
to.Ptr("10.0.0.0/16"),
},
},
Subnets: []*armnetwork.Subnet{
{
Name: to.Ptr("master"),
Properties: &armnetwork.SubnetPropertiesFormat{
AddressPrefix: to.Ptr("10.0.0.0/24"),
},
},
{
Name: to.Ptr("worker"),
Properties: &armnetwork.SubnetPropertiesFormat{
AddressPrefix: to.Ptr("10.0.1.0/24"),
},
},
},
},
}, nil)
if err != nil {
log.Errorf("Failed to create VNet: %v", err)
return err
}
log.Infof("Created VNet %s in resource group %s", vnetName, resourceGroup)
return nil
}
func main() {
log := utillog.GetLogger()

func deleteVNet(ctx context.Context, log *logrus.Entry, vnetClient *armnetwork.VirtualNetworksClient, resourceGroup string, vnetName string) error {
_, err := vnetClient.BeginDelete(ctx, resourceGroup, vnetName, nil)
if err != nil {
log.Errorf("Failed to delete VNet: %v", err)
return err
if err := run(context.Background(), log); err != nil {
if oDataError, ok := err.(msgraph_errors.ODataErrorable); ok {
spew.Dump(oDataError.GetErrorEscaped())
}
log.Fatal(err)
}
log.Infof("Deleted VNet %s in resource group %s", vnetName, resourceGroup)
return nil
}
2 changes: 1 addition & 1 deletion setup_resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ add_openshift_version() {
}

# Add the required OpenShift version
add_openshift_version "4.10.28" "test.com/a:b" "test.com/a:b"
add_openshift_version "4.13.40" "test.com/a:b" "test.com/a:b"

# Delete the existing cluster if it exists
echo "Deleting the existing cluster if it exists..."
Expand Down

0 comments on commit 771a895

Please sign in to comment.