Skip to content

Commit

Permalink
[api] reconcile CRD; OpenAPI schema validation (m3db#149)
Browse files Browse the repository at this point in the history
* [api] reconcile CRD; OpenAPI schema validation

- Always update CRD on startup so that the spec matches the build, with
  operator flag to disable CRD management.

- Add support for OpenAPI schema validation, gated behind a CLI flag
  until we test further.

- Clean up unused API objects.
  • Loading branch information
schallert authored Jun 3, 2019
1 parent 153cb10 commit 02fd4e6
Show file tree
Hide file tree
Showing 47 changed files with 12,587 additions and 182 deletions.
2 changes: 2 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ coverage:

ignore:
- "pkg/client/**/*"
- "**/*generated.go"
- "**/*generated*.go"

comment:
layout: "header, reach, diff, flags, footer"
Expand Down
12 changes: 11 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,19 @@ version = "3.3.8"
[[override]]
name = "github.com/m3db/prometheus_client_golang"
revision = "8ae269d24972b8695572fa6b2e3718b5ea82d6b4"

[[override]]
name = "github.com/m3db/prometheus_client_model"
revision = "8b2299a4bf7d7fc10835527021716d4b4a6e8700"

[[override]]
name = "github.com/m3db/prometheus_common"
revision = "25aaa3dff79bb48116615ebe1dea6a494b74ce77"

[[override]]
name = "github.com/m3db/prometheus_procfs"
revision = "1878d9fbb537119d24b21ca07effd591627cd160"

[[constraint]]
branch = "master"
name = "github.com/ant31/crd-validation"
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ asset-gen:
@echo generating assets
PATH=$(retool_bin_path):$(PATH) statik -src $(SELF_DIR)/assets -dest $(SELF_DIR)/pkg/ -p assets -f -m -c "$$LICENSE_HEADER"

# NB(schallert): order matters -- we want license generation after all else.
.PHONY: all-gen
all-gen: mock-gen kubernetes-gen license-gen asset-gen helm-bundle docs-api-gen
all-gen: mock-gen kubernetes-gen asset-gen helm-bundle docs-api-gen license-gen

# Ensure base commit had up-to-date generated artifacts
.PHONY: test-all-gen
Expand Down Expand Up @@ -232,13 +233,13 @@ dep-ensure: install-codegen-tools ## Run dep ensure to generate vendor directory
PATH=$(retool_bin_path):$(PATH) dep ensure

.PHONY: kubernetes-gen
kubernetes-gen: dep-ensure ## Generate boilerplate code for kubernetes packages
kubernetes-gen: install-codegen-tools dep-ensure ## Generate boilerplate code for kubernetes packages
@echo "--- $@"
@GOPATH=$(GOPATH) ./hack/update-generated.sh
@GOPATH=$(GOPATH) PATH=$(retool_bin_path):$(PATH) ./hack/update-generated.sh

.PHONY: verify-gen
verify-gen: dep-ensure ## Ensure all codegen is up to date
@GOPATH=$(GOPATH) ./hack/verify-generated.sh
@GOPATH=$(GOPATH) PATH=$(retool_bin_path):$(PATH) ./hack/verify-generated.sh

.PHONY: build-docker
build-docker: ## Build m3db-operator docker image with go binary
Expand Down
38 changes: 22 additions & 16 deletions cmd/m3db-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ const (
)

var (
_kubeCfgFile string
_masterURL string
_operatorName = "m3db_operator"
_metricsPath = "/metrics"
_metricsPort = ":8080"
_useProxy bool
_debugLog bool
_develLog bool
_humanTime bool
_kubeCfgFile string
_masterURL string
_operatorName = "m3db_operator"
_metricsPath = "/metrics"
_metricsPort = ":8080"
_useProxy bool
_debugLog bool
_develLog bool
_humanTime bool
_manageCRD bool
_enableCRDValidation bool
)

func init() {
Expand All @@ -75,6 +77,9 @@ func init() {
flag.BoolVar(&_develLog, "devel", false, "enable development logging mode")
flag.BoolVar(&_humanTime, "human-time", false, "print human-friendly timestamps")
flag.BoolVar(&_useProxy, "proxy", false, "use kubectl proxy for cluster communication")
flag.BoolVar(&_manageCRD, "manage-crd", true, "create and update the operator's CRD specs")
// Disabled by default until openAPI validation is more tested.
flag.BoolVar(&_enableCRDValidation, "enable-crd-validation", false, "enable openAPI validation of the CR")
flag.Parse()
}

Expand Down Expand Up @@ -111,13 +116,13 @@ func main() {
"environment": env,
}

config := promreporter.Configuration{
promCfg := promreporter.Configuration{
HandlerPath: _metricsPath,
ListenAddress: _metricsPort,
TimerType: "summary",
}

r, err := config.NewReporter(promreporter.ConfigurationOptions{
r, err := promCfg.NewReporter(promreporter.ConfigurationOptions{
OnError: func(err error) {
if err != nil {
logger.Error("prometheus reporter error", zap.Error(err))
Expand Down Expand Up @@ -177,7 +182,13 @@ func main() {
logger.Fatal("failed to create ID provider", zap.Error(err))
}

config := controller.Configuration{
ManageCRD: _manageCRD,
EnableValidation: _enableCRDValidation,
}

opts := []controller.Option{
controller.WithConfig(config),
controller.WithKubeInformerFactory(kubeInformerFactory),
controller.WithM3DBClusterInformerFactory(m3dbClusterInformerFactory),
controller.WithPodIdentityProvider(idProvider),
Expand All @@ -202,11 +213,6 @@ func main() {
go kubeInformerFactory.Start(stopCh)
go m3dbClusterInformerFactory.Start(stopCh)

// Init the controller
if err := controller.Init(); err != nil {
logger.Fatal("failed to init controller", zap.Error(err))
}

// Trap the INT and TERM signals
signalChan := make(chan os.Signal, 2)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
Expand Down
2 changes: 1 addition & 1 deletion hack/custom-boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018 Uber Technologies, Inc.
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 6 additions & 0 deletions hack/update-generated.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-
m3dboperator:v1alpha1 \
--go-header-file "${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt" \
"$@"

echo "Generating OpenAPI Schema definition"
openapi-gen --v=1 --logtostderr \
-h "${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt" \
-i github.com/m3db/m3db-operator/pkg/apis/m3dboperator/v1alpha1,k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/api/core/v1 \
-p github.com/m3db/m3db-operator/pkg/apis/m3dboperator/v1alpha1
36 changes: 21 additions & 15 deletions pkg/apis/m3dboperator/v1alpha1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ const (
ClusterConditionPodBootstrapping ClusterConditionType = "PodBootstrapping"
)

// M3DBCluster defines the cluster
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// M3DBCluster defines the cluster
// +k8s:openapi-gen=true
type M3DBCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.TypeMeta `json:",inline"`
// +k8s:openapi-gen=false
metav1.ObjectMeta `json:"metadata,omitempty"`
Type string `json:"type"`
Spec ClusterSpec `json:"spec"`
Status M3DBStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// M3DBClusterList represents a list of M3DB Clusters
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true
type M3DBClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand All @@ -68,6 +69,7 @@ type M3DBClusterList struct {

// M3DBStatus contains the current state the M3DB cluster along with a human
// readable message
// +k8s:openapi-gen=true
type M3DBStatus struct {
// State is a enum of green, yellow, and red denoting the health of the
// cluster
Expand Down Expand Up @@ -132,6 +134,7 @@ func (s *M3DBStatus) UpdateCondition(newCond ClusterCondition) {
}

// ClusterCondition represents various conditions the cluster can be in.
// +k8s:openapi-gen=true
type ClusterCondition struct {
// Type of cluster condition.
Type ClusterConditionType `json:"type,omitempty"`
Expand Down Expand Up @@ -167,22 +170,23 @@ const (
)

// ClusterSpec defines the desired state for a M3 cluster to be converge to.
// +k8s:openapi-gen=true
type ClusterSpec struct {
// Image specifies which docker image to use with the cluster
Image string `json:"image,omitempty" yaml:"image"`
Image string `json:"image,omitempty"`

// ReplicationFactor defines how many replicas
ReplicationFactor int32 `json:"replicationFactor,omitempty" yaml:"replicationFactor"`
ReplicationFactor int32 `json:"replicationFactor,omitempty"`

// NumberOfShards defines how many shards in total
NumberOfShards int32 `json:"numberOfShards,omitempty" yaml:"numberOfShards"`
NumberOfShards int32 `json:"numberOfShards,omitempty"`

// IsolationGroups specifies a map of key-value pairs. Defines which isolation groups
// to deploy persistent volumes for data nodes
IsolationGroups []IsolationGroup `json:"isolationGroups,omitempty" yaml:"isolationGroups"`
IsolationGroups []IsolationGroup `json:"isolationGroups,omitempty"`

// Namespaces specifies the namespaces this cluster will hold.
Namespaces []Namespace `json:"namespaces,omitempty" yaml:"namespaces"`
Namespaces []Namespace `json:"namespaces,omitempty"`

// EtcdEndpoints defines the etcd endpoints to use for service discovery. Must
// be set if no custom configmap is defined. If set, etcd endpoints will be
Expand All @@ -194,23 +198,23 @@ type ClusterSpec struct {
// default configmap with template variables for etcd endpoints will be used.
// See "Configuring M3DB" in the docs for more.
// +optional
ConfigMapName *string `json:"configMapName,omitempty" yaml:"configMapName"`
ConfigMapName *string `json:"configMapName,omitempty"`

// PodIdentityConfig sets the configuration for pod identity. If unset only
// pod name and UID will be used.
// +optional
PodIdentityConfig *PodIdentityConfig `json:"podIdentityConfig,omitempty" yaml:"podIdentityConfig"`
PodIdentityConfig *PodIdentityConfig `json:"podIdentityConfig,omitempty"`

// Resources defines memory / cpu constraints for each container in the
// cluster.
// +optional
ContainerResources corev1.ResourceRequirements `json:"containerResources,omitempty" yaml:"containerResources"`
ContainerResources corev1.ResourceRequirements `json:"containerResources,omitempty"`

// DataDirVolumeClaimTemplate is the volume claim template for an M3DB
// instance's data. It claims PersistentVolumes for cluster storage, volumes
// are dynamically provisioned by when the StorageClass is defined.
// +optional
DataDirVolumeClaimTemplate *corev1.PersistentVolumeClaim `json:"dataDirVolumeClaimTemplate,omitempty" yaml:"dataDirVolumeClaimTemplate"`
DataDirVolumeClaimTemplate *corev1.PersistentVolumeClaim `json:"dataDirVolumeClaimTemplate,omitempty"`

// PodSecurityContext allows the user to specify an optional security context
// for pods.
Expand All @@ -222,7 +226,7 @@ type ClusterSpec struct {

// Labels sets the base labels that will be applied to resources created by
// the cluster. // TODO(schallert): design doc on labeling scheme.
Labels map[string]string `json:"labels,omitempty" yaml:"labels"`
Labels map[string]string `json:"labels,omitempty"`

// Tolerations sets the tolerations that will be applied to all M3DB pods.
// +optional
Expand All @@ -235,6 +239,7 @@ type ClusterSpec struct {

// NodeAffinityTerm represents a node label and a set of label values, any of
// which can be matched to assign a pod to a node.
// +k8s:openapi-gen=true
type NodeAffinityTerm struct {
// Key is the label of the node.
Key string `json:"key"`
Expand All @@ -245,6 +250,7 @@ type NodeAffinityTerm struct {
}

// IsolationGroup defines the name of zone as well attributes for the zone configuration
// +k8s:openapi-gen=true
type IsolationGroup struct {
// Name is the value that will be used in StatefulSet labels, pod labels, and
// M3DB placement "isolationGroup" fields.
Expand Down
Loading

0 comments on commit 02fd4e6

Please sign in to comment.