-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
makefile.go
140 lines (110 loc) · 3.71 KB
/
makefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright 2019 The Kubernetes Authors.
Modifications copyright 2020 The Operator-SDK Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 templates
import (
"errors"
"sigs.k8s.io/kubebuilder/v2/pkg/model/file"
)
var _ file.Template = &Makefile{}
// Makefile scaffolds the Makefile
type Makefile struct {
file.TemplateMixin
// Image is controller manager image name
Image string
// Kustomize version to use in the project
KustomizeVersion string
// HelmOperatorVersion is the version of the helm-operator binary downloaded by the Makefile.
HelmOperatorVersion string
}
// SetTemplateDefaults implements input.Template
func (f *Makefile) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = "Makefile"
}
f.TemplateBody = makefileTemplate
f.IfExistsAction = file.Error
if f.Image == "" {
f.Image = "controller:latest"
}
if f.KustomizeVersion == "" {
return errors.New("kustomize version is required in scaffold")
}
if f.HelmOperatorVersion == "" {
return errors.New("helm-operator version is required in scaffold")
}
return nil
}
const makefileTemplate = `
# Image URL to use all building/pushing image targets
IMG ?= {{ .Image }}
all: docker-build
# Run against the configured Kubernetes cluster in ~/.kube/config
run: helm-operator
$(HELM_OPERATOR) run
# Install CRDs into a cluster
install: kustomize
$(KUSTOMIZE) build config/crd | kubectl apply -f -
# Uninstall CRDs from a cluster
uninstall: kustomize
$(KUSTOMIZE) build config/crd | kubectl delete -f -
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: kustomize
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
# Undeploy controller in the configured Kubernetes cluster in ~/.kube/config
undeploy: kustomize
$(KUSTOMIZE) build config/default | kubectl delete -f -
# Build the docker image
docker-build:
docker build . -t ${IMG}
# Push the docker image
docker-push:
docker push ${IMG}
PATH := $(PATH):$(PWD)/bin
SHELL := env PATH=$(PATH) /bin/sh
OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH := $(shell uname -m | sed 's/x86_64/amd64/')
# Download kustomize locally if necessary, preferring the $(pwd)/bin path over global if both exist.
.PHONY: kustomize
KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize:
ifeq (,$(wildcard $(KUSTOMIZE)))
ifeq (,$(shell which kustomize 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(KUSTOMIZE)) ;\
curl -sSLo - https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/{{ .KustomizeVersion }}/kustomize_{{ .KustomizeVersion }}_$(OS)_$(ARCH).tar.gz | \
tar xzf - -C bin/ ;\
}
else
KUSTOMIZE = $(shell which kustomize)
endif
endif
# Download helm-operator locally if necessary, preferring the $(pwd)/bin path over global if both exist.
.PHONY: helm-operator
HELM_OPERATOR = $(shell pwd)/bin/helm-operator
helm-operator:
ifeq (,$(wildcard $(HELM_OPERATOR)))
ifeq (,$(shell which helm-operator 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(HELM_OPERATOR)) ;\
curl -sSLo $(HELM_OPERATOR) https://github.com/operator-framework/operator-sdk/releases/download/{{ .HelmOperatorVersion }}/helm-operator_$(OS)_$(ARCH) ;\
chmod +x $(HELM_OPERATOR) ;\
}
else
HELM_OPERATOR = $(shell which helm-operator)
endif
endif
`