Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat :Add template flag to cyctl on module creation #456

Merged
merged 5 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions cyctl/internal/create/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ var (
--path '/path/to/charts' \
--version 'main'
`
valuesFile string
valuesFile string
templateName string
)

// createModule allows you to create module Custom Resource.
func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, path, version, namespace, valuesFile string) {
func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, path, version, namespace, valuesFile, templateName string) {

values, err := os.ReadFile(valuesFile)
if err != nil {
Expand All @@ -37,6 +38,17 @@ func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, pat
log.Fatalf("Error converting values file to JSON: %v", err)
}

if templateName != "" && (repo == "" && path == "" && version == "") {
temp, err := clientset.TemplateStore("cyclops").Get(templateName)
if err != nil {
fmt.Printf("Error from server (Template NotFound): %v\n", err)
return
}
repo = temp.Spec.URL
path = temp.Spec.Path
version = temp.Spec.Version
}

// Define a new Module object
newModule := v1alpha1.Module{
TypeMeta: v1.TypeMeta{
Expand Down Expand Up @@ -74,7 +86,12 @@ var (
Args: cobra.ExactArgs(1),
Aliases: []string{"modules"},
Run: func(cmd *cobra.Command, args []string) {
createModule(kubeconfig.Moduleset, args[0], repo, path, version, namespace, valuesFile)
// Custom validation:
// Either templateName or (repo and path) must be provided, if one is provided the other must be empty
if (templateName != "" && (repo != "" || path != "")) || (templateName == "" && (repo == "" || path == "")) {
log.Fatalf("Error: Either template or (repo and path) must be provided.")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add version here?

Suggested change
if (templateName != "" && (repo != "" || path != "")) || (templateName == "" && (repo == "" || path == "")) {
log.Fatalf("Error: Either template or (repo and path) must be provided.")
}
if (templateName != "" && (repo != "" || path != "" || version != "")) || (templateName == "" && (repo == "" || path == "")) {
log.Fatalf("Error: Either template or (repo, path and version) must be provided.")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version was not a compulsory flag that is why I left it. I have added it now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense @shivamrazorpay. This is just users dont use the --version flag to override the version in the template.
Can you push the update?

createModule(kubeconfig.Moduleset, args[0], repo, path, version, namespace, valuesFile, templateName)
},
}
)
Expand All @@ -85,7 +102,6 @@ func init() {
CreateModule.Flags().StringVarP(&path, "path", "p", "", "Path to the module charts")
CreateModule.Flags().StringVarP(&version, "version", "v", "", "Version of the module")
CreateModule.Flags().StringVarP(&valuesFile, "file", "f", "", "Path to the values.yaml file")
CreateModule.MarkFlagRequired("repo")
CreateModule.MarkFlagRequired("path")
CreateModule.Flags().StringVarP(&templateName, "template", "t", "", "Name of the template to use for the module creation")
CreateModule.MarkFlagRequired("file")
}
2 changes: 1 addition & 1 deletion cyctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Loading