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

Implement ORI, broker and poollet for Buckets #623

Merged
merged 6 commits into from
Feb 7, 2023
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
6 changes: 6 additions & 0 deletions .github/workflows/publish-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ jobs:
target: volumebroker
- name: onmetal-api-orictl-volume
target: orictl-volume
- name: onmetal-api-bucketpoollet
target: bucketpoollet
- name: onmetal-api-bucketbroker
target: bucketbroker
- name: onmetal-api-orictl-bucket
target: orictl-bucket
if: ${{ github.event.label.name == 'ok-to-image' }} || ${{ github.event.label.name == 'ok-to-🐳' }} || ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
Expand Down
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -ldflags="-s -w" -a -o bin/orictl-volume ./orictl/cmd/orictl-volume/main.go

FROM builder as bucketpoollet-builder

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -ldflags="-s -w" -a -o bin/bucketpoollet ./poollet/bucketpoollet/cmd/bucketpoollet/main.go


FROM builder as bucketbroker-builder

# TODO: Remove orictl-bucket once debug containers are more broadly available.
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -ldflags="-s -w" -a -o bin/bucketbroker ./broker/bucketbroker/cmd/bucketbroker/main.go && \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -ldflags="-s -w" -a -o bin/orictl-bucket ./orictl/cmd/orictl-bucket/main.go

FROM builder as orictl-bucket-builder

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -ldflags="-s -w" -a -o bin/orictl-bucket ./orictl/cmd/orictl-bucket/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot as manager
Expand Down Expand Up @@ -137,3 +158,25 @@ FROM debian:bullseye-slim as orictl-volume
WORKDIR /
COPY --from=orictl-volume-builder /workspace/bin/orictl-volume .
USER 65532:65532

FROM gcr.io/distroless/static:nonroot as bucketpoollet
WORKDIR /
COPY --from=bucketpoollet-builder /workspace/bin/bucketpoollet .
USER 65532:65532

ENTRYPOINT ["/bucketpoollet"]

# TODO: Switch to distroless as soon as ephemeral debug containers are more broadly available.
FROM debian:bullseye-slim as bucketbroker
WORKDIR /
COPY --from=bucketbroker-builder /workspace/bin/bucketbroker .
# TODO: Remove orictl-bucket as soon as ephemeral debug containers are more broadly available.
COPY --from=bucketbroker-builder /workspace/bin/orictl-bucket .
USER 65532:65532

ENTRYPOINT ["/bucketbroker"]

FROM debian:bullseye-slim as orictl-bucket
WORKDIR /
COPY --from=orictl-bucket-builder /workspace/bin/orictl-bucket .
USER 65532:65532
31 changes: 30 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ ORICTL_MACHINE_IMG ?= orictl-machine:latest
VOLUMEPOOLLET_IMG ?= volumepoollet:latest
VOLUMEBROKER_IMG ?= volumebroker:latest
ORICTL_VOLUME_IMG ?= orictl-volume:latest
BUCKETPOOLLET_IMG ?= bucketpoollet:latest
BUCKETBROKER_IMG ?= bucketbroker:latest
ORICTL_BUCKET_IMG ?= orictl-bucket:latest

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24.1
Expand Down Expand Up @@ -66,6 +69,11 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
$(CONTROLLER_GEN) rbac:roleName=broker-role paths="./broker/volumebroker/..." output:rbac:artifacts:config=config/volumepoollet-broker/broker-rbac
./hack/replace.sh config/volumepoollet-broker/broker-rbac/role.yaml 's/ClusterRole/Role/g'

# bucketpoollet-broker
$(CONTROLLER_GEN) rbac:roleName=manager-role paths="./poollet/bucketpoollet/controllers/..." output:rbac:artifacts:config=config/bucketpoollet-broker/poollet-rbac
$(CONTROLLER_GEN) rbac:roleName=broker-role paths="./broker/bucketbroker/..." output:rbac:artifacts:config=config/bucketpoollet-broker/broker-rbac
./hack/replace.sh config/bucketpoollet-broker/broker-rbac/role.yaml 's/ClusterRole/Role/g'

