Skip to content

Commit 71a7c42

Browse files
committed
operator controller and rework kustomize to support bindata
Dockerfile is updated to include new 'operator' binary and bindata directory. Bindata will contain all the CRDs, RBAC, and deployment files needed to deploy all the operators. Add Makefile targets for bindata, and run-operator. Forklift bindata impl from old CNOSP compute_node_operator. Implement new controllers/operator/openstack_controller.go which process files in /bindata. Calling 'make bindata' during a Renovate sync should keep things in sync. Jira: OSPRH-11244
1 parent 9d36d85 commit 71a7c42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1902
-280
lines changed

.github/workflows/build-openstack-operator.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
go_version: 1.21.x
1919
operator_sdk_version: 1.31.0
2020
bundle_dockerfile: ./bundle.Dockerfile
21-
catalog_extra_bundles_script: ./hack/pin-bundle-images.sh
2221
secrets:
2322
IMAGENAMESPACE: ${{ secrets.IMAGENAMESPACE }}
2423
QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ repos:
3030
language: system
3131
entry: make
3232
args: ['crd-schema-check']
33+
- id: make-bindata
34+
name: make-bindata
35+
language: system
36+
entry: make
37+
args: ['bindata']
3338
pass_filenames: false
3439

3540
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -47,6 +52,7 @@ repos:
4752
- id: destroyed-symlinks
4853
- id: check-yaml
4954
args: [-m]
55+
exclude: '^bindata/operator|^config/operator'
5056
- id: check-json
5157
- id: detect-private-key
5258
- id: end-of-file-fixer

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ RUN if [ ! -f $CACHITO_ENV_FILE ]; then go mod download ; fi
2626

2727
# Build manager
2828
RUN if [ -f $CACHITO_ENV_FILE ] ; then source $CACHITO_ENV_FILE ; fi ; env ${GO_BUILD_EXTRA_ENV_ARGS} go build ${GO_BUILD_EXTRA_ARGS} -a -o ${DEST_ROOT}/manager main.go
29+
RUN if [ -f $CACHITO_ENV_FILE ] ; then source $CACHITO_ENV_FILE ; fi ; env ${GO_BUILD_EXTRA_ENV_ARGS} go build ${GO_BUILD_EXTRA_ARGS} -a -o ${DEST_ROOT}/operator cmd/operator/main.go
2930

3031
RUN cp -r config/services ${DEST_ROOT}/services
32+
RUN cp -r bindata ${DEST_ROOT}/bindata
3133

3234
# Use distroless as minimal base image to package the manager binary
3335
# Refer to https://github.com/GoogleContainerTools/distroless for more details
@@ -65,9 +67,11 @@ WORKDIR /
6567

6668
# Install operator binary to WORKDIR
6769
COPY --from=builder ${DEST_ROOT}/manager .
70+
COPY --from=builder ${DEST_ROOT}/operator .
6871

6972
# Install services
7073
COPY --from=builder ${DEST_ROOT}/services ${OPERATOR_SERVICES}
74+
COPY --from=builder ${DEST_ROOT}/bindata /bindata
7175

7276
USER $USER_ID
7377

Makefile

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,30 @@ help: ## Display this help.
131131

132132
##@ Development
133133

