Skip to content

Commit

Permalink
feat: add catalog and update bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
lwpk110 committed May 14, 2024
1 parent af109a5 commit eebbdf3
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore build and test binaries.
bin/

bundle.Dockerfile
bundle.Dockerfile*
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ Dockerfile.cross
*~

bundle.Dockerfile
bundle
bundle

**/__debug_*

bundle*

kind-kubeconfig*

catalog.Dockerfile
catalog
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN go mod download
# Copy the go source
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/controller/ internal/controller/
COPY internal/ internal/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
56 changes: 47 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,28 @@ bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metada

.PHONY: bundle-build
bundle-build: ## Build the bundle image.
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) --no-cache .

.PHONY: bundle-push
bundle-push: ## Push the bundle image.
$(MAKE) docker-push IMG=$(BUNDLE_IMG)


.PHONY: bundle-buildx
bundle-buildx: ## Build the bundle image.
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
$(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag $(BUNDLE_IMG) -f bundle.Dockerfile.cross .
$(CONTAINER_TOOL) buildx rm project-v3-builder

.PHONY: bundle-run
bundle-run: ## Run the bundle image.
$(OPERATOR_SDK) run bundle $(BUNDLE_IMG)

.PHONY: bundle-cleanup
bundle-cleanup: ## Clean up the bundle image.
$(OPERATOR_SDK) cleanup $(PROJECT_NAME)

.PHONY: opm
OPM = ./bin/opm
opm: ## Download opm locally if necessary.
Expand All @@ -264,7 +280,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.39.0/$${OS}-$${ARCH}-opm ;\
chmod +x $(OPM) ;\
}
else
Expand All @@ -276,9 +292,6 @@ endif
# These images MUST exist in a registry and be pull-able.
BUNDLE_IMGS ?= $(BUNDLE_IMG)

# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)

# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
ifneq ($(origin CATALOG_BASE_IMG), undefined)
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
Expand All @@ -287,11 +300,36 @@ endif
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:latest

.PHONY: catalog-build
catalog-build: opm ## Build a catalog image.
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
catalog-build: opm ## Build a catalog manifests.
mkdir -p catalog
@if ! test -f ./catalog.Dockerfile; then \
$(OPM) generate dockerfile catalog; \
fi
$(OPM) alpha render-template basic -o yaml catalog-template.yaml > catalog/catalog.yaml

.PHONY: catalog-validate
catalog-validate: opm ## Validate a catalog manifests.
$(OPM) validate catalog

#.PHONY: catalog-render
#catalog-render: opm ## render a bundle to catalog.
# $(OPM) render $(BUNDLE_IMGS) --output=yaml >> catalog/catalog.yaml

.PHONY: catalog-docker-build
catalog-docker-build: ## Build a catalog image.
$(CONTAINER_TOOL) build -t ${CATALOG_IMG} -f catalog.Dockerfile .

# Push the catalog image.
.PHONY: catalog-push
catalog-push: ## Push a catalog image.
.PHONY: catalog-docker-push
catalog-docker-push: ## Push a catalog image.
$(MAKE) docker-push IMG=$(CATALOG_IMG)

.PHONY: catalog-docker-buildx
catalog-docker-buildx: ## Build and push a catalog image for cross-platform support
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
$(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) -f catalog.Dockerfile --tag ${CATALOG_IMG} .
$(CONTAINER_TOOL) buildx rm project-v3-builder
111 changes: 111 additions & 0 deletions catalog-template.yaml

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package main

