|
| 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 generator |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "strings" |
| 21 | + "text/template" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + // Sample catalog resource values |
| 26 | + // TODO: Make this configurable |
| 27 | + packageChannel = "alpha" |
| 28 | +) |
| 29 | + |
| 30 | +// CatalogPackageConfig contains the data needed to generate deploy/alm-catalog/package.yaml |
| 31 | +type CatalogPackageConfig struct { |
| 32 | + PackageName string |
| 33 | + ChannelName string |
| 34 | + CurrentCSV string |
| 35 | +} |
| 36 | + |
| 37 | +// renderCatalogPackage generates deploy/alm-catalog/package.yaml |
| 38 | +func renderCatalogPackage(w io.Writer, config *Config, catalogVersion string) error { |
| 39 | + t := template.New(catalogPackageYaml) |
| 40 | + t, err := t.Parse(catalogPackageTmpl) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("failed to parse catalog package template: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + name := strings.ToLower(config.Kind) |
| 46 | + cpConfig := CatalogPackageConfig{ |
| 47 | + PackageName: name, |
| 48 | + ChannelName: packageChannel, |
| 49 | + CurrentCSV: getCSVName(name, catalogVersion), |
| 50 | + } |
| 51 | + return t.Execute(w, cpConfig) |
| 52 | +} |
| 53 | + |
| 54 | +// CRDConfig contains the data needed to generate deploy/alm-catalog/crd.yaml |
| 55 | +type CRDConfig struct { |
| 56 | + Kind string |
| 57 | + KindSingular string |
| 58 | + KindPlural string |
| 59 | + GroupName string |
| 60 | + Version string |
| 61 | +} |
| 62 | + |
| 63 | +// renderCRD generates deploy/alm-catalog/crd.yaml |
| 64 | +func renderCRD(w io.Writer, config *Config) error { |
| 65 | + t := template.New(catalogCRDYaml) |
| 66 | + t, err := t.Parse(crdTmpl) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to parse catalog CRD template: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + kindSingular := strings.ToLower(config.Kind) |
| 72 | + crdConfig := CRDConfig{ |
| 73 | + Kind: config.Kind, |
| 74 | + KindSingular: kindSingular, |
| 75 | + KindPlural: kindSingular + "s", |
| 76 | + GroupName: groupName(config.APIVersion), |
| 77 | + Version: version(config.APIVersion), |
| 78 | + } |
| 79 | + return t.Execute(w, crdConfig) |
| 80 | +} |
| 81 | + |
| 82 | +// CSVConfig contains the data needed to generate deploy/alm-catalog/csv.yaml |
| 83 | +type CSVConfig struct { |
| 84 | + Kind string |
| 85 | + KindSingular string |
| 86 | + KindPlural string |
| 87 | + GroupName string |
| 88 | + CRDVersion string |
| 89 | + ProjectName string |
| 90 | + CSVName string |
| 91 | + Image string |
| 92 | + CatalogVersion string |
| 93 | +} |
| 94 | + |
| 95 | +// renderCatalogCSV generates deploy/alm-catalog/csv.yaml |
| 96 | +func renderCatalogCSV(w io.Writer, config *Config, image, catalogVersion string) error { |
| 97 | + t := template.New(catalogCSVYaml) |
| 98 | + t, err := t.Parse(catalogCSVTmpl) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to parse catalog CSV template: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + kindSingular := strings.ToLower(config.Kind) |
| 104 | + csvConfig := CSVConfig{ |
| 105 | + Kind: config.Kind, |
| 106 | + KindSingular: kindSingular, |
| 107 | + KindPlural: kindSingular + "s", |
| 108 | + GroupName: groupName(config.APIVersion), |
| 109 | + CRDVersion: version(config.APIVersion), |
| 110 | + CSVName: getCSVName(kindSingular, catalogVersion), |
| 111 | + Image: image, |
| 112 | + CatalogVersion: catalogVersion, |
| 113 | + ProjectName: config.ProjectName, |
| 114 | + } |
| 115 | + return t.Execute(w, csvConfig) |
| 116 | +} |
| 117 | + |
| 118 | +func getCSVName(name, version string) string { |
| 119 | + return name + ".v" + version |
| 120 | +} |
0 commit comments