1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 11:48:53 +00:00

Add a command/bin to generate the CRDs as yaml

This commit is contained in:
Antoine Legrand 2018-01-23 17:37:28 +01:00
parent 578369c2fb
commit 1c6922ba8a
4 changed files with 108 additions and 1 deletions
Makefile
cmd/po-crdgen
pkg/k8sutil

View file

@ -16,6 +16,9 @@ build: promu
short-build:
go install github.com/coreos/prometheus-operator/cmd/operator
crdgen:
go install github.com/coreos/prometheus-operator/cmd/po-crdgen
crossbuild: promu
@$(PROMU) crossbuild

53
cmd/po-crdgen/crdgen.go Normal file
View file

@ -0,0 +1,53 @@
package main
import (
"encoding/json"
"fmt"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
k8sutil "github.com/coreos/prometheus-operator/pkg/k8sutil"
prometheus "github.com/coreos/prometheus-operator/pkg/prometheus"
"github.com/ghodss/yaml"
extensionsobj "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"os"
)
// Config stores the user configuration input
type Config struct {
EnableValidation bool
Namespace string
Labels prometheus.Labels
CrdGroup string
CrdKinds monitoringv1.CrdKinds
}
func marshallCrd(crd *extensionsobj.CustomResourceDefinition, encode string) {
jsonBytes, err := json.MarshalIndent(crd, "", " ")
if err != nil {
fmt.Println("error:", err)
}
if encode == "json" {
os.Stdout.Write(jsonBytes)
} else {
yamlBytes, err := yaml.JSONToYAML(jsonBytes)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write([]byte("---\n"))
os.Stdout.Write(yamlBytes)
}
}
// PrintCrd write on stdout the Prometheus CRD files as json or yaml
func PrintCrd(conf Config) {
promCrd := k8sutil.NewPrometheusCustomResourceDefinition(conf.CrdKinds.Prometheus, conf.CrdGroup, conf.Labels.LabelsMap)
serviceMonitorCrd := k8sutil.NewServiceMonitorCustomResourceDefinition(conf.CrdKinds.ServiceMonitor, conf.CrdGroup, conf.Labels.LabelsMap)
alertManagerCrd := k8sutil.NewAlertmanagerCustomResourceDefinition(conf.CrdKinds.Alertmanager, conf.CrdGroup, conf.Labels.LabelsMap)
marshallCrd(promCrd, "yaml")
marshallCrd(serviceMonitorCrd, "yaml")
marshallCrd(alertManagerCrd, "yaml")
}

43
cmd/po-crdgen/main.go Normal file
View file

@ -0,0 +1,43 @@
// Copyright 2016 The prometheus-operator 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 main
import (
"flag"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
"k8s.io/api/core/v1"
"os"
)
var (
cfg Config
)
func init() {
cfg.CrdKinds = monitoringv1.DefaultCrdKinds
flagset := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagset.Var(&cfg.Labels, "labels", "Labels to be add to all resources created by the operator")
flagset.BoolVar(&cfg.EnableValidation, "with-validation", false, "Include the validation spec")
flagset.StringVar(&cfg.CrdGroup, "crd-apigroup", monitoringv1.Group, "prometheus CRD API group name")
flagset.Var(&cfg.CrdKinds, "crd-kinds", "customize CRD kind names")
flagset.StringVar(&cfg.Namespace, "namespace", v1.NamespaceAll, "Namespace to scope the interaction of the Prometheus Operator and the apiserver.")
flagset.Parse(os.Args[1:])
}
func main() {
PrintCrd(cfg)
os.Exit(0)
}

View file

@ -36,6 +36,12 @@ import (
"k8s.io/client-go/rest"
)
// CustomResourceDefinitionTypeMeta set the default kind/apiversion of CRD
var CustomResourceDefinitionTypeMeta metav1.TypeMeta = metav1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: "apiextensions.k8s.io/v1beta1",
}
// WaitForCRDReady waits for a third party resource to be available for use.
func WaitForCRDReady(listFunc func(opts metav1.ListOptions) (runtime.Object, error)) error {
err := wait.Poll(3*time.Second, 10*time.Minute, func() (bool, error) {
@ -211,6 +217,7 @@ func NewPrometheusCustomResourceDefinition(crdkind monitoringv1.CrdKind, group s
Name: crdkind.Plural + "." + group,
Labels: labels,
},
TypeMeta: CustomResourceDefinitionTypeMeta,
Spec: extensionsobj.CustomResourceDefinitionSpec{
Group: group,
Version: monitoringv1.Version,
@ -229,7 +236,7 @@ func NewServiceMonitorCustomResourceDefinition(crdkind monitoringv1.CrdKind, gro
Name: crdkind.Plural + "." + group,
Labels: labels,
},
TypeMeta: CustomResourceDefinitionTypeMeta,
Spec: extensionsobj.CustomResourceDefinitionSpec{
Group: group,
Version: monitoringv1.Version,
@ -248,6 +255,7 @@ func NewAlertmanagerCustomResourceDefinition(crdkind monitoringv1.CrdKind, group
Name: crdkind.Plural + "." + group,
Labels: labels,
},
TypeMeta: CustomResourceDefinitionTypeMeta,
Spec: extensionsobj.CustomResourceDefinitionSpec{
Group: group,
Version: monitoringv1.Version,