.PHONY: generate
generate: vgopath models-schema deepcopy-gen client-gen lister-gen informer-gen defaulter-gen conversion-gen openapi-gen applyconfiguration-gen
VGOPATH=$(VGOPATH) \
Expand Down Expand Up @@ -160,7 +168,8 @@ run: manifests generate fmt vet ## Run a controller from your host.
docker-build: \
docker-build-onmetal-apiserver docker-build-onmetal-controller-manager \
docker-build-machinepoollet docker-build-machinebroker docker-build-orictl-machine \
docker-build-volumepoollet docker-build-volumebroker docker-build-orictl-volume ## Build docker image with the manager.
docker-build-volumepoollet docker-build-volumebroker docker-build-orictl-volume |
docker-build-bucketpoollet docker-build-bucketbroker docker-build-orictl-bucket ## Build docker image with the manager.

.PHONY: docker-build-onmetal-apiserver
docker-build-onmetal-apiserver: ## Build onmetal-apiserver.
Expand Down Expand Up @@ -194,6 +203,18 @@ docker-build-volumebroker: ## Build volumebroker image.
docker-build-orictl-volume: ## Build orictl-volume image.
docker build --target orictl-volume -t ${ORICTL_VOLUME_IMG} .

.PHONY: docker-build-bucketpoollet
docker-build-bucketpoollet: ## Build bucketpoollet image.
docker build --target bucketpoollet -t ${BUCKETPOOLLET_IMG} .

.PHONY: docker-build-bucketbroker
docker-build-bucketbroker: ## Build bucketbroker image.
docker build --target bucketbroker -t ${BUCKETBROKER_IMG} .

