Skip to content

Commit 4398bf7

Browse files
authored
refactor: move command func to seperate file (openshift#522)
moved to command and logic for rendering in seperate files for readability Signed-off-by: ehila <ehila@redhat.com> feat: support multi file pattern Add MCO pattern to read input directory of different resources file types, this allows us the ability to read in initial state files such as mcp or infrastructure status during bootstrap. This also means we no longer need to be prescriptive with file names for PerformanceProfile manifests that the user can supply. This should mean less friction during bootstrap and allows for more accurate rendering of appropriately configured files. Signed-off-by: ehila <ehila@redhat.com> doc: updated docs for new render command flags Signed-off-by: ehila <ehila@redhat.com> fix: updated runtime api struct to no longer reference v1beta1 Signed-off-by: ehila <ehila@redhat.com> test: updated unit test to support new render command Updated unit test to use new render command and logic to ignore the randomly generated UUID during compare and validate Updated the test data to have the APIVersion Signed-off-by: ehila <ehila@redhat.com> Signed-off-by: ehila <ehila@redhat.com>
1 parent d01928d commit 4398bf7

File tree

11 files changed

+330
-106
lines changed

11 files changed

+330
-106
lines changed

docs/performanceprofile/performance_controller.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The operator can render manifests for all the components it supposes to create,
6262
You need to provide the following environment variables
6363

6464
```shell
65-
export PERFORMANCE_PROFILE_INPUT_FILES=<comma separated list of your Performance Profiles>
65+
export ASSET_INPUT_DIR=<input path containing performance profile manifests>
6666
export ASSET_OUTPUT_DIR=<output path for the rendered manifests>
6767
```
6868

@@ -75,7 +75,7 @@ _output/cluster-node-tuning-operator render
7575
Or provide the variables via command line arguments
7676

7777
```shell
78-
_output/cluster-node-tuning-operator render --performance-profile-input-files <path> --asset-output-dir<path>
78+
_output/cluster-node-tuning-operator render --asset-input-dir <path> --asset-output-dir <path>
7979
```
8080

8181
## Troubleshooting
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
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+
16+
package render
17+
18+
import (
19+
"fmt"
20+
"os"
21+
22+
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components"
23+
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/pflag"
26+
27+
"k8s.io/klog"
28+
)
29+
30+
type renderOpts struct {
31+
assetsInDir string
32+
assetsOutDir string
33+
}
34+
35+
// NewRenderCommand creates a render command.
36+
// The render command will read in the asset directory and walk the paths to ingest relevant data.
37+
// It will generate the machine configs based off of the supplied PerformanceProfiles and any manifest
38+
// needed to generate the machine configs.
39+
func NewRenderCommand() *cobra.Command {
40+
renderOpts := renderOpts{}
41+
42+
cmd := &cobra.Command{
43+
Use: "render",
44+
Short: "Render performance-addon-operator manifests",
45+
Run: func(cmd *cobra.Command, args []string) {
46+
47+
if err := renderOpts.Validate(); err != nil {
48+
klog.Fatal(err)
49+
}
50+
51+
if err := renderOpts.Run(); err != nil {
52+
klog.Fatal(err)
53+
}
54+
},
55+
}
56+
57+
renderOpts.AddFlags(cmd.Flags())
58+
59+
return cmd
60+
}
61+
62+
func (r *renderOpts) AddFlags(fs *pflag.FlagSet) {
63+
fs.StringVar(&r.assetsInDir, "asset-input-dir", components.AssetsDir, "Input path for the assets directory. (Can be a comma separated list of directories.)")
64+
fs.StringVar(&r.assetsOutDir, "asset-output-dir", r.assetsOutDir, "Output path for the rendered manifests.")
65+
// environment variables has precedence over standard input
66+
r.readFlagsFromEnv()
67+
}
68+
69+
func (r *renderOpts) readFlagsFromEnv() {
70+
if assetInDir := os.Getenv("ASSET_INPUT_DIR"); len(assetInDir) > 0 {
71+
r.assetsInDir = assetInDir
72+
}
73+
74+
if assetsOutDir := os.Getenv("ASSET_OUTPUT_DIR"); len(assetsOutDir) > 0 {
75+
r.assetsOutDir = assetsOutDir
76+
}
77+
}
78+
79+
func (r *renderOpts) Validate() error {
80+
if len(r.assetsOutDir) == 0 {
81+
return fmt.Errorf("asset-output-dir must be specified")
82+
}
83+
84+
return nil
85+
}
86+
87+
func (r *renderOpts) Run() error {
88+
return render(r.assetsInDir, r.assetsOutDir)
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
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+
16+
package render
17+
18+
import (
19+
"bytes"
20+
"errors"
21+
"fmt"
22+
"io"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
27+
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
28+
)
29+
30+
type manifest struct {
31+
Raw []byte
32+
}
33+
34+
// UnmarshalJSON unmarshals bytes of single kubernetes object to manifest.
35+
func (m *manifest) UnmarshalJSON(in []byte) error {
36+
if m == nil {
37+
return errors.New("manifest: UnmarshalJSON on nil pointer")
38+
}
39+
40+
// This happens when marshalling
41+
// <yaml>
42+
// --- (this between two `---`)
43+
// ---
44+
// <yaml>
45+
if bytes.Equal(in, []byte("null")) {
46+
m.Raw = nil
47+
return nil
48+
}
49+
50+
m.Raw = append(m.Raw[0:0], in...)
51+
return nil
52+
}
53+
54+
// parseManifests parses a YAML or JSON document that may contain one or more
55+
// kubernetes resources.
56+
func parseManifests(filename string, r io.Reader) ([]manifest, error) {
57+
d := yamlutil.NewYAMLOrJSONDecoder(r, 1024)
58+
var manifests []manifest
59+
for {
60+
m := manifest{}
61+
if err := d.Decode(&m); err != nil {
62+
if err == io.EOF {
63+
return manifests, nil
64+
}
65+
return manifests, fmt.Errorf("error parsing %q: %w", filename, err)
66+
}
67+
m.Raw = bytes.TrimSpace(m.Raw)
68+
if len(m.Raw) == 0 || bytes.Equal(m.Raw, []byte("null")) {
69+
continue
70+
}
71+
manifests = append(manifests, m)
72+
}
73+
}
74+
75+
func listFiles(dirPaths string) ([]string, error) {
76+
dirs := strings.Split(dirPaths, ",")
77+
results := []string{}
78+
for _, dir := range dirs {
79+
err := filepath.WalkDir(dir,
80+
func(path string, info os.DirEntry, err error) error {
81+
if err != nil {
82+
return err
83+
}
84+
if info.IsDir() {
85+
return nil
86+
}
87+
results = append(results, path)
88+
return nil
89+
})
90+
if err != nil {
91+
return nil, err
92+
}
93+
}
94+
return results, nil
95+
}

0 commit comments

Comments
 (0)