Created
May 10, 2020 19:23
-
-
Save achoimet/3aef4e318898086709aa1ec2113949dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
// Subcommands | |
aws := flag.NewFlagSet("aws", flag.ExitOnError) | |
inCluster := flag.NewFlagSet("inCluster", flag.ExitOnError) | |
outCluster := flag.NewFlagSet("outCluster", flag.ExitOnError) | |
// aws subcommand flag pointers | |
clusterName := aws.String("clusterName", "", "EKS Cluster name. (Required)") | |
clusterUrl := aws.String("clusterUrl", "", "EKS Cluster url. (Required)") | |
roleArn := aws.String("roleArn", "", "EKS Role to assume. (Required)") | |
undoAws := aws.Bool("undo", false, "undo the deployment") | |
undoInCluster := inCluster.Bool("undo", false, "undo the deployment") | |
undoOutCluster := outCluster.Bool("undo", false, "undo the deployment") | |
// Verify that a subcommand has been provided | |
// os.Arg[0] is the main command | |
// os.Arg[1] will be the subcommand | |
if len(os.Args) < 2 { | |
fmt.Println("aws or inCluster or outCluster subcommand is required") | |
os.Exit(1) | |
} | |
switch os.Args[1] { | |
case "aws": | |
err := aws.Parse(os.Args[2:]) | |
if err != nil { | |
fmt.Printf("failed to parse subcommand arguments: %v", err) | |
os.Exit(1) | |
} | |
case "inCluster": | |
err := inCluster.Parse(os.Args[2:]) | |
if err != nil { | |
fmt.Printf("failed to parse subcommand arguments: %v", err) | |
os.Exit(1) | |
} | |
case "outCluster": | |
err := outCluster.Parse(os.Args[2:]) | |
if err != nil { | |
fmt.Printf("failed to parse subcommand arguments: %v", err) | |
os.Exit(1) | |
} | |
default: | |
flag.PrintDefaults() | |
os.Exit(1) | |
} | |
// Check which subcommand was Parsed using the FlagSet.Parsed() function. Handle each case accordingly. | |
// FlagSet.Parse() will evaluate to false if no flags were parsed (i.e. the user did not provide any flags) | |
var clientSet *kubernetes.Clientset | |
var err error | |
if aws.Parsed() { | |
flag.Parse() | |
// Required Flags | |
if *clusterName == "" || *clusterUrl == "" || *roleArn == "" { | |
aws.PrintDefaults() | |
os.Exit(1) | |
} | |
//Create aws session | |
sess := session.Must(session.NewSession(&awssdk.Config{ | |
Region: awssdk.String("eu-west-3"), | |
})) | |
clientSet, err = k8sAuth.AuthenticateToEks(*clusterName, *clusterUrl, *roleArn, sess) | |
if err != nil { | |
fmt.Printf("failed to authenticate: %v", err) | |
os.Exit(1) | |
} | |
} | |
if outCluster.Parsed() { | |
flag.Parse() | |
clientSet, err = k8sAuth.AuthenticateOutOfCluster() | |
if err != nil { | |
fmt.Printf("failed to authenticate: %v", err) | |
os.Exit(1) | |
} | |
} | |
if inCluster.Parsed() { | |
flag.Parse() | |
clientSet, err = k8sAuth.AuthenticateInCluster() | |
if err != nil { | |
fmt.Printf("failed to authenticate: %v", err) | |
os.Exit(1) | |
} | |
} | |
if *undoAws || *undoOutCluster || *undoInCluster { | |
err = clientSet.CoreV1().Namespaces().Delete(appName, &metav1.DeleteOptions{}) | |
if err != nil && errors.ReasonForError(err) != metav1.StatusReasonNotFound { | |
fmt.Printf("failed to undo: %v", err) | |
os.Exit(1) | |
} | |
fmt.Println("App deleted.") | |
} else { | |
err = deploy(clientSet) | |
if err != nil { | |
fmt.Printf("failed to deploy: %v", err) | |
os.Exit(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment