Skip to content

Commit f5e366c

Browse files
authored
Downgrade kubebuilder, add Path CRD (#33)
1 parent 50cd0c0 commit f5e366c

39 files changed

+793
-215
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ COPY go.sum go.sum
1212
RUN go mod download
1313

1414
# Copy the go source
15-
COPY cmd/main.go cmd/main.go
15+
COPY main.go main.go
1616
COPY api/ api/
17-
COPY internal/controller/ internal/controller/
17+
COPY controllers/ controllers/
1818

1919
# Build
2020
# the GOARCH has not a default value to allow the binary be built according to the host where the command
2121
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2222
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2323
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
2525

2626
# Use distroless as minimal base image to package the manager binary
2727
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ ifeq ($(USE_IMAGE_DIGESTS), true)
4646
BUNDLE_GEN_FLAGS += --use-image-digests
4747
endif
4848

49+
# Set the Operator SDK version to use. By default, what is installed on the system is used.
50+
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
51+
OPERATOR_SDK_VERSION ?= v1.32.0
52+
4953
# Image URL to use all building/pushing image targets
5054
IMG ?= controller:latest
5155
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
@@ -58,12 +62,6 @@ else
5862
GOBIN=$(shell go env GOBIN)
5963
endif
6064

61-
# CONTAINER_TOOL defines the container tool to be used for building images.
62-
# Be aware that the target commands are only tested with Docker which is
63-
# scaffolded by default. However, you might want to replace it to use other
64-
# tools. (i.e. podman)
65-
CONTAINER_TOOL ?= docker
66-
6765
# Setting SHELL to bash allows bash commands to be executed by recipes.
6866
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
6967
SHELL = /usr/bin/env bash -o pipefail
@@ -115,38 +113,38 @@ test: manifests generate fmt vet envtest ## Run tests.
115113

116114
.PHONY: build
117115
build: manifests generate fmt vet ## Build manager binary.
118-
go build -o bin/manager cmd/main.go
116+
go build -o bin/manager main.go
119117

120118
.PHONY: run
121119
run: manifests generate fmt vet ## Run a controller from your host.
122-
go run ./cmd/main.go
120+
go run ./main.go
123121

124122
# If you wish built the manager image targeting other platforms you can use the --platform flag.
125123
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
126124
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
127125
.PHONY: docker-build
128126
docker-build: test ## Build docker image with the manager.
129-
$(CONTAINER_TOOL) build -t ${IMG} .
127+
docker build -t ${IMG} .
130128

131129
.PHONY: docker-push
132130
docker-push: ## Push docker image with the manager.
133-
$(CONTAINER_TOOL) push ${IMG}
131+
docker push ${IMG}
134132

135133
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
136134
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
137135
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
138136
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
139-
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> than the export will fail)
137+
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
140138
# To properly provided solutions that supports more than one platform you should use this option.
141139
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
142140
.PHONY: docker-buildx
143141
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
144142
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
145143
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
146-
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
147-
$(CONTAINER_TOOL) buildx use project-v3-builder
148-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
149-
- $(CONTAINER_TOOL) buildx rm project-v3-builder
144+
- docker buildx create --name project-v3-builder
145+
docker buildx use project-v3-builder
146+
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
147+
- docker buildx rm project-v3-builder
150148
rm Dockerfile.cross
151149

152150
##@ Deployment
@@ -157,20 +155,20 @@ endif
157155

158156
.PHONY: install
159157
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
160-
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
158+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
161159

162160
.PHONY: uninstall
163161
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
164-
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
162+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
165163

166164
.PHONY: deploy
167165
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
168166
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
169-
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
167+
$(KUSTOMIZE) build config/default | kubectl apply -f -
170168

171169
.PHONY: undeploy
172170
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
173-
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
171+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
174172

175173
##@ Build Dependencies
176174

@@ -180,37 +178,58 @@ $(LOCALBIN):
180178
mkdir -p $(LOCALBIN)
181179

182180
## Tool Binaries
183-
KUBECTL ?= kubectl
184181
KUSTOMIZE ?= $(LOCALBIN)/kustomize
185182
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
186183
ENVTEST ?= $(LOCALBIN)/setup-envtest
187184

188185
## Tool Versions
189-
KUSTOMIZE_VERSION ?= v5.0.1
190-
CONTROLLER_TOOLS_VERSION ?= v0.12.0
186+
KUSTOMIZE_VERSION ?= v3.8.7
187+
CONTROLLER_TOOLS_VERSION ?= v0.11.1
191188