import (
"flag"
"fmt"
"os"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"strings"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -68,6 +71,23 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

watchNamespaces, err := getWatchNamespaces()
if err != nil {
setupLog.Error(err, "unable to get WatchNamespace, "+
"the manager will watch and manage resources in all namespaces")
}

cachedNamespaces := make(map[string]cache.Config)

if len(watchNamespaces) > 0 {
setupLog.Info("watchNamespaces", "namespaces", watchNamespaces)
cachedNamespaces = make(map[string]cache.Config)
for _, ns := range watchNamespaces {
cachedNamespaces[ns] = cache.Config{}
}
} else {
setupLog.Info("watchNamespaces", "namespaces", "all")
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: server.Options{BindAddress: metricsAddr},
Expand All @@ -85,6 +105,7 @@ func main() {
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
Cache: cache.Options{DefaultNamespaces: cachedNamespaces},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -116,3 +137,30 @@ func main() {
os.Exit(1)
}
}

// getWatchNamespaces returns the Namespaces the operator should be watching for changes
func getWatchNamespaces() ([]string, error) {
// WatchNamespacesEnvVar is the constant for env variable WATCH_NAMESPACES
// which specifies the Namespaces to watch.
// An empty value means the operator is running with cluster scope.
var watchNamespacesEnvVar = "WATCH_NAMESPACES"

ns, found := os.LookupEnv(watchNamespacesEnvVar)
if !found {
return nil, fmt.Errorf("%s must be set", watchNamespacesEnvVar)
}
return cleanNamespaceList(ns), nil
}

func cleanNamespaceList(namespaces string) (result []string) {
unfilteredList := strings.Split(namespaces, ",")
result = make([]string, 0, len(unfilteredList))

for _, elem := range unfilteredList {
elem = strings.TrimSpace(elem)
if len(elem) != 0 {
result = append(result, elem)
}
}
return
}
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: quay.io/zncdata/hdfs-operator
newName: registry.cn-hangzhou.aliyuncs.com/luwei-dev/hdfs-operator
newTag: v0.0.1
13 changes: 13 additions & 0 deletions deploy/catalog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: operators.coreos.com/v1alpha1
kind: CatalogSource
metadata:
name: hdfs-catalog
# namespace: olm
spec:
sourceType: grpc
image: registry.cn-hangzhou.aliyuncs.com/luwei-dev/hdfs-operator-catalog:latest
displayName: hdfs-catalog
publisher: zncdata-labs
updateStrategy:
registryPoll:
interval: 20m
78 changes: 78 additions & 0 deletions deploy/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
apiVersion: hdfs.zncdata.dev/v1alpha1
kind: HdfsCluster
metadata:
labels:
app.kubernetes.io/name: hdfscluster
app.kubernetes.io/instance: hdfscluster-sample
app.kubernetes.io/part-of: hdfs-operator
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: hdfs-operator
name: hdfscluster-sample
spec:
image:
repository: docker.stackable.tech/stackable/hadoop
tag: 3.3.4-stackable24.3.0
clusterConfig:
zookeeperDiscoveryZNode: sample-hdfs
nameNode:
roleGroups:
default:
replicas: 2
config:
resources:
cpu:
min: 1
max: 2
memory:
limit: "3Gi"
logging:
namenode:
loggers:
test:
level: DEBUG
console:
level: WARN
file:
level: ERROR
formatNameNode:
loggers:
test-format:
level: DEBUG
journalNode:
roleGroups:
default:
replicas: 3
config:
resources:
cpu:
min: 1
max: 2
memory:
limit: "3Gi"
logging:
journalNode:
loggers:
test:
level: DEBUG
console:
level: WARN
file:
level: ERROR
dataNode:
roleGroups:
default:
replicas: 3
config:
resources:
cpu:
min: 1
max: 2
memory:
limit: "3Gi"
logging:
datanode:
loggers:
test:
level: DEBUG
console:
level: WARN
9 changes: 9 additions & 0 deletions deploy/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Namespace
metadata:
name: kubedatastack-operators
---
apiVersion: v1
kind: Namespace
metadata:
name: kubedatastack
10 changes: 10 additions & 0 deletions deploy/operator-group.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
name: zncdata-stack
namespace: kubedatastack-operators
spec:
# if not specified, the operator group will target all namespaces
# so the operator will be able to watch and manage resources in all namespaces
targetNamespaces:
- kubedatastack
11 changes: 11 additions & 0 deletions deploy/subscription.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: hdfs-sub
namespace: kubedatastack-operators
spec:
channel: stable
name: hdfs-operator
source: hdfs-catalog
sourceNamespace: olm
installPlanApproval: Automated

0 comments on commit eebbdf3

Please sign in to comment.