forked from openshift/hypershift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a install helm command to generate a helm chart with some templatization options similar to the install render --template command. the PR also introduces a way to template the registry overrides option of the operator required by Azure/ARO-HCP#698 Signed-off-by: Gerd Oberlechner <[email protected]>
- Loading branch information
Showing
7 changed files
with
348 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package install | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/openshift/hypershift/pkg/version" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
crclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var helmTemplateParams = TemplateParams{ | ||
Namespace: ".Release.Namespace", | ||
HyperShiftImage: ".Values.image", | ||
OIDCS3Name: ".Values.oidc.s3.name", | ||
OIDCS3Region: ".Values.oidc.s3.region", | ||
OIDCS3CredsSecret: ".Values.oidc.s3.credsSecret", | ||
OIDCS3CredsSecretKey: ".Values.oidc.s3.credsSecretKey", | ||
AWSPrivateRegion: ".Values.aws.private.region", | ||
AWSPrivateCredsSecret: ".Values.aws.private.credsSecret", | ||
AWSPrivateCredsSecretKey: ".Values.aws.private.credsSecretKey", | ||
ExternalDNSCredsSecret: ".Values.externaldns.credsSecret", | ||
ExternalDNSDomainFilter: ".Values.externaldns.domainFilter", | ||
ExternalDNSTxtOwnerID: ".Values.externaldns.txtOwnerId", | ||
ExternalDNSImage: ".Values.externaldns.image", | ||
RegistryOverrides: ".Values.registryOverrides", | ||
TemplateNamespace: false, | ||
TemplateParamWrapper: func(name string) string { | ||
return fmt.Sprintf("{{ %s }}", name) | ||
}, | ||
} | ||
|
||
func NewHelmRenderCommand(opts *Options) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "helm", | ||
Short: "Generate a Helm chart for the HyperShift Operator", | ||
SilenceUsage: true, | ||
} | ||
|
||
cmd.Flags().StringVar(&opts.OutputFile, "output-dir", "", "File to write the rendered manifests to. Writes to STDOUT if not specified.") | ||
|
||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
opts.ApplyDefaults() | ||
|
||
crds, manifests, err := hyperShiftOperatorTemplateManifest(opts, helmTemplateParams) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opts.OutputFile == "" { | ||
opts.OutputFile = "./chart" | ||
} | ||
err = writeManifestsToDir(crds, fmt.Sprintf("%s/crds", opts.OutputFile)) | ||
if err != nil { | ||
return err | ||
} | ||
err = writeManifestsToDir(manifests, fmt.Sprintf("%s/templates", opts.OutputFile)) | ||
if err != nil { | ||
return err | ||
} | ||
err = WriteChartYaml(opts.OutputFile) | ||
if err != nil { | ||
return err | ||
} | ||
err = WriteValuesFile(opts.OutputFile) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func WriteChartYaml(dir string) error { | ||
data := map[string]interface{}{ | ||
"apiVersion": "v2", | ||
"name": "hypershift-operator", | ||
"description": "A Helm chart for the HyperShift Operator", | ||
"type": "application", | ||
"version": "0.1.0", | ||
"appVersion": version.GetRevision(), | ||
} | ||
return writeYamlFile(fmt.Sprintf("%s/Chart.yaml", dir), data) | ||
} | ||
|
||
func WriteValuesFile(dir string) error { | ||
data := map[string]interface{}{ | ||
"image": "", | ||
"registryOverrides": "", | ||
"oidc": map[string]interface{}{ | ||
"s3": map[string]interface{}{ | ||
"name": "", | ||
"region": "", | ||
"credsSecret": "", | ||
"credsSecretKey": "", | ||
}, | ||
}, | ||
"aws": map[string]interface{}{ | ||
"private": map[string]interface{}{ | ||
"region": "", | ||
"credsSecret": "", | ||
"credsSecretKey": "", | ||
}, | ||
}, | ||
"externaldns": map[string]interface{}{ | ||
"credsSecret": "", | ||
"domainFilter": "", | ||
"txtOwnerId": "", | ||
"image": "", | ||
}, | ||
} | ||
return writeYamlFile(fmt.Sprintf("%s/values.yaml", dir), data) | ||
} | ||
|
||
func writeYamlFile(path string, data map[string]interface{}) error { | ||
yamlData, err := yaml.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
file, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
_, err = file.Write(yamlData) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func writeManifestsToDir(manifests []crclient.Object, dir string) error { | ||
err := os.MkdirAll(dir, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, manifest := range manifests { | ||
file, err := os.Create(fmt.Sprintf("%s/%s-%s.yaml", dir, strings.ToLower(manifest.GetObjectKind().GroupVersionKind().Kind), manifest.GetName())) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
err = render([]crclient.Object{manifest}, RenderFormatYaml, file) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package install | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func ExecuteTestHelmCommand(args []string) ([]byte, error) { | ||
// append helm to args | ||
args = append([]string{"helm"}, args...) | ||
cmd := NewCommand() | ||
cmd.SetArgs(args) | ||
b := bytes.NewBufferString("") | ||
cmd.SetOut(b) | ||
err := cmd.Execute() | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
return io.ReadAll(b) | ||
} | ||
|
||
func TestHelmCommand(t *testing.T) { | ||
// create a folder to hold test data and delete it afterwards | ||
tmpDir, err := os.MkdirTemp("", "test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
_, err = ExecuteTestHelmCommand([]string{"--output-dir", tmpDir}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// check if the output directory exists | ||
if _, err := os.Stat(tmpDir); os.IsNotExist(err) { | ||
t.Fatalf("Output directory %s does not exist", tmpDir) | ||
} | ||
|
||
// check if the crds directory exists ... | ||
for _, dir := range []string{"crds", "templates"} { | ||
dirPath := tmpDir + "/" + dir | ||
if _, err := os.Stat(dirPath); os.IsNotExist(err) { | ||
t.Fatalf("%s directory %s does not exist", dir, dirPath) | ||
} | ||
files, err := os.ReadDir(dirPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(files) == 0 { | ||
t.Fatalf("%s directory is empty", dirPath) | ||
} | ||
} | ||
|
||
// check if the Chart.yaml file exists | ||
chartPath := tmpDir + "/Chart.yaml" | ||
if _, err := os.Stat(chartPath); os.IsNotExist(err) { | ||
t.Fatalf("Chart.yaml file %s does not exist", chartPath) | ||
} | ||
|
||
// check if the values.yaml file exists | ||
valuesPath := tmpDir + "/values.yaml" | ||
if _, err := os.Stat(valuesPath); os.IsNotExist(err) { | ||
t.Fatalf("values.yaml file %s does not exist", valuesPath) | ||
} | ||
|
||
} |
Oops, something went wrong.