192-
#KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
189+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
193190
.PHONY: kustomize
194-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
191+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
195192
$(KUSTOMIZE): $(LOCALBIN)
196-
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
193+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
194+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
195+
rm -rf $(LOCALBIN)/kustomize; \
196+
fi
197+
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
197198

198199
.PHONY: controller-gen
199-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
200+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
200201
$(CONTROLLER_GEN): $(LOCALBIN)
201-
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
202+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
203+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
202204

203205
.PHONY: envtest
204206
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
205207
$(ENVTEST): $(LOCALBIN)
206208
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
207209

210+
.PHONY: operator-sdk
211+
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
212+
operator-sdk: ## Download operator-sdk locally if necessary.
213+
ifeq (,$(wildcard $(OPERATOR_SDK)))
214+
ifeq (, $(shell which operator-sdk 2>/dev/null))
215+
@{ \
216+
set -e ;\
217+
mkdir -p $(dir $(OPERATOR_SDK)) ;\
218+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
219+
curl -sSLo $(OPERATOR_SDK) https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_$${OS}_$${ARCH} ;\
220+
chmod +x $(OPERATOR_SDK) ;\
221+
}
222+
else
223+
OPERATOR_SDK = $(shell which operator-sdk)
224+
endif
225+
endif
226+
208227
.PHONY: bundle
209-
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
210-
operator-sdk generate kustomize manifests -q
228+
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
229+
$(OPERATOR_SDK) generate kustomize manifests -q
211230
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
212-
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle $(BUNDLE_GEN_FLAGS)
213-
operator-sdk bundle validate ./bundle
231+
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
232+
$(OPERATOR_SDK) bundle validate ./bundle
214233

215234
.PHONY: bundle-build
216235
bundle-build: ## Build the bundle image.

