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

Commit 794bc44

Browse files
author
Christopher Hein
authored
Merge pull request #29 from christopherhein/bug/gitignore-removing-cmd
Adding cmd directory and edit .gitignore
2 parents 7d65ebe + 81a3151 commit 794bc44

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
aws-operator
1+
/aws-operator
22
pod-role.json
33
.DS_Store
44
aws-operator.yml

cmd/aws-operator/main.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
homedir "github.com/mitchellh/go-homedir"
6+
7+
"github.com/christopherhein/aws-operator/pkg/config"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
"os"
12+
"strings"
13+
)
14+
15+
var (
16+
// cfgFile, kubeConfig, awsRegion all help support passed in flags into the server
17+
cfgFile, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket string
18+
19+
// rootCmd represents the base command when called without any subcommands
20+
rootCmd = &cobra.Command{
21+
Use: "aws-operator",
22+
Short: "AWS Operator manages your AWS Infrastructure using CRDs and Operators",
23+
Long: `TODO WRITE THIS`,
24+
Run: func(c *cobra.Command, _ []string) {
25+
c.Help()
26+
},
27+
}
28+
)
29+
30+
func main() {
31+
cobra.OnInitialize(initConfig)
32+
33+
if err := rootCmd.Execute(); err != nil {
34+
fmt.Println(err)
35+
os.Exit(1)
36+
}
37+
}
38+
39+
func init() {
40+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "f", "Config file (default is $HOME/.aws-operator.yaml)")
41+
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to local kubeconfig file (mainly used for development)")
42+
rootCmd.PersistentFlags().StringVarP(&awsRegion, "region", "r", "us-west-2", "AWS Region for resources to be created in")
43+
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "Info", "Log level for the CLI")
44+
rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "", "", "Log level for the CLI")
45+
rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "s3bucket,dynamodb", "Comma delimited list of CRDs to deploy")
46+
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")
47+
rootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "aws-operator", "To configure the operator you need a base bucket to contain the resources")
48+
49+
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
50+
viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig"))
51+
viper.BindPFlag("region", rootCmd.PersistentFlags().Lookup("region"))
52+
viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))
53+
viper.BindPFlag("logfile", rootCmd.PersistentFlags().Lookup("logfile"))
54+
viper.BindPFlag("resources", rootCmd.PersistentFlags().Lookup("resources"))
55+
viper.BindPFlag("clustername", rootCmd.PersistentFlags().Lookup("cluster-name"))
56+
viper.BindPFlag("bucket", rootCmd.PersistentFlags().Lookup("bucket"))
57+
}
58+
59+
// initConfig reads in config file and ENV variables if set.
60+
func initConfig() {
61+
if cfgFile != "" {
62+
viper.SetConfigFile(cfgFile)
63+
} else {
64+
home, err := homedir.Dir()
65+
if err != nil {
66+
fmt.Println(err)
67+
os.Exit(1)
68+
}
69+
70+
viper.AddConfigPath(home)
71+
viper.SetConfigName(".aws-operator")
72+
}
73+
74+
replacer := strings.NewReplacer(".", "_")
75+
viper.SetEnvKeyReplacer(replacer)
76+
viper.AutomaticEnv()
77+
78+
if err := viper.ReadInConfig(); err == nil {
79+
fmt.Println("Using config file:", viper.ConfigFileUsed())
80+
}
81+
}
82+
83+
func getConfig() (*config.Config, error) {
84+
resourcesList := strings.Split(resources, ",")
85+
config := &config.Config{
86+
Region: awsRegion,
87+
Kubeconfig: kubeconfig,
88+
LoggingConfig: &config.LoggingConfig{
89+
File: logFile,
90+
Level: logLevel,
91+
FullTimestamps: true,
92+
DisableTimestamps: false,
93+
},
94+
Resources: resourcesList,
95+
ClusterName: clusterName,
96+
Bucket: bucket,
97+
}
98+
99+
return config, nil
100+
}

cmd/aws-operator/server.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"github.com/christopherhein/aws-operator/pkg/logger"
5+
"github.com/christopherhein/aws-operator/pkg/server"
6+
"github.com/sirupsen/logrus"
7+
"github.com/spf13/cobra"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
)
12+
13+
var serverCmd = &cobra.Command{
14+
Use: "server",
15+
Short: "Run the operator",
16+
Long: ``,
17+
Run: func(cmd *cobra.Command, args []string) {
18+
config, err := getConfig()
19+
if err != nil {
20+
logrus.Fatalf("%s", err)
21+
}
22+
23+
logger, err := logger.Configure(config.LoggingConfig)
24+
if err != nil {
25+
logrus.Fatalf("Failed to configure logging: '%s'" + err.Error())
26+
}
27+
config.Logger = logger
28+
29+
signalChan := make(chan os.Signal, 1)
30+
stopChan := make(chan struct{})
31+
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
32+
33+
server.New(config).Run(stopChan)
34+
35+
for {
36+
select {
37+
case <-signalChan:
38+
logger.Info("shutdown signal received, exiting...")
39+
close(stopChan)
40+
return
41+
}
42+
}
43+
44+
},
45+
}
46+
47+
func init() {
48+
rootCmd.AddCommand(serverCmd)
49+
}

cmd/aws-operator/version.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
goVersion "github.com/christopherhein/go-version"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var (
10+
shortened = false
11+
version = "dev"
12+
commit = "none"
13+
date = "unknown"
14+
versionCmd = &cobra.Command{
15+
Use: "version",
16+
Short: "Version will output the current aws-operator build information",
17+
Long: ``,
18+
Run: func(_ *cobra.Command, _ []string) {
19+
var response string
20+
versionOutput := goVersion.New(version, commit, date)
21+
22+
if shortened {
23+
response = versionOutput.ToShortened()
24+
} else {
25+
response = versionOutput.ToJSON()
26+
}
27+
fmt.Printf("%+v", response)
28+
return
29+
},
30+
}
31+
)
32+
33+
func init() {
34+
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
35+
rootCmd.AddCommand(versionCmd)
36+
}

0 commit comments

Comments
 (0)