.PHONY: docker-build-orictl-bucket
docker-build-orictl-bucket: ## Build orictl-bucket image.
docker build --target orictl-bucket -t ${ORICTL_BUCKET_IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${CONTROLLER_IMG}
Expand Down Expand Up @@ -256,6 +277,14 @@ kind-load-volumepoollet:
kind-load-volumebroker:
kind load docker-image ${VOLUMEBROKER_IMG}

.PHONY: kind-load-bucketpoollet
kind-load-bucketpoollet:
kind load docker-image ${BUCKETPOOLLET_IMG}

.PHONY: kind-load-bucketbroker
kind-load-bucketbroker:
kind load docker-image ${BUCKETBROKER_IMG}

.PHONY: kind-load
kind-load: kind-load-apiserver kind-load-controller ## Load the apiserver and controller in kind.

Expand Down
31 changes: 31 additions & 0 deletions broker/bucketbroker/api/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 OnMetal 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 v1alpha1

const (
LabelsAnnotation = "bucketbroker.api.onmetal.de/labels"

AnnotationsAnnotation = "bucketbroker.api.onmetal.de/annotations"

CreatedLabel = "bucketbroker.api.onmetal.de/created"
)

const (
ManagerLabel = "bucketbroker.api.onmetal.de/manager"
)

const (
BucketBrokerManager = "bucketbroker"
)
149 changes: 149 additions & 0 deletions broker/bucketbroker/apiutils/apiutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2022 OnMetal 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 apiutils

import (
"context"
"encoding/json"
"fmt"

"github.com/onmetal/controller-utils/metautils"
storagev1alpha1 "github.com/onmetal/onmetal-api/api/storage/v1alpha1"
bucketbrokerv1alpha1 "github.com/onmetal/onmetal-api/broker/bucketbroker/api/v1alpha1"
orimeta "github.com/onmetal/onmetal-api/ori/apis/meta/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func GetObjectMetadata(o metav1.Object) (*orimeta.ObjectMetadata, error) {
annotations, err := GetAnnotationsAnnotation(o)
if err != nil {
return nil, err
}

labels, err := GetLabelsAnnotation(o)
if err != nil {
return nil, err
}

var deletedAt int64
if !o.GetDeletionTimestamp().IsZero() {
deletedAt = o.GetDeletionTimestamp().UnixNano()
}

return &orimeta.ObjectMetadata{
Id: o.GetName(),
Annotations: annotations,
Labels: labels,
Generation: o.GetGeneration(),
CreatedAt: o.GetCreationTimestamp().UnixNano(),
DeletedAt: deletedAt,
}, nil
}

func SetObjectMetadata(o metav1.Object, metadata *orimeta.ObjectMetadata) error {
if err := SetAnnotationsAnnotation(o, metadata.Annotations); err != nil {
return err
}
if err := SetLabelsAnnotation(o, metadata.Labels); err != nil {
return err
}
return nil
}

func SetCreatedLabel(o metav1.Object) {
metautils.SetLabel(o, bucketbrokerv1alpha1.CreatedLabel, "true")
}

func IsCreated(o metav1.Object) bool {
return metautils.HasLabel(o, bucketbrokerv1alpha1.CreatedLabel)
}

func PatchControlledBy(ctx context.Context, c client.Client, owner, controlled client.Object) error {
base := controlled.DeepCopyObject().(client.Object)
if err := ctrl.SetControllerReference(owner, controlled, c.Scheme()); err != nil {
return err
}

if err := c.Patch(ctx, controlled, client.MergeFrom(base)); err != nil {
return fmt.Errorf("error patching object to be controlled: %w", err)
}
return nil
}

func PatchCreated(ctx context.Context, c client.Client, o client.Object) error {
base := o.DeepCopyObject().(client.Object)
SetCreatedLabel(o)
if err := c.Patch(ctx, o, client.MergeFrom(base)); err != nil {
return fmt.Errorf("error patching object to created: %w", err)
}
return nil
}

func SetLabelsAnnotation(o metav1.Object, labels map[string]string) error {
data, err := json.Marshal(labels)
if err != nil {
return fmt.Errorf("error marshalling labels: %w", err)
}
metautils.SetAnnotation(o, bucketbrokerv1alpha1.LabelsAnnotation, string(data))
return nil
}

func GetLabelsAnnotation(o metav1.Object) (map[string]string, error) {
data, ok := o.GetAnnotations()[bucketbrokerv1alpha1.LabelsAnnotation]
if !ok {
return nil, fmt.Errorf("object has no labels at %s", bucketbrokerv1alpha1.LabelsAnnotation)
}

var labels map[string]string
if err := json.Unmarshal([]byte(data), &labels); err != nil {
return nil, err
}

return labels, nil
}

func SetAnnotationsAnnotation(o metav1.Object, annotations map[string]string) error {
data, err := json.Marshal(annotations)
if err != nil {
return fmt.Errorf("error marshalling annotations: %w", err)
}
metautils.SetAnnotation(o, bucketbrokerv1alpha1.AnnotationsAnnotation, string(data))
return nil
}

func GetAnnotationsAnnotation(o metav1.Object) (map[string]string, error) {
data, ok := o.GetAnnotations()[bucketbrokerv1alpha1.AnnotationsAnnotation]
if !ok {
return nil, fmt.Errorf("object has no annotations at %s", bucketbrokerv1alpha1.AnnotationsAnnotation)
}

var annotations map[string]string
if err := json.Unmarshal([]byte(data), &annotations); err != nil {
return nil, err
}

return annotations, nil
}

func SetBucketManagerLabel(bucket *storagev1alpha1.Bucket, manager string) {
metautils.SetLabel(bucket, bucketbrokerv1alpha1.ManagerLabel, manager)
}

func IsManagedBy(o metav1.Object, manager string) bool {
actual, ok := o.GetLabels()[bucketbrokerv1alpha1.ManagerLabel]
return ok && actual == manager
}
Loading