Skip to content

Commit 33d5369

Browse files
committed
Add configurable multi-arch build support
Signed-off-by: Catherine Chan-Tse <[email protected]>
1 parent 0eed476 commit 33d5369

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

Diff for: docs/tutorial.md

+22-17
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ An operator isn't much good without an API to work with. Create a new Custom
4343
Resource Definition (CRD) API with group `cache`, version `v1`, and Kind
4444
`Memcached`.
4545

46-
Use the `create api` command to scaffold the `MemcachedController`,
46+
Use the `create api` command to scaffold the `MemcachedReconciler`,
4747
`MemcachedSpec`, `MemcachedStatus` and `Memcached`. These files represent the
4848
API. The plugin may show some debug statements which is normal as it is still in
4949
the alpha state.
@@ -67,7 +67,7 @@ $ tree
6767
│ └── com
6868
│ └── example
6969
│ ├── Memcached.java
70-
│ ├── MemcachedController.java
70+
│ ├── MemcachedReconciler.java
7171
│ ├── MemcachedSpec.java
7272
│ └── MemcachedStatus.java
7373
└── resources
@@ -87,7 +87,7 @@ The `java-operator-plugins` project uses the APIs from [java-operator-sdk](https
8787

8888
#### `MemcachedSpec`
8989

90-
Initially, the scaffolded Spec file will be empty. The operator developer needs
90+
Initially, the scaffolded Spec file, `MemcachedSpec`, will be empty. The operator developer needs
9191
to add attributes to this file according to their needs. For the `Memcached`
9292
example, we will add the size field as shown in the example below.
9393

@@ -251,11 +251,11 @@ By now we have the API defined in `Memcached.java`, `MemcachedSpec.java`,
251251
`MemcachedStatus.java`. We also have the CRD and the sample Custom Resource.
252252
This isn't enough, we still need a controller to reconcile these items.
253253

254-
The `create api` command will have scaffolded a skeleton `MemcachedController.java`.
254+
The `create api` command will have scaffolded a skeleton `MemcachedReconciler.java`.
255255
This controller implements the `ResourceController` interface from the
256256
`java-operator-sdk`. This interface has some important and useful methods.
257257

258-
Initially the `MemcachedController.java` will contain the empty stubs for
258+
Initially the `MemcachedReconciler.java` will contain the empty stubs for
259259
`reconcile`. In this section we will fill in
260260
the controller logic in these methods. We will also add a
261261
`createMemcachedDeployment` method that will create the Deployment for our
@@ -268,7 +268,7 @@ changes to the Deployment.
268268
### reconcile
269269

270270
In this section we will focus on implementing the `reconcile`
271-
method. In the `MemcachedController.java` you will see a `// TODO: fill in logic`
271+
method. In the `MemcachedReconciler.java` you will see a `// TODO: fill in logic`
272272
comment. At this line we will first add code to get the Deployment.
273273

274274
```
@@ -303,7 +303,7 @@ else we need to return `UpdateControl.updateStatus(resource)`
303303

304304
After getting the Deployment, we get the current and required replicas. Add the
305305
following lines below the `if (deployment == null)` block in your
306-
`MemcachedController.java` file.
306+
`MemcachedReconciler.java` file.
307307

308308
```
309309
int currentReplicas = deployment.getSpec().getReplicas();
@@ -327,7 +327,7 @@ The above sections will cover reconciling any `size` changes to the Spec. In the
327327
next section, we will look at handling the changes to the `nodes` list from the
328328
Status.
329329

330-
Let's get the list of pods and their names. In the `MemcachedController.java`,
330+
Let's get the list of pods and their names. In the `MemcachedReconciler.java`,
331331
add the following code below the `if (currentReplicas != requiredReplicas) {`
332332
block.
333333

@@ -443,7 +443,7 @@ In the next section, we will walk you through creating the
443443

444444
Creating Kubernetes objects via APIs can be quite verbose which is why putting
445445
them in helper methods can make the code more readable. The
446-
`MemcachedController.java` needs to create a Deployment if it does not exist. In
446+
`MemcachedReconciler.java` needs to create a Deployment if it does not exist. In
447447
the `reconcile` we make a call to a helper,
448448
`createMemcachedDeployment`.
449449

@@ -452,7 +452,7 @@ the [`fabric8`](https://fabric8.io/) `DeploymentBuilder` class. Notice the
452452
Deployment specifies the `memcached` image for the pod.
453453

454454
Below your `labelsForMemcached(Memcached m)` block in the
455-
`MemcachedController.java`, add the following method.
455+
`MemcachedReconciler.java`, add the following method.
456456

457457
```
458458
private Deployment createMemcachedDeployment(Memcached m) {
@@ -496,11 +496,11 @@ Below your `labelsForMemcached(Memcached m)` block in the
496496
Now we have a `reconcile` method. It calls
497497
`createMemcachedDeployment` which we have implemented above.
498498

499-
We have now implemented the `MemcachedController.java`.
499+
We have now implemented the `MemcachedReconciler.java`.
500500

501501
## Include Dependencies
502502

503-
Please add below dependencies in `MemcachedController.java` file.
503+
Please add below dependencies in `MemcachedReconciler.java` file.
504504

505505
```
506506
import io.fabric8.kubernetes.api.model.ContainerBuilder;
@@ -564,15 +564,20 @@ The following steps will show how to run your operator in the cluster.
564564

565565
The `java-operator-plugins` project will scaffold out a Makefile to give
566566
Operator SDK users a familiar interface. Using the `docker-*` targets you can
567-
conveniently build your and push your operator's image to registry. In our
568-
example, we are using `quay.io`, but any docker registry should work.
567+
conveniently build and push your operator's image to a registry.
568+
569+
To build and push the docker image, you will need to specify the name of the image, `IMG`,
570+
that will be built as well as the operating system, `OS`, and the architecture,
571+
`ARCH`, that the image will be built for. In this example, we are using
572+
`quay.io` as the registry the image will be pushed to, but any docker registry should work.
569573

570574
```
571-
make docker-build docker-push IMG=quay.io/YOURUSER/memcached-quarkus-operator:v0.0.1
575+
make docker-build docker-push IMG=quay.io/YOURUSER/memcached-quarkus-operator:v0.0.1 OS=linux ARCH=arm64
572576
```
573577

574-
This will build the docker image
575-
`quay.io/YOURUSER/memcached-quarkus-operator:v0.0.1` and push it to the registry.
578+
This will build the docker image
579+
`quay.io/YOURUSER/memcached-quarkus-operator:v0.0.1` for linux/arm64 and push it
580+
to the `quay.io` registry.
576581

577582
You can verify it is in your docker registry:
578583

Diff for: pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ type Makefile struct {
2929
// Image is controller manager image name
3030
Image string
3131

32+
// OS is the operating system to use for building the image
33+
OS string
34+
35+
// Arch is the architecture to use for building the image
36+
Arch string
37+
3238
// Kustomize version to use in the project
3339
KustomizeVersion string
3440

@@ -50,6 +56,15 @@ func (f *Makefile) SetTemplateDefaults() error {
5056
f.Image = "controller:latest"
5157
}
5258

59+
if f.OS == "" && f.Arch == "" {
60+
// Default OS/Arch to linux/amd64
61+
f.OS = "linux"
62+
f.Arch = "amd64"
63+
} else if f.OS == "" || f.Arch == "" {
64+
// Require both OS and Arch
65+
return errors.New("Both OS and Arch are required to be set if not using default")
66+
}
67+
5368
if f.KustomizeVersion == "" {
5469
return errors.New("kustomize version is required in scaffold")
5570
}
@@ -62,8 +77,11 @@ func (f *Makefile) SetTemplateDefaults() error {
6277
}
6378

6479
const makefileTemplate = `
65-
# Image URL to use all building/pushing image targets
80+
# Image URL to use for all building/pushing image targets
6681
IMG ?= {{ .Image }}
82+
# Operating system and architecture to use for building image
83+
OS ?= {{ .OS }}
84+
ARCH ?= {{ .Arch }}
6785
6886
all: docker-build
6987
@@ -86,7 +104,7 @@ help: ## Display this help.
86104
##@ Build
87105
88106
docker-build: ## Build docker image with the manager.
89-
mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.image=${IMG}
107+
mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.builder=docker -Dquarkus.container-image.image=${IMG} -Dquarkus.docker.buildx.platform=${OS}/${ARCH}
90108
91109
docker-push: ## Push docker image with the manager.
92110
mvn package -Dquarkus.container-image.push=true -Dquarkus.container-image.image=${IMG}

0 commit comments

Comments
 (0)