Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Adding cmd directory and edit .gitignore
Browse files Browse the repository at this point in the history
**Why:**

* My .gitignore was too open in it's selector

**This change addresses the need by:**

* close #28

Signed-off-by: Christopher Hein <me@christopherhein.com>
  • Loading branch information
christopherhein committed Aug 3, 2018
1 parent 7d65ebe commit 81a3151
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
aws-operator
/aws-operator
pod-role.json
.DS_Store
aws-operator.yml
Expand Down
100 changes: 100 additions & 0 deletions cmd/aws-operator/main.go
@@ -0,0 +1,100 @@
package main

import (
"fmt"
homedir "github.com/mitchellh/go-homedir"

"github.com/christopherhein/aws-operator/pkg/config"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)

var (
// cfgFile, kubeConfig, awsRegion all help support passed in flags into the server
cfgFile, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket string

// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "aws-operator",
Short: "AWS Operator manages your AWS Infrastructure using CRDs and Operators",
Long: `TODO WRITE THIS`,
Run: func(c *cobra.Command, _ []string) {
c.Help()
},
}
)

func main() {
cobra.OnInitialize(initConfig)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "f", "Config file (default is $HOME/.aws-operator.yaml)")
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to local kubeconfig file (mainly used for development)")
rootCmd.PersistentFlags().StringVarP(&awsRegion, "region", "r", "us-west-2", "AWS Region for resources to be created in")
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "Info", "Log level for the CLI")
rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "", "", "Log level for the CLI")
rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "s3bucket,dynamodb", "Comma delimited list of CRDs to deploy")
rootCmd.PersistentFlags().StringVarP(&clusterName, "cluster-name", "i", "aws-operator", "Cluster name for the Application to run as, used to label the Cloudformation templated to avoid conflict")
rootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "aws-operator", "To configure the operator you need a base bucket to contain the resources")

viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig"))
viper.BindPFlag("region", rootCmd.PersistentFlags().Lookup("region"))
viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))
viper.BindPFlag("logfile", rootCmd.PersistentFlags().Lookup("logfile"))
viper.BindPFlag("resources", rootCmd.PersistentFlags().Lookup("resources"))
viper.BindPFlag("clustername", rootCmd.PersistentFlags().Lookup("cluster-name"))
viper.BindPFlag("bucket", rootCmd.PersistentFlags().Lookup("bucket"))
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

viper.AddConfigPath(home)
viper.SetConfigName(".aws-operator")
}

replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

func getConfig() (*config.Config, error) {
resourcesList := strings.Split(resources, ",")
config := &config.Config{
Region: awsRegion,
Kubeconfig: kubeconfig,
LoggingConfig: &config.LoggingConfig{
File: logFile,
Level: logLevel,
FullTimestamps: true,
DisableTimestamps: false,
},
Resources: resourcesList,
ClusterName: clusterName,
Bucket: bucket,
}

return config, nil
}
49 changes: 49 additions & 0 deletions cmd/aws-operator/server.go
@@ -0,0 +1,49 @@
package main

import (
"github.com/christopherhein/aws-operator/pkg/logger"
"github.com/christopherhein/aws-operator/pkg/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"os/signal"
"syscall"
)

var serverCmd = &cobra.Command{
Use: "server",
Short: "Run the operator",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
config, err := getConfig()
if err != nil {
logrus.Fatalf("%s", err)
}

logger, err := logger.Configure(config.LoggingConfig)
if err != nil {
logrus.Fatalf("Failed to configure logging: '%s'" + err.Error())
}
config.Logger = logger

signalChan := make(chan os.Signal, 1)
stopChan := make(chan struct{})
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

server.New(config).Run(stopChan)

for {
select {
case <-signalChan:
logger.Info("shutdown signal received, exiting...")
close(stopChan)
return
}
}

},
}

func init() {
rootCmd.AddCommand(serverCmd)
}
36 changes: 36 additions & 0 deletions cmd/aws-operator/version.go
@@ -0,0 +1,36 @@
package main

import (
"fmt"
goVersion "github.com/christopherhein/go-version"
"github.com/spf13/cobra"
)

var (
shortened = false
version = "dev"
commit = "none"
date = "unknown"
versionCmd = &cobra.Command{
Use: "version",
Short: "Version will output the current aws-operator build information",
Long: ``,
Run: func(_ *cobra.Command, _ []string) {
var response string
versionOutput := goVersion.New(version, commit, date)

if shortened {
response = versionOutput.ToShortened()
} else {
response = versionOutput.ToJSON()
}
fmt.Printf("%+v", response)
return
},
}
)

func init() {
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
rootCmd.AddCommand(versionCmd)
}

0 comments on commit 81a3151

Please sign in to comment.