134+
# (dprince) FIXME: controller-gen crd didn't seem to like multiple paths so I didn't split it. So we can continue using kubebuilder
135+
# I did split out the rbac for both binaries so we can use separate roles
134136
.PHONY: manifests
135137
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
136-
$(CONTROLLER_GEN) rbac:roleName=manager-role crd$(CRDDESC_OVERRIDE) webhook paths="./..." output:crd:artifacts:config=config/crd/bases && \
138+
mkdir -p config/operator/rbac && \
139+
$(CONTROLLER_GEN) crd$(CRDDESC_OVERRIDE) output:crd:artifacts:config=config/crd/bases webhook paths="./..." && \
140+
$(CONTROLLER_GEN) rbac:roleName=manager-role paths="{./apis/client/...,./apis/core/...,./apis/dataplane/...,./controllers/client/...,./controllers/core/...,./controllers/dataplane/...,./pkg/...}" output:dir=config/rbac && \
141+
$(CONTROLLER_GEN) rbac:roleName=operator-role paths="./controllers/operator/..." paths="./apis/operator/..." output:dir=config/operator/rbac && \
137142
rm -f apis/bases/* && cp -a config/crd/bases apis/
138143

139144
.PHONY: generate
140145
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
141146
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
142147

148+
.PHONY: bindata
149+
bindata: kustomize yq ## Build docker image with the manager.
150+
mkdir -p bindata/crds bindata/rbac bindata/operator
151+
$(KUSTOMIZE) build config/crd > bindata/crds/crds.yaml
152+
$(KUSTOMIZE) build config/default > bindata/operator/operator.yaml
153+
cp config/operator/managers.yaml bindata/operator/
154+
cp config/operator/rabbit.yaml bindata/operator/
155+
$(KUSTOMIZE) build config/rbac > bindata/rbac/rbac.yaml
156+
/bin/bash hack/sync-bindata.sh
157+
143158
.PHONY: fmt
144159
fmt: ## Run go fmt against code.
145160
go fmt ./...
@@ -207,8 +222,18 @@ run: manifests generate fmt vet ## Run a controller from your host.
207222
source hack/export_related_images.sh && \
208223
go run ./main.go -metrics-bind-address ":$(METRICS_PORT)" -health-probe-bind-address ":$(HEALTH_PORT)"
209224

225+
.PHONY: run-operator
226+
run-operator: export METRICS_PORT?=8080
227+
run-operator: export HEALTH_PORT?=8081
228+
run-operator: export ENABLE_WEBHOOKS?=false
229+
run-operator: export BASE_BINDATA?=bindata
230+
run-operator: export OPERATOR_IMAGE_URL=${IMG}
231+
run-operator: manifests generate fmt vet ## Run a controller from your host.
232+
source hack/export_operator_related_images.sh && \
233+
go run ./cmd/operator/main.go -metrics-bind-address ":$(METRICS_PORT)" -health-probe-bind-address ":$(HEALTH_PORT)"
234+
210235
.PHONY: docker-build
211-
docker-build: ## Build docker image with the manager.
236+
docker-build: ## Build docker image with the manager.
212237
podman build -t ${IMG} . ${DOCKER_BUILD_ARGS}
213238

214239
.PHONY: docker-push
@@ -273,7 +298,7 @@ GINKGO_TESTS ?= ./tests/... ./apis/client/... ./apis/core/... ./apis/dataplane/.
273298
KUTTL ?= $(LOCALBIN)/kubectl-kuttl
274299

275300
## Tool Versions
276-
KUSTOMIZE_VERSION ?= v3.8.7
301+
KUSTOMIZE_VERSION ?= v5.5.0 #(dprince: bumped to aquire new features like --load-restrictor)
277302
CONTROLLER_TOOLS_VERSION ?= v0.14.0
278303
CRD_MARKDOWN_VERSION ?= v0.0.3
279304
KUTTL_VERSION ?= 0.17.0
@@ -341,11 +366,10 @@ endif
341366
.PHONY: bundle
342367
bundle: build manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
343368
$(OPERATOR_SDK) generate kustomize manifests -q
344-
cd config/manager && \
345-
$(KUSTOMIZE) edit set image controller=$(IMG) && \
346-
$(KUSTOMIZE) edit add patch --kind Deployment --name controller-manager --namespace system --patch "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/env/0\", \"value\": {\"name\": \"OPENSTACK_RELEASE_VERSION\", \"value\": \"$(OPENSTACK_RELEASE_VERSION)\"}}]"
347-
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
348-
cp dependencies.yaml ./bundle/metadata
369+
cd config/operator/deployment/ && $(KUSTOMIZE) edit set image controller=$(IMG) && \
370+
$(KUSTOMIZE) edit add patch --kind Deployment --name controller-operator --namespace system --patch "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/env/0\", \"value\": {\"name\": \"OPENSTACK_RELEASE_VERSION\", \"value\": \"$(OPENSTACK_RELEASE_VERSION)\"}}]"
371+
sed -i -e 's|operator:image|$(IMG)|' config/operator/deployment/deployment.yaml
372+
$(KUSTOMIZE) build config/operator --load-restrictor='LoadRestrictionsNone' | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
349373
$(OPERATOR_SDK) bundle validate ./bundle
350374

351375
.PHONY: bundle-build

apis/bases/operator.openstack.org_openstacks.yaml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ spec:
1515
singular: openstack
1616
scope: Namespaced
1717
versions:
18-
- name: v1beta1
18+
- additionalPrinterColumns:
19+
- jsonPath: .status.deployedOperatorCount
20+
name: Deployed Operator Count
21+
type: integer
22+
- description: Status
23+
jsonPath: .status.conditions[0].status
24+
name: Status
25+
type: string
26+
name: v1beta1
1927
schema:
2028
openAPIV3Schema:
2129
properties:
@@ -26,11 +34,36 @@ spec:
2634
metadata:
2735
type: object
2836
spec:
29-
properties:
30-
foo:
31-
type: string
3237
type: object
3338
status:
39+
properties:
40+
conditions:
41+
items:
42+
properties:
43+
lastTransitionTime:
44+
format: date-time
45+
type: string
46+
message:
47+
type: string
48+
reason:
49+
type: string
50+
severity:
51+
type: string
52+
status:
53+
type: string
54+
type:
55+
type: string
56+
required:
57+
- lastTransitionTime
58+
- status
59+
- type
60+
type: object
61+
type: array
62+
deployedOperatorCount:
63+
type: integer
64+
observedGeneration:
65+
format: int64
66+
type: integer
3467
type: object
3568
type: object
3669
served: true
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package v1beta1
17+
18+
import (
19+
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
20+
)
21+
22+
// OpenStack Condition Types used by API objects.
23+
const (
24+
// OpenStackOperatorReadyCondition Status=True condition which indicates if operators have been deployed
25+
OpenStackOperatorReadyCondition condition.Type = "OpenStackOperatorReadyCondition"
26+
)
27+
28+
// Common Messages used by Openstack operator
29+
const (
30+
//
31+
// OpenStackOperator condition messages
32+
//
33+
34+
// OpenStackOperatorErrorMessage
35+
OpenStackOperatorErrorMessage = "OpenStackOperator error occured %s"
36+
37+
// OpenStackOperatorReadyInitMessage
38+
OpenStackOperatorReadyInitMessage = "OpenStackOperator not started"
39+
40+
// OpenStackOperatorReadyRunningMessage
41+
OpenStackOperatorReadyRunningMessage = "OpenStackOperator in progress"
42+
43+
// OpenStackOperatorReadyMessage
44+
OpenStackOperatorReadyMessage = "OpenStackOperator completed"
45+
46+
)

apis/operator/v1beta1/openstack_types.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,32 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
)
2223

23-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25-
2624
// OpenStackSpec defines the desired state of OpenStack
2725
type OpenStackSpec struct {
28-
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
29-
// Important: Run "make" to regenerate code after modifying this file
30-
31-
// Foo is an example field of OpenStack. Edit openstack_types.go to remove/update
32-
Foo string `json:"foo,omitempty"`
3326
}
3427

3528
// OpenStackStatus defines the observed state of OpenStack
3629
type OpenStackStatus struct {
37-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38-
// Important: Run "make" to regenerate code after modifying this file
39-
}
4030

41-
//+kubebuilder:object:root=true
42-
//+kubebuilder:subresource:status
31+
// +operator-sdk:csv:customresourcedefinitions:type=status,xDescriptors={"urn:alm:descriptor:io.kubernetes.conditions"}
32+
// Conditions
33+
Conditions condition.Conditions `json:"conditions,omitempty" optional:"true"`
34+
35+
// DeployedOperatorCount - the number of operators deployed
36+
DeployedOperatorCount *int `json:"deployedOperatorCount,omitempty"`
37+
38+
// ObservedGeneration - the most recent generation observed for this object.
39+
ObservedGeneration int64 `json:"observedGeneration,omitempty"` // no spec yet so maybe we don't need this
40+
}
4341

42+
// +kubebuilder:object:root=true
43+
// +kubebuilder:subresource:status
44+
// +kubebuilder:printcolumn:name="Deployed Operator Count",type=integer,JSONPath=`.status.deployedOperatorCount`
45+
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[0].status",description="Status"
4446
// OpenStack is the Schema for the openstacks API
4547
type OpenStack struct {
4648
metav1.TypeMeta `json:",inline"`

apis/operator/v1beta1/zz_generated.deepcopy.go

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/operator/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444

4545
operatorv1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/operator/v1beta1"
4646
operatorcontrollers "github.com/openstack-k8s-operators/openstack-operator/controllers/operator"
47-
// +kubebuilder:scaffold:imports
4847
)
4948

5049
var (
@@ -55,7 +54,6 @@ var (
5554
func init() {
5655
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5756
utilruntime.Must(operatorv1beta1.AddToScheme(scheme))
58-
// +kubebuilder:scaffold:scheme
5957
}
6058

6159
func main() {
@@ -96,7 +94,7 @@ func main() {
9694
},
9795
HealthProbeBindAddress: probeAddr,
9896
LeaderElection: enableLeaderElection,
99-
LeaderElectionID: "40ba705e.openstack.org",
97+
LeaderElectionID: "20ca801f.openstack.org",
10098
WebhookServer: webhook.NewServer(
10199
webhook.Options{
102100
Port: 9443,
@@ -147,7 +145,8 @@ func main() {
147145
setupLog.Error(err, "unable to create controller", "controller", "OpenStack")
148146
os.Exit(1)
149147
}
150-
// +kubebuilder:scaffold:builder
148+
operatorcontrollers.SetupEnv()
149+
151150
if err := mgr.AddHealthzCheck("healthz", checker); err != nil {
152151
setupLog.Error(err, "unable to set up health check")
153152
os.Exit(1)

0 commit comments

Comments
 (0)