Skip to content

Commit

Permalink
ci: E2E Framework [Dev CLI] [6/6] (#2530)
Browse files Browse the repository at this point in the history
add deploy cli
  • Loading branch information
matmerr authored Feb 5, 2024
1 parent 5778117 commit e735d6e
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ test/scale/generated/*

# test env file
*.env

# omit e2e bin
test/e2e/bin/*
2 changes: 2 additions & 0 deletions test/e2e/cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This package is not for use in tests or authoring tests
## It merely provides a way to use the test infra in a way that is not coupled to the test framework, i.e. manually deploying a cluster on the fly
91 changes: 91 additions & 0 deletions test/e2e/cmd/clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"os/exec"
"os/user"
"strconv"
"strings"
"time"

"github.com/Azure/azure-container-networking/test/e2e/framework/azure"
"github.com/Azure/azure-container-networking/test/e2e/framework/types"
"github.com/spf13/cobra"
)

func newClusterCmd() *cobra.Command {
clusterCmd := &cobra.Command{
Use: "cluster",
Short: "deploys a cluster",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}

clusterCmd.AddCommand(newBYOCiliumCmd())

return clusterCmd
}

func newBYOCiliumCmd() *cobra.Command {
byocilium := &cobra.Command{
Use: "byocilium",
Short: "deploys a BYO Cilium Cluster",
RunE: func(cmd *cobra.Command, args []string) error {
job := types.NewJob("deploy BYO Cilium Cluster")

sub, err := GetCurrentAzCLISubscriptionID()
if err != nil {
return fmt.Errorf("failed to get subscription id: %w", err)
}

curuser, _ := user.Current()
clusterName := curuser.Username + "-byocilium-" + strconv.FormatInt(time.Now().Unix(), 10)

job.AddStep(&azure.CreateResourceGroup{
SubscriptionID: sub,
ResourceGroupName: clusterName,
Location: "westus2",
}, nil)

job.AddStep(&azure.CreateVNet{
VnetName: "testvnet",
VnetAddressSpace: "10.0.0.0/9",
}, nil)

job.AddStep(&azure.CreateSubnet{
SubnetName: "testsubnet",
SubnetAddressSpace: "10.0.0.0/12",
}, nil)

job.AddStep(&azure.CreateBYOCiliumCluster{
ClusterName: clusterName,
PodCidr: "10.128.0.0/9",
DNSServiceIP: "192.168.0.10",
ServiceCidr: "192.168.0.0/28",
}, nil)

if err := job.Run(); err != nil {
return err // nolint // wrapping this error is noise, Cobra will handle
}

fmt.Printf("\nto get the kubeconfig for this cluster, run:\n\n\taz aks get-credentials --resource-group %s --name %s\n\n", clusterName, clusterName)

return nil
},
}

return byocilium
}

func GetCurrentAzCLISubscriptionID() (string, error) {
// this function requires Azure CLI to be installed, as even the Azure SDK for Go makes a call to it when using the Azure CLI credential type:
// https://github.com/Azure/azure-sdk-for-go/blob/0cda95c7a7e55361d9602a7c8a141eec584f75cc/sdk/azidentity/azure_cli_credential.go#L116

cmd := exec.Command("az", "account", "show", "--query=id", "-o", "tsv")
output, err := cmd.Output()
if err != nil {
return "", err //nolint // wrapping this error is noise, caller will handle
}
return strings.TrimSpace(string(output)), nil
}
63 changes: 63 additions & 0 deletions test/e2e/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// this package is not used for e2e tests, but rather
// leverage the e2e framework to deploy components for
// quick access

package main

import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

func main() {
rootCmd := NewRootCmd()

cobra.OnInitialize(func() {
viper.AutomaticEnv()
initCommandFlags(rootCmd.Commands())
})

cobra.CheckErr(rootCmd.Execute())
}

func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "acndev",
Short: "Manual CLI for deploying specific ACN components, leveraging the e2e framework",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
viper.AutomaticEnv() // read in environment variables that match
return nil
},
}

clusterCmd := newClusterCmd()

rootCmd.AddCommand(clusterCmd)

return rootCmd
}

func initCommandFlags(commands []*cobra.Command) {
for _, cmd := range commands {
// bind vars from env or conf to pflags
err := viper.BindPFlags(cmd.Flags())
cobra.CheckErr(err)

c := cmd
c.Flags().VisitAll(func(flag *pflag.Flag) {
if viper.IsSet(flag.Name) && viper.GetString(flag.Name) != "" {
err := c.Flags().Set(flag.Name, viper.GetString(flag.Name))
cobra.CheckErr(err)
}
})

// call recursively on subcommands
if cmd.HasSubCommands() {
initCommandFlags(cmd.Commands())
}
}
}

0 comments on commit e735d6e

Please sign in to comment.