|
| 1 | +// Copyright 2018 The Operator-SDK Authors |
| 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 | +package genutil |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "path/filepath" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/operator-framework/operator-sdk/internal/scaffold" |
| 23 | + "github.com/operator-framework/operator-sdk/internal/scaffold/input" |
| 24 | + "github.com/operator-framework/operator-sdk/internal/util/k8sutil" |
| 25 | + "github.com/operator-framework/operator-sdk/internal/util/projutil" |
| 26 | + |
| 27 | + log "github.com/sirupsen/logrus" |
| 28 | +) |
| 29 | + |
| 30 | +// CRDGen generates CRDs for all APIs in pkg/apis. |
| 31 | +func CRDGen() error { |
| 32 | + projutil.MustInProjectRoot() |
| 33 | + |
| 34 | + absProjectPath := projutil.MustGetwd() |
| 35 | + repoPkg := projutil.GetGoPkg() |
| 36 | + |
| 37 | + gvMap, err := k8sutil.ParseGroupSubpackages(scaffold.ApisDir) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("failed to parse group versions: (%v)", err) |
| 40 | + } |
| 41 | + gvb := &strings.Builder{} |
| 42 | + for g, vs := range gvMap { |
| 43 | + gvb.WriteString(fmt.Sprintf("%s:%v, ", g, vs)) |
| 44 | + } |
| 45 | + |
| 46 | + log.Infof("Running CRD generation for Custom Resource group versions: [%v]\n", gvb.String()) |
| 47 | + |
| 48 | + s := &scaffold.Scaffold{} |
| 49 | + cfg := &input.Config{ |
| 50 | + Repo: repoPkg, |
| 51 | + AbsProjectPath: absProjectPath, |
| 52 | + ProjectName: filepath.Base(absProjectPath), |
| 53 | + } |
| 54 | + crds, err := k8sutil.GetCRDs(scaffold.CRDsDir) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + for _, crd := range crds { |
| 59 | + g, v, k := crd.Spec.Group, crd.Spec.Version, crd.Spec.Names.Kind |
| 60 | + if v == "" { |
| 61 | + if len(crd.Spec.Versions) != 0 { |
| 62 | + v = crd.Spec.Versions[0].Name |
| 63 | + } else { |
| 64 | + return fmt.Errorf("crd of group %s kind %s has no version", g, k) |
| 65 | + } |
| 66 | + } |
| 67 | + r, err := scaffold.NewResource(g+"/"+v, k) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + err = s.Execute(cfg, |
| 72 | + &scaffold.CRD{Resource: r, IsOperatorGo: projutil.IsOperatorGo()}, |
| 73 | + ) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + log.Info("CRD generation complete.") |
| 80 | + return nil |
| 81 | +} |
0 commit comments