Skip to content

Commit

Permalink
update kubebuilder and operator-sdk version (#212)
Browse files Browse the repository at this point in the history
* update to kubebuilder go/v4 project layout

* update build commands after moving main.go into cmd

* align PROJECT with kubebuilder docs

* upgrade sdk version

* upgrade opm version

* update securityContext to run in restricted mode
  • Loading branch information
maskarb authored Jan 12, 2024
1 parent c930a50 commit 9b0294e
Show file tree
Hide file tree
Showing 111 changed files with 123 additions and 88 deletions.
14 changes: 3 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@ COPY go.sum go.sum
COPY vendor/ vendor/

# Copy the go source
COPY main.go main.go
COPY cmd/ cmd/
COPY api/ api/
COPY clusterversion/ clusterversion/
COPY collector/ collector/
COPY controllers/ controllers/
COPY crhchttp/ crhchttp/
COPY dirconfig/ dirconfig/
COPY packaging/ packaging/
COPY sources/ sources/
COPY storage/ storage/
COPY strset/ strset/
COPY internal/ internal/

# Copy git to inject the commit during build
COPY .git .git
# Build
RUN GIT_COMMIT=$(git rev-list -1 HEAD) && \
echo " injecting GIT COMMIT: $GIT_COMMIT" && \
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} GOFLAGS=-mod=vendor \
go build -ldflags "-w -s -X github.com/project-koku/koku-metrics-operator/controllers.GitCommit=$GIT_COMMIT" -a -o manager main.go
go build -ldflags "-w -s -X github.com/project-koku/koku-metrics-operator/controllers.GitCommit=$GIT_COMMIT" -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ARCH = $(shell go env GOARCH)

# Set the Operator SDK version to use. By default, what is installed on the system is used.
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
OPERATOR_SDK_VERSION ?= v1.32.0
OPERATOR_SDK_VERSION ?= v1.33.0

# CONTAINER_TOOL defines the container tool to be used for building images.
# Be aware that the target commands are only tested with Docker which is
Expand Down Expand Up @@ -157,15 +157,15 @@ test: manifests generate fmt vet envtest ## Run tests.

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

SECRET_ABSPATH ?= ./testing
WATCH_NAMESPACE ?= koku-metrics-operator
# Run against the configured Kubernetes cluster in ~/.kube/config
.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
kubectl apply -f testing/sa.yaml
WATCH_NAMESPACE=$(WATCH_NAMESPACE) SECRET_ABSPATH=$(SECRET_ABSPATH) GIT_COMMIT=$(GIT_COMMIT) go run ./main.go
WATCH_NAMESPACE=$(WATCH_NAMESPACE) SECRET_ABSPATH=$(SECRET_ABSPATH) GIT_COMMIT=$(GIT_COMMIT) go run cmd/main.go

# If you wish to build the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
Expand Down Expand Up @@ -371,7 +371,7 @@ ifeq (,$(shell which opm 2>/dev/null))
set -e ;\
mkdir -p $(dir $(OPM)) ;\
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.34.0/$${OS}-$${ARCH}-opm ;\
chmod +x $(OPM) ;\
}
else
Expand Down
25 changes: 14 additions & 11 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
domain: openshift.io
layout: go.kubebuilder.io/v2
layout:
- go.kubebuilder.io/v4
plugins:
manifests.sdk.operatorframework.io/v2: {}
scorecard.sdk.operatorframework.io/v2: {}
projectName: koku-metrics-operator
repo: github.com/project-koku/koku-metrics-operator
resources:
-
controller: true
domain: openshift.io
group: koku-metrics-cfg
kind: KokuMetricsConfig
path: github.com/project-koku/koku-metrics-operator/api/v1beta1
version: v1beta1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: openshift.io
group: koku-metrics-cfg
kind: KokuMetricsConfig
path: github.com/project-koku/koku-metrics-operator/api/v1beta1
version: v1beta1
version: "3"
plugins:
manifests.sdk.operatorframework.io/v2: {}
scorecard.sdk.operatorframework.io/v2: {}
25 changes: 19 additions & 6 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

