This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
/
application.go
97 lines (80 loc) · 3.8 KB
/
application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright 2021 GramLabs, Inc.
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 generate
import (
"os"
"github.com/spf13/cobra"
konjurev1beta2 "github.com/thestormforge/konjure/pkg/api/core/v1beta2"
"github.com/thestormforge/konjure/pkg/konjure"
"github.com/thestormforge/optimize-controller/v2/cli/internal/commander"
"github.com/thestormforge/optimize-controller/v2/internal/application"
"github.com/thestormforge/optimize-go/pkg/config"
)
type ApplicationOptions struct {
// Config is the Optimize Configuration used to generate the application
Config *config.OptimizeConfig
// IOStreams are used to access the standard process streams
commander.IOStreams
Generator application.Generator
Resources []string
DefaultResource konjurev1beta2.Kubernetes
}
func NewApplicationCommand(o *ApplicationOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "application",
Aliases: []string{"app"},
Args: cobra.ExactArgs(0),
Short: "Generate an application",
Long: "Generate an application descriptor",
Hidden: true, // Application descriptor use is deprecated
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
commander.SetStreams(&o.IOStreams, cmd)
o.Generator.DefaultReader = cmd.InOrStdin()
o.Generator.WorkingDirectory, err = os.Getwd()
return
},
RunE: commander.WithoutArgsE(o.generate),
}
cmd.Flags().StringVar(&o.Generator.Name, "name", "", "set the application `name`")
cmd.Flags().StringSliceVar(&o.Generator.Goals, "goals", nil, "specify the application optimization objective")
cmd.Flags().BoolVar(&o.Generator.Documentation.Disabled, "no-comments", false, "suppress documentation comments on output")
cmd.Flags().StringVar(&o.Generator.ScenarioFile, "test-case-file", "", "specify either a StormForge Performance (.js) or Locust (.py) test case `file`")
cmd.Flags().StringArrayVarP(&o.Resources, "resources", "r", nil, "additional resources to consider")
cmd.Flags().StringArrayVar(&o.DefaultResource.Namespaces, "namespace", nil, "select resources from a specific namespace")
cmd.Flags().StringVar(&o.DefaultResource.NamespaceSelector, "ns-selector", "", "`sel`ect resources from labeled namespaces")
cmd.Flags().StringVarP(&o.DefaultResource.Selector, "selector", "l", "", "`sel`ect only labeled resources")
_ = cmd.MarkFlagFilename("test-case-file", "js", "py")
return cmd
}
func (o *ApplicationOptions) generate() error {
if len(o.Resources) > 0 {
// Add explicitly requested resources
o.Generator.Resources = append(o.Generator.Resources, konjure.NewResource(o.Resources...))
} else if o.DefaultResource.Selector == "" && o.Generator.Name != "" {
// Add a default the label selector based on the name
o.DefaultResource.Selector = "app.kubernetes.io/name=" + o.Generator.Name
}
// Only include the default resource if it has values
if !o.isDefaultResourceEmpty() {
o.Generator.Resources = append(o.Generator.Resources, konjure.Resource{Kubernetes: &o.DefaultResource})
}
// Generate the application
return o.Generator.Execute(o.YAMLWriter())
}
func (o *ApplicationOptions) isDefaultResourceEmpty() bool {
// TODO Should we check that `kubectl` is available and can return something meaningful?
return len(o.DefaultResource.Namespaces) == 0 &&
o.DefaultResource.NamespaceSelector == "" &&
len(o.DefaultResource.Types) == 0 &&
o.DefaultResource.Selector == ""
}