PROJECT

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
15
domain: onyxia.sh
26
layout:
3-
- go.kubebuilder.io/v4
7+
- go.kubebuilder.io/v3
48
plugins:
59
manifests.sdk.operatorframework.io/v2: {}
610
scorecard.sdk.operatorframework.io/v2: {}
711
projectName: s3-operator
8-
repo: github.com/inseefrlab/s3-operator
12+
repo: github.com/phlg/s3-operator-downgrade
913
resources:
1014
- api:
1115
crdVersion: v1
1216
namespaced: true
1317
controller: true
1418
domain: onyxia.sh
15-
group: s3
19+
group: s3.onyxia.sh
1620
kind: Bucket
17-
path: github.com/inseefrlab/s3-operator/api/v1alpha1
21+
path: github.com/phlg/s3-operator-downgrade/api/v1alpha1
1822
version: v1alpha1
1923
- api:
2024
crdVersion: v1
2125
namespaced: true
2226
controller: true
2327
domain: onyxia.sh
24-
group: s3
28+
group: s3.onyxia.sh
2529
kind: Policy
26-
path: github.com/inseefrlab/s3-operator/api/v1alpha1
30+
path: github.com/phlg/s3-operator-downgrade/api/v1alpha1
31+
version: v1alpha1
32+
- api:
33+
crdVersion: v1
34+
namespaced: true
35+
controller: true
36+
domain: onyxia.sh
37+
group: s3.onyxia.sh
38+
kind: Path
39+
path: github.com/phlg/s3-operator-downgrade/api/v1alpha1
2740
version: v1alpha1
2841
version: "3"

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,28 @@ This Operator SDK based tool aims at managing S3 related resources (buckets, pol
1111

1212
This operator has been successfully tested with :
1313

14-
- Kubernetes : 1.25, 1.26
15-
- MinIO : 2023-05-27T05:56:19Z
14+
- Kubernetes : 1.25, 1.26, 1.27
15+
- MinIO : 2023-05-27T05:56:19Z (up to v0.3.0 included), 2023-11-20T22-40-07Z (from v0.4.0 onwards)
1616

1717
## Description
1818

1919
At its heart, the operator revolves around CRDs that match S3 resources :
2020

2121
- `buckets.s3.onyxia.sh`
2222
- `policies.s3.onyxia.sh`
23+
- `paths.s3.onyxia.sh`
2324

2425
The custom resources based on these CRDs are a somewhat simplified projection of the real S3 resources. From the operator's point of view :
2526

2627
- A `Bucket` CR matches a S3 bucket, and only has a name, a quota (actually two, [see Bucket example in *Usage* section below](#bucket)), and optionally, a set of paths
2728
- A `Policy` CR matches a "canned" policy (not a bucket policy, but a global one, that can be attached to a user), and has a name, and its actual content (IAM JSON)
29+
- A `Path` CR matches a set of paths inside of a policy. This is akin to the `paths` property of the `Bucket` CRD, except `Path` is not responsible for Bucket creation.
2830

2931
Each custom resource based on these CRDs on Kubernetes is to be matched with a resource on the S3 instance. If the CR and the corresponding S3 resource diverge, the operator will create or update the S3 resource to bring it back to .
3032

3133
Two important caveats :
3234

33-
- It is one-way - if something happens on the S3 side directly (instead of going through the CRs), the operator ha s no way of reacting. At best, the next trigger will overwrite the S3 state with the declared state in the k8s custom resource.
35+
- It is one-way - if something happens on the S3 side directly (instead of going through the CRs), the operator has no way of reacting. At best, the next trigger will overwrite the S3 state with the declared state in the k8s custom resource.
3436
- For now, the operator won't delete any resource on S3 - if a CR is removed, its matching resource on S3 will still be present. This behavior was primarily picked to avoid data loss for bucket, but also applied to policies.
3537

3638
## Installation
@@ -48,15 +50,15 @@ helm install <name> s3-operator --values <yaml-file/url> # see below for the pa
4850

4951
### Running from source
5052

51-
Alternatively, if you just wish to try out the operator without actually, it is also possible to just clone this repository, and run the operator locally - outside of the Kubernetes cluster. This requires Go 1.19+ :
53+
Alternatively, if you just wish to try out the operator without actually installing it, it is also possible to just clone this repository, and run the operator locally - outside of the Kubernetes cluster. This requires Go 1.19+, and prior installation of the CRDs located in `config/crd/bases`, typically with `kubectl`. After which, you can simply run :
5254

5355
```shell
5456
git clone https://github.com/InseeFrLab/s3-operator.git # or use a tag/release
5557
cd s3-operator
5658
go run main.go --s3-endpoint-url *** --s3-access-key *** --s3-secret-key *** # see below for the parameters
5759
```
5860

59-
To quote the Operator SDK README (also visible below), running the operator this way *will automatically use the current context in your kubeconfig file (i.e. whatever cluster `kubectl cluster-info` shows).* RBAC-wise, you need to be able to freely manipulate the custom resources associated to the operator (`Bucket` and `Policy`) in every namespace - [see also the generated ClusterRole manifest](https://github.com/InseeFrLab/s3-operator/blob/main/config/rbac/role.yaml).
61+
To quote the Operator SDK README (also visible below), running the operator this way *will automatically use the current context in your kubeconfig file (i.e. whatever cluster `kubectl cluster-info` shows).* RBAC-wise, you need to be able to freely manipulate the custom resources associated to the operator (`Bucket`, `Policy` and `Path`) in every namespace - [see also the generated ClusterRole manifest](https://github.com/InseeFrLab/s3-operator/blob/main/config/rbac/role.yaml).
6062

6163
### Kustomize
6264

@@ -165,7 +167,30 @@ spec:
165167
}
166168
]
167169
}
170+
```
171+
172+
### Path example
173+
174+
```yaml
175+
apiVersion: s3.onyxia.sh/v1alpha1
176+
kind: Path
177+
metadata:
178+
labels:
179+
app.kubernetes.io/name: path
180+
app.kubernetes.io/instance: path-sample
181+
app.kubernetes.io/part-of: s3-operator
182+
app.kubernetes.io/managed-by: kustomize
183+
app.kubernetes.io/created-by: s3-operator
184+
name: path-sample
185+
spec:
186+
# Bucket name (on S3 server, not a Bucket CR's metadata.name)
187+
bucketName: shared-bucket
168188

189+
# Paths to create on the bucket
190+
paths:
191+
- /home/alice
192+
- /home/bob
193+
169194

170195
```
171196

api/v1alpha1/bucket_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type BucketSpec struct {
4343

4444
// BucketStatus defines the observed state of Bucket
4545
type BucketStatus struct {
46-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
47-
// Important: Run "make" to regenerate code after modifying this file
46+
// Status management using Conditions.
47+
// See also : https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
4848
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
4949
}
5050

api/v1alpha1/groupversion_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package v1alpha1 contains API Schema definitions for the s3 v1alpha1 API group
17+
// Package v1alpha1 contains API Schema definitions for the s3.onyxia.sh v1alpha1 API group
1818
// +kubebuilder:object:generate=true
1919
// +groupName=s3.onyxia.sh
2020
package v1alpha1

0 commit comments

Comments
 (0)