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

Commit

Permalink
Merge pull request #97 from christopherhein/chore/95-cmd-dir
Browse files Browse the repository at this point in the history
Using AWS Service Operator Name
  • Loading branch information
Christopher Hein authored Sep 29, 2018
2 parents 0ae16c9 + 18707f0 commit c91eab4
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/aws-operator
/aws-service-operator
pod-role.json
.DS_Store
aws-operator.yaml
/aws-service-operator.yaml
kubeconfig
dist/
.envrc
8 changes: 4 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_name: aws-operator
project_name: aws-service-operator

# This will be useful in making sure that all files are updated before
# a release is made.
Expand All @@ -8,8 +8,8 @@ before:

# Builds the binary for each platform
builds:
- binary: aws-operator
main: ./cmd/aws-operator/
- binary: aws-service-operator
main: ./cmd/aws-service-operator/
goos:
- darwin
- windows
Expand Down Expand Up @@ -43,7 +43,7 @@ release:
# Creates a Docker container with the operator packaged into it for distribution
dockers:
- image: awsserviceoperator/aws-service-operator
binary: aws-operator
binary: aws-service-operator
dockerfile: Dockerfile
tag_templates:
- "{{ .Tag }}"
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM alpine
MAINTAINER Christopher Hein <me@christopherhein.com>
MAINTAINER Christopher Hein <heichris@amazon.com>

RUN apk --no-cache add openssl musl-dev ca-certificates
COPY aws-operator /usr/local/bin/
COPY aws-service-operator /usr/local/bin/

ENTRYPOINT ["/usr/local/bin/aws-operator"]
ENTRYPOINT ["/usr/local/bin/aws-service-operator"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dateStr := $(shell date +%s)

.PHONY: build
build:
go build -ldflags "-X main.commit=$(commitSHA) -X main.date=$(dateStr)" ./cmd/aws-operator
go build -ldflags "-X main.commit=$(commitSHA) -X main.date=$(dateStr)" ./cmd/aws-service-operator

.PHONY: release
release:
Expand Down
103 changes: 103 additions & 0 deletions cmd/aws-service-operator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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, accountID 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")
rootCmd.PersistentFlags().StringVarP(&accountID, "account-id", "a", "", "AWS Account ID, this is used to configure outputs and operate on the proper account.")

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"))
viper.BindPFlag("accountid", rootCmd.PersistentFlags().Lookup("account-id"))
}

// 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,
AccountID: accountID,
}

return config, nil
}
49 changes: 49 additions & 0 deletions cmd/aws-service-operator/server.go
Original file line number Diff line number Diff line change
@@ -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-service-operator/version.go
Original file line number Diff line number Diff line change
@@ -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 c91eab4

Please sign in to comment.