| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- // Copyright 2018
- //
- // 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 crdvalidation
- import (
- "encoding/json"
- "flag"
- "os"
- "strings"
- "github.com/ghodss/yaml"
- extensionsobj "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
- )
- // Config stores the user configuration input
- type Config struct {
- SpecDefinitionName string
- EnableValidation bool
- OutputFormat string
- Labels Labels
- Annotations Labels
- ResourceScope string
- Group string
- Kind string
- Version string
- Plural string
- SpecReplicasPath string
- StatusReplicasPath string
- LabelSelectorPath string
- Categories []string
- ShortNames []string
- GetOpenAPIDefinitions GetAPIDefinitions
- }
- type Labels struct {
- LabelsString string
- LabelsMap map[string]string
- }
- // Implement the flag.Value interface
- func (labels *Labels) String() string {
- return labels.LabelsString
- }
- // Merge labels create a new map with labels merged.
- func (labels *Labels) Merge(otherLabels map[string]string) map[string]string {
- mergedLabels := map[string]string{}
- for key, value := range otherLabels {
- mergedLabels[key] = value
- }
- for key, value := range labels.LabelsMap {
- mergedLabels[key] = value
- }
- return mergedLabels
- }
- // Implement the flag.Set interface
- func (labels *Labels) Set(value string) error {
- m := map[string]string{}
- if value != "" {
- splited := strings.Split(value, ",")
- for _, pair := range splited {
- sp := strings.Split(pair, "=")
- m[sp[0]] = sp[1]
- }
- }
- (*labels).LabelsMap = m
- (*labels).LabelsString = value
- return nil
- }
- func NewCustomResourceDefinition(config Config) *extensionsobj.CustomResourceDefinition {
- crd := &extensionsobj.CustomResourceDefinition{
- ObjectMeta: metav1.ObjectMeta{
- Name: config.Plural + "." + config.Group,
- Labels: config.Labels.LabelsMap,
- Annotations: config.Annotations.LabelsMap,
- },
- TypeMeta: CustomResourceDefinitionTypeMeta,
- Spec: extensionsobj.CustomResourceDefinitionSpec{
- Group: config.Group,
- Version: config.Version,
- Scope: extensionsobj.ResourceScope(config.ResourceScope),
- Names: extensionsobj.CustomResourceDefinitionNames{
- Plural: config.Plural,
- Kind: config.Kind,
- Categories: config.Categories,
- ShortNames: config.ShortNames,
- },
- Subresources: &extensionsobj.CustomResourceSubresources{
- Status: &extensionsobj.CustomResourceSubresourceStatus {
- },
- Scale: &extensionsobj.CustomResourceSubresourceScale {
- SpecReplicasPath: config.SpecReplicasPath,
- StatusReplicasPath: config.StatusReplicasPath,
- LabelSelectorPath: &config.LabelSelectorPath,
- },
- },
- },
- }
- if config.SpecDefinitionName != "" && config.EnableValidation == true {
- crd.Spec.Validation = GetCustomResourceValidation(config.SpecDefinitionName, config.GetOpenAPIDefinitions)
- }
- return crd
- }
- func MarshallCrd(crd *extensionsobj.CustomResourceDefinition, outputFormat string) error {
- jsonBytes, err := json.Marshal(crd)
- if err != nil {
- return err
- }
- var r unstructured.Unstructured
- if err := json.Unmarshal(jsonBytes, &r.Object); err != nil {
- return err
- }
- unstructured.RemoveNestedField(r.Object, "status")
- jsonBytes, err = json.MarshalIndent(r.Object, "", " ")
- if err != nil {
- return err
- }
- if outputFormat == "json" {
- _, err = os.Stdout.Write(jsonBytes)
- if err != nil {
- return err
- }
- } else {
- yamlBytes, err := yaml.JSONToYAML(jsonBytes)
- if err != nil {
- return err
- }
- _, err = os.Stdout.Write([]byte("---\n"))
- if err != nil {
- return err
- }
- _, err = os.Stdout.Write(yamlBytes)
- if err != nil {
- return err
- }
- }
- return nil
- }
- // InitFlags prepares command line flags parser
- func InitFlags(cfg *Config, flagset *flag.FlagSet) *flag.FlagSet {
- flagset.Var(&cfg.Labels, "labels", "Labels")
- flagset.Var(&cfg.Annotations, "annotations", "Annotations")
- flagset.BoolVar(&cfg.EnableValidation, "with-validation", true, "Add CRD validation field, default: true")
- flagset.StringVar(&cfg.Group, "apigroup", "custom.example.com", "CRD api group")
- flagset.StringVar(&cfg.SpecDefinitionName, "spec-name", "", "CRD spec definition name")
- flagset.StringVar(&cfg.OutputFormat, "output", "yaml", "output format: json|yaml")
- flagset.StringVar(&cfg.Kind, "kind", "", "CRD Kind")
- flagset.StringVar(&cfg.ResourceScope, "scope", string(extensionsobj.NamespaceScoped), "CRD scope: 'Namespaced' | 'Cluster'. Default: Namespaced")
- flagset.StringVar(&cfg.Version, "version", "v1", "CRD version, default: 'v1'")
- flagset.StringVar(&cfg.Plural, "plural", "", "CRD plural name")
- flagset.StringVar(&cfg.SpecReplicasPath, "spec-replicas-path", ".spec.replicas", "CRD spec replicas path")
- flagset.StringVar(&cfg.StatusReplicasPath, "status-replicas-path", ".status.replicas", "CRD status replicas path")
- flagset.StringVar(&cfg.LabelSelectorPath, "label-selector-path", ".status.labelSelector", "CRD label selector path")
- return flagset
- }
|