Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MadeChanges #1

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions cmd/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@
package cmd

import (
"craft/utils"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"os"
pathLib "path"
"path/filepath"
"strings"
"text/template"

"craft/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
resourceFile string
baseOperator string
)

func renderTemplate(operatorPath string) {
resourceDef := fmt.Sprintf("api/%s/%s_types.go",
apiFileObj.Version,
strings.ToLower(apiFileObj.Resource))

utils.MinCmdExec("svn checkout https://github.com/salesforce/craft/trunk/_base-operator", operatorPath)
baseOperator = pathLib.Join(operatorPath, "_base-operator")

dirs := []string{"controllers", "reconciler", "main.go", "Dockerfile", "v1/resource.go", "Makefile"}
for _, dir := range dirs {
path := pathLib.Join(baseOperator, dir)
Expand Down Expand Up @@ -62,6 +65,8 @@ func renderTemplate(operatorPath string) {
log.Fatal(err)
}
}

utils.CmdExec("rm -rf _base-operator", operatorPath)
}

func cpFile(operatorPath string) {
Expand Down Expand Up @@ -140,7 +145,6 @@ func codeBuildCmd() *cobra.Command {
cmd.PersistentFlags().StringVarP(&resourceFile, "resourceFile", "r", "", "resourcefile with properties of resource")
cmd.MarkPersistentFlagRequired("controllerFile")
cmd.MarkPersistentFlagRequired("resourceFile")
// cmd.MarkPersistentFlagRequired("environ")

if err := viper.BindPFlag("controllerFile", cmd.Flags().Lookup("controllerFile")); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (api *ApiFileStruct) loadApi(path string) {
if err != nil {
log.Fatal(err)
}
yaml.Unmarshal([]byte(stream), &api)
_ = yaml.Unmarshal([]byte(stream), &api)

api.LeaderElectionID = randomHex()
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"craft/utils"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -64,27 +65,45 @@ func createCmd() *cobra.Command {
if err != nil {
log.Fatal(err)
}
total := time.Now()
fmt.Println("Creating operator in $GOPATH/src")
start := time.Now()
cmdString := fmt.Sprintf("craft build code -c %s -r %s", apiFile, resourceFile)
utils.CmdExec(cmdString, pwd)
elapsed:= time.Since(start)
fmt.Println("Created operator in $GOPATH/src in", elapsed.Round(time.Second).String())

fmt.Println("Building operator.yaml for deployment")
start = time.Now()
cmdString = fmt.Sprintf("craft build deploy -c %s -r %s", apiFile, resourceFile)
utils.CmdExec(cmdString, pwd)
elapsed = time.Since(start)
fmt.Println("Built operator.yaml for deployment in", elapsed.Round(time.Second).String())

fmt.Println("Building operator and resource docker images")
start = time.Now()
cmdString = fmt.Sprintf("craft build image -b -c %s --podDockerFile %s", apiFile, podDockerFile)
utils.CmdExec(cmdString, pwd)
elapsed = time.Since(start)
fmt.Println("Built operator and resource docker images in", elapsed.Round(time.Second).String())

if dockerPush {
fmt.Println("Pushing operator image to docker")
start = time.Now()
cmdString = fmt.Sprintf("docker push %s", apiFileObj.OperatorImage)
utils.CmdExec(cmdString, pwd)
elapsed = time.Since(start)
fmt.Println("Pushed operator image to docker in", elapsed.Round(time.Second).String())

fmt.Println("Pushing resource image to docker")
start = time.Now()
cmdString = fmt.Sprintf("docker push %s", apiFileObj.Image)
utils.CmdExec(cmdString, pwd)
elapsed = time.Since(start)
fmt.Println("Pushed resource image to docker in", elapsed.Round(time.Second).String())
}
totalTime := time.Since(total)
fmt.Println("Total time required for completion is", totalTime.Round(time.Second).String())
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
package cmd

import (
"fmt"
"log"
log "github.com/sirupsen/logrus"
"os"
"path/filepath"

Expand All @@ -31,8 +30,9 @@ func initCmd() *cobra.Command {
log.Fatal(err)
}

utils.CmdExec(fmt.Sprintf("cp -r %s/ %s", initDir, folder),
craftDir)
cmdString := "svn checkout https://github.com/salesforce/craft/trunk/init"
utils.MinCmdExec(cmdString, folder)
log.Infof("Created sample controller and resource files")
},
}

Expand Down
5 changes: 0 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd

import (
"os"
pathLib "path"
"path/filepath"
"strings"

Expand All @@ -23,8 +22,6 @@ var (
craftInstallPath = "/usr/local/craft"
goSrc = os.ExpandEnv("$GOPATH/src")
craftDir string
initDir string
baseOperator string
debug bool
)

Expand All @@ -34,8 +31,6 @@ func setCraftDir() {
if err != nil {
log.Fatal(err)
}
baseOperator = pathLib.Join(craftDir, "_base-operator")
initDir = pathLib.Join(craftDir, "init")

log.Info("CraftDir: ", craftDir)
}
Expand Down
107 changes: 107 additions & 0 deletions examples/wordpress-operator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
NAMESPACE ?= default
IMAGEPULLSECRETS ?= ''

# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

#namespace =
#secret_name =
#secret_engine =
#docker_registry =

#user = $(shell VAULT_TOKEN=$(VAULT_TOKEN) vault read -address=$(VAULT_ADDR) -field=username $(secret_engine))
#password = $(shell VAULT_TOKEN=$(VAULT_TOKEN) vault read -address=$(VAULT_ADDR) -field=password $(secret_engine))
#server = $(shell VAULT_TOKEN=$(VAULT_TOKEN) vault read -address=$(VAULT_ADDR) -field=server $(secret_engine))

all: manager

# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
go build -o bin/manager main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
go run ./main.go

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests
@ kubectl create secret docker-registry --dry-run=true $(secret_name) --docker-server=$(server) --docker-username=$(user) --docker-password=$(password) --namespace=$(namespace) -o yaml > config/manager/secret.yaml

cd config/manager && kustomize edit set image controller=${IMG} && kustomize edit set namespace ${NAMESPACE}
kustomize build config/default | kubectl apply -f -

# Generate manifests to install an operator
operator: manifests

cd config/manager && kustomize edit set image controller=${IMG}
cd config/default && kustomize edit set namespace ${NAMESPACE}
kustomize build config/default > ${FILE}


# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run go fmt against code
fmt:
go fmt ./...

# Run go vet against code
vet:
go vet ./...

# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."

# Docker login
docker-login:
@ echo $(password) | docker login $(server) -u $(user) --password-stdin

# Build the docker image
docker-build: test
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
# @{ \
# set -e ;\
# CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
# cd $$CONTROLLER_GEN_TMP_DIR ;\
# go mod init tmp ;\
# go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
# rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
# }

CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
6 changes: 3 additions & 3 deletions examples/wordpress-operator/config/controller.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"domain": "example.com",
"namespace": "craft",
"version": "v1",
"operator_image": "craftsf/wordpress-operator:v0.0.1",
"image": "craftsf/wordpress:v0.0.1",
"imagePullSecrets": "",
"operator_image": "bhaddu/wordpress-operator:v0.0.1",
"image": "bhaddu/wordpress:v0.0.1",
"imagePullSecrets": "docker-registry-credentials",
"imagePullPolicy": "IfNotPresent",
"cpu_limit": "500m",
"memory_limit": "200Mi"
Expand Down
16 changes: 5 additions & 11 deletions examples/wordpress-operator/config/deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ spec:
description: WordpressAPI is the Schema for the WordpressAPIs API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
Expand Down Expand Up @@ -116,9 +112,7 @@ spec:
state:
type: string
statusPayload:
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
of cluster Important: Run "make" to regenerate code after modifying
this file'
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state of cluster Important: Run "make" to regenerate code after modifying this file'
type: string
terminated:
description: ContainerStateTerminated is a terminated state of a container.
Expand Down Expand Up @@ -369,7 +363,7 @@ spec:
- --enable-leader-election
command:
- /manager
image: craftsf/wordpress-operator:v0.0.1
image: bhaddu/wordpress-operator:v0.0.1
name: manager
resources:
limits:
Expand All @@ -390,6 +384,6 @@ spec:
name: https
resources: {}
imagePullSecrets:
- {}
- name: docker-registry-credentials
terminationGracePeriodSeconds: 10
status: {}
Loading