Skip to content

Commit

Permalink
Add initial CLI of controller-bootstrap with Makefile (#5)
Browse files Browse the repository at this point in the history
Issue: [aws-controllers-k8s/community/issues/1358](aws-controllers-k8s/community#1358)

Description of changes:

- This PR is to add the initial implementation of controller-bootstrap CLI with subcommand, arguments, and Makefile.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
JamesJHPark authored Jun 22, 2022
1 parent 1bdef64 commit 357b00d
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 7 deletions.
55 changes: 48 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ SHELL := /bin/bash # Use bash syntax
# Set up variables
GO111MODULE=on

AWS_SERVICE=$(shell echo $(SERVICE))
SERVICE_MODEL_NAME=$(shell echo $(MODEL_NAME))
ifeq ($(SERVICE_MODEL_NAME),)
SERVICE_MODEL_NAME=""
endif

ROOT_DIR=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
CONTROLLER_DIR=${ROOT_DIR}/../${AWS_SERVICE}-controller
CONTROLLER_BOOTSTRAP=./bin/controller-bootstrap
CODE_GEN_DIR=${ROOT_DIR}/../code-generator

AWS_SDK_GO_VERSION=$(shell curl -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/aws/aws-sdk-go/releases/latest | jq -r '.tag_name')
ACK_RUNTIME_VERSION=$(shell curl -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/aws-controllers-k8s/runtime/releases/latest | jq -r '.tag_name')

.DEFAULT_GOAL=run
DRY_RUN="false"
EXISTING_CONTROLLER="false"

# Build ldflags
VERSION ?= "v0.0.0"
GITCOMMIT=$(shell git rev-parse HEAD)
Expand All @@ -11,13 +31,34 @@ GO_LDFLAGS=-ldflags "-X main.version=$(VERSION) \
-X main.buildHash=$(GITCOMMIT) \
-X main.buildDate=$(BUILDDATE)"

.PHONY: all test
# We need to use the codegen tag when building and testing because the
# aws-sdk-go/private/model/api package is gated behind a build tag "codegen"...
GO_CMD_FLAGS=-tags codegen

.PHONY: build, generate, init, run, clean

build:
@go build ${GO_CMD_FLAGS} -o ${CONTROLLER_BOOTSTRAP} ./cmd/controller-bootstrap/main.go

generate: build
@${CONTROLLER_BOOTSTRAP} generate -s ${AWS_SERVICE} -r ${ACK_RUNTIME_VERSION} -v ${AWS_SDK_GO_VERSION} -d=${DRY_RUN} -e=${EXISTING_CONTROLLER} -o ${ROOT_DIR}/../${AWS_SERVICE}-controller -m ${SERVICE_MODEL_NAME}

all: test
init: generate
@export SERVICE=${AWS_SERVICE}
@cd ${CODE_GEN_DIR} && make -i build-controller >/dev/null 2>/dev/null
@cd ${CONTROLLER_DIR} && go mod tidy
@cd ${CODE_GEN_DIR} && make -i build-controller >/dev/null 2>/dev/null
@cd ${CONTROLLER_DIR} && go mod tidy
@cd ${CODE_GEN_DIR} && make build-controller
@echo "${AWS_SERVICE}-controller generated successfully, look inside ${AWS_SERVICE}-controller/INSTRUCTIONS.md for further instructions"

test: ## Run code tests
go test -v ./...
run:
@if [ -f ${CONTROLLER_DIR}/cmd/controller/main.go ]; then \
EXISTING_CONTROLLER="true"; \
make generate; \
else \
make init; \
fi

help: ## Show this help.
@grep -F -h "##" $(MAKEFILE_LIST) | grep -F -v grep | sed -e 's/\\$$//' \
| awk -F'[:#]' '{print $$1 = sprintf("%-30s", $$1), $$4}'
clean:
@rm -rf ${CONTROLLER_DIR}/..?* ${CONTROLLER_DIR}/.[!.]* ${CONTROLLER_DIR}/*
19 changes: 19 additions & 0 deletions cmd/controller-bootstrap/command/apiutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package command

// TODO: getServiceResources infers aws-sdk-go to fetch the service metadata and custom resource names
func getServiceResources() error {
return nil
}
32 changes: 32 additions & 0 deletions cmd/controller-bootstrap/command/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package command

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

var templateCmd = &cobra.Command{
Use: "generate",
Short: "generate template files in an ACK service controller repository",
RunE: generateController,
}

// TODO: generateController creates the initial directories and files for a service controller
// repository by rendering go template files.
// When a controller is already existing, then this method only updates the project
// description files.
func generateController(cmd *cobra.Command, args []string) error {
return nil
}
82 changes: 82 additions & 0 deletions cmd/controller-bootstrap/command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package command

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

const (
appName = "controller-bootstrap"
appShortDesc = "A bootstrap tool to initialize an ACK service controller repository"
)

var (
optServiceAlias string
optRuntimeVersion string
optAWSSDKGoVersion string
optDryRun bool
optExistingController bool
optOutputPath string
optModelName string
)

// rootCmd represents the base command when called without any subcommands
// placeholder for cobra description
var rootCmd = &cobra.Command{
Use: appName,
Short: appShortDesc,
}

func init() {
rootCmd.PersistentFlags().StringVarP(
&optServiceAlias, "aws-service-alias", "s", "", "AWS service alias",
)
rootCmd.PersistentFlags().StringVarP(
&optRuntimeVersion, "ack-runtime-version", "r", "", "Version of aws-controllers-k8s/runtime",
)
rootCmd.PersistentFlags().StringVarP(
&optAWSSDKGoVersion, "aws-sdk-go-version", "v", "", "Version of github.com/aws/aws-sdk-go used to infer service metadata and resources",
)
rootCmd.PersistentFlags().BoolVarP(
&optDryRun, "dry-run", "d", false, "Optional: if true, output files to stdout",
)
rootCmd.PersistentFlags().BoolVarP(
&optExistingController, "existing-controller", "e", false, "Optional: if true, update the existing service controller",
)
rootCmd.PersistentFlags().StringVarP(
&optOutputPath, "output", "o", "", "Path to ACK service controller directory to bootstrap",
)
rootCmd.PersistentFlags().StringVarP(
&optModelName, "model-name", "m", "", "Optional: service model name of the corresponding service alias",
)
rootCmd.MarkPersistentFlagRequired("aws-service-alias")
rootCmd.MarkPersistentFlagRequired("ack-runtime-version")
rootCmd.MarkPersistentFlagRequired("aws-sdk-go-version")
rootCmd.MarkPersistentFlagRequired("output")
rootCmd.AddCommand(templateCmd)
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
20 changes: 20 additions & 0 deletions cmd/controller-bootstrap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package main

import "controller-bootstrap/cmd/controller-bootstrap/command"

func main() {
command.Execute()
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module controller-bootstrap

go 1.18

require github.com/spf13/cobra v1.5.0

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 comments on commit 357b00d

Please sign in to comment.