metricscfgv1beta1 "github.com/project-koku/koku-metrics-operator/api/v1beta1"
"github.com/project-koku/koku-metrics-operator/controllers"
"github.com/project-koku/koku-metrics-operator/internal/controller"
// +kubebuilder:scaffold:imports
)

Expand All @@ -48,11 +49,12 @@ func init() {
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.Parse()

opts := zap.Options{
Development: true,
Expand All @@ -75,8 +77,10 @@ func main() {
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,

LeaderElection: enableLeaderElection,
LeaderElectionID: "91c624a5.openshift.io",
Cache: cache.Options{DefaultNamespaces: map[string]cache.Config{watchNamespace: {}}},
Expand All @@ -86,13 +90,13 @@ func main() {
os.Exit(1)
}

clientset, err := controllers.GetClientset()
clientset, err := controller.GetClientset()
if err != nil {
setupLog.Error(err, "unable to get clientset")
os.Exit(1)
}

if err = (&controllers.MetricsConfigReconciler{
if err = (&controller.MetricsConfigReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Clientset: clientset,
Expand All @@ -105,6 +109,15 @@ func main() {

// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
21 changes: 21 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,38 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
livenessProbe:
httpGet:
path: /healthz
port: 8081
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
httpGet:
path: /readyz
port: 8081
initialDelaySeconds: 5
periodSeconds: 10
resources:
limits:
cpu: 500m
memory: 500Mi
requests:
cpu: 100m
memory: 20Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
terminationMessagePolicy: FallbackToLogsOnError
volumeMounts:
- mountPath: /tmp/koku-metrics-operator-reports
name: koku-metrics-operator-reports
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
serviceAccountName: controller-manager
terminationGracePeriodSeconds: 10
volumes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ metadata:
operators.operatorframework.io/project_layout: go.kubebuilder.io/v2
repository: https://github.com/project-koku/koku-metrics-operator
support: Cost Management
name: koku-metrics-operator.v0.0.0
namespace: placeholder
labels:
operatorframework.io/arch.amd64: supported
operatorframework.io/arch.arm64: supported
operatorframework.io/arch.ppc64le: supported
operatorframework.io/arch.s390x: supported
operatorframework.io/os.linux: supported
name: koku-metrics-operator.v0.0.0
namespace: placeholder
spec:
apiservicedefinitions: {}
customresourcedefinitions:
Expand Down Expand Up @@ -60,6 +60,7 @@ spec:
- email: costmanagement@redhat.com
name: costmanagement
maturity: alpha
minKubeVersion: 1.24.0
provider:
name: Red Hat
version: 0.0.0
9 changes: 5 additions & 4 deletions config/scorecard/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
resources:
- bases/config.yaml
patchesJson6902:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patches:
- path: patches/basic.config.yaml
target:
group: scorecard.operatorframework.io
version: v1alpha3
kind: Configuration
name: config
version: v1alpha3
- path: patches/olm.config.yaml
target:
group: scorecard.operatorframework.io
version: v1alpha3
kind: Configuration
name: config
#+kubebuilder:scaffold:patchesJson6902
version: v1alpha3
File renamed without changes.
2 changes: 1 addition & 1 deletion collector/collector.go → internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
logr "sigs.k8s.io/controller-runtime/pkg/log"

metricscfgv1beta1 "github.com/project-koku/koku-metrics-operator/api/v1beta1"
"github.com/project-koku/koku-metrics-operator/dirconfig"
"github.com/project-koku/koku-metrics-operator/internal/dirconfig"
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"

metricscfgv1beta1 "github.com/project-koku/koku-metrics-operator/api/v1beta1"
"github.com/project-koku/koku-metrics-operator/dirconfig"
"github.com/project-koku/koku-metrics-operator/strset"
"github.com/project-koku/koku-metrics-operator/testutils"
"github.com/project-koku/koku-metrics-operator/internal/dirconfig"
"github.com/project-koku/koku-metrics-operator/internal/strset"
"github.com/project-koku/koku-metrics-operator/internal/testutils"
)

const epsilon = 0.00001
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/prometheus/common/model"

metricscfgv1beta1 "github.com/project-koku/koku-metrics-operator/api/v1beta1"
"github.com/project-koku/koku-metrics-operator/testutils"
"github.com/project-koku/koku-metrics-operator/internal/testutils"
)

var trueDef = true
Expand Down Expand Up @@ -79,7 +79,7 @@ type mockPrometheusConnectionPolling struct {
func sleepContext(ctx context.Context, delay time.Duration) error {
select {
case <-ctx.Done():
return ctxTimeout
return errCtxTimeout
case <-time.After(delay):
return nil
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestTestPrometheusConnectionPolling(t *testing.T) {
{
name: "test query error",
wait: 15 * time.Millisecond,
queryResult: &mockPromResult{err: ctxTimeout},
queryResult: &mockPromResult{err: errCtxTimeout},
wantedError: errTest,
},
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion collector/report.go → internal/collector/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"path/filepath"
"strings"

"github.com/project-koku/koku-metrics-operator/strset"
"github.com/project-koku/koku-metrics-operator/internal/strset"
)

type dataInterface interface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"strings"
"testing"

"github.com/project-koku/koku-metrics-operator/strset"
"github.com/project-koku/koku-metrics-operator/internal/strset"
)

var (
ctxTimeout = errors.New("context timeout")
errTest = errors.New("test error")
errCtxTimeout = errors.New("context timeout")
errTest = errors.New("test error")
)

type badReader struct{}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//

package controllers
package controller

import (
"context"
Expand Down Expand Up @@ -32,13 +32,13 @@ import (
logr "sigs.k8s.io/controller-runtime/pkg/log"

metricscfgv1beta1 "github.com/project-koku/koku-metrics-operator/api/v1beta1"
cv "github.com/project-koku/koku-metrics-operator/clusterversion"
"github.com/project-koku/koku-metrics-operator/collector"
"github.com/project-koku/koku-metrics-operator/crhchttp"
"github.com/project-koku/koku-metrics-operator/dirconfig"
"github.com/project-koku/koku-metrics-operator/packaging"
"github.com/project-koku/koku-metrics-operator/sources"
"github.com/project-koku/koku-metrics-operator/storage"
cv "github.com/project-koku/koku-metrics-operator/internal/clusterversion"
"github.com/project-koku/koku-metrics-operator/internal/collector"
"github.com/project-koku/koku-metrics-operator/internal/crhchttp"
"github.com/project-koku/koku-metrics-operator/internal/dirconfig"
"github.com/project-koku/koku-metrics-operator/internal/packaging"
"github.com/project-koku/koku-metrics-operator/internal/sources"
"github.com/project-koku/koku-metrics-operator/internal/storage"
)

const HOURS_IN_DAY int = 23 // first hour is 0: 0 -> 23 == 24 hrs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//

package controllers
package controller

import (
"context"
Expand All @@ -28,11 +28,11 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"

metricscfgv1beta1 "github.com/project-koku/koku-metrics-operator/api/v1beta1"
"github.com/project-koku/koku-metrics-operator/collector"
"github.com/project-koku/koku-metrics-operator/dirconfig"
"github.com/project-koku/koku-metrics-operator/mocks"
"github.com/project-koku/koku-metrics-operator/storage"
"github.com/project-koku/koku-metrics-operator/testutils"
"github.com/project-koku/koku-metrics-operator/internal/collector"
"github.com/project-koku/koku-metrics-operator/internal/dirconfig"
"github.com/project-koku/koku-metrics-operator/internal/mocks"
"github.com/project-koku/koku-metrics-operator/internal/storage"
"github.com/project-koku/koku-metrics-operator/internal/testutils"
)

var (
Expand Down
Loading

0 comments on commit 9b0294e

Please sign in to comment.